User: starksm 
  Date: 02/04/09 11:06:01

  Modified:    src/main/org/jboss/mq Tag: Branch_2_4 Connection.java
  Log:
  Integrate connection ping patch
  
  Revision  Changes    Path
  No                   revision
  
  
  No                   revision
  
  
  1.3.2.3   +168 -14   jbossmq/src/main/org/jboss/mq/Connection.java
  
  Index: Connection.java
  ===================================================================
  RCS file: /cvsroot/jboss/jbossmq/src/main/org/jboss/mq/Connection.java,v
  retrieving revision 1.3.2.2
  retrieving revision 1.3.2.3
  diff -u -r1.3.2.2 -r1.3.2.3
  --- Connection.java   28 Nov 2001 06:15:27 -0000      1.3.2.2
  +++ Connection.java   9 Apr 2002 18:06:00 -0000       1.3.2.3
  @@ -11,21 +11,22 @@
   import java.io.IOException;
   import java.io.Serializable;
   import java.util.Collection;
  -
   import java.util.HashMap;
   import java.util.HashSet;
   import java.util.Iterator;
  -
   import java.util.LinkedList;
   import java.util.Properties;
   import javax.jms.ConnectionMetaData;
  -
   import javax.jms.Destination;
   import javax.jms.ExceptionListener;
   import javax.jms.JMSException;
   import javax.jms.Queue;
   import javax.jms.Topic;
   
  +import EDU.oswego.cs.dl.util.concurrent.Semaphore;
  +import EDU.oswego.cs.dl.util.concurrent.ClockDaemon;
  +import EDU.oswego.cs.dl.util.concurrent.ThreadFactory;
  +
   import org.jboss.logging.Logger;
   import org.jboss.mq.il.ClientIL;
   import org.jboss.mq.il.ClientILService;
  @@ -38,7 +39,7 @@
    * @author     Norbert Lataille ([EMAIL PROTECTED])
    * @author     Hiram Chirino ([EMAIL PROTECTED])
    * @created    August 16, 2001
  - * @version    $Revision: 1.3.2.2 $
  + * @version    $Revision: 1.3.2.3 $
    */
   public class Connection implements java.io.Serializable, javax.jms.Connection
   {
  @@ -64,20 +65,49 @@
      
      // The object that sets up the client IL
      protected ClientILService clientILService;
  -   
  +   /**
  +    * Manages the thread that pings the connection to see if it is 'alive'
  +    */
  +   static protected ClockDaemon clockDaemon = new ClockDaemon();
  +
  +   /**
  +    * How often to ping the connection
  +    */
  +   protected long pingPeriod = 1000 * 60;
  +
  +   /**
  +    * This feild is reset when a ping is sent, set when ponged.
  +    */
  +   protected boolean ponged=true;
  +
  +   /**
  +    * This is used to know when the PingTask is running
  +    */
  +   Semaphore pingTaskSemaphore = new Semaphore(1);
  +
  +   /**
  +    * Identifies the PinkTask in the ClockDaemon
  +    */
  +   Object pingTaskId;
  +
  +   /**
  +    * Set a soon as close() is called on the connection.
  +    */
  +   protected volatile boolean closing = false;
  +
      //LinkedList of all created sessions by this connection
  -   HashSet          createdSessions;
  +   HashSet createdSessions;
      // Numbers subscriptions
  -   int              subscriptionCounter = Integer.MIN_VALUE;
  +   int subscriptionCounter = Integer.MIN_VALUE;
      //Is the connection closed ?
  -   boolean          closed;
  +   boolean closed;
      // Used to control tranactions
      SpyXAResourceManager spyXAResourceManager;
      
      //The class that created this connection
      GenericConnectionFactory genericConnectionFactory;
      //Last message ID returned
  -   private int      lastMessageID;
  +   private int lastMessageID;
      
      //the exceptionListener
      private ExceptionListener exceptionListener;
  @@ -86,7 +116,26 @@
      private StringBuffer sb = new StringBuffer();
      private char[]   charStack = new char[22];
      public static ThreadGroup threadGroup = new ThreadGroup( "JBossMQ Client 
Threads" );
  -   
  +
  +   /**
  +    * Static class initializer..
  +    */
  +   static
  +   {
  +      log.debug("Setting the clockDaemon's thread factory");
  +      clockDaemon.setThreadFactory(
  +         new ThreadFactory()
  +         {
  +            public Thread newThread(Runnable r)
  +            {
  +               Thread t = new Thread(threadGroup, r, "Connection Monitor Thread");
  +               t.setDaemon(true);
  +               return t;
  +            }
  +         }
  +      );
  +   }
  +
      //////////////////////////////////////////////////////////////
      // Constructors
      //////////////////////////////////////////////////////////////
  @@ -130,7 +179,7 @@
         
         // Setup the XA Resource manager,
         spyXAResourceManager = new SpyXAResourceManager( this );
  -      
  +      startPingThread();
         if( trace )
            log.trace( "Connection establishment successful" );
      }
  @@ -285,16 +334,15 @@
         boolean trace = log.isTraceEnabled();
         if( trace )
            log.trace( "Closing sessions, ClientID=" + connectionToken.getClientID() );
  +      closing = true;
         //notify his sessions
         synchronized ( createdSessions )
         {
  -         
            Object[] vect = createdSessions.toArray();
            for ( int i = 0; i < vect.length; i++ )
            {
               ( ( SpySession )vect[i] ).close();
            }
  -         
         }
         if( trace )
         {
  @@ -309,6 +357,8 @@
         {
            throw new SpyJMSException( "Cannot close properly the connection", e );
         }
  +      // Clean up after the ping thread..
  +      stopPingThread();
         stopILService();
         if( trace )
            log.trace( "Disconnected from server" );
  @@ -317,7 +367,7 @@
         // on this connection have been closed.
         closed = true;
      }
  -   
  +
      //called by a TemporaryDestination which is going to be deleted()
      public void deleteTemporaryDestination( SpyDestination dest )
         throws JMSException
  @@ -407,6 +457,10 @@
      
      public void asynchFailure( String reason, Exception e )
      {
  +      // Exceptions due to closing will be ignored.
  +      if( closing == true )
  +         return;
  +
         JMSException excep = new SpyJMSException( reason );
         excep.setLinkedException( e );
         excep.fillInStackTrace();
  @@ -762,4 +816,104 @@
         }
         
      }
  +
  +
  +   /**
  +    * #Description of the Method
  +    *
  +    * @param serverTime  Description of Parameter
  +    */
  +   public void asynchPong(long serverTime)
  +   {
  +      if( log.isDebugEnabled() )
  +         log.debug("PONG, serverIL="+serverIL);
  +      ponged = true;
  +   }
  +
  +   //Send a message to the serverIL
  +   void pingServer(long clientTime)
  +      throws JMSException
  +   {
  +
  +      if (closed)
  +      {
  +         throw new IllegalStateException("The connection is closed");
  +      }
  +
  +      try
  +      {
  +         log.debug("PING");
  +         serverIL.ping(connectionToken, clientTime);
  +      }
  +      catch (Exception e)
  +      {
  +         throw new SpyJMSException("Cannot ping the JMS server", e);
  +      }
  +   }
  +
  +   private void startPingThread()
  +   {
  +      // Ping thread does not need to be running if the
  +      // ping period is 0.
  +      if( pingPeriod == 0 )
  +         return;
  +      pingTaskId = clockDaemon.executePeriodically(pingPeriod,new PingTask(), true);
  +   }
  +
  +   private void stopPingThread()
  +   {
  +      // Ping thread was not running if ping period is 0.
  +      if( pingPeriod == 0 )
  +         return;
  +
  +      clockDaemon.cancel(pingTaskId);
  +
  +      //Aquire the Semaphore to make sure the ping task is not running.
  +      try
  +      {
  +         pingTaskSemaphore.attempt(1000 * 10);
  +      }
  +      catch (InterruptedException e)
  +      {
  +         Thread.currentThread().interrupt();
  +      }
  +   }
  +
  +   /**
  +    * #Description of the Class
  +    */
  +   class PingTask implements Runnable
  +   {
  +      /**
  +       * Main processing method for the PingTask object
  +       */
  +      public void run()
  +      {
  +         try
  +         {
  +            pingTaskSemaphore.acquire();
  +         } catch ( InterruptedException e )
  +         {
  +            return;
  +         }
  +         try
  +         {
  +            if (ponged == false)
  +            {
  +               // Server did not pong use with in the timeout
  +               // period..  Assuming the connection is dead.
  +               throw new SpyJMSException("", new IOException("ping timeout."));
  +            }
  +            ponged = false;
  +            pingServer(System.currentTimeMillis());
  +         }
  +         catch (JMSException e)
  +         {
  +            asynchFailure("Connection Failed", e.getLinkedException());
  +         } finally
  +         {
  +            pingTaskSemaphore.release();}
  +      }
  +   }
  +
   }
  
  
  

_______________________________________________________________

Sponsored by:
Looking for hip toys and fun scwag.  There is no better place
then the good friends at ThinkGeek. http://www.ThinkGeek.com/
_______________________________________________
Jboss-development mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/jboss-development

Reply via email to