-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
shi.pengyan
committed
Oct 26, 2015
1 parent
3bf99c6
commit 00aa875
Showing
10 changed files
with
331 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
src/main/java/com/spy/mvc/mybatis/mina/CustomProtocolHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" | ||
xsi:schemaLocation=" | ||
http://www.springframework.org/schema/beans | ||
http://www.springframework.org/schema/beans/spring-beans.xsd | ||
http://www.springframework.org/schema/context | ||
http://www.springframework.org/schema/context/spring-context.xsd" | ||
default-autowire="byName"> | ||
|
||
|
||
<bean name="customProtocolHandler" class="com.spy.mvc.mybatis.mina.CustomProtocolHandler" /> | ||
<bean id="loggingFilter" class="org.apache.mina.filter.logging.LoggingFilter" /> | ||
|
||
<!-- The filter chain. --> | ||
<bean id="filterChainBuilder" class="org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder"> | ||
<property name="filters"> | ||
<map> | ||
<entry key="loggingFilter" value-ref="loggingFilter" /> | ||
<!-- <entry key="codecFilter" value-ref="snmpCodecFilter" /> --> | ||
</map> | ||
</property> | ||
</bean> | ||
|
||
<bean class="org.springframework.beans.factory.config.CustomEditorConfigurer"> | ||
<property name="customEditors"> | ||
<map> | ||
<entry key="java.net.SocketAddress"> | ||
<bean class="org.apache.mina.integration.beans.InetSocketAddressEditor" /> | ||
</entry> | ||
</map> | ||
</property> | ||
</bean> | ||
|
||
<!-- The IoAcceptor which binds to port 8888 --> | ||
<!-- 这里有很多的Acceptor,需要选择正确的acceptor,否则协议不对应则不可以使用 --> | ||
<bean id="ioAcceptor" class="org.apache.mina.transport.socket.nio.NioSocketAcceptor" init-method="bind" | ||
destroy-method="unbind"> | ||
<property name="defaultLocalAddress" value=":8888" /> | ||
<property name="handler" ref="customProtocolHandler" /> | ||
<property name="filterChainBuilder" ref="filterChainBuilder" /> | ||
</bean> | ||
|
||
</beans> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
55 changes: 55 additions & 0 deletions
55
src/test/java/com/spy/mvc/mybatis/test/mina/client/CustomIoHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
|
||
|
||
} |
47 changes: 47 additions & 0 deletions
47
src/test/java/com/spy/mvc/mybatis/test/mina/client/MinaClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/test/java/com/spy/mvc/mybatis/test/mina/intergration/IntergrationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"); | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
src/test/java/com/spy/mvc/mybatis/test/mina/server/CustomIoHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
|
||
} |
46 changes: 46 additions & 0 deletions
46
src/test/java/com/spy/mvc/mybatis/test/mina/server/MinaServer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"); | ||
} | ||
|
||
} |