參考:http://mina.apache.org/mina-project/userguide/ch17-spring-integration/ch17-spring-integration.html
本文演示了如何將MINA應(yīng)用程序與Spring集成岩臣。我在博客上寫了這篇文章,盡管把它放在這里,但實(shí)際上是這些信息所屬的地方。可以在將Apache MINA與Spring集成中找到原始副本锚国。
應(yīng)用結(jié)構(gòu)
我們將采用具有以下構(gòu)造的標(biāo)準(zhǔn)MINA應(yīng)用程序
- 一個處理程序
- 兩個過濾器-日志記錄過濾器和ProtocolCodec過濾器
- NioDatagram套接字
初始化碼
讓我們先來看代碼。為了簡單起見,我們省略了粘合代碼概页。
public void initialize() throws IOException {
// Create an Acceptor
NioDatagramAcceptor acceptor = new NioDatagramAcceptor();
// Add Handler
acceptor.setHandler(new ServerHandler());
acceptor.getFilterChain().addLast("logging",
new LoggingFilter());
acceptor.getFilterChain().addLast("codec",
new ProtocolCodecFilter(new SNMPCodecFactory()));
// Create Session Configuration
DatagramSessionConfig dcfg = acceptor.getSessionConfig();
dcfg.setReuseAddress(true);
logger.debug("Starting Server......");
// Bind and be ready to listen
acceptor.bind(new InetSocketAddress(DEFAULT_PORT));
logger.debug("Server listening on "+DEFAULT_PORT);
}
整合過程
要與Spring集成,我們需要執(zhí)行以下操作:
- 設(shè)置IO處理程序
- 創(chuàng)建過濾器并添加到鏈中
- 創(chuàng)建套接字并設(shè)置套接字參數(shù)
注意:最新的MINA版本沒有特定于Spring的軟件包练慕,如其早期版本惰匙。該程序包現(xiàn)在稱為Integration Beans,以使該實(shí)現(xiàn)適用于所有DI框架铃将。
讓我們看一下Spring xml文件项鬼。請注意,我已經(jīng)從xml中刪除了通用部分劲阎,并且只放置了實(shí)現(xiàn)該實(shí)現(xiàn)所需的特定內(nèi)容绘盟。該示例源自MINA版本隨附的聊天示例。請參考聊天附帶的xml示例。
現(xiàn)在讓我們把東西拉在一起
讓我們在spring上下文文件中設(shè)置IO處理程序
<!-- The IoHandler implementation -->
<bean id="trapHandler" class="com.ashishpaliwal.udp.mina.server.ServerHandler">
讓我們創(chuàng)建過濾器鏈
<bean id="snmpCodecFilter" class="org.apache.mina.filter.codec.ProtocolCodecFilter">
<constructor-arg>
<bean class="com.ashishpaliwal.udp.mina.snmp.SNMPCodecFactory" />
</constructor-arg>
</bean>
<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>
在這里龄毡,我們創(chuàng)建IoFilter的實(shí)例吠卷。看到對于ProtocolCodec工廠沦零,我們使用了構(gòu)造函數(shù)注入撤嫩。日志過濾器的創(chuàng)建很簡單。定義了要使用的過濾器的bean之后蠢终,我們現(xiàn)在創(chuàng)建要用于實(shí)現(xiàn)的過濾器鏈序攘。我們定義一個ID為“ FilterChainBuidler”的bean,并向其中添加定義的過濾器寻拂。我們已經(jīng)準(zhǔn)備就緒程奠,我們只需要創(chuàng)建Socket并調(diào)用bind
讓我們完成創(chuàng)建套接字和完成鏈的最后一部分
<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 161 -->
<bean id="ioAcceptor" class="org.apache.mina.transport.socket.nio.NioDatagramAcceptor" init-method="bind" destroy-method="unbind">
<property name="defaultLocalAddress" value=":161" />
<property name="handler" ref="trapHandler" />
<property name="filterChainBuilder" ref="filterChainBuilder" />
</bean>
現(xiàn)在,我們創(chuàng)建ioAcceptor祭钉,設(shè)置IO處理程序和Filter Chain∶樯常現(xiàn)在,我們必須編寫一個函數(shù)以使用Spring讀取此文件并啟動我們的應(yīng)用程序慌核。這是代碼
public void initializeViaSpring() throws Exception {
new ClassPathXmlApplicationContext("trapReceiverContext.xml");
}