Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Threading in an Application Server

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.

like image 723
mainstringargs Avatar asked Feb 10 '09 13:02

mainstringargs


People also ask

What is thread in application server?

Thread Pools in the Application ServerTo help performance, the Application Server maintains one or more thread pools. It is possible to assign specific thread pools to connector modules and to the ORB. One thread pool can serve multiple connector modules and enterprise beans.

What is a threading process?

Threading is a manufacturing and metal processing method used to create the helical detailed edges of a screw to enable it to be fastened to another piece of wood, metal or material. Threading may be the source of corrosion in some materials due to the crevices created by the process.

What is threading in software development?

A thread represents a part of a program which can be run independently. It is also the basic unit to which an operating system allocates processor time. Having multiple threads in this way can improve the performance of the application in a big way.

What is threading explain with example?

Thread is often referred to as a lightweight process. The process can be split down into so many threads. For example, in a browser, many tabs can be viewed as threads. MS Word uses many threads - formatting text from one thread, processing input from another thread, etc.


2 Answers

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.

like image 161
Sarel Botha Avatar answered Oct 04 '22 10:10

Sarel Botha


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

like image 33
Robin Avatar answered Oct 04 '22 11:10

Robin