[jboss-remoting-commits] JBoss Remoting SVN: r5084 - remoting2/branches/2.x/src/main/org/jboss/remoting/transport/socket.

jboss-remoting-commits at lists.jboss.org jboss-remoting-commits at lists.jboss.org
Wed Apr 22 19:59:35 EDT 2009


Author: ron.sigal at jboss.com
Date: 2009-04-22 19:59:35 -0400 (Wed, 22 Apr 2009)
New Revision: 5084

Added:
   remoting2/branches/2.x/src/main/org/jboss/remoting/transport/socket/TimedOutputStream.java
Log:
JBREM-1120: New OutputStream for output timeout facility.

Added: remoting2/branches/2.x/src/main/org/jboss/remoting/transport/socket/TimedOutputStream.java
===================================================================
--- remoting2/branches/2.x/src/main/org/jboss/remoting/transport/socket/TimedOutputStream.java	                        (rev 0)
+++ remoting2/branches/2.x/src/main/org/jboss/remoting/transport/socket/TimedOutputStream.java	2009-04-22 23:59:35 UTC (rev 5084)
@@ -0,0 +1,127 @@
+package org.jboss.remoting.transport.socket;
+
+import java.io.IOException;
+import java.io.OutputStream;
+import java.util.Timer;
+import java.util.TimerTask;
+
+import org.apache.log4j.Logger;
+
+public class TimedOutputStream extends OutputStream
+{
+   static private Timer timer = new Timer("TimedOutputStreamTimer", true);
+   static private Logger log = Logger.getLogger(TimedOutputStream.class);
+   
+   private OutputStream os;
+   private int outputTimeout;
+   private OutputTimerTask timerTask;
+   private Object lock = new Object();
+   
+   public TimedOutputStream(OutputStream os, int outputTimeout)
+   {
+      this.os = os;
+      this.outputTimeout = outputTimeout;
+   }
+   
+   public void close() throws IOException
+   {
+      os.close();
+   }
+   
+   public void write(int b) throws IOException
+   {
+      synchronized (lock)
+      {
+         if (timerTask == null)
+         {
+            try
+            {
+               timerTask = new OutputTimerTask(this);
+               timer.schedule(timerTask, outputTimeout);
+            }
+            catch (IllegalStateException e)
+            {
+               timer = new Timer("TimedOutputStreamTimer", true);
+               timer.schedule(new OutputTimerTask(this), outputTimeout);
+               log.info("scheduled OutputTimerTask: " + outputTimeout);
+            }
+         }
+      }
+      
+      try
+      {
+         os.write(b);
+      }
+      finally
+      {
+         synchronized (lock)
+         {
+            timerTask.cancel();
+            timerTask = null;
+         }
+      }
+   }
+   
+   public void write(byte b[], int off, int len) throws IOException
+   {
+      synchronized (lock)
+      {
+         if (timerTask == null)
+         {
+            try
+            {
+               timerTask = new OutputTimerTask(this);
+               timer.schedule(timerTask, outputTimeout);
+            }
+            catch (IllegalStateException e)
+            {
+               timer = new Timer("TimedOutputStreamTimer", true);
+               timer.schedule(new OutputTimerTask(this), outputTimeout);  
+            }
+         }
+      }
+      
+      try
+      {
+         os.write(b, off, len);
+      }
+      finally
+      {
+         synchronized (lock)
+         {
+            timerTask.cancel();
+            timerTask = null;
+         }
+      }
+   }
+   
+   static class OutputTimerTask extends TimerTask
+   {
+      private TimedOutputStream tos;
+      
+      public OutputTimerTask(TimedOutputStream tos)
+      {
+         this.tos = tos;
+      }
+      
+      public void run()
+      {
+         try
+         {
+            log.info("closing: " + tos);
+            tos.close();
+            tos = null;
+         }
+         catch (IOException e)
+         {
+            log.debug("unable to close " + tos);
+         }
+      }
+      
+      public boolean cancel()
+      {
+         tos = null;
+         return super.cancel();
+      }
+   }
+}




More information about the jboss-remoting-commits mailing list