Return-Path: X-Original-To: apmail-tomcat-users-archive@www.apache.org Delivered-To: apmail-tomcat-users-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 539B86E5E for ; Tue, 7 Jun 2011 15:39:39 +0000 (UTC) Received: (qmail 70394 invoked by uid 500); 7 Jun 2011 15:39:35 -0000 Delivered-To: apmail-tomcat-users-archive@tomcat.apache.org Received: (qmail 70260 invoked by uid 500); 7 Jun 2011 15:39:35 -0000 Mailing-List: contact users-help@tomcat.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: "Tomcat Users List" Delivered-To: mailing list users@tomcat.apache.org Received: (qmail 70251 invoked by uid 99); 7 Jun 2011 15:39:35 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 07 Jun 2011 15:39:35 +0000 X-ASF-Spam-Status: No, hits=-0.1 required=5.0 tests=FREEMAIL_FROM,HK_RANDOM_ENVFROM,RCVD_IN_DNSWL_LOW,RFC_ABUSE_POST,SPF_PASS X-Spam-Check-By: apache.org Received-SPF: pass (athena.apache.org: domain of alzrck@gmail.com designates 209.85.218.45 as permitted sender) Received: from [209.85.218.45] (HELO mail-yi0-f45.google.com) (209.85.218.45) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 07 Jun 2011 15:39:28 +0000 Received: by yia27 with SMTP id 27so1430926yia.18 for ; Tue, 07 Jun 2011 08:39:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:content-type:mime-version:subject:from :in-reply-to:date:content-transfer-encoding:message-id:references:to :x-mailer; bh=IzhaSxQQtWvXXYrhfLDJ4eAE2wHflBEcdOsmlhJRXxY=; b=YTVPrUUOMuhhMzpL2DgtUUUV5Eo1mZuupZNxXxbp0FSR3ycJle6q6ERKR5W/xu6CUv 3mTty9cBnewwDmZKJyhQh04/BIGyRrRl03PXvOw4iv0dvPDaS5/vA5r26GKwvvMfklZt Fmidrc/VP8J8JhmY6DULvxFY7Dd+r6Pc6jn5E= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=content-type:mime-version:subject:from:in-reply-to:date :content-transfer-encoding:message-id:references:to:x-mailer; b=UbwTjicSbPY9AhK3yIXrGYeq2fMdsR/gUFBWegAOENBkPJZpNFN1xWFKvctkQyXaSP 7my5LujYNox8vTp4M0N+Ly5r0EbsADi8KHu3Q2tCd4XBA4ihh0aP17aJgZziwihCMQX3 Twslgyu0wWHOwRPb/Vc60nONX9VBsw8zA4Wgw= Received: by 10.236.76.106 with SMTP id a70mr597831yhe.139.1307461145435; Tue, 07 Jun 2011 08:39:05 -0700 (PDT) Received: from [10.0.1.4] ([201.255.57.177]) by mx.google.com with ESMTPS id k10sm174351yhj.36.2011.06.07.08.39.02 (version=TLSv1/SSLv3 cipher=OTHER); Tue, 07 Jun 2011 08:39:04 -0700 (PDT) Content-Type: text/plain; charset=us-ascii Mime-Version: 1.0 (Apple Message framework v1084) Subject: Re: how to correct stop a thread and avoid leaks From: alexis In-Reply-To: <4dee449b.0b610e0a.6050.ffffcf93@mx.google.com> Date: Tue, 7 Jun 2011 12:38:59 -0300 Content-Transfer-Encoding: quoted-printable Message-Id: <1EA939D4-DD1C-408A-B0F2-B2017F2DF7B1@gmail.com> References: <4dee449b.0b610e0a.6050.ffffcf93@mx.google.com> To: Tomcat Users List X-Mailer: Apple Mail (2.1084) 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=20 > if ((inputLine =3D in.readLine()) !=3D 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. >=20 > 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.=20 > // (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: > >=20 > 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.=20 >=20 > 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. >=20 > Bill >=20 > -----Original Message----- > From: alexis [mailto:alzrck@gmail.com]=20 > Sent: June 6, 2011 8:10 PM > To: Tomcat Users List > Subject: how to correct stop a thread and avoid leaks >=20 > 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) >=20 > 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 >=20 > 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) >=20 > 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 >=20 >=20 > Thanks in advance >=20 >=20 > -- > //initalize listener > Listener =3D new Thread((Runnable) new > Listener(Integer.parseInt(prop.getProperty("listenonport")))); > if (!Listener.isAlive()) { > Listener.setName("ListenerThread"); > Listener.setDaemon(true); > Listener.start(); > } >=20 > --=20 >=20 > package lesi.p1; >=20 > 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; >=20 > /** > * > * @author alz > */ > public class Listener implements Runnable { >=20 > private static org.apache.log4j.Logger log =3D = Logger.getLogger(Listener.class); > private Socket clientSocket =3D null; > private ServerSocket serverSocket =3D null; > private int listenport =3D 0; > Thread CDRThread =3D null; >=20 > public Listener(int port) { > this.listenport =3D port; > } >=20 > @Override > public void run() { > try { > log.debug("About to listen on port: " + this.listenport); > serverSocket =3D new ServerSocket(this.listenport); >=20 > while (true) { > clientSocket =3D serverSocket.accept(); > CDRThread =3D new Thread((Runnable) new = CDRExec(clientSocket)); > if (!CDRThread.isAlive()) { > CDRThread.setName("CDRThread"); > CDRThread.setDaemon(true); > CDRThread.start(); > } > } > } catch (IOException e) { >=20 > log.error("Catcher IOException: ", e); > } finally { > try { > log.info("Closing socket"); > serverSocket.close(); > } catch (IOException ex) { > log.error("Error trying to close port", ex); > } >=20 > if (CDRThread.isAlive()) { > log.info("Stopping CDRExec Thread"); > CDRThread.interrupt(); > } >=20 > } > } >=20 > class CDRExec implements Runnable { >=20 > public Boolean keepRunning =3D true; // !! Shutdown flag! Set = to false to exit loop > private Socket client =3D null; >=20 > public CDRExec(Socket client) { > this.client =3D client; > } >=20 > @Override > public void run() { > String inputLine =3D ""; > StringBuilder sb =3D new StringBuilder(); > Parser p =3D null; > BufferedReader in =3D null; >=20 > log.debug("Thread: " + Thread.currentThread().getId() + " = name: " + > Thread.currentThread().getName()); >=20 > try { > p =3D new Parser(); > in =3D new BufferedReader(new = InputStreamReader(client.getInputStream())); >=20 > while (keepRunning) { // !! Use the shutdown flag = instead of true > if ((inputLine =3D in.readLine()) !=3D null) { > p.parseCDR(inputLine); > } > inputLine =3D null; > } > } catch (IOException e1) { > try { > in.close(); > } catch (Throwable ignore) { > } > p =3D null; > } > } > } > } >=20 >=20 >=20 >=20 >=20 > --------------------------------------------------------------------- > To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org > For additional commands, e-mail: users-help@tomcat.apache.org >=20 --------------------------------------------------------------------- To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org For additional commands, e-mail: users-help@tomcat.apache.org