spring 集成 mina入门

一、applicationContent.xml




	
	
	 
   		 
	   		
	   		 
	    
	 

	
	  
	    
	      
	       
	    
	  
	

	  
	      
	      
	      
	  

	
		
			
				
					
				
			
		
	 



二、业务处理类:

package com.nfhn.mina;

import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IoSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.nfhn.dto.TestDTO;
import com.nfhn.service.TestService;
import com.nfhn.util.XMLParser;

public class ServerHandle extends IoHandlerAdapter {

	private static Logger log = LoggerFactory.getLogger(ServerHandle.class);
	@Autowired
	private TestService testService;
	
	@Override
	public void messageReceived(IoSession session, Object message) throws Exception {
		try {
			String msg = (String)message;
			log.info("mina recieve message: " + msg);
			TestDTO dto = (TestDTO)XMLParser.unmarshal(msg, TestDTO.class);
			log.info(dto.toString());
			testService.insert(dto);
			session.write(System.currentTimeMillis());
		} catch(Exception e) {
			e.printStackTrace();
			throw e;
		}
	}
	@Override
	public void exceptionCaught(IoSession session, Throwable cause)
			throws Exception {
		System.out.println("error");
		super.exceptionCaught(session, cause);
	}
	

	
}

三、客户端:

package com.nfhn.action;

import java.net.InetSocketAddress;
import java.nio.charset.Charset;

import org.apache.log4j.Logger;
import org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder;
import org.apache.mina.core.future.ConnectFuture;
import org.apache.mina.core.future.IoFuture;
import org.apache.mina.core.future.IoFutureListener;
import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.textline.TextLineCodecFactory;
import org.apache.mina.filter.logging.LoggingFilter;
import org.apache.mina.transport.socket.DatagramConnector;
import org.apache.mina.transport.socket.nio.NioDatagramConnector;

import com.nfhn.dto.TestDTO;
import com.nfhn.util.XMLParser;

public class ClientNIO extends IoHandlerAdapter{
    private static Logger log = Logger.getLogger(ClientNIO.class);
    private DatagramConnector connector;
    private IoSession session;
    private String hostname = "127.0.0.1";
    private int port = 8888;

    public ClientNIO() {
    	inti();
    }
    private void inti() {
    	log.info("client");
        connector = new NioDatagramConnector();
        connector.setHandler(this);
        DefaultIoFilterChainBuilder chain = connector.getFilterChain();
        chain.addLast("logger", new LoggingFilter());
        chain.addLast("codec", new ProtocolCodecFilter(
                new TextLineCodecFactory(Charset.forName("UTF-8"))));
        IoFuture connFuture = connector.connect(new InetSocketAddress(hostname, port));
        connFuture.addListener(new IoFutureListener() {
            public void operationComplete(IoFuture future) {
                ConnectFuture connFuture = (ConnectFuture) future;
                if (connFuture.isConnected()) {
                    session = future.getSession();
                    try {
                    	sendData();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                } else {
                    log.error("Not connected...exiting");
                }
            }
        });
    }

    private void sendData() throws InterruptedException {
		TestDTO dto = new TestDTO();
		dto.setMessage("nfhn");
		dto.setTimes(String.valueOf(System.currentTimeMillis()));
		String str = XMLParser.marshal(dto, TestDTO.class);
		session.write(str);
    }
    

    @Override
	public void messageReceived(IoSession session, Object message)
			throws Exception {
		log.info("client recieve message: " + message.toString());
		Thread.sleep(10000);
		sendData();
	}
	public static void main(String[] args) {
        new ClientNIO();
    }

	
}

四、依赖包:

2.0.0-M2
		1.1.7
 
			org.apache.mina
			mina-core
			${mina.version}
		
		  
			org.apache.mina 
			mina-integration-spring 
			${mina.spring.version} 
		 
		 
			org.apache.mina 
			mina-integration-beans 
			${mina.version}
		  






你可能感兴趣的:(java后台)