Netty <===> NotNetty

Norman Maurer norman.maurer at googlemail.com
Fri Oct 14 02:27:53 EDT 2011


2011/10/13 zx spectrum <webakaunt at gmail.com>:
> Is there anything specific that needs to be done when connecting a netty
> client to a non Netty server ( developed in some other language, etc.. )?
> Besides this:
>     val bootstrap = new ClientBootstrap(
>       new NioClientSocketChannelFactory(
>         Executors.newCachedThreadPool,
>         Executors.newCachedThreadPool ) )
>     bootstrap.setPipelineFactory( new ChannelPipelineFactory {
>         def getPipeline = {
>           Channels.pipeline( channelHandlers.toArray : _* )
>         }
>     } )
>     bootstrap.setOption( "remoteAddress", new InetSocketAddress( host, port
> ) )
>     val channel = bootstrap.connect.getChannel   // <= this succeeds
>     clientChannel.write( ChannelBuffers.wrappedBuffer( message ) )
> It throws a NotYetConnected exception.
> If I do "clientChannel.write( ChannelBuffers.wrappedBuffer( message ), new
> InetSocketAddress( host, port ) )" instead, it throws a
> "java.net.ConnectException: connection timed out"
> Thank you!

Hi,

you don't need anything special when connecting netty driven network
code with other programms. The problem in your code is that you expect
that bootstrap.connect() will "block" until the connection is
established. Thats not what it does, as all this kind of operations
are async by nature in netty.

So you have to choices here:

1) Register an ChannelFutureListener to the ChannelFuture that gets
returned by Bootstrap.connect(). And do the write once the future get
notified that the connect was successful
2) use bootstrap.connect(...).awaitUninterruptibly() which will block
until the connect was done.

Its up to you.. If you want to benefit from the async nature of netty
I would suggest you to go with 1). This is also better in terms of
performance.

Bye,
Norman



More information about the netty-users mailing list