tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Bill Miller" <>
Subject RE: how to correct stop a thread and avoid leaks
Date Tue, 07 Jun 2011 15:33:46 GMT
If you want to work with threads in tomcat you need to know about the Tomcat
org.apache.catalina.LifecycleListener interface. If you implement the interface and register
class within server.xml your class will be notified of Tomcat life cycle events (Start, Stop,
etc...). You can then do appropriate things when those events happen.

Here is an example method that is the only method in LifecycleListener:
public class TomcatServerLifecycleListener implements LifecycleListener
	public void lifecycleEvent(LifecycleEvent event)
			//Call a method on your Runnable that will trigger it to exit the Run()
			// (or set a Boolean to stop looping, I've adjusted your code below too; how
			// set the flag is up to you)
Here is the line to be added to Server.xml to register a class as a listener:
<Listener className=""/>

So if you created an object like I have above and gave it a static method that allows you
to insert
an object that needs to be notified of a shutdown event, then you will be able to control
threads properly. You could even have your Runnable implement the LifecycleListener directly
have your registered object keep a list of objects that need to be notified. 

There are dozens of ways to do this, pick one that works for you. Don't forget to investigate
all of
the LifecycleEvent types in case you have use for the other options.


-----Original Message-----
From: alexis [] 
Sent: June 6, 2011 8:10 PM
To: Tomcat Users List
Subject: how to correct stop a thread and avoid leaks

Hello, im running an app that has a class that implements ServletContextListener (Server class),
that class i create a thread of Listener (that implements Runnable)

Listener starts a ServerSocket that listen on a port, and in a periodically manner, this app
receives a string, parses the string and stores the data inside a database. Flow is

1. receive a tcp connection (syn , syn+ack, ack)
2. receives lines over and over again inside this connections (each line is parsed and stored)

Below is the code of Listener class and how i call this class from Server class. What im facing
im not able to stop Listener in a correct way. doing Listener.interrupt() does nothing. How
can i
safely stop Listener thread

Thanks in advance

 //initalize listener
        Listener = new Thread((Runnable) new
        if (!Listener.isAlive()) {


package lesi.p1;

import org.apache.log4j.Logger;

 * @author alz
public class Listener implements Runnable {

    private static org.apache.log4j.Logger log = Logger.getLogger(Listener.class);
    private Socket clientSocket = null;
    private ServerSocket serverSocket = null;
    private int listenport = 0;
    Thread CDRThread = null;

    public Listener(int port) {
        this.listenport = port;
    public void run() {
        try {
            log.debug("About to listen on port: " + this.listenport);
            serverSocket = new ServerSocket(this.listenport);

            while (true) {
                clientSocket = serverSocket.accept();
                CDRThread = new Thread((Runnable) new CDRExec(clientSocket));
                if (!CDRThread.isAlive()) {
        } catch (IOException e) {

            log.error("Catcher IOException: ", e);
        } finally {
            try {
      "Closing socket");
            } catch (IOException ex) {
                log.error("Error trying to close port", ex);

            if (CDRThread.isAlive()) {
      "Stopping CDRExec Thread");


    class CDRExec implements Runnable {

	public Boolean keepRunning = true;	// !! Shutdown flag! Set to false to exit loop
        private Socket client = null;

        public CDRExec(Socket client) {
            this.client = client;

        public void run() {
            String inputLine = "";
            StringBuilder sb = new StringBuilder();
            Parser p = null;
            BufferedReader in = null;

            log.debug("Thread: " + Thread.currentThread().getId() + " name: " +

            try {
                p = new Parser();
                in = new BufferedReader(new InputStreamReader(client.getInputStream()));

                while (keepRunning) {	// !! Use the shutdown flag instead of true
                    if ((inputLine = in.readLine()) != null) {
                    inputLine = null;
            } catch (IOException e1) {
                try {
                } catch (Throwable ignore) {
                p = null;

To unsubscribe, e-mail:
For additional commands, e-mail:

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message