Are JSF/Primefaces AJAX requests really asynchronous?

11,239

They're really asynchronous (JS context isn't blocked; i.e. you can run other arbitrary JS code at the same moment without being blocked). The behaviour you're seeing is because they're queued. So it look like as if they are not asynchronous.

This queueing behaviour is specified in chapter 13.3.2 of the JSF 2 specification:

13.3.2 Ajax Request Queueing

All Ajax requests must be put into a client side request queue before they are sent to the server to ensure Ajax requests are processed in the order they are sent. The request that has been waiting in the queue the longest is the next request to be sent. After a request is sent, the Ajax request callback function must remove the request from the queue (also known as dequeuing). If the request completed successfully, it must be removed from the queue. If there was an error, the client must be notified, but the request must still be removed from the queue so the next request can be sent. The next request (the oldest request in the queue) must be sent. Refer to the jsf.ajax.request JavaScript documentation for more specifics about the Ajax request queue.

This is done so to ensure integrity and threadsafety of the JSF view state (and inherently thus also view scoped beans).

Share:
11,239
Olivier J.
Author by

Olivier J.

Updated on July 17, 2022

Comments

  • Olivier J.
    Olivier J. almost 2 years

    I'm new to JSF so I don't know if the behaviour I'm facing is normal.

    I have this code:

    <p:selectBooleanCheckbox id="locationChoice1" value="#{login.locationChoice1}">
        <p:ajax listener="#{login.chooseLocationType1}" update="locationChoice1 locationChoice2 positionChoice" />
        <p:ajax listener="#{login.retrieveGalaxies}" update="test"  />
    </p:selectBooleanCheckbox>
    

    My login.retrieveGalaxies function has a call to sleep(8000) function to simulate the delay. I expect my componenents locationChoice1, locationChoice2 and positionChoice to be updated in 1 or 2 seconds and my test component to be updated in 8 secondes but all are updates in 8 seconds.

    Is this the correct behaviour?

    I tried to play with async parameter but it didn't change the result.

  • Olivier J.
    Olivier J. over 11 years
    Ok so it's the expected behaviour thank you BalusC. Is there any way to fire multiple ajax requests at same time ?
  • BalusC
    BalusC over 11 years
    Yes, but not via JSF. Use plain jQuery+Servlet or so.
  • Cagatay Civici
    Cagatay Civici over 11 years
    Set async true on p:ajax so that it is not queued.
  • Baimai Wu
    Baimai Wu over 9 years
    @BalusC So in the above example, two ajax requests are queued, The first one will be handled within 1 or 2 seconds, So still does not explain why it is waiting for the same amount of time.
  • Baimai Wu
    Baimai Wu over 9 years
    I guess there is only one ajax request.