Skip to content

Commit

Permalink
add mina;
Browse files Browse the repository at this point in the history
  • Loading branch information
shi.pengyan committed Oct 26, 2015
1 parent 3bf99c6 commit 00aa875
Show file tree
Hide file tree
Showing 10 changed files with 331 additions and 1 deletion.
24 changes: 23 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
<name>mvc-mybatis Maven Webapp</name>
<url>http://maven.apache.org</url>


<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

Expand All @@ -26,6 +25,8 @@
<spring.version>3.2.8.RELEASE</spring.version>
<mybatis.version>3.3.0</mybatis.version>
<mybatisspring.version>1.2.3</mybatisspring.version>

<mina.version>2.0.9</mina.version>
</properties>


Expand Down Expand Up @@ -169,6 +170,7 @@
<version>${mybatisspring.version}</version>
</dependency>

<!-- druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
Expand All @@ -182,6 +184,26 @@
<version>${mysqlconnector.version}</version>
</dependency>

<!-- mina -->

<dependency>
<groupId>org.apache.mina</groupId>
<artifactId>mina-core</artifactId>
<version>${mina.version}</version>
</dependency>

<dependency>
<groupId>org.apache.mina</groupId>
<artifactId>mina-integration-beans</artifactId>
<version>${mina.version}</version>
<exclusions>
<exclusion>
<groupId>org.apache.mina</groupId>
<artifactId>mina-core</artifactId>
</exclusion>
</exclusions>
</dependency>

<!-- test -->
<dependency>
<groupId>junit</groupId>
Expand Down
31 changes: 31 additions & 0 deletions src/main/java/com/spy/mvc/mybatis/mina/CustomProtocolHandler.java
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);
}

}
44 changes: 44 additions & 0 deletions src/main/resources/spring/spring-mina.xml
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>
1 change: 1 addition & 0 deletions src/main/resources/spring/spring.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,5 @@

<!-- <import resource="spring-security.xml" /> -->
<import resource="spring-mybatis.xml" />
<import resource="spring-mina.xml" />
</beans>
11 changes: 11 additions & 0 deletions src/test/java/com/spy/mvc/mybatis/test/mina/Const.java
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;
}
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 src/test/java/com/spy/mvc/mybatis/test/mina/client/MinaClient.java
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");
}
}
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");
}
}
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 src/test/java/com/spy/mvc/mybatis/test/mina/server/MinaServer.java
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");
}

}

0 comments on commit 00aa875

Please sign in to comment.