Netty (4.0.4) version compress/decompress string messages error

I want to apply compress/decompress on Netty client/server I use the following code for pipeline in both client and sever:

@Override
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast("framer", new DelimiterBasedFrameDecoder(
    8192, Delimiters.lineDelimiter()));

pipeline.addLast("decoder", new StringDecoder());
pipeline.addLast("encoder", new StringEncoder());
pipeline.addLast("gzipdeflater", ZlibCodecFactory.newZlibEncoder(ZlibWrapper.GZIP));
pipeline.addLast("gzipinflater", ZlibCodecFactory.newZlibDecoder(ZlibWrapper.GZIP));

// and then business logic.
pipeline.addLast("handler", new NettyClientHandler());        
}

and the server as:

@Override
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast("framer", new DelimiterBasedFrameDecoder(
    8192, Delimiters.lineDelimiter()));
pipeline.addLast("decoder", new StringDecoder());
pipeline.addLast("encoder", new StringEncoder());
pipeline.addLast("gzipdeflater", ZlibCodecFactory.newZlibEncoder(ZlibWrapper.GZIP));
pipeline.addLast("gzipinflater", ZlibCodecFactory.newZlibDecoder(ZlibWrapper.GZIP));
//GlibDecoder
//pipeline.addLast("decoder", new ZlibDecoder());
//pipeline.addLast("encoder", new StringEncoder());
// and then business logic.
pipeline.addLast("handler", new NettyServerHandler());        
}

and I got the following error in the client on starting the connection

WARNING: Failed to initialize a channel. Closing: [id: 0x3553bb5c] java.lang.NoClassDefFoundError: com/jcraft/jzlib/Inflater at io.netty.handler.codec.compression.JZlibDecoder.(JZlibDecoder.java:28) at io.netty.handler.codec.compression.ZlibCodecFactory.newZlibDecoder(ZlibCodecFactory.java:86) at testChat.NettyClientInitializer.initChannel(NettyClientInitializer.java:36) at testChat.NettyClientInitializer.initChannel(NettyClientInitializer.java:21) at io.netty.channel.ChannelInitializer.channelRegistered(ChannelInitializer.java:70) at io.netty.channel.DefaultChannelHandlerContext.invokeChannelRegistered(DefaultChannelHandlerContext.java:188) at io.netty.channel.DefaultChannelHandlerContext.fireChannelRegistered(DefaultChannelHandlerContext.java:174) at io.netty.channel.DefaultChannelPipeline.fireChannelRegistered(DefaultChannelPipeline.java:730) at io.netty.channel.AbstractChannel$AbstractUnsafe.register0(AbstractChannel.java:426) at io.netty.channel.AbstractChannel$AbstractUnsafe.access$100(AbstractChannel.java:367) at io.ne tty.channel.AbstractChannel$AbstractUnsafe$1.run(AbstractChannel.java:403) at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:353) at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:366) at io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:101) at java.lang.Thread.run(Thread.java:722) Caused by: java.lang.ClassNotFoundException: com.jcraft.jzlib.Inflater at java.net.URLClassLoader$1.run(URLClassLoader.java:366) at java.net.URLClassLoader$1.run(URLClassLoader.java:355) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:354) at java.lang.ClassLoader.loadClass(ClassLoader.java:423) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308) at java.lang.ClassLoader.loadClass(ClassLoader.java:356) ... 15 more

Exception in thread "main" java.nio.channels.ClosedChannelException

the client/server work fine without compress stuff I try to put the compression/decompression before string encoding but I got the same error? any help please?


You need to add the following dependency in your pom.xml:

  <dependency>
    <groupId>com.jcraft</groupId>
    <artifactId>jzlib</artifactId>
      <version>1.1.2</version>
  </dependency>

This is because netty declare all dependencies as optional.


Thanks for Comments after many trials I found the correct solution for my Question as: - in netbeans I added jzlib-1.1.2.jar to my project. - the correct order for the pipline as the following code:

pipeline.addLast("deflater", ZlibCodecFactory.newZlibEncoder(ZlibWrapper.GZIP));
pipeline.addLast("inflater", ZlibCodecFactory.newZlibDecoder(ZlibWrapper.GZIP));

pipeline.addLast("framer", new DelimiterBasedFrameDecoder(
        8192, Delimiters.lineDelimiter()));


pipeline.addLast("decoder", new MyStringDecoder());
pipeline.addLast("encoder", new MyStringEncoder()); 

in both Client and Server

链接地址: http://www.djcxy.com/p/33054.html

上一篇: 用ASM生成工作invokedynamic指令

下一篇: Netty(4.0.4)版本压缩/解压缩字符串消息错误