服务端代码如下:
Java代码 收藏代码
public class MinaTimeServer {
/** We will use a port above 1024 to be able to launch the server with a standard user */
private static final int PORT = 9123;
/**
* The server implementation. It's based on TCP, and uses a logging filter
* plus a text line decoder.
*/
public static void main(String[] args) throws IOException {
// Create the acceptor
IoAcceptor acceptor = new NioSocketAcceptor();
// Add two filters : a logger and a codec
acceptor.getFilterChain().addLast( "logger", new LoggingFilter() );
acceptor.getFilterChain().addLast( "codec", new ProtocolCodecFilter( new TextLineCodecFactory( Charset.forName( "UTF-8" ))));
//acceptor.getFilterChain().addLast("exector", new ExecutorFilter(Executors.newCachedThreadPool()));
acceptor.getFilterChain().addLast("exector", new ExecutorFilter());
// Attach the business logic to the server
acceptor.setHandler( new TimeServerHandler() );
// Configurate the buffer size and the iddle time
acceptor.getSessionConfig().setReadBufferSize( 2048 );
acceptor.getSessionConfig().setIdleTime( IdleStatus.BOTH_IDLE, 10 );
// And bind !
acceptor.bind( new InetSocketAddress(PORT) );
}
}
业务处理器(这里当作一个时间服务器,接受到信息之后就立刻返回服务器当前时间)
Java代码 收藏代码
public class TimeServerHandler extends IoHandlerAdapter
{
/**
* 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
{
String str = message.toString();
if( str.trim().equalsIgnoreCase("quit") ) {
// "Quit" ? let's get out ...
session.close(true);
return;
}
// Send the current date back to the client
Date date = new Date();
session.write( date.getTime());
System.out.println("Message written...");
}
/**
* 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 ));
}
}
public void mutiSendMsg() {
// 创建客户端连接器.
NioSocketConnector connector = new NioSocketConnector();
connector.getFilterChain().addLast("logger", new LoggingFilter());
connector.getFilterChain().addLast(
"codec",
new ProtocolCodecFilter(new TextLineCodecFactory(Charset
.forName("UTF-8")))); // 设置编码过滤器
connector.setConnectTimeoutMillis(30000);
connector.setHandler(new TimeClientHandler());// 设置事件处理器
ConnectFuture cf = connector.connect(new InetSocketAddress("127.0.0.1",
9908));// 建立连接
cf.awaitUninterruptibly();// 等待连接创建完成
cf.getSession().write("hello");// 发送消息
cf.getSession().write("quit");// 发送消息
cf.getSession().getCloseFuture().awaitUninterruptibly();// 等待连接断开
connector.dispose();
}
public static void main(String[] args) {
/*
MinaTimeClient client = new MinaTimeClient();
for(int i = 0; i < 1000; i ++) {
client.mutiSendMsg();
}*/
//int size = keywordMap.size();
long startTime = System.currentTimeMillis();
int thread_num = 100;
int client_num = 2000;
// TODO Auto-generated method stub
ExecutorService exec = Executors.newCachedThreadPool();
// 50个线程可以同时访问
final Semaphore semp = new Semaphore(thread_num);
final CountDownLatch countDownLatch = new CountDownLatch(client_num);
List<Thread> list = new ArrayList<Thread>();
// 模拟2000个客户端访问
for (int index = 0; index < client_num; index++) {
final int NO = index;
Thread run = new Thread() {
public void run() {
while(true){
try {
// 获取许可
//semp.acquire();