Tim Tim - 1 year ago 96
Ajax Question

Abort long ajax call from a commandButton actionListener?

I would like to be able to stop the execution of a lengthy task started with a commandButton:

<p:commandButton id="startBatch"value="Go" actionListener="#{batchController.sendBatch()}"ajax="true" />

So in my Model I added a boolean "abort" and inside sendBatch() I added a check to stop the loop when the boolean becomes true:

for (int i = 1; i <= batch.size(); i++) {
if (batchModel.isAbort()) {

And in my XHTML I added a new button that calls a method that sets this boolean to true:

<p:commandButton id="abortBatchButton"

The problem is that abort() is called after the sendBatch() method is done.

Is there a simple way to implement this ?

Answer Source

By default p:commandButtons use Ajax. However, multiple actions (requests) are queued and executed synchronically by default. So, if you have started a lengthy action, the next action will be processed when the previous actions are finished.

If you don't want to queue actions, you should use async="true" on the respective buttons.

In your case:

<p:commandButton id="startBatch"
                 actionListener="#{batchController.sendBatch()}" />

I've removed ajax="true", since it's default.