Search code examples
javanetwork-programmingnettynio

Netty messageReceived, writing object not working


Server code:

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();
        }
    }

Pipeline:

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;
    }

}

I override the messageReceiveed method, in my MessageHandler class:

@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();
    }
}

The object I'm trying to send (Message):

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;
    }

}

Finally my Client code:

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();
    }
}

If I change the Message Object to Date, So I write a Date Object, it works fine. The messageReceived method is not actually being called since I tried just printing a random sentence in the start of the messageReceived method and it didn't print. I'm new to netty and networking its self so I'm pretty clueless now, I've tried a couple of things all resulting with no difference to my current situation. I'm not sure why but it just doesn't want to write the Message Object, maybe it's the encoding part of it?? Even then, a Date will send perfectly fine, so I'm stumped, any help is appreciated, thanks.


Solution

  • Your Message class must implement java.io.Serializable.