Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.6k views
in Technique[技术] by (71.8m points)

java - Netty: ctx.channel().writeAndFlush(out) not working when data is being sent from thread other than Netty's worker thread

I am using Netty in one of my projects. There is a flow in which I have to send some data to the client from some other thread (other netty's worker thread):

final ChannelFuture writeAndFlushFuture = ctx.channel().writeAndFlush(out)
                        .addListener(new GenericFutureListener<Future<? super Void>>() {
                            @Override
                            public void operationComplete(Future<? super Void> future) throws Exception {
                                LOGGER.info("=========> dsgdsfgdsfgdfsgdfsgsdgfd");
                            }
                        });

Also, I am waiting on client response after it receives my payload. I associated a timeout with it as well, so that whenever client doesn't reply back within time frame, I close the context from the server (assuming erroneous connection).

There is something strange going on. Whenever I send the payload to the client, I am not getting this printed in my logs

dsgdsfgdsfgdfsgdfsgsdgfd

But, it's getting printed just before I am closing the connection due to timeout. I am not sure what I might be doing wrong as to not send the payload right away, but just before closing the connection.

What could be happening here?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Actually, there was a flush missing when I was writing object to context:

ctx.writeAndFlush(commands);

Now, its working fine. But strange, it was working fine when was being executed within Netty's worker thread, even without flush.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...