Spring-mybaits-mina整合,启动多端口监听

Spring-mybaits-mina三大框架的整合,mina可以使用Spring的数据库数据库连接池,无需额外定义mybaits连接池。

可以自定义监听多端口,端口数据可以从数据库中读取

不想看博客的话,项目完整下载地址:http://download.csdn.net/detail/j506825719/9743712

首先上spring的配置文件

<?xml version="1.0" encoding="UTF-8"?>  
  
<beans xmlns="http://www.springframework.org/schema/beans"  
  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  
    xmlns:mvc="http://www.springframework.org/schema/mvc"  
  
    xmlns:context="http://www.springframework.org/schema/context"  
  
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd  
  
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
  
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">  
  
   
  
    <mvc:default-servlet-handler/>  
    <!-- 连接数据库源 -->
  <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName"  value="oracle.jdbc.driver.OracleDriver"/>
		<property name="url"  value="jdbc:oracle:thin:@127.0.0.1:1521:orcl"/>
		<property name="username"  value="ookami"/>
		<property name="password"  value="123456"/>
	</bean>
	<!-- 定义sqlSessionFactory -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="configLocation" value="classpath:mybatis-config.xml"></property>
    </bean>
	<!-- 配置SqlSessionTemplate(数据库会话面板) -->    
    <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">    
        <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory" />    
    </bean> 
<mvc:annotation-driven></mvc:annotation-driven>  
  
    <context:component-scan base-package="com.gszh.devicereceive.*" >  <!-- 设定扫描目录 -->
  
</context:component-scan>  
  	
  	
  	<bean class="org.springframework.beans.factory.config.CustomEditorConfigurer">
		<property name="customEditors">
			<map>
				<entry key="java.net.SocketAddress" value="org.apache.mina.integration.beans.InetSocketAddressEditor"> </entry>
			</map>
		</property>
	</bean>
	
	<!--多端口监听的配置-->
   	<bean id="ConfigLearn" class="configLearn.ConfigLearn" init-method="bind"/>
   
	<!--主要实例-->
	<bean id="ioAcceptor" class="org.apache.mina.transport.socket.nio.NioSocketAcceptor">
		<property name="handler" ref="SampleHandler" />
		<property name="reuseAddress" value="true" />
        <property name="filterChainBuilder" ref="filterChainBuilder" />
	</bean>

	<!--消息处理函数-->
	<bean id="SampleHandler" class="com.gszh.devicereceive.listener.MinaServerHanlder" />

    
     <!--过滤器的配置-->
	<bean id="executorFilter" class="org.apache.mina.filter.executor.ExecutorFilter" />
	<bean id="mdcInjectionFilter" class="org.apache.mina.filter.logging.MdcInjectionFilter">
		<constructor-arg value="remoteAddress" />
	</bean>
	<bean id="codecFilter" class="org.apache.mina.filter.codec.ProtocolCodecFilter">
        <constructor-arg> <bean class="com.gszh.devicereceive.listener.MyDecode"></bean> </constructor-arg>
    </bean>
    <bean id="loggingFilter" class="org.apache.mina.filter.logging.LoggingFilter" />
    <bean id="filterChainBuilder" class="org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder">
        <property name="filters">
            <map>
                <entry key="executor" value-ref="executorFilter" />
                <entry key="mdcInjectionFilter" value-ref="mdcInjectionFilter" />
                <entry key="codecFilter" value-ref="codecFilter" />
                <entry key="loggingFilter" value-ref="loggingFilter" />
            </map>
        </property>
    </bean>
    
    
   
  
</beans>

启动多端口监听的类

/**
 * 
 */
package configLearn;

import java.io.IOException;
import java.net.InetSocketAddress;

import org.apache.mina.core.service.IoAcceptor;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @author Mu
 *
 */
public class ConfigLearn {
	@Autowired
	private SqlSessionTemplate sstSessionTemplate;
	@Autowired
	private IoAcceptor acceptor;
	
	public void bind() throws IOException{		
		//如果想要绑定多端口监听的话,在这里绑定多个即可
		acceptor.bind(new InetSocketAddress(9988));
		acceptor.bind(new InetSocketAddress(9989));
	}
}

定义解码器

/**
 * 
 */
package com.gszh.devicereceive.listener;

import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolCodecFactory;
import org.apache.mina.filter.codec.ProtocolDecoder;
import org.apache.mina.filter.codec.ProtocolEncoder;

/**
 * @author Mu
 *
 */
public class MyDecode implements ProtocolCodecFactory {
	
	private final CSDataEncoder encoder;
    private final CSDataDecoder decoder;
    
    public MyDecode() {
        encoder = new CSDataEncoder();
        
        decoder = new CSDataDecoder();
       
    }
    
	@Override
	public ProtocolDecoder getDecoder(IoSession iosession) throws Exception {
		
		return this.decoder;
	}

	@Override
	public ProtocolEncoder getEncoder(IoSession iosession) throws Exception {
		
		return this.encoder;
	}

	
}
encode

package com.gszh.devicereceive.listener;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolEncoderAdapter;
import org.apache.mina.filter.codec.ProtocolEncoderOutput;

public class CSDataEncoder extends ProtocolEncoderAdapter { 
	
	public void encode(IoSession session, Object message, ProtocolEncoderOutput output) throws Exception { 
		
		System.out.println("encode");
		
	} 
} 
decode

package com.gszh.devicereceive.listener;
import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.CumulativeProtocolDecoder;
import org.apache.mina.filter.codec.ProtocolDecoderOutput;

public class CSDataDecoder extends CumulativeProtocolDecoder {
	@Override
	protected boolean doDecode(IoSession session, IoBuffer buffer,ProtocolDecoderOutput output) throws Exception {

		System.out.println("doDecode");
		
		return false;
	}
} 


消息处理函数

package com.gszh.devicereceive.listener;
import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IoSession;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;

public class MessageHanlder extends IoHandlerAdapter {
	
	@Autowired
	public SqlSessionTemplate sst;
	
	//消息处理函数
	@Override
	public void messageReceived(IoSession session, Object message)throws Exception {
		System.out.println("收到");
	}
}

完整代码如上,至于spring和mybaits的整合,这里不再列出,有什么疑问的请留言。





©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页