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

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

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

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

首先上spring的配置文件

  
  
  
  
   
  
      
    
  
		
		
		
		
	
	
    
        
        
    
	    
        
            
     
  
  
      
  
  
  	
  	
  	
		
			
				 
			
		
	
	
	
   	
   
	
	
		
		
        
	

	
	

    
     
	
	
		
	
	
          
    
    
    
        
            
                
                
                
                
            
        
    
    
    
   
  

启动多端口监听的类

/**
 * 
 */
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的整合,这里不再列出,有什么疑问的请留言。





你可能感兴趣的:(Spring-mybaits-mina整合,启动多端口监听)