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));
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值