问题
I have a Java program/thread that I want to deploy into an Application Server (GlassFish). The thread should run as a "service" that starts when the Application Server starts and stops when the Application Server closes.
How would I go about doing this? It's not really a Session Bean or MDB. It's just a thread.
回答1:
Check out the LifecycleListener interface:
http://glassfish.java.net/nonav/docs/v3/api/index.html?com/sun/appserv/server/LifecycleListener.html
http://docs.oracle.com/cd/E18930_01/html/821-2418/beamc.html
回答2:
I've only done this with Tomcat, but it should work in Glassfish.
Create a Listener class that implements javax.servlet.ServletContextListener, then put it in web.xml. It will be notified when your web app is started and destroyed.
A simple Listener class:
public class Listener implements javax.servlet.ServletContextListener {
MyThread myThread;
public void contextInitialized(ServletContextEvent sce) {
myThread = new MyThread();
myThread.start();
}
public void contextDestroyed(ServletContextEvent sce) {
if (myThread != null) {
myThread.setStop(true);
myThread.interrupt();
}
}
}
This goes in web.xml after your last 'context-param' and before your first 'servlet':
<listener>
<listener-class>atis.Listener</listener-class>
</listener>
Don't know whether this kind of thing is recommended or not, but it has worked fine for me in the past.
回答3:
This is not something that you should do in any app server, unless you have access to managed threads provided by the app server. I am not familiar with Glassfish, but you could do this in Websphere or Weblogic using a commonj WorkManager.
Apparently, the same can be accomplished in Glassfish and JBOSS via a JCA WorkManager (which I am not familiar with).
回答4:
Create a servlet whose init method starts a thread which is the main program.
public void init() throws ServletException {
mailThread = new MailSendThread();
mailThread.start();
}
In our application's web.xml file add a servlet that includes a load-on-startup element where the number is the order in which it starts.
<servlet>
<servlet-name>Mail Sending Servlet</servlet-name>
<servlet-class>MailServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
回答5:
I also need to create several threads where each thread will open a socket to another remote processes running in my Glassfish App. Server. I looked into the LifecycleListener bean provided by Glassfish that you need to implement.
I created a prototype to perform the threading and socket work in the LifecycleListener implementation and it really didn't help with the management of these resources. To get access to the LifecycleListener I had to put a public static method that would execute the actions desired.
I see no value in LifecycleListener because I could have performed the exact same work inside my EJB, which is the client calling the LifecycleListener. Because there really is no proper management of the Thread and Socket in the bean.
I was told that JCA may be the best way to go. I have not tried this.
回答6:
I start a timed object with the timer service and only one single expiration. Then, in the timeout, I do what I wanted to do with the thread.
http://onjava.com/pub/a/onjava/2004/10/13/j2ee-timers.html
For me it worked since it uses J2EE components and is a different thread.
来源:https://stackoverflow.com/questions/532360/threading-in-an-application-server