0

I want a commandButton to call a method in my managed bean that will process something asynchronously (new Thread) then the request should complete immediately without waiting the new Thread to finish. I want this async code to execute for as long it needs, independent of the user session.

But the request doesn't complete and keeps waiting the Thread to finish, I don't understand why. Did I get something wrong here?

xhtml:

<p:commandButton 
    value="Update" 
    id="updateButtonId" 
    styleClass="internalButton" 
    action="#{myBean.processUpdate}"
    onstart="PF('loading').show()" 
    oncomplete="PF('loading').hide()"
    update="myDataTableId" />

Bean:

@ManagedBean(name="myBean")
@ViewScoped
public class MyBean {
    ...
    public void processUpdate(){

        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                //do something...           
            }
        });
        thread.start();
    }
}
qxlab
  • 1,506
  • 4
  • 20
  • 48
  • @BalusC why duplicate? I can see in the other question that you recommend to not write session-independent code, but that's what I need... I just want to trigger an asynchronous code... – qxlab Dec 09 '17 at 22:31
  • It's a duplicate since the other Q/A provide the solutions to do work async in the right/correct way... – Kukeltje Dec 10 '17 at 12:30
  • @Kukeltje I disagree. For starters, the examples there are using EJB which I'm not. Secondly I still don't understand why the request won't finish since the code is running into a separate thread.. this was also not answered there and that was my question here..... – qxlab Dec 10 '17 at 20:09
  • You should not do it the way you do it... That is the main point – Kukeltje Dec 10 '17 at 20:32

0 Answers0