Can I start a ManagedThread in a Singleton Enterprise Java Bean?

≡放荡痞女 提交于 2019-12-11 19:53:56

问题


I'm trying to start a thread in a Singleton EJB but java.lang.IllegalStateException is being thrown. This is my (cut-down) class:

Singleton
@LocalBean
@Startup
public class WatcherEJB {


    @Resource(name = "concurrent/masterActionsThreadFactor")
    ManagedThreadFactory threadFactory;

    Thread watcherThread;

    @PostConstruct
    public void startUp() {

        //Setup the listener using the ThreadFactory
        watcherThread = threadFactory.newThread(new Runnable() {

            @Override
            public void run() {
                //System.out.println("Watcher Thread started");
            }
        });
        watcherThread.start(); //java.lang.IllegalStateException thrown here
    } 
}

I'm assuming that there's a problem with when I'm trying to start the Thread object or does Java EE 7 not allow Managed threads in singletons?


回答1:


What application server do You use?

If it's WildFly You probably run into this issue: https://issues.jboss.org/browse/WFLY-2343




回答2:


I came across this thread when looking for a solution and I thought that I would post this link to a thread which I believe answers the question. I know it has been a while since this question was asked but it should be useful for future reference!

Glassfish 4 - Using Concurrency API to create Managed Threads



来源:https://stackoverflow.com/questions/20446682/can-i-start-a-managedthread-in-a-singleton-enterprise-java-bean

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!