`
sdyjmc
  • 浏览: 5369 次
最近访客 更多访客>>
社区版块
存档分类
最新评论

Apace Mina Hello world - server

 
阅读更多

import java.net.InetSocketAddress;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.mina.core.service.IoAcceptor;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.serialization.ObjectSerializationCodecFactory;
import org.apache.mina.transport.socket.nio.NioSocketAcceptor;

public class MiniServer extends Thread {
	private static final Log log = LogFactory.getLog(MiniServer.class);

	private ExecutorService executor;

	private int portnumber;

	public MiniServer(int portnumber, ExecutorService executor) {
		this.portnumber = portnumber;
		this.executor = executor;
	}

	public void run() {
		try {
			IoAcceptor acceptor = new NioSocketAcceptor();
			acceptor.getFilterChain().addLast("codec", new ProtocolCodecFilter(new ObjectSerializationCodecFactory()));
			acceptor.setHandler(new NutServerHandler(executor));
			acceptor.getSessionConfig().setReadBufferSize(20480);
			acceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, 10);
			acceptor.bind(new InetSocketAddress(portnumber));

			log.info("成功打开网络套接字:" + portnumber);
		} catch (Exception ex) {
			log.error("创建网络服务异常");
			log.error(ex.getMessage(), ex);
		}
	}

	public static void main(String[] args) throws Exception {
		ThreadPoolExecutor executor = new ThreadPoolExecutor(10, 20, 1000, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(5), new ThreadPoolExecutor.CallerRunsPolicy());
		// 启动服务器
		new MiniServer(7000, executor).start();
	}
}


import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;

import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;

import cn.benguo.platform.nut.core.util.Parameter;
import cn.benguo.platform.nut.server.thread.ProcessCallable;

public class NutServerHandler extends IoHandlerAdapter {
	private ExecutorService executor;

	public NutServerHandler(ExecutorService executor) {
		this.executor = executor;
	}

	/**
	 * Trap exceptions.
	 */
	@Override
	public void exceptionCaught(IoSession session, Throwable cause) throws Exception {
		cause.printStackTrace();
	}

	/**
	 * If the message is 'quit', we exit by closing the session. Otherwise, we
	 * return the current date.
	 */
	@Override
	public void messageReceived(IoSession session, Object message) throws Exception {
		Parameter p = (Parameter) message;
		Callable<Object[]> call = new ProcessCallable(p);
		Future<Object[]> task = executor.submit(call);
		session.write(task.get());
	}

	/**
	 * On idle, we just write a message on the console
	 */
	@Override
	public void sessionIdle(IoSession session, IdleStatus status) throws Exception {
		System.out.println("IDLE " + session.getIdleCount(status));
	}
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics