spring如何整合mina

linsir 发布于 2010/11/17 23:39
阅读 4K+
收藏 1

使用mina框架,请问怎么和spring整合啊。

加载中
0
红薯
红薯

二者重叠的地方几乎没有吧?

0
linsir
linsir

我的意思是说。我创建一个web项目。我tomcat启动的时候就把mina服务端线程启动。而不是创建一个java项目启动mina。

0
Flexer
Flexer

public class MonitorConfig implements ServletContextAware {
 public static final String MHS = "messageHandlerMap";
 private String host;
 private int port;
 private String charset;
 private int threadNum;
 private Map<String, MessageHandler> messageHandlers;
 private ServletContext servletContext;

 public void setServletContext(ServletContext servletContext) {
  this.servletContext = servletContext;
  this.startMonitorServer();
 }

 public void startMonitorServer() {
  try {
   MonitorServer server = new MonitorServer(host, port, charset,
     threadNum, messageHandlers);
   server.start();
  } catch (IOException e) {
   e.printStackTrace();
  }
  servletContext.setAttribute(MHS, messageHandlers);
 }

 public int getPort() {
  return port;
 }

 public void setPort(int port) {
  this.port = port;
 }

 public String getCharset() {
  return charset;
 }

 public void setCharset(String charset) {
  this.charset = charset;
 }

 public int getThreadNum() {
  return threadNum;
 }

 public void setThreadNum(int threadNum) {
  this.threadNum = threadNum;
 }

 public Map<String, MessageHandler> getMessageHandlers() {
  return messageHandlers;
 }

 public void setMessageHandlers(Map<String, MessageHandler> messageHandlers) {
  this.messageHandlers = messageHandlers;
 }

 public String getHost() {
  return host;
 }

 public void setHost(String host) {
  this.host = host;
 }
}

配置文件中:

<bean id="monitorConfig"
  class="com.linkage.olcom5mp.am.udp.MonitorConfig">
  <property name="port">
   <value type="int">12345</value>
  </property>
  <property name="charset">
   <value type="java.lang.String">GBK</value>
  </property>
  <property name="threadNum">
   <value type="int">2</value>
  </property>
  <property name="messageHandlers">
   <map key-type="java.lang.String"
    value-type="com.linkage.olcom.am.server.MessageHandler">
    <entry key="alarm" value-ref="alarmMonitorHandler"/>
    <entry key="alarmcancel" value-ref="alarmCancelHandler"/>
    <entry key="mostate" value-ref="moStateHandler"/>
   </map>
  </property>
 </bean>

//MessageHandler,alarmMonitorHandler,moStateHandler,alarmCancelHandler等类的代码省略了

其实就是实现ServletContextAware接口,并且在setServletContext方法里面让你的MINA服务端线程启动。

你试试!

0
大湿胸
大湿胸

简单:

作为spring的bean注入

<!-- 定义消息编解码过滤器链-->

<bean id="filterChainBuilder"

class="org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder">

<property name="filters">

<map>

<entry key="codecFilter"

value-ref="codecFilter"/>

<entry key="loggingFilter"

value-ref="loggingFilter"/>

</map>

</property>

</bean>

<!-- The IoAcceptor which binds to port 2345 server side-->

<bean id="minaAcceptor"

class="org.apache.mina.transport.socket.nio.NioSocketAcceptor"

init-method="bind"

destroy-method="unbind">

<property name="defaultLocalAddress"

value=":2345"/>

<property name="handler"

ref="connectHandler"/>

<property name="reuseAddress"

value="true"/>

<property name="filterChainBuilder"

ref="filterChainBuilder"/>

</bean>

0
返回顶部
顶部