设为首页 加入收藏

TOP

Netty使用实例(一)
2014-11-24 02:50:53 来源: 作者: 【 】 浏览:4
Tags:Netty 使用 实例

贴上两个自己写好的Netty使用实例,以便备注,以下两个例子基于netty-3.5.7.Final.jar用Junit进行测试


第一个例子:简单的发送字符串,接收字符串“Hello, World”


package com.wujintao.netty;


import java.net.InetSocketAddress;
import java.util.concurrent.Executors;


import org.jboss.netty.bootstrap.ClientBootstrap;
import org.jboss.netty.bootstrap.ServerBootstrap;
import org.jboss.netty.channel.ChannelFuture;
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.NioClientSocketChannelFactory;
import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
import org.jboss.netty.handler.codec.string.StringDecoder;
import org.jboss.netty.handler.codec.string.StringEncoder;
import org.junit.Test;


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();
}
}


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();
}
}



/**
* Netty VS MinaNetty基于Pipeline处理,Mina基于Filter过滤
* Netty的事件驱动模型具有更好的扩展性和易用性
* Https,SSL,PB,RSTP,Text &Binary等协议支持
* Netty中UDP传输有更好的支持官方测试Netty比Mina性能更好
* @author Administrator
*
*/
public class TestCase {


public void testServer() {
//初始化channel的辅助类,为具体子类提供公共数据结构
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;
}
});
//创建服务器端channel的辅助类,接收connection请求
bootstrap.bind(new InetSocketAddress(8080));
}



public void testClient() {
//创建客户端channel的辅助类,发起connection请求
ClientBootstrap bootstrap = new ClientBootstrap(
new NioClientSocketChannelFactory(
Executors.newCachedThreadPool(),
Executors.newCachedThreadPool()));
//It means one same HelloWorldClientHandler instance is going to handle multiple Channels and consequently the data will be corrupted.
//基于上面这个描述,必须用到ChannelPipelineFactory每次创建一个pipeline
bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
public ChannelPipeline getPipel

首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Netty源码学习笔记 下一篇运用Spring注解实现Netty服务器端..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: