0

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?

D-Dᴙum
  • 7,689
  • 8
  • 58
  • 97
  • @perissf: Working with threads in the EJB container is discouraged in **JAVA EE 6 and prior**. In **JAVA EE 7** You can work with threads using `ManagedThreadFactory`, `ManagedExecutorService` and `ManagedScheduledExecutorService`. http://docs.oracle.com/javaee/7/tutorial/doc/concurrency-utilities.htm#GKJIQ8 – pWoz Dec 09 '13 at 13:31
  • You are right. I have never worked on this yet. I am going to remove the comment. – perissf Dec 09 '13 at 13:57

2 Answers2

1

What application server do You use?

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

pWoz
  • 1,649
  • 3
  • 20
  • 30
  • It's Glassfish 4. Whilst I'm only a beginner in the field of Java 7 EE I cannot see a reason why this should not work. There is nothing I see in the docs to say what I am doing is prohibited. – D-Dᴙum Dec 09 '13 at 22:15
  • The issue you posted describes what I see too so it may not be implementation specific? – D-Dᴙum Dec 09 '13 at 22:23
1

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

Community
  • 1
  • 1
Ed Mackenzie
  • 679
  • 6
  • 16