NioServerSocketChannelFactory创建服务端的ServerSocketChannel,采用多线程执行非阻塞IO,和Mina的设计
模式一样,都采用了Reactor模式。其中bossExecutor、workerExecutor是两个线程池,bossExecutor用来接收客户端连接,
workerExecutor用来执行非阻塞的IO操作,主要是read,write。
使用到的jar
<dependency>
<groupId>org.jboss.netty</groupId>
<artifactId>netty</artifactId>
<version>3.2.9.Final</version>
</dependency>
</dependencies>
import java.net.InetSocketAddress;
import java.util.concurrent.Executors;
import org.jboss.netty.bootstrap.ServerBootstrap;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.channel.ChannelPipelineFactory;
import org.jboss.netty.channel.ChannelStateEvent;
import org.jboss.netty.channel.Channels;
import org.jboss.netty.channel.ExceptionEvent;
import org.jboss.netty.channel.MessageEvent;
import org.jboss.netty.channel.SimpleChannelHandler;
import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
import org.jboss.netty.handler.codec.string.StringDecoder;
import org.jboss.netty.handler.codec.string.StringEncoder;
public class DiscardServer {
public static void main(String[] args) {
// Server服务启动器
ServerBootstrap bootstrap = new ServerBootstrap(
new NioServerSocketChannelFactory(
Executors.newCachedThreadPool(),
Executors.newCachedThreadPool()));
// 设置一个处理客户端消息和各种消息事件的类(Handler)
bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
@Override
public ChannelPipeline getPipeline() throws Exception {
ChannelPipeline pipeline = Channels.pipeline();
pipeline.addLast("encode",new StringEncoder());
pipeline.addLast("decode",new StringDecoder());
pipeline.addLast("handler",new HelloServerHandler());
return pipeline;
}
});
// 开放8000端口供客户端访问。
bootstrap.bind(new InetSocketAddress(8000));
}
private static class HelloServerHandler extends SimpleChannelHandler {
/**
* 当有客户端绑定到服务端的时候触发,打印"Hello world, I'm server."
*
* @alia OneCoder
* @author lihzh
*/
@Override
public void channelConnected(ChannelHandlerContext ctx,
ChannelStateEvent e) {
System.out.println("Hello world, I'm server.");
}
@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e)
throws Exception {
String message = (String) e.getMessage();
System.out.println(message);
e.getChannel().close();
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e)
throws Exception {
System.out.println("服务端异常"+e.getCause());
e.getChannel().close();
}
}
}
public class HelloClient {
// Client服务启动器
private static ClientBootstrap bootstrap = new ClientBootstrap(
new NioClientSocketChannelFactory(
Executors.newCachedThreadPool(),
Executors.newCachedThreadPool()));
public static int i=0;
public static void main(String args[]) throws InterruptedException {
while(true) {
// 设置一个处理服务端消息和各种消息事件的类(Handler)
bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
@Override
public ChannelPipeline getPipeline() throws Exception {
ChannelPipeline pipeline = Channels.pipeline();
pipeline.addLast("encode",new StringEncoder());
pipeline.addLast("decode",new StringDecoder());
pipeline.addLast("handler",new HelloClientHandler());
return pipeline;
}
});
// 连接到本地的8000端口的服务端
bootstrap.connect(new InetSocketAddress(
"127.0.0.1", 8000));
System.out.println("计数:"+(i++));
}
}
private static class HelloClientHandler extends SimpleChannelHandler {
@Override
public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e)
throws Exception {
System.out.println("客服端---"+e.getCause());
}
/**
* 当绑定到服务端的时候触发,打印"Hello world, I'm client."
*
* @alia OneCoder
* @author lihzh
*/
@Override
public void channelConnected(ChannelHandlerContext ctx,
ChannelStateEvent e) {
e.getChannel().write("xxxxxxxxxxxxx");
}
@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e)
throws Exception {
e.getChannel().close();
}
}
}