object message sent with Netty can't be decoded correctly
up vote
0
down vote
favorite
I try to send object message PingMessage
with netty. Add ObjectEncoder
and ObjectDecoder
in channel pipeline. Client has sent packet successfully, but server can't decode the message. ObjectDecoder#decode() just returns null. Netty version is 4.1.28.Final.
PingMessage
public class PingMessage implements Serializable {
private static final long serialVersionUID = 1L;
private int flag = 0xff3e;
private String ping = "ping";
public int getFlag() {
return flag;
}
public void setFlag(int flag) {
this.flag = flag;
}
public String getPing() {
return ping;
}
public void setPing(String ping) {
this.ping = ping;
}
}
client
public class HeartbeatInitializer extends ChannelInitializer<Channel> {
protected void initChannel(Channel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast(new ObjectEncoder());
pipeline.addLast(new ObjectDecoder(Integer.MAX_VALUE, ClassResolvers.weakCachingResolver(PongMessage.class.getClassLoader())));
pipeline.addLast(new IdleStateHandler(BusinessConstant.READER_IDLE_SECONDS, BusinessConstant.WRITER_IDLE_SECONDS, 0));
pipeline.addLast(new HeartbeatServerHandler());
pipeline.addLast(new ClientHandler());
}
}
public class HeartbeatServerHandler extends ChannelDuplexHandler {
private static Logger logger = LoggerFactory.getLogger(HeartbeatServerHandler.class);
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
if (evt instanceof IdleStateEvent) {
IdleStateEvent idleState = (IdleStateEvent)evt;
if (idleState.state() == IdleState.READER_IDLE) {
} else if (idleState.state() == IdleState.WRITER_IDLE) {
try {
ChannelFuture future = ctx.writeAndFlush(new PingMessage()).sync();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
server
public class HeartbeatInitializer extends ChannelInitializer<Channel> {
protected void initChannel(Channel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast(new ObjectEncoder());
pipeline.addLast(new ObjectDecoder(Integer.MAX_VALUE, ClassResolvers.weakCachingResolver(this.class.getClassLoader())));
pipeline.addLast(new IdleStateHandler(BusinessConstant.READER_IDLE_SECONDS, BusinessConstant.WRITER_IDLE_SECONDS, 0));
pipeline.addLast(new HeartbeatServerHandler());
pipeline.addLast(new ClientHandler());
}
}
public class HeartbeatServerHandler extends ChannelDuplexHandler {
private static Logger logger = LoggerFactory.getLogger(HeartbeatServerHandler.class);
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
if (msg instanceof PingMessage) {
logger.info("received ping");
return;
}
}
}
netty source code debug info
AbstractChannelHandlerContext#invokeChannelRead()
((ChannelInboundHandler) handler()).channelRead(this, msg);
the msg
variable is
UnpooledUnsafeDirectByteBuf(ridx: 0, widx: 61, cap: 1024)
ObjectDecoder#decode() just return null
super class of ObjectDecoder
is LengthFieldBasedFrameDecoder
. LengthFieldBasedFrameDecoder#decode() return null
// LengthFieldBasedFrameDecoder#decode()
int frameLengthInt = (int) frameLength;
if (in.readableBytes() < frameLengthInt) {
return null; // get executed and return null
}
java netty
|
show 6 more comments
up vote
0
down vote
favorite
I try to send object message PingMessage
with netty. Add ObjectEncoder
and ObjectDecoder
in channel pipeline. Client has sent packet successfully, but server can't decode the message. ObjectDecoder#decode() just returns null. Netty version is 4.1.28.Final.
PingMessage
public class PingMessage implements Serializable {
private static final long serialVersionUID = 1L;
private int flag = 0xff3e;
private String ping = "ping";
public int getFlag() {
return flag;
}
public void setFlag(int flag) {
this.flag = flag;
}
public String getPing() {
return ping;
}
public void setPing(String ping) {
this.ping = ping;
}
}
client
public class HeartbeatInitializer extends ChannelInitializer<Channel> {
protected void initChannel(Channel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast(new ObjectEncoder());
pipeline.addLast(new ObjectDecoder(Integer.MAX_VALUE, ClassResolvers.weakCachingResolver(PongMessage.class.getClassLoader())));
pipeline.addLast(new IdleStateHandler(BusinessConstant.READER_IDLE_SECONDS, BusinessConstant.WRITER_IDLE_SECONDS, 0));
pipeline.addLast(new HeartbeatServerHandler());
pipeline.addLast(new ClientHandler());
}
}
public class HeartbeatServerHandler extends ChannelDuplexHandler {
private static Logger logger = LoggerFactory.getLogger(HeartbeatServerHandler.class);
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
if (evt instanceof IdleStateEvent) {
IdleStateEvent idleState = (IdleStateEvent)evt;
if (idleState.state() == IdleState.READER_IDLE) {
} else if (idleState.state() == IdleState.WRITER_IDLE) {
try {
ChannelFuture future = ctx.writeAndFlush(new PingMessage()).sync();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
server
public class HeartbeatInitializer extends ChannelInitializer<Channel> {
protected void initChannel(Channel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast(new ObjectEncoder());
pipeline.addLast(new ObjectDecoder(Integer.MAX_VALUE, ClassResolvers.weakCachingResolver(this.class.getClassLoader())));
pipeline.addLast(new IdleStateHandler(BusinessConstant.READER_IDLE_SECONDS, BusinessConstant.WRITER_IDLE_SECONDS, 0));
pipeline.addLast(new HeartbeatServerHandler());
pipeline.addLast(new ClientHandler());
}
}
public class HeartbeatServerHandler extends ChannelDuplexHandler {
private static Logger logger = LoggerFactory.getLogger(HeartbeatServerHandler.class);
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
if (msg instanceof PingMessage) {
logger.info("received ping");
return;
}
}
}
netty source code debug info
AbstractChannelHandlerContext#invokeChannelRead()
((ChannelInboundHandler) handler()).channelRead(this, msg);
the msg
variable is
UnpooledUnsafeDirectByteBuf(ridx: 0, widx: 61, cap: 1024)
ObjectDecoder#decode() just return null
super class of ObjectDecoder
is LengthFieldBasedFrameDecoder
. LengthFieldBasedFrameDecoder#decode() return null
// LengthFieldBasedFrameDecoder#decode()
int frameLengthInt = (int) frameLength;
if (in.readableBytes() < frameLengthInt) {
return null; // get executed and return null
}
java netty
We will need more infos but just let me say thatChannelFuture future = ctx.writeAndFlush(new PingMessage()).sync();
will not work (and should even produce an exception) as you block the EventLoop. You should never callsync*
orawait*
in the EventLoop
– Norman Maurer
Nov 15 at 6:46
@NormanMaurer I've debugged the client code and no exception found. I use wireshark to capture packets and find "ping" packet which is 61 bytes. But the server just can't decode bytes to PingMessage. How can I provide more infomation? I don't post code about PongMessage(from server to client), which can be decoded well by client. Does Netty support both directions message processing?
– Hel
Nov 15 at 6:49
@NormanMaurer I addsync()
for debug purpose to assure that messages written without exception.
– Hel
Nov 15 at 6:56
1. You didn't add aLengthFieldBasedFrameDecoder
in your ChannelInitializer but mentioned it in your question. Why? 2. I tried your code and for me everything woks fine. Maybe you imported something wrong since all your classes have the same name.
– Ingrim4
Nov 15 at 13:17
1
Are you sure you have the same version of the serializable class on both class-path. ?
– Norman Maurer
Nov 16 at 16:29
|
show 6 more comments
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I try to send object message PingMessage
with netty. Add ObjectEncoder
and ObjectDecoder
in channel pipeline. Client has sent packet successfully, but server can't decode the message. ObjectDecoder#decode() just returns null. Netty version is 4.1.28.Final.
PingMessage
public class PingMessage implements Serializable {
private static final long serialVersionUID = 1L;
private int flag = 0xff3e;
private String ping = "ping";
public int getFlag() {
return flag;
}
public void setFlag(int flag) {
this.flag = flag;
}
public String getPing() {
return ping;
}
public void setPing(String ping) {
this.ping = ping;
}
}
client
public class HeartbeatInitializer extends ChannelInitializer<Channel> {
protected void initChannel(Channel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast(new ObjectEncoder());
pipeline.addLast(new ObjectDecoder(Integer.MAX_VALUE, ClassResolvers.weakCachingResolver(PongMessage.class.getClassLoader())));
pipeline.addLast(new IdleStateHandler(BusinessConstant.READER_IDLE_SECONDS, BusinessConstant.WRITER_IDLE_SECONDS, 0));
pipeline.addLast(new HeartbeatServerHandler());
pipeline.addLast(new ClientHandler());
}
}
public class HeartbeatServerHandler extends ChannelDuplexHandler {
private static Logger logger = LoggerFactory.getLogger(HeartbeatServerHandler.class);
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
if (evt instanceof IdleStateEvent) {
IdleStateEvent idleState = (IdleStateEvent)evt;
if (idleState.state() == IdleState.READER_IDLE) {
} else if (idleState.state() == IdleState.WRITER_IDLE) {
try {
ChannelFuture future = ctx.writeAndFlush(new PingMessage()).sync();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
server
public class HeartbeatInitializer extends ChannelInitializer<Channel> {
protected void initChannel(Channel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast(new ObjectEncoder());
pipeline.addLast(new ObjectDecoder(Integer.MAX_VALUE, ClassResolvers.weakCachingResolver(this.class.getClassLoader())));
pipeline.addLast(new IdleStateHandler(BusinessConstant.READER_IDLE_SECONDS, BusinessConstant.WRITER_IDLE_SECONDS, 0));
pipeline.addLast(new HeartbeatServerHandler());
pipeline.addLast(new ClientHandler());
}
}
public class HeartbeatServerHandler extends ChannelDuplexHandler {
private static Logger logger = LoggerFactory.getLogger(HeartbeatServerHandler.class);
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
if (msg instanceof PingMessage) {
logger.info("received ping");
return;
}
}
}
netty source code debug info
AbstractChannelHandlerContext#invokeChannelRead()
((ChannelInboundHandler) handler()).channelRead(this, msg);
the msg
variable is
UnpooledUnsafeDirectByteBuf(ridx: 0, widx: 61, cap: 1024)
ObjectDecoder#decode() just return null
super class of ObjectDecoder
is LengthFieldBasedFrameDecoder
. LengthFieldBasedFrameDecoder#decode() return null
// LengthFieldBasedFrameDecoder#decode()
int frameLengthInt = (int) frameLength;
if (in.readableBytes() < frameLengthInt) {
return null; // get executed and return null
}
java netty
I try to send object message PingMessage
with netty. Add ObjectEncoder
and ObjectDecoder
in channel pipeline. Client has sent packet successfully, but server can't decode the message. ObjectDecoder#decode() just returns null. Netty version is 4.1.28.Final.
PingMessage
public class PingMessage implements Serializable {
private static final long serialVersionUID = 1L;
private int flag = 0xff3e;
private String ping = "ping";
public int getFlag() {
return flag;
}
public void setFlag(int flag) {
this.flag = flag;
}
public String getPing() {
return ping;
}
public void setPing(String ping) {
this.ping = ping;
}
}
client
public class HeartbeatInitializer extends ChannelInitializer<Channel> {
protected void initChannel(Channel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast(new ObjectEncoder());
pipeline.addLast(new ObjectDecoder(Integer.MAX_VALUE, ClassResolvers.weakCachingResolver(PongMessage.class.getClassLoader())));
pipeline.addLast(new IdleStateHandler(BusinessConstant.READER_IDLE_SECONDS, BusinessConstant.WRITER_IDLE_SECONDS, 0));
pipeline.addLast(new HeartbeatServerHandler());
pipeline.addLast(new ClientHandler());
}
}
public class HeartbeatServerHandler extends ChannelDuplexHandler {
private static Logger logger = LoggerFactory.getLogger(HeartbeatServerHandler.class);
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
if (evt instanceof IdleStateEvent) {
IdleStateEvent idleState = (IdleStateEvent)evt;
if (idleState.state() == IdleState.READER_IDLE) {
} else if (idleState.state() == IdleState.WRITER_IDLE) {
try {
ChannelFuture future = ctx.writeAndFlush(new PingMessage()).sync();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
server
public class HeartbeatInitializer extends ChannelInitializer<Channel> {
protected void initChannel(Channel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast(new ObjectEncoder());
pipeline.addLast(new ObjectDecoder(Integer.MAX_VALUE, ClassResolvers.weakCachingResolver(this.class.getClassLoader())));
pipeline.addLast(new IdleStateHandler(BusinessConstant.READER_IDLE_SECONDS, BusinessConstant.WRITER_IDLE_SECONDS, 0));
pipeline.addLast(new HeartbeatServerHandler());
pipeline.addLast(new ClientHandler());
}
}
public class HeartbeatServerHandler extends ChannelDuplexHandler {
private static Logger logger = LoggerFactory.getLogger(HeartbeatServerHandler.class);
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
if (msg instanceof PingMessage) {
logger.info("received ping");
return;
}
}
}
netty source code debug info
AbstractChannelHandlerContext#invokeChannelRead()
((ChannelInboundHandler) handler()).channelRead(this, msg);
the msg
variable is
UnpooledUnsafeDirectByteBuf(ridx: 0, widx: 61, cap: 1024)
ObjectDecoder#decode() just return null
super class of ObjectDecoder
is LengthFieldBasedFrameDecoder
. LengthFieldBasedFrameDecoder#decode() return null
// LengthFieldBasedFrameDecoder#decode()
int frameLengthInt = (int) frameLength;
if (in.readableBytes() < frameLengthInt) {
return null; // get executed and return null
}
java netty
java netty
edited Nov 15 at 15:16
asked Nov 15 at 1:24
Hel
10218
10218
We will need more infos but just let me say thatChannelFuture future = ctx.writeAndFlush(new PingMessage()).sync();
will not work (and should even produce an exception) as you block the EventLoop. You should never callsync*
orawait*
in the EventLoop
– Norman Maurer
Nov 15 at 6:46
@NormanMaurer I've debugged the client code and no exception found. I use wireshark to capture packets and find "ping" packet which is 61 bytes. But the server just can't decode bytes to PingMessage. How can I provide more infomation? I don't post code about PongMessage(from server to client), which can be decoded well by client. Does Netty support both directions message processing?
– Hel
Nov 15 at 6:49
@NormanMaurer I addsync()
for debug purpose to assure that messages written without exception.
– Hel
Nov 15 at 6:56
1. You didn't add aLengthFieldBasedFrameDecoder
in your ChannelInitializer but mentioned it in your question. Why? 2. I tried your code and for me everything woks fine. Maybe you imported something wrong since all your classes have the same name.
– Ingrim4
Nov 15 at 13:17
1
Are you sure you have the same version of the serializable class on both class-path. ?
– Norman Maurer
Nov 16 at 16:29
|
show 6 more comments
We will need more infos but just let me say thatChannelFuture future = ctx.writeAndFlush(new PingMessage()).sync();
will not work (and should even produce an exception) as you block the EventLoop. You should never callsync*
orawait*
in the EventLoop
– Norman Maurer
Nov 15 at 6:46
@NormanMaurer I've debugged the client code and no exception found. I use wireshark to capture packets and find "ping" packet which is 61 bytes. But the server just can't decode bytes to PingMessage. How can I provide more infomation? I don't post code about PongMessage(from server to client), which can be decoded well by client. Does Netty support both directions message processing?
– Hel
Nov 15 at 6:49
@NormanMaurer I addsync()
for debug purpose to assure that messages written without exception.
– Hel
Nov 15 at 6:56
1. You didn't add aLengthFieldBasedFrameDecoder
in your ChannelInitializer but mentioned it in your question. Why? 2. I tried your code and for me everything woks fine. Maybe you imported something wrong since all your classes have the same name.
– Ingrim4
Nov 15 at 13:17
1
Are you sure you have the same version of the serializable class on both class-path. ?
– Norman Maurer
Nov 16 at 16:29
We will need more infos but just let me say that
ChannelFuture future = ctx.writeAndFlush(new PingMessage()).sync();
will not work (and should even produce an exception) as you block the EventLoop. You should never call sync*
or await*
in the EventLoop– Norman Maurer
Nov 15 at 6:46
We will need more infos but just let me say that
ChannelFuture future = ctx.writeAndFlush(new PingMessage()).sync();
will not work (and should even produce an exception) as you block the EventLoop. You should never call sync*
or await*
in the EventLoop– Norman Maurer
Nov 15 at 6:46
@NormanMaurer I've debugged the client code and no exception found. I use wireshark to capture packets and find "ping" packet which is 61 bytes. But the server just can't decode bytes to PingMessage. How can I provide more infomation? I don't post code about PongMessage(from server to client), which can be decoded well by client. Does Netty support both directions message processing?
– Hel
Nov 15 at 6:49
@NormanMaurer I've debugged the client code and no exception found. I use wireshark to capture packets and find "ping" packet which is 61 bytes. But the server just can't decode bytes to PingMessage. How can I provide more infomation? I don't post code about PongMessage(from server to client), which can be decoded well by client. Does Netty support both directions message processing?
– Hel
Nov 15 at 6:49
@NormanMaurer I add
sync()
for debug purpose to assure that messages written without exception.– Hel
Nov 15 at 6:56
@NormanMaurer I add
sync()
for debug purpose to assure that messages written without exception.– Hel
Nov 15 at 6:56
1. You didn't add a
LengthFieldBasedFrameDecoder
in your ChannelInitializer but mentioned it in your question. Why? 2. I tried your code and for me everything woks fine. Maybe you imported something wrong since all your classes have the same name.– Ingrim4
Nov 15 at 13:17
1. You didn't add a
LengthFieldBasedFrameDecoder
in your ChannelInitializer but mentioned it in your question. Why? 2. I tried your code and for me everything woks fine. Maybe you imported something wrong since all your classes have the same name.– Ingrim4
Nov 15 at 13:17
1
1
Are you sure you have the same version of the serializable class on both class-path. ?
– Norman Maurer
Nov 16 at 16:29
Are you sure you have the same version of the serializable class on both class-path. ?
– Norman Maurer
Nov 16 at 16:29
|
show 6 more comments
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53311147%2fobject-message-sent-with-netty-cant-be-decoded-correctly%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
We will need more infos but just let me say that
ChannelFuture future = ctx.writeAndFlush(new PingMessage()).sync();
will not work (and should even produce an exception) as you block the EventLoop. You should never callsync*
orawait*
in the EventLoop– Norman Maurer
Nov 15 at 6:46
@NormanMaurer I've debugged the client code and no exception found. I use wireshark to capture packets and find "ping" packet which is 61 bytes. But the server just can't decode bytes to PingMessage. How can I provide more infomation? I don't post code about PongMessage(from server to client), which can be decoded well by client. Does Netty support both directions message processing?
– Hel
Nov 15 at 6:49
@NormanMaurer I add
sync()
for debug purpose to assure that messages written without exception.– Hel
Nov 15 at 6:56
1. You didn't add a
LengthFieldBasedFrameDecoder
in your ChannelInitializer but mentioned it in your question. Why? 2. I tried your code and for me everything woks fine. Maybe you imported something wrong since all your classes have the same name.– Ingrim4
Nov 15 at 13:17
1
Are you sure you have the same version of the serializable class on both class-path. ?
– Norman Maurer
Nov 16 at 16:29