How do I send an object in netty using a MessageToByteEncoder? If you can give an example of the Echo server code, that is, we receive the object and send it back.
- Why choose MessageToByteEncoder? Why not use a higher level abstraction - ObjectEncoder? - Sergey Gornostaev
- @Sergey Gornostaev is my first server on netty and here are the materials on which I wrote the code netty.io/wiki/user-guide-for-4.x.html please tell me why I can not get an answer from the server what could be the reason? I even tried to send a simple intovy value using the "ctx.writeint (a)" method from the main handler, but on the client side I didn’t receive anything, so I’ll be grateful if you give an example with the encoder you suggested - Clool Mear
- How do I know? The reasons may be many. - Sergey Gornostaev
- @SergeyGornostaev I am sorry, the comment did not correct this question that I opened and brought code ru.stackoverflow.com/questions/858978/… - Clool Mear
|
1 answer
Example with ObjectEncoder , the successor of MessageToByteEncoder :
Person.java
public class Person implements Serializable { private final Long id; private String name; public Person(Long id, String name) { this.id = id; this.name = name; } public Long getId() { return id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String toString() { return getName(); } } SimpleClient.java
public class SimpleClient { public static void main(String[] args) throws Exception { EventLoopGroup group = new NioEventLoopGroup(); try { new Bootstrap().group(group) .channel(NioSocketChannel.class) .handler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel socketChannel) { ChannelPipeline pipeline = socketChannel.pipeline(); pipeline.addLast(new ObjectEncoder()); pipeline.addLast(new ObjectDecoder(ClassResolvers.cacheDisabled(null))); pipeline.addLast(new ChannelInboundHandlerAdapter() { @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { ctx.writeAndFlush(new Person(1L, "John")); } @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { Person person = (Person) msg; System.out.println(person); } }); } }) .connect("localhost", 1234) .sync() .channel() .closeFuture() .sync(); } finally { group.shutdownGracefully(); } } } SimpleServer.java
public class SimpleServer { public static void main(String[] args) throws Exception { EventLoopGroup group = new NioEventLoopGroup(); try { new ServerBootstrap().group(group) .channel(NioServerSocketChannel.class) .childHandler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel socketChannel) { ChannelPipeline pipeline = socketChannel.pipeline(); pipeline.addLast(new ObjectEncoder()); pipeline.addLast(new ObjectDecoder(ClassResolvers.cacheDisabled(null))); pipeline.addLast(new ChannelInboundHandlerAdapter() { @Override public void channelRead(ChannelHandlerContext ctx, Object msg) { Person person = (Person) msg; System.out.println(person); person.setName("John Doe"); ctx.writeAndFlush(person) .addListener(ChannelFutureListener.CLOSE); } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { cause.printStackTrace(); ctx.channel().close(); } }); } }) .bind("localhost", 1234) .sync() .channel() .closeFuture() .syncUninterruptibly(); } finally { group.shutdownGracefully(); } } } |