Netty messageReceived,写入对象不起作用

时间:2013-06-13 21:58:36

标签: java networking netty nio

服务器代码:

public static void main(String[] args) {
        try {
            ServerBootstrap bootstrap = new ServerBootstrap(new NioServerSocketChannelFactory(Executors.newCachedThreadPool(), Executors.newCachedThreadPool()));
            bootstrap.setPipelineFactory(new PipelineFactory());
            bootstrap.bind(new InetSocketAddress("localhost", port));
            System.out.println("Listening on " + port);
        } catch(Exception exception) {
            exception.printStackTrace();
        }
    }

管道:

import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.channel.ChannelPipelineFactory;
import org.jboss.netty.channel.DefaultChannelPipeline;
import org.jboss.netty.handler.codec.serialization.ClassResolvers;
import org.jboss.netty.handler.codec.serialization.ObjectDecoder;

public class PipelineFactory implements ChannelPipelineFactory {

    @Override
    public ChannelPipeline getPipeline() throws Exception {
        ChannelPipeline pipeline = new DefaultChannelPipeline();
        try {
            pipeline.addLast("decoder", new ObjectDecoder(ClassResolvers.cacheDisabled(getClass().getClassLoader())));
            pipeline.addLast("messagehandler", new MessageHandler());
        } catch(Exception exception) {
            exception.printStackTrace();
        }
        return pipeline;
    }

}

我在MessageHandler类中重写了messageReceiveed方法:

@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) {
    try {
        if (!e.getChannel().isConnected()) {
            System.out.println("Not connected to server...");
            return;
        }
        Message message = (Message) e.getMessage();
        queueMessage(message);
        super.messageReceived(ctx, e);
    } catch(Exception exception) {
        exception.printStackTrace();
    }
}

我正在尝试发送的对象(消息):

public class Message {

    private String text;
    private byte rights;

    public Message(String text, int rights) {
        this.text = text;
        this.rights = (byte) rights;
    }

    public String getText() {
        return this.text;
    }

    public byte getRights() {
        return this.rights;
    }

}

最后我的客户代码:

public static void main(String[] args) {
    try {
        ClientBootstrap bootstrap = new ClientBootstrap(new NioClientSocketChannelFactory(Executors.newCachedThreadPool(), Executors.newCachedThreadPool()));
        bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
            public ChannelPipeline getPipeline() throws Exception {
                return Channels.pipeline(new ObjectEncoder(), new ObjectDecoder(ClassResolvers.cacheDisabled(getClass().getClassLoader())));
            }
        });
        ChannelFuture cf = bootstrap.connect(new InetSocketAddress("localhost", 5656));
        cf.awaitUninterruptibly();
        Channel channel = cf.getChannel();
        channel.write(new Message("Hello", 0));
        if (channel.isConnected() && cf.isDone()) {
            System.out.println("Message sent!");
        } else {
            System.out.println("Message has not been sent, killing client.");
        }
    } catch(Exception exception) {
        exception.printStackTrace();
    }
}

如果我将消息对象更改为Date,那么我写一个Date对象,它可以正常工作。实际上没有调用messageReceived方法,因为我尝试在messageReceived方法的开头只打印一个随机语句,但它没有打印。我是netty和网络的新手,所以我现在很无能为力,我已经尝试了几件事情,这对我目前的情况没有任何影响。我不确定为什么,但它只是不想写消息对象,也许它是它的编码部分?即便如此,一个约会发送完美,所以我很难过,任何帮助都表示赞赏,谢谢。

1 个答案:

答案 0 :(得分:5)

您的Message类必须实现java.io.Serializable。