문제

I have used the solution described in this discussion to implement BackgroundJobManager as an inner class inside my JSF bean class. I have also created an ABCTask (also an inner class) as a thread that will run at the scheduled time for the BackgroundJobManager. I have a requirement to push a message onto the JSF page but doing so from the task class ABCTask results in an NPE. The same thing works for the outer bean so i'm convinced it's something to do with the context of this inner class and the bean. Would appreciate if anyone knows a solution for this.

My inner class code is as below :

 public class ABCTask implements Runnable {

    public ABCTask() {

    }

    @Override
    public void run() {           
        setTimeoutOccuredFlag(true);

        try {
                if (getActiveControlledAgent().isEventLogRunning()) {
                    getActiveControlledAgent().setEventLogRunning(false);
                }
                printTimeoutMessage();
        logger_o.fine("Now leaving the ABCTask ...");
        } catch (Exception e) {
            logger_o.error("An error while idling "+e.getMessage());
        }
    }

}

The $printTimeoutMessage() is as follows :

void printTimeoutMessage() {
    FacesUtils.addErrorMessage(MESSAGES.getString("common.timeoutOccured"));
}
도움이 되었습니까?

해결책

You're basically manually spawning a thread. The FacesContext is only available in the thread serving the HTTP request which matches the URL pattern of the FacesServlet, not in other threads. The FacesContext#getCurrentInstance() would return null in all other threads.

Think once again, how would you ever send a message to the HTTP response without that the client has sent a HTTP request? By default, you can't send anything to the client without that it has requested for it.

Look for poll/push techniques. This concrete problem is completely unrelated to the class hierarchy/scope. In the future, it'd be more helpful for you and us if you've identified what exactly is null. You seem to not have done that at all.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top