java netty swap高_Netty NIO 框架性能压测 – 长链接

package org.dueam.sample.netty;

package org.dueam.sample.netty;

import java.io.BufferedReader;

import java.io.InputStreamReader;

import java.net.InetSocketAddress;

import java.util.HashMap;

import java.util.Map;

import java.util.Random;

import java.util.concurrent.Executors;

import org.jboss.netty.bootstrap.ServerBootstrap;

import org.jboss.netty.buffer.ChannelBuffer;

import org.jboss.netty.buffer.DynamicChannelBuffer;

import org.jboss.netty.channel.Channel;

import org.jboss.netty.channel.ChannelFactory;

import org.jboss.netty.channel.ChannelHandlerContext;

import org.jboss.netty.channel.ChannelPipeline;

import org.jboss.netty.channel.ChannelStateEvent;

import org.jboss.netty.channel.ExceptionEvent;

import org.jboss.netty.channel.MessageEvent;

import org.jboss.netty.channel.SimpleChannelHandler;

import org.jboss.netty.channel.ChannelHandler.Sharable;

import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;

public class ChatServer {

public static void main(String[] args) throws Exception {

if(args.length <1){

args = new String[]{"9876","true"};

}

ChannelFactory factory = new NioServerSocketChannelFactory(Executors

.newCachedThreadPool(), Executors.newCachedThreadPool());

ServerBootstrap bootstrap = new ServerBootstrap(factory);

ChatServerHandler handler = new ChatServerHandler();

ChannelPipeline pipeline = bootstrap.getPipeline();

pipeline.addLast("chat", handler);

bootstrap.setOption("child.tcpNoDelay", true);

bootstrap.setOption("child.keepAlive", true);

int port = Integer.valueOf(args[0]);

bootstrap.bind(new InetSocketAddress(port));

boolean fillChat = "true".equals(args[1]);

if (fillChat) {

ChannelManagerThread cmt = new ChannelManagerThread();

cmt.start();

}

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

while (true) {

String command = br.readLine();

if ("dump".equals(command)) {

System.out.println("当前活着的数量:" + channel.size());

} else if ("help".equals(command)) {

System.out.println("命令列表:");

System.out.println("dump:打印当前情况");

System.out.println("help:帮助文档");

}

}

}

final static Random random = new Random();

static int max = 0;

static class ChannelManagerThread extends Thread {

@Override

public void run() {

while (true) {

try {

if(max < channel.size()){

max = channel.size() ;

System.out.println("live:"+channel.size());

}

for (Channel s : channel.values()) {

if (random.nextInt(100)>70) {

ChannelBuffer cb = new DynamicChannelBuffer(256);

cb.writeBytes("Hey!有人来找你了!".getBytes());

s.write(cb);

}

}

sleep(500);

} catch (InterruptedException e) {

}

}

}

}

final static Map channel = new HashMap();

static void log(String message) {

System.out.println(message);

}

@Sharable

static class ChatServerHandler extends SimpleChannelHandler {

@Override

public void channelConnected(ChannelHandlerContext ctx,

ChannelStateEvent e) {

Channel ch = e.getChannel();

ChannelBuffer cb = new DynamicChannelBuffer(256);

cb.writeBytes("Hell!你来了啊!".getBytes());

ch.write(cb);

channel.put(e.getChannel().getId(), e.getChannel());

}

@Override

public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) {

}

@Override

public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) {

e.getCause().printStackTrace();

channel.remove(e.getChannel().getId());

log("remove channel by exception! id:" + e.getChannel().getId());

e.getChannel().close();

}

@Override

public void channelDisconnected(ChannelHandlerContext ctx,

ChannelStateEvent e) throws Exception {

channel.remove(e.getChannel().getId());

log("remove channel by exception! id:" + e.getChannel().getId());

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值