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 <[email protected]> wrote: > 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. Then when the server > receives the message "shutdown" it executes this: > > > public class ShutdownCommand extends MinaServerProtocolCommand { > ... > public ProtocolResponse execute() throws Exception { > boolean closeImmediately = false; > Object[] args = getArgs(); > if (args.length > 0) { > String closeImmediatelyStr = (String)args[0]; > closeImmediately = > Boolean.parseBoolean(closeImmediatelyStr); > } > CloseFuture closeFuture = > getSession().close(closeImmediately); > closeFuture.addListener(new CloseSessionListener()); > return null; > } > ... > class CloseSessionListener implements IoFutureListener<CloseFuture> { > public void operationComplete(CloseFuture future) { > getServer().shutDown(); > } > } > } > > > 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 { > ... > public synchronized void shutDown() { > if (shutDown) { > return; > } > logger.info("shutting down"); > if (textProtocolProcessor != null) { > textProtocolProcessor.shutdown(); > } > if (binaryProtocolProcessor != null) { > binaryProtocolProcessor.shutdown(); > } > shutdownCore(); > shutDown = true; > } > > private void shutdownCore() { > logger.debug("unregistering cache node mbean: {}", > cacheNodeObjectName.toString()); > try { > jmxMBeanServer.unregisterMBean(cacheNodeObjectName); > } > catch (JMException e) { > logger.error("error occurred while unregistering > cache node mbean", e); > } > > logger.debug("shutting cache loader threads"); > cacheNode.shutdownThreads(); > > logger.debug("shutting down stats timer"); > timer.cancel(); > } > ... > } > > > And the protocol processor shutdown looks something like this: > > public class ProtocolProcessor { > ... > public void shutdown() { > if (protocolAcceptorObjectName != null) { > logger.debug("{} protocol processor unregistering > protocol acceptor mbean: {}", protocolType, > protocolAcceptorObjectName.toString()); > try { > > jmxMBeanServer.unregisterMBean(protocolAcceptorObjectName); > } > catch (JMException e) { > logger.error("error occurred while > unregistering protocol acceptor mbean", e); > } > } > logger.debug("{} protocol processor shutting down server > socket", protocolType); > protocolAcceptor.unbind(); > protocolAcceptor.dispose(); > } > ... > } > > > (FYI - protocolAcceptor is an NioSocketAcceptor in the above.) > > > HTH, > > DR >
