Mina的JMX支持

以下是一个增加了JMX支持的Mina Echo Server.

package org.apache.mina.echoServer;

import java.lang.management.ManagementFactory;

import java.net.InetSocketAddress;

import javax.management.MBeanServer;

import javax.management.ObjectName;

import org.apache.mina.transport.socket.SocketAcceptor;

import org.apache.mina.transport.socket.nio.NioSocketAcceptor;

import org.apache.mina.integration.jmx.IoServiceMBean;

public class Main { public static void main(String[] args) throws Exception { SocketAcceptor acceptor = new NioSocketAcceptor(); acceptor.setHandler(new EchoProtocolHandler()); acceptor.bind(new InetSocketAddress(PORT)); System.out.println("Listening on port " + 8293); MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer(); IoServiceMBean acceptorMBean = new IoServiceMBean(acceptor); ObjectName acceptorName = new ObjectName(acceptor.getClass().getPackage().getName() + ":type=acceptor,name=" + acceptor.getClass().getSimpleName()); mBeanServer.registerMBean(acceptorMBean, acceptorName); } }

package org.apache.mina.echoServer;

import org.apache.mina.core.buffer.IoBuffer;

import org.apache.mina.core.service.IoHandlerAdapter;

import org.apache.mina.core.session.IdleStatus;

import org.apache.mina.core.session.IoSession;

import org.apache.mina.filter.ssl.SslFilter;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

public class EchoProtocolHandler extends IoHandlerAdapter { private static final Logger LOGGER = LoggerFactory.getLogger(EchoProtocolHandler.class); @Override public void exceptionCaught(IoSession session, Throwable cause) { session.close(true); } @Override public void messageReceived(IoSession session, Object message) throws Exception { LOGGER.info("Received : " + message); session.write(((IoBuffer) message).duplicate()); } }

ivy.xml:

启动服务是需要加VM参数-Dcom.sun.management.jmxremote:

命令行启动:java -Dcom.sun.management.jmxremote -classpath org.apache.mina.echoServer.Main

Eclipse中启动:Run->Debug configuration中,Name: Main , VM arguments: -Dcom.sun.management.jmxremote

启动JConsole,连接org.apache.mina.echoServer.Main;

进入Main的监控窗口后MBean->操作,可以手工运行acceptor的方法,例如点击Dispose,会关闭acceptor。

posted on 2014-03-18 08:08  leechau  阅读(202)  评论(0编辑  收藏  举报

导航