I am trying to use the new Concurrency API to enter ManagedThreadFactory and use it for an Oracle tutorial .
Here is an example of what I am saying:
@Singleton
@Startup
public class Demo {
@Resource(name="concurrent/__DefaultManagedThreadFactory") ManagedThreadFactory threadFactory;
@PostConstruct
public void startup() {
threadFactory.newThread(
new Runnable() {
@Override
public void run() {
System.out.println("Do something.");
}
}
).start();
}
}
I am developing in Eclipse using the Glassfish plugin. When I republish after making changes, I always get this line in the server log. It appears once for each start () call we make:
SEVERE: java.lang.IllegalStateException: Module (my application) is disabled
In fact, this does not throw an IllegalStateException, simply reporting that it was thrown (and caught) inside Glassfish. The application deploys normally, but none of the threads start. If I subsequently reprint it a second time, the “error” will disappear and the threads will start as expected.
"" Glassfish ( Eclipse), , "". - ( ).
API Concurrency? ? , ManagedExcecutorService.
: ManagedThread Singleton Enterprise Java Bean?, , , - , . !
UPDATE: Per-Axel Felth . ! , :
@Singleton
@Startup
public class Demo {
@Resource(name="java:comp/DefaultManagedThreadFactory") ManagedThreadFactory threadFactory;
@EJB private ConcurrencyInitializer concurrencyInitializer;
@EJB private Demo self;
@PostConstruct
public void startup() {
self.startThread();
}
@Asynchronous
public void startThread() {
concurrencyInitializer.init();
threadFactory.newThread(
new Runnable() {
@Override
public void run() {
System.out.println("Do something.");
}
}
).start();
}
}
@Singleton
public class ConcurrencyInitializer {
public static final long RETRY_DELAY = 500L;
public static final int MAX_RETRIES = 20;
public boolean init() {
final AtomicBoolean done = new AtomicBoolean(false);
int i = 0;
try {
while (!done.get() && i++ < MAX_RETRIES) {
executorService.submit(new Runnable() {
@Override
public void run() {
done.set(true);
}
});
Thread.sleep(RETRY_DELAY);
}
} catch(InterruptedException e) {
}
return done.get();
}
}