1
votes

I am using JSF to create a web app.

I have an application scoped bean that has a infinite loop so that it periodically performs an action.

My problem is that this bean (due to the infinite loop) is blocking the whole application. I thought that the bean would run on its own thread. Isnt that the case with JSF, that each managed bean is running on own thread by default?

Should I create a thread and let the infinite loop run in that thread instead?

thank you

1

1 Answers

6
votes

Does application scoped beans run on seperate thread in JSF?

No, it definitely doesn't.

Should I create a thread and let the infinite loop run in that thread instead?

No, you shouldn't. You should just create a scheduled task. Best way to that is to use a @Singleton @Schedule EJB.

@Singleton
public class SomeDailyJob {

    @Schedule(hour="0", minute="0", second="0", persistent=false)
    public void run() {
        // Do your job here which should run daily at midnight.
    }

} 

That's all. No additional configuration of manually messing with threads necessary. If you want to access its state -if any- from in a JSF managed bean, just inject with @EJB the usual way.

See also: