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));
}
}
分享到:
相关推荐
这个“Apache MINA (2) Hello World!”示例旨在帮助初学者理解MINA的基本用法。 MINA的核心概念是基于NIO(非阻塞I/O)的事件驱动模型,它允许应用程序在处理多个连接时保持高效率。在MINA中,我们创建一个服务端来...
**Mina入门:Mina版之HelloWorld** Apache Mina是一个开源项目,它提供了一个高度模块化、高性能的网络通信框架。Mina旨在简化网络应用的开发,支持多种传输协议,如TCP、UDP、HTTP、FTP等。在这个“Mina入门:Mina...
return newFixedLengthResponse(NanoHTTPD.HTTP_OK, "text/html", "<h1>Hello, World!</h1>"); } else { return newFixedLengthResponse(NanoHTTPD.HTTP_NOT_FOUND, "text/plain", "404 Not Found"); } } ...
其中值得一提的是,Netty 的主要贡献者之一也是 Apache MINA(另一个流行的网络应用框架)的重要贡献者。这表明 Netty 在设计之初就吸收了大量来自 MINA 的经验教训,并在此基础上进行了创新和发展。 ##### 1.3 ...
相对于其他Java中的XMPP库,如JabberClient或Apache Mina,Smack以其易用性、强大的功能和活跃的社区支持而受到开发者喜爱。它的API设计使得开发者可以快速上手,同时提供的扩展性使其在各种复杂的XMPP应用中表现...