Netty(4.0.4)版本压缩/解压缩字符串消息错误
我想在Netty客户机/服务器上应用压缩/解压缩我在客户机和服务器上使用以下代码进行管道:
@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());
}
和服务器一样:
@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());
}
我在客户端启动连接时出现以下错误
警告:无法初始化频道。 关闭:[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)导致: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) java.URLClassLoader.findClass(URLClassLoader.java:354)在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
线程“main”中的异常java.nio.channels.ClosedChannelException
客户端/服务器工作正常,没有压缩的东西我试图把字符串编码之前的压缩/解压缩,但我得到了同样的错误? 请帮忙吗?
您需要在您的pom.xml中添加以下依赖项:
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jzlib</artifactId>
<version>1.1.2</version>
</dependency>
这是因为netty声明所有的依赖关系是可选的。
感谢您的评论经过多次试验后,我发现我的问题的正确解决方案为: - 在netbeans中,我将jzlib-1.1.2.jar添加到了我的项目中。 - 管道的正确顺序如以下代码所示:
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());
在客户端和服务器中
链接地址: http://www.djcxy.com/p/33053.html上一篇: Netty (4.0.4) version compress/decompress string messages error
下一篇: ActiveMQ some consumers not picking up tasks if they arrive after producer