日期:2014-05-16  浏览次数:20898 次

Apache mina 与Spring整合设计
其实很简单,看了网上很多例子,但是发现还是Apache自己写的比较好,于是就抄下来了。
先说配置文件,注意这里不能使用延迟加载和“byName”的方式找bean
<?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>


再然后我们只需要实现MyProtocolHandler就可以了,只在接收数据加了处理,数据传输用的是byte[],MinaEndpoint里面来执行自己的代码就行了
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);
	}

}

最后就是EndPoint了,其实就两个方法,一个接收,一个发送
public class MinaEndpoint implements Endpoint {
	
	private IoSession session;
	
	public MinaEndpoint(IoSession session) {