views:

913

answers:

6

I have a Java program/thread that I want to deploy into an App Server (glassfish). The thread should run as a "service" that starts when the App server starts and stops when the App Server closes. How would I go about doing this? Its not really a Session Bean or MDB.. Its just a thread.

A: 

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>
Michael Glenn
+1  A: 

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).

Robin
+3  A: 

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.

sjbotha
This seems to me much cleaner than hijacking a servlet.init() method. Still, I feel a bit uncomfortable about starting threads in an AppServer.
Guillaume
+1 this is the was to do it. if you are unconfortable about managing your own thread.. start up quartz inside the app server. but do it inside the SCL like described here..
Andreas Petersson
+2  A: 

Check out the LifeCycle Listener:

https://glassfish.dev.java.net/javaee5/api/com/sun/appserv/server/LifecycleListener.html

http://docs.sun.com/app/docs/doc/820-4336/beamc?a=view

John Clingan
A: 

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.

Peter Delaney
A: 

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.

Rafa de Castro