Create new RichFaces Documentation Jira issue

This will launch the RichFaces Jira page - to complete your feedback please login if needed, and submit the Jira.

JBoss.orgCommunity Documentation

6.1.11.  < a4j:queue > available since 3.3.0

The <a4j:queue> component enqueues set of Ajax requests sent from client. The RichFaces components with built-in Ajax can reference the queue to optimize Ajax requests.

The RichFaces Queue has four different types: global default, view scoped default, view scoped named and form-based default queue (general Queue principles are good documented in the "Queue Principles" section). The current section will take closer to the form based queue. The usage of other types is similar.

In order to disable or enable the <a4j:queue> component on the page you can use the "disabled" attribute.

The "requestDelay" attribute defines delay time for all the requests fired by the action components.

The "size" attribute specifies the number of requests that can be stored in the queue at a time. The attribute helps to prevent server overloading. It is also possible to determine queue's behaviour when it's size is exceeded. Use the "sizeExceededBehavior" for this purpose. There are four possible strategies of exceeded queue's behavior:

  • "dropNext" drops next request that should be fired

  • "dropNew" drops the incoming request

  • "fireNext" immediately fires the next request in line to be fired

  • "fireNew" immediately fires the incoming request.

Example:


<h:form>
    <a4j:queue size="2" requestDelay="500" sizeExceededBehavior="dropNext" onsizeexceeded="alert('The size of the queue is exceeded')" />
    <h:inputText value="#{bean.a}">
        <a4j:support event="onkeyup" />
    </h:inputText>
    <h:inputText value="#{bean.b}">
        <a4j:support event="onblur" />
    </h:inputText>
    <h:selectBooleanCheckbox value="#{bean.check}" id="checkboxID">
        <a4j:support id="checkboxSupport" event="onchange" />
    </h:selectBooleanCheckbox>
</h:form>

In this example if the queue has more than 2 requests waiting to be processed the next event will be dropped and a message (the "onsizeexceeded" attribute fires a JavaScript function) saying that the queues is exceeded will be displayed.

The "ignoreDupResponses" attribute that takes a boolean value can also help optimize your Ajax requests. If set to true, response processing for request will not occur if a similar request is already waiting in the queue. New request will be fired immediately when the response from the previous one returns.

Example:


<h:form>
      <a4j:queue requestDelay="500" ignoreDupResponses="true" />
      <h:inputText value="#{bean.a}">
            <a4j:support event="onkeyup" />
      </h:inputText>
</h:form>

In this example, the requests are glued together and only the last one is submitted.

Another key attribute that easies server load is "timeout" . The attribute specifies the amount of time an item can be in the queue before the sent event is be aborted and dropped from the queue.

If the request is sent and response is not returned within the time frame defined in this attribute - the request is aborted, and the next one is sent.

Example:


<h:form>
      <a4j:queue timeout="1000" />
      <h:inputText value="#{bean.a}">
            <a4j:support event="onkeyup" />
      </h:inputText>
</h:form>

In this case if the sever doesn't respond within a second the request will be aborted.

As you can see the implementation of the queue provides some custom event handlers that you may use to call JavaScript functions.

The "oncomplete" is fired after request completed. In this event handler request object is be passed as a parameter. Thus queue is be accessible using request.queue. And the element which was a source of the request is available using this.

Example:


<h:form>
      <a4j:queue oncomplete="alert(request.queue.getSize())" requestDelay="1000" />
      <h:inputText value="#{bean.a}">
            <a4j:support event="onkeyup" />
      </h:inputText>
      <h:selectBooleanCheckbox value="#{bean.check}">
            <a4j:support event="onchange"/>
      </h:selectBooleanCheckbox>
</h:form>

In this example you can see how the number of requests waiting in the queue change. You will get a message with the number of the requests in the queue.

The "onbeforedomupdate" event handler called before updating DOM on a client side.

The "onrequestqueue" event handler called after the new request has been added to queue. And the "onrequestdequeue" event handler called after the request has been removed from queue.

The "onsubmit" event handler called after request is completed. This attribute allows to invoke JavaScript code before an Ajax request is sent.

Table of <a4j:queue> attributes.



Visit the Queue Page at the RichFaces LiveDemo for examples of component usage and their sources.

Useful articles:

"Queue Principles" section of the RichFaces developer guide describes general Queue principles.