netty future

ligengyong2010 2013-10-28 05:01:07
本人新编写了一个netty的hello程序,代码如下:
public class HelloWorldServer {
public static void main(String[]args){
// Server服务启动器
ServerBootstrap bootstrap =
new ServerBootstrap(
new NioServerSocketChannelFactory(
Executors.newCachedThreadPool(),
Executors.newCachedThreadPool())
);
bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
public ChannelPipeline getPipeline() {
ChannelPipeline pipeline = Channels.pipeline();
pipeline.addLast("decoder", new StringDecoder());
pipeline.addLast("encoder", new StringEncoder());
pipeline.addLast("handler", new HelloWorldServerHandler());
return pipeline;
}
});
bootstrap.bind(new InetSocketAddress(8088));
}
}

public class HelloWorldServerHandler extends SimpleChannelHandler{
public void channelConnected(ChannelHandlerContext ctx,ChannelStateEvent e)
throws Exception {
e.getChannel().write("Hello, World");
}
public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) {
System.out.println("Unexpected exception from downstream." + e.getCause());
e.getChannel().close();
}
}

public class HelloWorldClient {
public static void main(String []args){
ClientBootstrap bootstrap = new ClientBootstrap(
new NioClientSocketChannelFactory(
Executors.newCachedThreadPool(),
Executors.newCachedThreadPool())
);
bootstrap.setPipelineFactory(
new ChannelPipelineFactory(){
public ChannelPipeline getPipeline() {
ChannelPipeline pipeline = Channels.pipeline();
pipeline.addLast("decoder", new StringDecoder());
pipeline.addLast("encoder", new StringEncoder());
pipeline.addLast("handler", new HelloWorldClientHandler());
return pipeline;
}
}
);
ChannelFuture future = bootstrap.connect(new InetSocketAddress("localhost", 8088));
future.getChannel().getCloseFuture().awaitUninterruptibly();
bootstrap.releaseExternalResources();
}

}

public class HelloWorldClientHandler extends SimpleChannelHandler {
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) {
String message = (String) e.getMessage();
System.out.println(message);
e.getChannel().close();
}
public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) {
System.out.println("Unexpected exception from downstream." + e.getCause());
e.getChannel().close();
}
}
在例子中ChannelFuture和java本身的Future有什么区别呀,另外可否有高手简单介绍一下nio netty和mina的区别,nio需要注册读和写事件,mina也是时间过滤的 netty采用什么机理呢
...全文
73 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧