逻辑就是在处理handler前加入一个处理符,然后

channelReadComplete这个事件进行处理。
同时注意客服端的配置:
 public void connect(String addr, int port, final String xml, final String key,final boolean flag) throws Exception {
EventLoopGroup group = new NioEventLoopGroup();
try {
Bootstrap b = new Bootstrap();
b.group(group).channel(NioSocketChannel.class).option(ChannelOption.TCP_NODELAY, true)
.option(ChannelOption.RCVBUF_ALLOCATOR, new FixedRecvByteBufAllocator(1024*1024))//这行配置比较重要
.handler(new ChannelInitializer<SocketChannel>() {
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast(new EsbClientHandler(xml, key,flag));
}
});
ChannelFuture f = b.connect(addr, port);
// 等待客户端关闭连接
f.channel().closeFuture().sync();
} catch (Exception e) {
e.printStackTrace();
} finally {
group.shutdownGracefully();
}
}

源码如下:

 public class EsbClientHandler extends ChannelHandlerAdapter {

     private static Logger logger = Logger.getLogger(EsbClientHandler.class);

     private ByteBuf byteMsg;

     private boolean flag;

     /**
* 临时客户端数据key
*/
private String key = "key"; public EsbClientHandler(String xml, String key, boolean flag) {
this.key = key;
this.flag = flag;
byte[] req = null;
try {
req = xml.getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
byteMsg = Unpooled.buffer(req.length);
byteMsg.writeBytes(req);
} @Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
ctx.writeAndFlush(byteMsg);
} @Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
try {
ByteBuf buf = (ByteBuf) msg;
byte[] req = new byte[buf.readableBytes()];
buf.readBytes(req);
String body = new String(req, "UTF-8");
flag = true;
String xml = WebUtil.getXmlStr(body);
analysisXml(xml);
} catch (Exception e) {
e.printStackTrace();
} finally {
ReferenceCountUtil.release(msg);
} } @Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
ctx.flush();
if (flag) {
ctx.close();
} else {
ctx.read();
}
} @Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
// 释放资源
ctx.close();
} /**
*
* 客户端解析服务端返回的xml数据,进行数据操作
*
* @param xml
*/
public void analysisXml(String xml) {
System.out.println("获取服务器接收报文:" + xml);
logger.info("开始解析xml:");
logger.info(xml);
NettyMap.setDataMap(key, xml);
}
}

最新文章

  1. 【转】java环境配置
  2. 64位Windows7升级IE11后无法启动的解决办法
  3. IOS中的内存不足警告处理(译)
  4. javascript中的undefined,null,&quot;&quot;,0和false的云集
  5. struct 结构
  6. [Swust OJ 771]--奶牛农场(几何题,画图就好)
  7. 遍历Javascript数组的一种方法!
  8. UVALive 6885 Flowery Trails
  9. VueJS 事件修饰符
  10. Delphi工程版本号修改工具
  11. js监听事件
  12. 【爆料】-《堪培拉大学毕业证书》Canberra一模一样原件
  13. onload 和 domready
  14. 包管理工具之Pipenv
  15. JavaWeb——tomcat manager 403 Access Denied .You are not authorized to view this page.
  16. c# 对象集合转Json
  17. 2017-2018-2 20155224 『网络对抗技术』Exp9:Web安全基础
  18. eclipse copy web project后修改context root
  19. VS:error C3872: '0xe044': this character is not allowed in an identifier解决方法
  20. Linux基础之权限-你弄得明白吗?

热门文章

  1. Nested List Weight Sum
  2. 项目(1-2)ES32获取mpu9250传入数据库
  3. ansible 批量部署准备工作
  4. 12-网页,网站,微信公众号基础入门(编写后台PHP程序,实现Airkiss配网)
  5. [Python] Python忽略warning警告错误
  6. GoCN每日新闻(2019-11-10)
  7. nginx 常用全局变量
  8. SpringMVC相关试题
  9. charles安装及使用
  10. ubuntu之路——day17.1 用np.pad做padding