Apache mina 与Spring整合设计

其实很简单,看了网上很多例子,但是发现还是Apache自己写的比较好,于是就抄下来了。
先说配置文件,[color=red]注意这里不能使用延迟加载和“byName”的方式找bean[/color]

<?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) {
this.session = session;
}

public void send(byte[] data) {
if (session.isConnected() && ArrayUtils.isEmpty(data)) {
ByteBuffer byteBuffer = ByteBuffer.allocate(data.length);
byteBuffer.put(data);
byteBuffer.flip();
session.write(byteBuffer);
}
}

public void receive(byte[] data) {
//Service的实现操作
}

其实这玩意主要还是配置文件,这个还是抄自IoAcceptorFactoryBean的API的说明,呵呵
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值