Return-Path: Delivered-To: apmail-mina-users-archive@www.apache.org Received: (qmail 81111 invoked from network); 29 Jan 2010 02:36:17 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.3) by minotaur.apache.org with SMTP; 29 Jan 2010 02:36:17 -0000 Received: (qmail 12810 invoked by uid 500); 29 Jan 2010 02:36:16 -0000 Delivered-To: apmail-mina-users-archive@mina.apache.org Received: (qmail 12743 invoked by uid 500); 29 Jan 2010 02:36:15 -0000 Mailing-List: contact users-help@mina.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: users@mina.apache.org Delivered-To: mailing list users@mina.apache.org Received: (qmail 12733 invoked by uid 99); 29 Jan 2010 02:36:15 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 29 Jan 2010 02:36:15 +0000 X-ASF-Spam-Status: No, hits=-0.0 required=10.0 tests=SPF_PASS X-Spam-Check-By: apache.org Received-SPF: pass (athena.apache.org: domain of paliwalashish@gmail.com designates 209.85.160.53 as permitted sender) Received: from [209.85.160.53] (HELO mail-pw0-f53.google.com) (209.85.160.53) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 29 Jan 2010 02:36:05 +0000 Received: by pwi4 with SMTP id 4so2450321pwi.32 for ; Thu, 28 Jan 2010 18:35:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:in-reply-to:references :date:message-id:subject:from:to:content-type :content-transfer-encoding; bh=o1NSOx7VdGn6BF3T12bGGzAWpPs+6W/1QDDrm12U7WI=; b=axNH4utF39ow0tInmtgTp2wEHm5Uixu7rzbjEBWW8+rtyOlKxI8Gagdcq7wxjXcUaD Ip0imMxtqwHhCxztcoJglCDVBXs0rte1izh1wjWwbfNrRvSdXrpXzltz+ibbfJQ+BsLM uXPWQ5wChxL5S9w5fqw8HXtqt89VVtOPcEjWA= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type:content-transfer-encoding; b=K7Jm97HZ9y0P/b2OTGmhn9pzl2cxcrHTiv+leVNK6BwtwvqsXHqZ47i0hW3HVnjiN2 c02qmPkB7HX/CBOQ0jejJau9oy4iHT6+mM0hq8SEWebwcvBUBWjXT5qlXcxzuVVVpFIX i8uog2zeAALF5uT1UrPTW0tFv39ABguTphc4c= MIME-Version: 1.0 Received: by 10.140.247.16 with SMTP id u16mr216756rvh.2.1264732545325; Thu, 28 Jan 2010 18:35:45 -0800 (PST) In-Reply-To: <4B61B0BE.60102@darose.net> References: <4B616267.6080109@informal.com.br> <4B61B0BE.60102@darose.net> Date: Fri, 29 Jan 2010 08:05:45 +0530 Message-ID: Subject: Re: how to shutdown a mina application From: Ashish To: users@mina.apache.org Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Thanks David! @jose For Ctrl + C, like scenario, create a shutdown hook and register. In the shutdown hook code, do the cleaning stuff. thanks ashish On Thu, Jan 28, 2010 at 9:13 PM, David Rosenstrauch wro= te: > On 01/28/2010 05:09 AM, jose vilmar estacio de souza wrote: >> >> Hi all >> Perhaps a stupid question, but I didn't find an answer in the examples. >> I'd like to know how to shutdown a mina application in an elegant way. >> Actually I kill my applications pressing ctrl+c. >> >> What I'd like: >> a: Block incoming connections. >> b: Close all open sessions gracefully. >> C: Finish the application. >> >> Any ideas? >> Thanks. > > I did it by adding a shutdown command to the protocol. =A0Then when the s= erver > receives the message "shutdown" it executes this: > > > public class ShutdownCommand extends MinaServerProtocolCommand { > ... > =A0 =A0 =A0 =A0public ProtocolResponse execute() throws Exception { > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0boolean closeImmediately =3D false; > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0Object[] args =3D getArgs(); > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0if (args.length > 0) { > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0String closeImmediatelyStr= =3D (String)args[0]; > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0closeImmediately =3D > Boolean.parseBoolean(closeImmediatelyStr); > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0} > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0CloseFuture closeFuture =3D > getSession().close(closeImmediately); > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0closeFuture.addListener(new CloseSessionLi= stener()); > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0return null; > =A0 =A0 =A0 =A0} > ... > =A0 =A0 =A0 =A0class CloseSessionListener implements IoFutureListener { > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0public void operationComplete(CloseFuture = future) { > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0getServer().shutDown(); > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0} > =A0 =A0 =A0 =A0} > } > > > getServer().shutDown() then does additional cleanup, like shutting down > thread pools & timers, as well as calling out to my mina "protocol > processors" telling them to shut down: > > > public class ProfileCacheServer implements Server { > ... > =A0 =A0 =A0 =A0public synchronized void shutDown() { > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0if (shutDown) { > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0return; > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0} > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0logger.info("shutting down"); > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0if (textProtocolProcessor !=3D null) { > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0textProtocolProcessor.shut= down(); > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0} > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0if (binaryProtocolProcessor !=3D null) { > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0binaryProtocolProcessor.sh= utdown(); > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0} > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0shutdownCore(); > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0shutDown =3D true; > =A0 =A0 =A0 =A0} > > =A0 =A0 =A0 =A0private void shutdownCore() { > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0logger.debug("unregistering cache node mbe= an: {}", > cacheNodeObjectName.toString()); > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0try { > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0jmxMBeanServer.unregisterM= Bean(cacheNodeObjectName); > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0} > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0catch (JMException e) { > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0logger.error("error occurr= ed while unregistering > cache node mbean", e); > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0} > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0logger.debug("shutting cache loader thread= s"); > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0cacheNode.shutdownThreads(); > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0logger.debug("shutting down stats timer"); > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0timer.cancel(); > =A0 =A0 =A0 =A0} > ... > } > > > And the protocol processor shutdown looks something like this: > > public class ProtocolProcessor { > ... > =A0 =A0 =A0 =A0public void shutdown() { > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0if (protocolAcceptorObjectName !=3D null) = { > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0logger.debug("{} protocol = processor unregistering > protocol acceptor mbean: {}", protocolType, > protocolAcceptorObjectName.toString()); > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0try { > > =A0jmxMBeanServer.unregisterMBean(protocolAcceptorObjectName); > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0} > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0catch (JMException e) { > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0logger.err= or("error occurred while > unregistering protocol acceptor mbean", e); > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0} > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0} > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0logger.debug("{} protocol processor shutti= ng down server > socket", protocolType); > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0protocolAcceptor.unbind(); > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0protocolAcceptor.dispose(); > =A0 =A0 =A0 =A0} > ... > } > > > (FYI - protocolAcceptor is an NioSocketAcceptor in the above.) > > > HTH, > > DR >