Bhaskar
Bhaskar

Reputation: 7523

Netty : Blocking call to get a connected Server Channel?

The call on ServerBootstrap.bind() returns a Channel but this is not in a Connected status and thus cannot be used for writing to client.

All the examples in Netty documentation show writing to a Channel from its ChannelHandler's events like channelConnected - I want to be able to get a connected Channel not in the event but as a reference outside the event , lets say some client code using my server component. One way is to manually code for waiting for channelConnected event and then copying the Channel reference.But this may be reinventing the wheel.

So the question is : Is there a blocking call available in Netty that returns a Connected Channel ?

edit : I am using Oio Channels , not Nio.

Upvotes: 2

Views: 2660

Answers (2)

Nicholas
Nicholas

Reputation: 16066

You could create a blocking call, but I think you maligned the event based approach too quickly. This is a contrived example, just to make sure I understand what you're trying to do:

  1. Netty Server starts
  2. A DataPusher service starts.
  3. When a client connects, the DataPusher grabs a reference to the client channel and writes some data to it.
  4. The client receives the pushed data shortly after connecting.

More or less correct ?

To do this, your DataPusher (or better yet, one of its minions) can be registered as a ChannelHandler in the server pipeline you create. Make it extend org.jboss.netty.channel.SimpleChannelHandler. The handler might look like this:

DataPusher dataPusher = getMyDataPusherReference();

public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) {
   dataPusher.doYourThing(e.getChannel());  // do something in another thread....
}

If you are determined to make it a blocking call from the DataPusher's perspective, just have it wait on a latch and have the minion drop the latch.

Not sure if that's what your'e looking for.....

Upvotes: 3

user207421
user207421

Reputation: 311055

After all the exchanges above, I still don't see that any of this is necessary.

Surely all you have to do is just accept the connection from the external service; don't register it for any events; then, when the other client connects, register for I/O events on both channels.

The external service doesn't know or care whether you are blocked in a thread waiting for another connection, or just not responding for some other reason.

If he's writing to you, his writes will succeed anyway, up to the size of your socket receive buffer, whether you are blocking or not, as long as you aren't actually reading from him. When that buffer fills up, he will block until you read some of it.

If he is reading from you, he will block until you send something, and again what you are doing in the meantime is invisible to him.

So I think you just need to simplify your thinking, and align it more with the wonderful world of non-blocking I/O.

Upvotes: 1

Related Questions