We have a servlet as follows:
public class CacheRefresher extends HttpServlet {
private static final long START_TIMEOUT = 120*1000;
public void init(ServletConfig servletConfig) throws ServletException {
super.init(servletConfig);
new Thread(new Worker()).start();
}
private class Worker implements Runnable {
public Worker() { }
public void run() {
try {
Thread.sleep(START_TIMEOUT);
} catch (InterruptedException e) {
}
while(true) {
MyService myService = null;
try {
myService = ServiceFactory.getInstance().getMyService();
myService.doSomething();
} catch (Exception ex){
ex.printStackTrace();
}finally {
ServiceFactory.getInstance().releaseMyService(myService);
}
try {
Thread.sleep(timeout);
} catch (InterruptedException e) {
}
}
}
}
}
Its purpose is to periodically call a service. There will only be a single instance of this Servlet, which will be created on server startup. MyService is an EJB.
How bad is this? I know spawning threads from EJBs is not allowed, but what about the other way around? What will happen on server shutdown?
Conceptualy i dont see a problem with invoking ejb methods from multiple threads (even if you created the threads yourself). For the ejb-container that will be just another client among others.
From your example it looks like the soul purpose of you servlet is to start a bunch of timers. If you can use ejb 3.1, there is java ee standard way to do that.
First a Singleton ejb that launches the timers on startup
Then a SLSB that handles the timeout: