FacesContext.getCurrentInstance () returns null in the Runnable class

I try to get FacesContextby calling FacesContext.getCurrentInstance()in a run()class method Runnable, but returns null.

public class Task implements Runnable {

    @Override
    public void run() {
        FacesContext context = FacesContext.getCurrentInstance(); // null!
        // ...
    }

}

How is this caused and how can I solve it?

+5
source share
1 answer

FacesContextstored as ThreadLocalin the thread responsible for the HTTP request that called FacesServlet, which is responsible for the creation FacesContext. This thread usually only passes through JSF-controlled bean methods. FacesContextnot available in other threads generated by this thread.

. , , HTTP- HTTP . - HTTP.

-. : ? ? Runnable .

, .

public class Task implements Runnable {

    private Work work;

    public Task(Work work) {
        this.work = work;
    }

    @Override
    public void run() {
        // Just use work.
    }

}
Work work = (Work) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("work");
Task task = new Task(work);
// ...

, , , . , , , , . "push". SSE websockets. - : JSF/Java EE. PrimeFaces, <p:push>. OmniFaces, <o:socket>.


, Runnable - Java EE . Q & A, , :

+12

All Articles