tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From alexis <alz...@gmail.com>
Subject Re: how to correct stop a thread and avoid leaks
Date Tue, 07 Jun 2011 15:38:59 GMT
yes, did that and it's working now. when contextDestroyed is called, i set the bool to false
and force the loop out. What i was missing is loop is exited when 
> if ((inputLine = in.readLine()) != null)


this is completed, so, from Server class what i did is

Listener.requestStop(); // this causes to take the boolean to false to exit the loop
while(Listener.isAlive())
{
Thread.sleep(1500);
}

Causing main thread (Server) to wait, until a new message is processed by the in.ReadLine,
then parsed, then exit the loop and close the Listener thread, and then Listener.isAlive()
is not true anymore, now it's working perfect.


Thanks for the help.



On Jun 7, 2011, at 12:33 PM, Bill Miller wrote:

> 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
your
> 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)
> 	{
> 		if(Lifecycle.STOP_EVENT.equals(event.getType()))
> 		{
> 			//Call a method on your Runnable that will trigger it to exit the Run()
> method. 
> 			// (or set a Boolean to stop looping, I've adjusted your code below too; how
> you
> 			// 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="com.services.tomcat.TomcatServerLifecycleListener"/>
> 
> 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
your
> threads properly. You could even have your Runnable implement the LifecycleListener directly
and
> 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.
> 
> Bill
> 
> -----Original Message-----
> From: alexis [mailto:alzrck@gmail.com] 
> 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), on
> 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 is
> 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
> Listener(Integer.parseInt(prop.getProperty("listenonport"))));
>        if (!Listener.isAlive()) {
>            Listener.setName("ListenerThread");
>            Listener.setDaemon(true);
>            Listener.start();
>        }
> 
> -- 
> 
> package lesi.p1;
> 
> import java.io.BufferedReader;
> import java.io.IOException;
> import java.io.InputStreamReader;
> import java.net.ServerSocket;
> import java.net.Socket;
> 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;
>    }
> 
>    @Override
>    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()) {
>                    CDRThread.setName("CDRThread");
>                    CDRThread.setDaemon(true);
>                    CDRThread.start();
>                }
>            }
>        } catch (IOException e) {
> 
>            log.error("Catcher IOException: ", e);
>        } finally {
>            try {
>                log.info("Closing socket");
>                serverSocket.close();
>            } catch (IOException ex) {
>                log.error("Error trying to close port", ex);
>            }
> 
>            if (CDRThread.isAlive()) {
>                log.info("Stopping CDRExec Thread");
>                CDRThread.interrupt();
>            }
> 
>        }
>    }
> 
>    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;
>        }
> 
>        @Override
>        public void run() {
>            String inputLine = "";
>            StringBuilder sb = new StringBuilder();
>            Parser p = null;
>            BufferedReader in = null;
> 
>            log.debug("Thread: " + Thread.currentThread().getId() + " name: " +
> Thread.currentThread().getName());
> 
>            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) {
>                        p.parseCDR(inputLine);
>                    }
>                    inputLine = null;
>                }
>            } catch (IOException e1) {
>                try {
>                    in.close();
>                } catch (Throwable ignore) {
>                }
>                p = null;
>            }
>        }
>    }
> }
> 
> 
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Mime
View raw message