在使用Netty的Handler来对我们的channel进行各种处理的时候,有时候可能会遇到一个Hander无法做到将所有业务全部处理完成,所以可能就会需要我们去使用Handler链来完成,就好比我们javaWeb中的Filter过滤器链。
在使用Handler链之前呢,我们先模拟一个简单的业务,比如我首先要将服务端返回的数据(比如服务端返回了个“hi”)按原样在控制台打印出来,然后还需要将数据以大写的方式打印出来(其实这个简单的业务完全可以在一个Handler里面就可以完成了,但是为了使用到我们的Handler链,所以我还是把这个简单的业务拆分成两个Handler来处理)
那么首先我们就来定义好两Handler
第一个Handler,用来将接受到的服务端的数据原因输出
import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.buffer.ChannelBuffers;
import org.jboss.netty.channel.*;
import java.nio.channels.ClosedChannelException;
public class HiHandler extends SimpleChannelHandler {
/**
* 接收服务端消息
*/
@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
String str=(String) e.getMessage();
System.out.println(str);
super.messageReceived(ctx, e);//如果要使用Handler链的话,这句代码很关键,作用就是将我们的ChannelHandler上下文对象和事件对象传递给下一个Handler
}
}
第二个Handler,用来将服务端的数据以大写形式输出
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.ChannelStateEvent;
import org.jboss.netty.channel.ExceptionEvent;
import org.jboss.netty.channel.MessageEvent;
import org.jboss.netty.channel.SimpleChannelHandler;
public class HiHandler_2 extends SimpleChannelHandler {
/**
* 接收服务端消息
*/
@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
String str=(String) e.getMessage();
System.out.println(str.toUpperCase());
super.messageReceived(ctx, e);
}
}
接下来,将我们写好的两个Handler配置给Netty的客户端
import java.net.InetSocketAddress;
import java.util.Scanner;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import org.jboss.netty.bootstrap.ClientBootstrap;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelFuture;
import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.channel.ChannelPipelineFactory;
import org.jboss.netty.channel.Channels;
import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;
import org.jboss.netty.handler.codec.string.StringDecoder;
import org.jboss.netty.handler.codec.string.StringEncoder;
public class Client {
public static void main(String[] args) throws InterruptedException {
//服务类
ClientBootstrap bootStrap=new ClientBootstrap();
//创建两个线程池
ExecutorService boss=Executors.newCachedThreadPool();
ExecutorService worker=Executors.newCachedThreadPool();
//设置socket工厂
bootStrap.setFactory(new NioClientSocketChannelFactory(boss, worker));
//设置管道工厂
bootStrap.setPipelineFactory(new ChannelPipelineFactory() {
@Override
public ChannelPipeline getPipeline() throws Exception {
ChannelPipeline pipe=Channels.pipeline();
pipe.addLast("decoder", new StringDecoder());
pipe.addLast("encoder",new StringEncoder());
pipe.addLast("hiHandler", new HiHandler());//这里就是在给我们的客户端配置要使用的Handler
pipe.addLast("hiHandler_2", new HiHandler_2());
return pipe;
}
});
//连接服务器
ChannelFuture connect=bootStrap.connect(new InetSocketAddress("127.0.0.1", 10101));
Channel channel=connect.getChannel();
System.out.println("Client Start!!!");
//向服务器发送消息
Scanner scanner=new Scanner(System.in);
while(true) {
Thread.sleep(500);
System.out.println("请输入发送的消息:");
channel.write(scanner.next());
}
}
}
最后测试结果如下:

可以看到,从服务端发送过来的“hi”字符先被第一个handler以原样的方式打印出来,然后又被第二个handler以大写的方式打印了一次
如果将第一个Handler中messageReceived方法里的super.messageReceived(ctx, e)这句代码去掉,那么结果就是

由此可知,如果不加前面这句代码的话,handler链就不会传递了

本文通过实例演示如何在Netty中使用Handler链处理客户端与服务端的交互数据,包括两个Handler的定义及如何通过super.messageReceived方法实现链式处理。

334

被折叠的 条评论
为什么被折叠?



