Closing the Client / Sender
Christian Migowski
chrismfwrd at gmail.com
Wed Aug 12 03:34:01 EDT 2009
Normally Frederics remark is right, but as this is a client with just
one connection that is about to be closed you don't have to worry
about deadlocks or blocks - by not using the ChannelFutureListener you
save resources (one thread and some objects ;))
christian!
On Wed, Aug 12, 2009 at 9:14 AM, Frederic Bregier<fredbregier at free.fr> wrote:
>
> Hi,
>
> I'm not sure either of the question, but to be a bit more simpler, to
> replace the two lines Christian proposes, you can use the following:
>
> this.channel.write(buffer).addListener(ChannelFutureListener.CLOSE);
>
> which has the advantage to close the channel immediately after the write and
> to not introduce a await method in the ChannelHandler (see API doc why).
>
> HTH,
> Frederic
>
>
> christian wrote:
>>
>> I am not sure if i understand your problem, but in case you just want
>> to close the connection to the server in your client/sender class,
>> shouldn't it be enough to replace the last line in your notify methods
>> with something like
>>
>> this.channel.write(buffer).awaitUninterruptibly();
>> this.channel.close().awaitUninterruptibly();
>>
>> (you could skip the last await)?
>> Then you could dispose the factory with releaseExternalResources() and
>> Netty should eat none of your bread anymore ;)
>>
>> hth,
>> christian!
>>
>>
>> On Tue, Aug 11, 2009 at 8:38 PM, Michael
>> McGrady<mmcgrady at topiatechnology.com> wrote:
>>> Sorry, forgot the text. How do I close the client after notify(....)
>>> sends
>>> the data to the application logic without closing the server?
>>> Mike
>>>
>>> Mike McGrady
>>> Principal Investigator AF081-028 AFRL SBIR
>>> Senior Engineer
>>> Topia Technology, Inc
>>> 1.253.720.3365
>>> mmcgrady at topiatechnology.com
>>>
>>> SERVER Manager Code
>>> NettyKarmaReceiverAdapter receiver = new NettyKarmaReceiverAdapter(new
>>> InetSocketAddress("localhost", 8080)) ;
>>> receiver.start() ;
>>> CLIENT Manager Code
>>> NettyKarmaSenderAdapter sender = new NettyKarmaSenderAdapter("localhost",
>>> 8080) ;
>>> sender.start() ;
>>> sender.notify("Hello, World!") ;
>>> sender.stop() ;
>>> RECEIVER ADAPTER Code
>>>
>>> public NettyKarmaReceiverAdapter(InetSocketAddress address) {
>>> this.address = address;
>>> this.factory =
>>> new NioServerSocketChannelFactory(
>>> Executors.newCachedThreadPool(),
>>> Executors.newCachedThreadPool());
>>> this.bootstrap = new ServerBootstrap(factory);
>>> }
>>> private void setup ( ) {
>>> this.bootstrap.setOption("child.tcpNoDelay", true);
>>> this.bootstrap.setOption("child.keepAlive", true);
>>> this.bootstrap.setPipelineFactory(new
>>> KarmaReceiverPipelineFactory(this));
>>> }
>>> public void start ( ) {
>>> setup() ;
>>> this.bootstrap.bind(address); }
>>> SENDER ADAPTER Code
>>> public NettyKarmaSenderAdapter ( String host, int port ) {
>>> this.host = host;
>>> this.port = port;
>>> }
>>> public void setup ( ) {
>>> ChannelFactory factory =
>>> new NioClientSocketChannelFactory ( Executors
>>> .newCachedThreadPool ( ), Executors.newCachedThreadPool ( ) );
>>> this.bootstrap = new ClientBootstrap ( factory );
>>>
>>> this.bootstrap
>>> .setPipelineFactory ( new KarmaSenderPipelineFactory (this) );
>>> this.bootstrap.setOption ( "tcpNoDelay", true );
>>> this.bootstrap.setOption ( "keepAlive", true );
>>> ChannelFuture future =
>>> this.bootstrap.connect ( new InetSocketAddress ( this.host, this.port )
>>> );
>>>
>>> // Wait until the connection attempt succeeds or fails.
>>> this.channel = future.awaitUninterruptibly ( ).getChannel ( );
>>> if ( !future.isSuccess ( ) ) {
>>> future.getCause ( ).printStackTrace ( );
>>> factory.releaseExternalResources ( );
>>> return;
>>> }
>>> }
>>>
>>> public void start ( ) {
>>> setup ( );
>>> }
>>>
>>> public int stop ( ) {
>>> ????????????????????????????????????????
>>> return +2;
>>> }
>>> public void notify ( File file ) {
>>> try {
>>> KarmaChunkedFile chunks = new KarmaChunkedFile(file) ;
>>> try {
>>> while (chunks.hasNextChunk()) {
>>> Object object = chunks.nextChunk ( ) ;
>>> byte [] bytes = ByteObjectConverter.toBytes(object) ;
>>>
>>> ChannelBuffer buffer = ChannelBuffers.wrappedBuffer(bytes) ;
>>> this.channel.write ( buffer );
>>> }
>>> } catch ( Exception e ) {
>>> // TODO Auto-generated catch block
>>> e.printStackTrace();
>>> }
>>> } catch ( IOException e ) {
>>> // TODO Auto-generated catch block
>>> e.printStackTrace();
>>> }
>>> }
>>> public void notify ( KarmaAumlet aumlet ) {
>>> byte [] bytes = ByteObjectConverter.toBytes(aumlet) ;
>>> ChannelBuffer buffer = ChannelBuffers.wrappedBuffer(bytes) ;
>>> this.channel.write ( buffer );
>>> }
>>>
>>> public void notify ( String message ) {
>>> byte [] bytes = ByteObjectConverter.toBytes(new KarmaMessage(message)) ;
>>> ChannelBuffer buffer = ChannelBuffers.wrappedBuffer(bytes) ;
>>> this.channel.write ( buffer );
>>> }
>>> SENDER PIPELINE Code
>>>
>>> public class KarmaSenderPipelineFactory implements ChannelPipelineFactory
>>> {
>>> private final NettyKarmaSenderAdapter adapter ;
>>>
>>> public KarmaSenderPipelineFactory (NettyKarmaSenderAdapter adapter) {
>>> this.adapter = adapter;
>>> }
>>>
>>> public ChannelPipeline getPipeline ( ) throws Exception {
>>> ChannelPipeline pipeline = pipeline ( );
>>> SSLEngine engine =
>>> KarmaSslContextFactory.getClientContext ( ).createSSLEngine ( );
>>> engine.setUseClientMode ( true );
>>> pipeline.addLast ( "ssl", new KarmaSslHandler ( engine ) );
>>> pipeline.addLast ( "universal", new KarmaSenderCodecHandler (
>>> adapter) );
>>>
>>> return pipeline;
>>> }
>>> }
>>> SENDER CODEC HANDLER Code
>>>
>>>
>>> public class KarmaReceiverPipelineFactory implements
>>> ChannelPipelineFactory
>>> {
>>> private final NettyKarmaReceiverAdapter adapter ;
>>>
>>> public KarmaReceiverPipelineFactory ( NettyKarmaReceiverAdapter adapter )
>>> {
>>> this.adapter = adapter;
>>> }
>>>
>>> public ChannelPipeline getPipeline ( ) throws Exception {
>>> ChannelPipeline pipeline = pipeline ( );
>>> SSLEngine engine =
>>> SslContextFactory.getServerContext ( ).createSSLEngine ( );
>>> engine.setUseClientMode ( false );
>>> pipeline.addLast ( "ssl", new SslHandler ( engine ) );
>>> pipeline.addLast ( "universal", new KarmaReceiverCodecHandler (
>>> adapter ) );
>>>
>>> return pipeline;
>>> }
>>> }
>>> RECEIVER PIPELINE Code
>>>
>>>
>>> public class KarmaReceiverPipelineFactory implements
>>> ChannelPipelineFactory
>>> {
>>> private final NettyKarmaReceiverAdapter adapter ;
>>>
>>> public KarmaReceiverPipelineFactory ( NettyKarmaReceiverAdapter adapter )
>>> {
>>> this.adapter = adapter;
>>> }
>>>
>>> public ChannelPipeline getPipeline ( ) throws Exception {
>>> ChannelPipeline pipeline = pipeline ( );
>>> SSLEngine engine =
>>> KarmaSslContextFactory.getServerContext ( ).createSSLEngine ( );
>>> engine.setUseClientMode ( false );
>>> pipeline.addLast ( "ssl", new SslHandler ( engine ) );
>>> pipeline.addLast ( "universal", new KarmaReceiverCodecHandler (
>>> adapter ) );
>>>
>>> return pipeline;
>>> }
>>> }
>>> RECEIVER CODEC HANDLER Code
>>>
>>> public KarmaReceiverCodecHandler ( KarmaReceiver receiver ) {
>>> this.receiver = receiver;
>>> this.charsetName = Charset.defaultCharset ( ).name ( );
>>> }
>>> public void messageReceivedUpstream (
>>> ChannelHandlerContext ctx, MessageEvent evt ) throws Exception {
>>> Object originalMessage = evt.getMessage ( );
>>> Object decodedMessage =
>>> decode ( ctx, evt.getChannel ( ), originalMessage );
>>>
>>> if ( originalMessage == decodedMessage ) {
>>> ctx.sendUpstream ( evt );
>>> } else {
>>> if ( ! ( ( evt ).getMessage ( ) instanceof ChannelBuffer ) ) {
>>> fireMessageReceived ( ctx, decodedMessage, evt.getRemoteAddress ( ) );
>>> }
>>> }
>>> }
>>>
>>> private Object decode (
>>> ChannelHandlerContext ctx, Channel channel, Object msg )
>>> throws Exception {
>>> if ( ! ( msg instanceof ChannelBuffer ) ) {
>>> return msg;
>>> }
>>>
>>> ChannelBuffer buffer = ( ( ChannelBuffer ) msg ).copy ( );
>>> int readableBytes = buffer.readableBytes ( );
>>> byte [ ] bytes = new byte [ readableBytes ];
>>> buffer.readBytes ( bytes );
>>> Object object = ByteObjectConverter.toObject ( bytes );
>>> if (object instanceof KarmaMessage ) {
>>> this.receiver.update ( ((KarmaMessage)object).message ) ;
>>> } else if (object instanceof KarmaChunk ) {
>>> this.receiver.update ( ((KarmaChunk)object) ) ;
>>> } else if (object instanceof Serializable ) {
>>> this.receiver.update ( ((KarmaAumlet)object) ) ; }
>>>
>>> return ( ( ChannelBuffer ) msg ).toString ( charsetName );
>>> }
>>>
>>>
>>>
>>>
>>>
>>>
>>> _______________________________________________
>>> netty-users mailing list
>>> netty-users at lists.jboss.org
>>> https://lists.jboss.org/mailman/listinfo/netty-users
>>>
>>>
>>
>> _______________________________________________
>> netty-users mailing list
>> netty-users at lists.jboss.org
>> https://lists.jboss.org/mailman/listinfo/netty-users
>>
>>
>
>
> -----
> Hardware/Software Architect
> --
> View this message in context: http://n2.nabble.com/Closing-the-Client---Sender-tp3425932p3429078.html
> Sent from the Netty User Group mailing list archive at Nabble.com.
>
> _______________________________________________
> netty-users mailing list
> netty-users at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/netty-users
>
More information about the netty-users
mailing list