Java 如何将ObjectDecoder添加到netty服务器

Java 如何将ObjectDecoder添加到netty服务器,java,multithreading,io,netty,data-synchronization,Java,Multithreading,Io,Netty,Data Synchronization,服务器代码来自: 以下是DatagramPacket处理程序: package net.bounceme.dur.netty; import io.netty.buffer.Unpooled; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.SimpleChannelInboundHandler; import io.netty.channel.socket.DatagramPacket; impor

服务器代码来自:

以下是
DatagramPacket
处理程序:

package net.bounceme.dur.netty;

import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.socket.DatagramPacket;
import io.netty.util.CharsetUtil;
import java.util.Random;
import java.util.logging.Logger;

public class ServerDatagramHandler extends SimpleChannelInboundHandler<DatagramPacket> {

    private static final Logger log = Logger.getLogger(ServerDatagramHandler.class.getName());
    private static final Random random = new Random();

    public ServerDatagramHandler() {
        log.info("..started..");
    }

    // Quotes from Mohandas K. Gandhi:
    private static final String[] quotes = {
        "Where there is love there is life.",
        "First they ignore you, then they laugh at you, then they fight you, then you win.",
        "Be the change you want to see in the world.",
        "The weak can never forgive. Forgiveness is the attribute of the strong.",};

    private static String nextQuote() {
        int quoteId;
        synchronized (random) {
            quoteId = random.nextInt(quotes.length);
        }
        return quotes[quoteId];
    }

    @Override
    public void channelRead0(ChannelHandlerContext ctx, DatagramPacket packet) throws Exception {
        System.err.println(packet);
        if ("QOTM?".equals(packet.content().toString(CharsetUtil.UTF_8))) {
            ctx.write(new DatagramPacket(
                    Unpooled.copiedBuffer("QOTM: " + nextQuote(), CharsetUtil.UTF_8), packet.sender()));
        }
    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) {
        ctx.flush();
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
        log.severe(cause.toString());
    }
}
就我而言,
Quote
只是一个字符串包装器,只有一个字段,
toString
返回引号。当然,它
实现了Serializable
,并使用
serialVersionUID

当我查看时,我看不到在服务器上的何处添加ObjectEncoder

有以下片段:

 // Set up the pipeline factory.
 bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
  public ChannelPipeline getPipeline() throws Exception {
   return Channels.pipeline(
    new ObjectDecoder(ClassResolvers.cacheDisabled(getClass().getClassLoader())),
    new DateHandler()
   );
  };
 });
但是,如何将其实现到QOTM服务器中?我正在浏览,但还没有找到相关的文本来解释这一点。
ObjectEncoder
ObjectDecoder
都没有出现在本书的文本中

另见:


我添加了如下编码器和解码器以发送各种POJO:

客户:

        bootstrap.handler(new ChannelInitializer<SocketChannel>() {
        @Override
        public void initChannel(SocketChannel ch) throws Exception {
            ch.pipeline().addLast(new ObjectEncoder());
            ch.pipeline().addLast(new ObjectDecoder(ClassResolvers.cacheDisabled(null)));
            ch.pipeline().addLast(customHandler1);
            ch.pipeline().addLast(customHandler2);
            ch.pipeline().addLast(customHandler3);
        }
    });
bootstrap.handler(新的ChannelInitializer(){
@凌驾
public void initChannel(SocketChannel ch)引发异常{
ch.pipeline().addLast(新的ObjectEncoder());
ch.pipeline().addLast(新的ObjectDecoder(ClassResolvers.cacheDisabled(null));
ch.pipeline().addLast(customHandler1);
ch.pipeline().addLast(customHandler2);
ch.pipeline().addLast(customHandler3);
}
});
服务器:

        bootstrap.option(ChannelOption.SO_REUSEADDR, true);
        bootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                    .childHandler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        public void initChannel(SocketChannel ch) throws Exception {
                            ch.pipeline().addLast(new ObjectDecoder(ClassResolvers.cacheDisabled(null)));
                            ch.pipeline().addLast(new ObjectEncoder());
                            ch.pipeline().addLast(customHandler1);
                            ch.pipeline().addLast(customHandler2);
                            ch.pipeline().addLast(customHandler3);
                        }
                    });
bootstrap.option(ChannelOption.SO\u REUSEADDR,true);
bootstrap.group(bossGroup,workerGroup).channel(NioServerSocketChannel.class)
.childHandler(新的通道初始值设定项(){
@凌驾
public void initChannel(SocketChannel ch)引发异常{
ch.pipeline().addLast(新的ObjectDecoder(ClassResolvers.cacheDisabled(null));
ch.pipeline().addLast(新的ObjectEncoder());
ch.pipeline().addLast(customHandler1);
ch.pipeline().addLast(customHandler2);
ch.pipeline().addLast(customHandler3);
}
});

当您添加
对象解码器
对象编码器
时,您是否也有自定义编码/反编码器?它们将
消息扩展到消息解码器
        bootstrap.handler(new ChannelInitializer<SocketChannel>() {
        @Override
        public void initChannel(SocketChannel ch) throws Exception {
            ch.pipeline().addLast(new ObjectEncoder());
            ch.pipeline().addLast(new ObjectDecoder(ClassResolvers.cacheDisabled(null)));
            ch.pipeline().addLast(customHandler1);
            ch.pipeline().addLast(customHandler2);
            ch.pipeline().addLast(customHandler3);
        }
    });
        bootstrap.option(ChannelOption.SO_REUSEADDR, true);
        bootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                    .childHandler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        public void initChannel(SocketChannel ch) throws Exception {
                            ch.pipeline().addLast(new ObjectDecoder(ClassResolvers.cacheDisabled(null)));
                            ch.pipeline().addLast(new ObjectEncoder());
                            ch.pipeline().addLast(customHandler1);
                            ch.pipeline().addLast(customHandler2);
                            ch.pipeline().addLast(customHandler3);
                        }
                    });