From 00aa875ba8892867270e6f279f4fc448eb1e2b1f Mon Sep 17 00:00:00 2001 From: "shi.pengyan" Date: Mon, 26 Oct 2015 15:01:04 +0800 Subject: [PATCH] add mina; --- pom.xml | 24 +++++++- .../mybatis/mina/CustomProtocolHandler.java | 31 ++++++++++ src/main/resources/spring/spring-mina.xml | 44 +++++++++++++++ src/main/resources/spring/spring.xml | 1 + .../com/spy/mvc/mybatis/test/mina/Const.java | 11 ++++ .../test/mina/client/CustomIoHandler.java | 55 ++++++++++++++++++ .../mybatis/test/mina/client/MinaClient.java | 47 ++++++++++++++++ .../mina/intergration/IntergrationTest.java | 17 ++++++ .../test/mina/server/CustomIoHandler.java | 56 +++++++++++++++++++ .../mybatis/test/mina/server/MinaServer.java | 46 +++++++++++++++ 10 files changed, 331 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/spy/mvc/mybatis/mina/CustomProtocolHandler.java create mode 100644 src/main/resources/spring/spring-mina.xml create mode 100644 src/test/java/com/spy/mvc/mybatis/test/mina/Const.java create mode 100644 src/test/java/com/spy/mvc/mybatis/test/mina/client/CustomIoHandler.java create mode 100644 src/test/java/com/spy/mvc/mybatis/test/mina/client/MinaClient.java create mode 100644 src/test/java/com/spy/mvc/mybatis/test/mina/intergration/IntergrationTest.java create mode 100644 src/test/java/com/spy/mvc/mybatis/test/mina/server/CustomIoHandler.java create mode 100644 src/test/java/com/spy/mvc/mybatis/test/mina/server/MinaServer.java diff --git a/pom.xml b/pom.xml index e849dbb..d3d4958 100644 --- a/pom.xml +++ b/pom.xml @@ -8,7 +8,6 @@ mvc-mybatis Maven Webapp http://maven.apache.org - UTF-8 @@ -26,6 +25,8 @@ 3.2.8.RELEASE 3.3.0 1.2.3 + + 2.0.9 @@ -169,6 +170,7 @@ ${mybatisspring.version} + com.alibaba druid @@ -182,6 +184,26 @@ ${mysqlconnector.version} + + + + org.apache.mina + mina-core + ${mina.version} + + + + org.apache.mina + mina-integration-beans + ${mina.version} + + + org.apache.mina + mina-core + + + + junit diff --git a/src/main/java/com/spy/mvc/mybatis/mina/CustomProtocolHandler.java b/src/main/java/com/spy/mvc/mybatis/mina/CustomProtocolHandler.java new file mode 100644 index 0000000..6598115 --- /dev/null +++ b/src/main/java/com/spy/mvc/mybatis/mina/CustomProtocolHandler.java @@ -0,0 +1,31 @@ +package com.spy.mvc.mybatis.mina; + +import org.apache.mina.core.service.IoHandlerAdapter; +import org.apache.mina.core.session.IoSession; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * @author spy + * + * @datetime 2015年10月26日 下午2:00:48 + */ +public class CustomProtocolHandler extends IoHandlerAdapter { + + private static final Logger logger = LoggerFactory.getLogger(CustomProtocolHandler.class); + + @Override + public void messageReceived(IoSession session, Object message) throws Exception { + super.messageReceived(session, message); + + logger.info("received msg is {}", message); + + } + + @Override + public void messageSent(IoSession session, Object message) throws Exception { + // TODO Auto-generated method stub + super.messageSent(session, message); + } + +} diff --git a/src/main/resources/spring/spring-mina.xml b/src/main/resources/spring/spring-mina.xml new file mode 100644 index 0000000..9e228cd --- /dev/null +++ b/src/main/resources/spring/spring-mina.xml @@ -0,0 +1,44 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/main/resources/spring/spring.xml b/src/main/resources/spring/spring.xml index b31fcd5..7576e83 100644 --- a/src/main/resources/spring/spring.xml +++ b/src/main/resources/spring/spring.xml @@ -47,4 +47,5 @@ + \ No newline at end of file diff --git a/src/test/java/com/spy/mvc/mybatis/test/mina/Const.java b/src/test/java/com/spy/mvc/mybatis/test/mina/Const.java new file mode 100644 index 0000000..0a37b33 --- /dev/null +++ b/src/test/java/com/spy/mvc/mybatis/test/mina/Const.java @@ -0,0 +1,11 @@ +package com.spy.mvc.mybatis.test.mina; + +/** + * @author spy + * + * @datetime 2015年10月26日 下午2:51:32 + */ +public class Const { + + public static int PORT = 8888; +} diff --git a/src/test/java/com/spy/mvc/mybatis/test/mina/client/CustomIoHandler.java b/src/test/java/com/spy/mvc/mybatis/test/mina/client/CustomIoHandler.java new file mode 100644 index 0000000..b89976b --- /dev/null +++ b/src/test/java/com/spy/mvc/mybatis/test/mina/client/CustomIoHandler.java @@ -0,0 +1,55 @@ +package com.spy.mvc.mybatis.test.mina.client; + +import org.apache.mina.core.service.IoHandlerAdapter; +import org.apache.mina.core.session.IoSession; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * @author spy + * + * @datetime 2015年10月26日 上午11:23:17 + */ +public class CustomIoHandler extends IoHandlerAdapter { + + public static final Logger logger = LoggerFactory.getLogger(CustomIoHandler.class); + + @Override + public void sessionCreated(IoSession session) throws Exception { + // TODO Auto-generated method stub + + } + + @Override + public void sessionOpened(IoSession session) throws Exception { + // TODO Auto-generated method stub + + for (int i = 0; i < 20; i++) { + session.write("hello world " + i); + } + session.write("bye"); + } + + @Override + public void exceptionCaught(IoSession session, Throwable cause) throws Exception { + // TODO Auto-generated method stub + logger.error(cause.getMessage()); + } + + @Override + public void messageReceived(IoSession session, Object message) throws Exception { + // TODO Auto-generated method stub + logger.info("received msg is {}", message); + if (message.toString().equalsIgnoreCase("bye")) { + session.close(true); + } + } + + @Override + public void messageSent(IoSession session, Object message) throws Exception { + // TODO Auto-generated method stub + logger.info("sent msg is {}", message); + } + + +} diff --git a/src/test/java/com/spy/mvc/mybatis/test/mina/client/MinaClient.java b/src/test/java/com/spy/mvc/mybatis/test/mina/client/MinaClient.java new file mode 100644 index 0000000..73f2d15 --- /dev/null +++ b/src/test/java/com/spy/mvc/mybatis/test/mina/client/MinaClient.java @@ -0,0 +1,47 @@ +package com.spy.mvc.mybatis.test.mina.client; + +import java.net.InetSocketAddress; +import java.nio.charset.Charset; + +import org.apache.mina.core.future.ConnectFuture; +import org.apache.mina.core.service.IoConnector; +import org.apache.mina.core.session.IoSession; +import org.apache.mina.filter.codec.ProtocolCodecFilter; +import org.apache.mina.filter.codec.textline.TextLineCodecFactory; +import org.apache.mina.filter.logging.LoggingFilter; +import org.apache.mina.transport.socket.nio.NioSocketConnector; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.spy.mvc.mybatis.test.mina.Const; + +/** + * @author spy + * + * @datetime 2015年10月26日 上午11:09:34 + */ +public class MinaClient { + private static final Logger logger = LoggerFactory.getLogger(MinaClient.class); + + public static void main(String[] args) { + IoConnector connector = new NioSocketConnector(); + connector.setConnectTimeoutMillis(10 * 1000); + + connector.getFilterChain().addLast("logger", new LoggingFilter()); + connector.getFilterChain().addLast("codec", + new ProtocolCodecFilter(new TextLineCodecFactory(Charset.forName("UTF-8")))); + + connector.setHandler(new CustomIoHandler()); + + IoSession session = null; + + ConnectFuture future = connector.connect(new InetSocketAddress(Const.PORT)); + future.awaitUninterruptibly(); + session = future.getSession(); + + session.getCloseFuture().awaitUninterruptibly(); + connector.dispose(); + + logger.info("client end"); + } +} diff --git a/src/test/java/com/spy/mvc/mybatis/test/mina/intergration/IntergrationTest.java b/src/test/java/com/spy/mvc/mybatis/test/mina/intergration/IntergrationTest.java new file mode 100644 index 0000000..90454fa --- /dev/null +++ b/src/test/java/com/spy/mvc/mybatis/test/mina/intergration/IntergrationTest.java @@ -0,0 +1,17 @@ +package com.spy.mvc.mybatis.test.mina.intergration; + +import org.springframework.context.support.ClassPathXmlApplicationContext; + +import com.spy.mvc.mybatis.test.service.BaseSpringTest; + +/** + * @author spy + * + * @datetime 2015年10月26日 下午2:08:26 + */ +public class IntergrationTest extends BaseSpringTest { + + public static void main(String[] args) { + ClassPathXmlApplicationContext ct = new ClassPathXmlApplicationContext("spring/spring-mina.xml"); + } +} diff --git a/src/test/java/com/spy/mvc/mybatis/test/mina/server/CustomIoHandler.java b/src/test/java/com/spy/mvc/mybatis/test/mina/server/CustomIoHandler.java new file mode 100644 index 0000000..0d41447 --- /dev/null +++ b/src/test/java/com/spy/mvc/mybatis/test/mina/server/CustomIoHandler.java @@ -0,0 +1,56 @@ +package com.spy.mvc.mybatis.test.mina.server; + +import org.apache.mina.core.service.IoHandlerAdapter; +import org.apache.mina.core.session.IdleStatus; +import org.apache.mina.core.session.IoSession; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * @author spy + * + * @datetime 2015年10月26日 上午11:17:10 + */ +public class CustomIoHandler extends IoHandlerAdapter { + + private static final Logger logger = LoggerFactory.getLogger(CustomIoHandler.class); + + @Override + public void sessionCreated(IoSession session) throws Exception { + } + + @Override + public void sessionOpened(IoSession session) throws Exception { + } + + @Override + public void sessionClosed(IoSession session) throws Exception { + } + + @Override + public void sessionIdle(IoSession session, IdleStatus status) throws Exception { + + } + + @Override + public void exceptionCaught(IoSession session, Throwable cause) throws Exception { + logger.error(cause.getMessage()); + session.close(true); + } + + @Override + public void messageReceived(IoSession session, Object message) throws Exception { + logger.info("receive msg is {}", message); + if ("bye".equals(message)) { + session.write("bye"); + return; + } + session.write("this is server message, " + message); + } + + @Override + public void messageSent(IoSession session, Object message) throws Exception { + logger.info("send msg is {}", message); + } + +} diff --git a/src/test/java/com/spy/mvc/mybatis/test/mina/server/MinaServer.java b/src/test/java/com/spy/mvc/mybatis/test/mina/server/MinaServer.java new file mode 100644 index 0000000..17caa7d --- /dev/null +++ b/src/test/java/com/spy/mvc/mybatis/test/mina/server/MinaServer.java @@ -0,0 +1,46 @@ +package com.spy.mvc.mybatis.test.mina.server; + +import java.io.IOException; +import java.net.InetSocketAddress; +import java.nio.charset.Charset; + +import org.apache.mina.core.service.IoAcceptor; +import org.apache.mina.core.session.IdleStatus; +import org.apache.mina.filter.codec.ProtocolCodecFilter; +import org.apache.mina.filter.codec.textline.TextLineCodecFactory; +import org.apache.mina.filter.logging.LoggingFilter; +import org.apache.mina.transport.socket.nio.NioSocketAcceptor; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.spy.mvc.mybatis.test.mina.Const; + +/** + * @author spy + * + * @datetime 2015年10月26日 上午11:09:23 + */ +public class MinaServer { + private static final Logger logger = LoggerFactory.getLogger(MinaServer.class); + + public static void main(String[] args) { + IoAcceptor acceptor = new NioSocketAcceptor(); + + //TODO还是手动控制比较方便 + acceptor.getFilterChain().addLast("logger", new LoggingFilter()); + acceptor.getFilterChain().addLast("codec", + new ProtocolCodecFilter(new TextLineCodecFactory(Charset.forName("UTF-8")))); + + acceptor.setHandler(new CustomIoHandler()); + acceptor.getSessionConfig().setReadBufferSize(2048); + acceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, 10); + + try { + acceptor.bind(new InetSocketAddress(Const.PORT)); + } catch (IOException e) { + logger.error(e.getMessage()); + } + logger.debug("Mina Server started"); + } + +}