Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Channel.isWritable notofication in Netty

Tags:

java

netty

I have a scenario where I need to keep writing to a Channel. In case it is unable to take further data ( because of the buffers being full etc ) , I need to stop writing , but need to resume as soon as the channel becomes writable again. How do I determine this instant when a channel becomes writable again ? Is there a callback event that is fired which I can override ? I am extending SimpleChannelUpstreamHandler in my business handler - this has a method channelInterestChanged() - but not sure if this is correct hook to tap into ?

I was hoping to avoid using primitive thread-based wait mechanisms like wait-notify as these will involve context switching. Any non-blocking way to achieve this wait ?

like image 542
Bhaskar Avatar asked Apr 13 '12 15:04

Bhaskar


People also ask

What is a channel in Netty?

Netty Channel Channel is a component providing users a ways to process I/O operations, such as read and write. A ChannelPipeline encapsulates a series of ChannelHandler instances as two-way linked list. Inbound and outbound events that flow through a channel can be intercepted by the ChannelPipeline.

What is Netty bootstrap?

A Bootstrap that makes it easy to bootstrap a Channel to use for clients. The AbstractBootstrap. bind() methods are useful in combination with connectionless transports such as datagram (UDP).


1 Answers

You can override the channelInterestChanged() method.

Something like that:

public class MyHandler extends SimpleChannelUpstreamHandler {
    ....

    @Override
    public void channelInterestChanged(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
        if (e.getChannel().isWritable() {
            .....
        }
    }
}

This should do the job

like image 51
Norman Maurer Avatar answered Oct 16 '22 16:10

Norman Maurer