Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Netty (4.0.4) version compress/decompress string messages error

Tags:

java

gzip

netty

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.netty.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?

like image 561
Tareq Saleh Avatar asked Apr 27 '26 14:04

Tareq Saleh


1 Answers

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.

like image 66
Norman Maurer Avatar answered Apr 29 '26 02:04

Norman Maurer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!