Not receiving all data after attempting to close the channel.

Michael McGrady mmcgrady at topiatechnology.com
Thu Nov 19 13:59:41 EST 2009


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

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/netty-users/attachments/20091119/dce2a3ac/attachment-0001.html 


More information about the netty-users mailing list