Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [faces-dev] Maintaining context in inter thread communication

Indeed. See also https://stackoverflow.com/q/2803160 and https://stackoverflow.com/q/3787514.


On Sun, Jan 21, 2024 at 8:28 AM Thomas Andraschko via faces-dev <faces-dev@xxxxxxxxxxx> wrote:
FacesContext is just available in the thread which is associated with a current JSF HTTP request.
You should try websockets for such cases.

Clement Levallois via faces-dev <faces-dev@xxxxxxxxxxx> schrieb am So., 21. Jan. 2024, 12:48:
Dear Faces community,

This is my first contribution to the mailing list. Very happy to do so after years of benefitting from this great framework.

My question is about an issue I encounter with a Stateless EJB containing an @Aynchronous method. When this method returns, it triggers a callback in a SessionScoped bean, which is using FacesContext.getCurrrentInstance() to then navigate to a new page.

The issue: FacesContext.getCurrrentInstance() throws an error. This is due, AFAIK, to the single thread model of statefull beans in JSF. Inter thread communication as the one I am trying doesn't maintain state. Is there a workaound it?

A simplified code version:
@Stateless
public class LongRunningProcessBean {

    @Asynchronous
    public void executeLongRunningOperation(Consumer<String> callback) {
        // retrieve topNodes...

        // Invoke the callback when top nodes have bee retrieved
        callback.accept("success!");
    }
}

@Named
@SessionScoped
public class CowoBean {

    @EJB
    private LongRunningProcessBean longRunningProcessBean;

    public void startLongRunningOperation() {
        longRunningProcessBean.executeLongRunningOperation(this::onOperationComplete);
    }

    private void onOperationComplete(Strint result) {
            if (result.equals("success"){
               FacesContext context = FacesContext.getCurrentInstance(); // **error, not able to retrieve Context**
               context.getApplication().getNavigationHandler().handleNavigation(context, null, "/cowo/results.xhtml?faces-redirect=true");
            }
    } }


The thread on Twitter where I first discussed the issue:


Thank you and best regards,

Clement
-- 
Sent with Proton Mail secure email.
_______________________________________________
faces-dev mailing list
faces-dev@xxxxxxxxxxx
To unsubscribe from this list, visit https://www.eclipse.org/mailman/listinfo/faces-dev
_______________________________________________
faces-dev mailing list
faces-dev@xxxxxxxxxxx
To unsubscribe from this list, visit https://www.eclipse.org/mailman/listinfo/faces-dev

Back to the top