Not receiving all data after attempting to close the channel.

Stephen Pape Stephen.Pape.ctr at RL.af.mil
Thu Nov 19 14:24:43 EST 2009


Thanks for the reply. Isn't what you're doing essentially the same thing?
You're getting notified via operationComplete() that the write was
(supposedly) completed, then allowing execution to continue. That seems like
what I'm doing, except I immediately close the channel.

I found a similar thread about this topic here:
http://www.jboss.org/netty/community.html#nabble-td3398725|a3398725


Trustin Lee wrote:
> 
> You can achieve this pretty simply:
> 
>   channel.write(ChannelBuffers.EMPTY_BUFFER)
>          .addListener(ChannelFutureListener.CLOSE);
> 

Given that response, I assumed my method should work. I was doing the same
thing, adding a listener to close after the last write completed. I wasn't
using an empty buffer because my channel pipeline couldn't accept a
ChannelBuffer directly. 

Is there some reason that the channel can't be closed immediately after a
write has completed?

-Stephen


Mike McGrady wrote:
> 
> My solution was to use monitors with the asynchronous Netty functionality.
> 
> Here is the monitor:
> 
> 
> 
> 	private class WriteKarmaManagerMonitor {
> 		private final AtomicBoolean managerIsSent = new AtomicBoolean(false);
> 
> 		protected void managerIsSent() {
> 			this.managerIsSent.set(true);
> 		}
> 
> 		protected AtomicBoolean isManagerSent() {
> 			return this.managerIsSent;
> 		}
> 	}
> 
> And here is its use:
> 
> 
> 
> 		while (!this.channel.isWritable()) {
> 			try {
> 				Thread.sleep(3);
> 			} catch (InterruptedException e) {
> 				logger.log(InternalLogLevel.ERROR, e.getMessage());
> 			}
> 		}
> 
> 		ChannelFuture future = this.channel.write(buffer);
> 
> 		future.addListener(new ChannelFutureListener() {
> 
> 			public void operationComplete(ChannelFuture future) {
> 				// Notify that snnd is complete monitor.
> 				synchronized (NettyKarmaSender.this.writeManagerMonitor) {
> 
> 					NettyKarmaSender.this.writeManagerMonitor.managerIsSent();
> 					NettyKarmaSender.this.writeManagerMonitor.notifyAll();
> 					NettyKarmaSender.this.managerSent++;
> 					count++;
> 				}
> 			}
> 
> 		});
> 
> 		// Make sure manager is written before proceeding.
> 		synchronized (this.writeManagerMonitor) {
> 			if (!this.writeManagerMonitor.isManagerSent().get()) {
> 				try {
> 
> 					this.writeManagerMonitor.wait();
> 				} catch (InterruptedException e) {
> 					logger.log(InternalLogLevel.ERROR, e.getMessage());
> 				}
> 			}
> 		}
> 
> From: "Pape, Stephen R CTR USAF AFMC AFRL/RISE"
> <Stephen.Pape.ctr at RL.af.mil>
> Date: November 19, 2009 7:24:17 AM PST
> To: <netty-users at lists.jboss.org>
> Subject: Not receiving all data after attempting to close the channel.
> Reply-To: Netty Users <netty-users at lists.jboss.org>
> 
> Hello,
>  
> I’m having a problem with both 3.2.0.ALPHA1 and 3.1.5.GA (haven’t tested
> other versions).
>  
> Basically, in order to make sure that all pending data is written, I have
> something like this:
>  
> private volatile ChannelFuture writeFuture = null;
>                 private volatile Channel channel = null; // Set later
>  
> public void write(Object obj)
> {
>    writeFuture = channel.write(obj);
> }
>  
> public void close()
> {
>   If(this.writeFuture != null)
>      this.writeFuture.addListener(this);
>   else
>      this.channel.close();
> }
>  
> public void operationComplete(ChannelFuture future)
> {
>   // Thread.sleep(1000);
>   future.getChannel.close();
> }
>  
>  
> This class is being used to write Objects in a loop. Immediately after the
> loop the close method is called.
>  
> I verified that the correct writeFuture is being used in the close()
> method, but the remote side is not receiving all of the data. When I write
> 10 objects across, sometimes only 7 or 8 will make it through.
>  
> If I uncomment Thread.sleep(), all of the data is consistently received by
> the remote side, so it would seem somehow I’m closing the channel too
> early. Is there something I’m doing wrong?
>  
> Thanks!
>  
> -Stephen
> _______________________________________________
> netty-users mailing list
> netty-users at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/netty-users
> 
> 
> From: "Pape, Stephen R CTR USAF AFMC AFRL/RISE"
> <Stephen.Pape.ctr at RL.af.mil>
> Date: November 19, 2009 7:24:17 AM PST
> To: <netty-users at lists.jboss.org>
> Subject: Not receiving all data after attempting to close the channel.
> Reply-To: Netty Users <netty-users at lists.jboss.org>
> 
> Hello,
>  
> I’m having a problem with both 3.2.0.ALPHA1 and 3.1.5.GA (haven’t tested
> other versions).
>  
> Basically, in order to make sure that all pending data is written, I have
> something like this:
>  
> private volatile ChannelFuture writeFuture = null;
>                 private volatile Channel channel = null; // Set later
>  
> public void write(Object obj)
> {
>    writeFuture = channel.write(obj);
> }
>  
> public void close()
> {
>   If(this.writeFuture != null)
>      this.writeFuture.addListener(this);
>   else
>      this.channel.close();
> }
>  
> public void operationComplete(ChannelFuture future)
> {
>   // Thread.sleep(1000);
>   future.getChannel.close();
> }
>  
>  
> This class is being used to write Objects in a loop. Immediately after the
> loop the close method is called.
>  
> I verified that the correct writeFuture is being used in the close()
> method, but the remote side is not receiving all of the data. When I write
> 10 objects across, sometimes only 7 or 8 will make it through.
>  
> If I uncomment Thread.sleep(), all of the data is consistently received by
> the remote side, so it would seem somehow I’m closing the channel too
> early. Is there something I’m doing wrong?
>  
> Thanks!
>  
> -Stephen
> _______________________________________________
> netty-users mailing list
> netty-users at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/netty-users
> 
> 
> 
> 
> 
> On Nov 19, 2009, at 7:24 AM, Pape, Stephen R CTR USAF AFMC AFRL/RISE
> wrote:
> 
>> Hello,
>>  
>> I’m having a problem with both 3.2.0.ALPHA1 and 3.1.5.GA (haven’t tested
>> other versions).
>>  
>> Basically, in order to make sure that all pending data is written, I have
>> something like this:
>>  
>> private volatile ChannelFuture writeFuture = null;
>>                 private volatile Channel channel = null; // Set later
>>  
>> public void write(Object obj)
>> {
>>    writeFuture = channel.write(obj);
>> }
>>  
>> public void close()
>> {
>>   If(this.writeFuture != null)
>>      this.writeFuture.addListener(this);
>>   else
>>      this.channel.close();
>> }
>>  
>> public void operationComplete(ChannelFuture future)
>> {
>>   // Thread.sleep(1000);
>>   future.getChannel.close();
>> }
>>  
>>  
>> This class is being used to write Objects in a loop. Immediately after
>> the loop the close method is called.
>>  
>> I verified that the correct writeFuture is being used in the close()
>> method, but the remote side is not receiving all of the data. When I
>> write 10 objects across, sometimes only 7 or 8 will make it through.
>>  
>> If I uncomment Thread.sleep(), all of the data is consistently received
>> by the remote side, so it would seem somehow I’m closing the channel too
>> early. Is there something I’m doing wrong?
>>  
>> Thanks!
>>  
>> -Stephen
>> _______________________________________________
>> 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
> 
> 

-- 
View this message in context: http://n2.nabble.com/Not-receiving-all-data-after-attempting-to-close-the-channel-tp4033482p4033860.html
Sent from the Netty User Group mailing list archive at Nabble.com.



More information about the netty-users mailing list