日期:2014-05-16 浏览次数:20898 次
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd" default-lazy-init="false"> <!-- This makes it possible to specify java.net.SocketAddress values (e.g. :80 below) as Strings. They will be converted into java.net.InetSocketAddress objects by Spring. --> <bean class="org.springframework.beans.factory.config.CustomEditorConfigurer"> <property name="customEditors"> <map> <entry key="java.net.SocketAddress"> <bean class="org.apache.mina.integration.spring.InetSocketAddressEditor" /> </entry> </map> </property> </bean> <!-- The IoHandler implementation --> <bean id="myHandler" class="com.mina.MyProtocolHandler"> </bean> <bean id="filterChainBuilder" class="org.apache.mina.integration.spring.DefaultIoFilterChainBuilderFactoryBean"> <property name="filters"> <list> <bean class="org.apache.mina.filter.LoggingFilter" /> </list> </property> </bean> <!-- By default MINA uses an ExecutorThreadModel. This demonstrates how to use your own with some non default settings. The threadModel will be set on the SocketAcceptorConfig defined below. To configure a ExecutorFilter directly you will have to use the ThreadModel.MANUAL ThreadModel instead. --> <bean id="threadModel" class="org.apache.mina.integration.spring.ExecutorThreadModelFactoryBean"> <property name="serviceName" value="MyMinaService" /> <property name="executor"> <bean class="org.apache.mina.integration.spring.ThreadPoolExecutorFactoryBean"> <property name="corePoolSize" value="2" /> <property name="maxPoolSize" value="30" /> <property name="keepAliveSeconds" value="30" /> </bean> </property> </bean> <bean id="ioAcceptor" class="org.apache.mina.integration.spring.IoAcceptorFactoryBean"> <property name="target"> <bean class="org.apache.mina.transport.socket.nio.SocketAcceptor" /> </property> <property name="bindings"> <list> <bean class="org.apache.mina.integration.spring.Binding"> <property name="address" value=":8081" /> <property name="handler" ref="myHandler" /> <property name="serviceConfig"> <bean class="org.apache.mina.transport.socket.nio.SocketAcceptorConfig"> <property name="filterChainBuilder" ref="filterChainBuilder" /> <property name="reuseAddress" value="true" /> <property name="threadModel" ref="threadModel" /> </bean> </property> </bean> </list> </property> </bean> </beans>
public class MyProtocolHandler extends IoHandlerAdapter { @Override public void messageReceived(IoSession session, Object message) throws Exception { if (message instanceof ByteBuffer) { ByteBuffer rb = (ByteBuffer) message; byte[] moMessage = new byte[rb.remaining()]; rb.get(moMessage); Endpoint endpoint = new MinaEndpoint(session); endpoint.receive(moMessage); } super.messageReceived(session, message); } }
public class MinaEndpoint implements Endpoint { private IoSession session; public MinaEndpoint(IoSession session) {