问题
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