Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are JSF/Primefaces AJAX requests really asynchronous?

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.

like image 531
Olivier J. Avatar asked Dec 06 '12 15:12

Olivier J.


1 Answers

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).

like image 63
BalusC Avatar answered Nov 06 '22 05:11

BalusC