User: chirino 
  Date: 01/09/26 20:27:42

  Modified:    src/main/org/jboss/mq Connection.java
                        GenericConnectionFactory.java
  Log:
  Due to popular demand the PingThread is now shared by all the connections
  in one VM.  And the the ping period is configurable at each ConnectionFactory.
  
  Revision  Changes    Path
  1.7       +82 -84    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.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- Connection.java   2001/09/26 05:02:27     1.6
  +++ Connection.java   2001/09/27 03:27:42     1.7
  @@ -6,6 +6,7 @@
    */
   package org.jboss.mq;
   import EDU.oswego.cs.dl.util.concurrent.Semaphore;
  +import EDU.oswego.cs.dl.util.concurrent.ClockDaemon;
   
   import java.io.File;
   import java.io.FileInputStream;
  @@ -36,7 +37,7 @@
    *
    * @author    Norbert Lataille ([EMAIL PROTECTED])
    * @author    Hiram Chirino ([EMAIL PROTECTED])
  - * @version   $Revision: 1.6 $
  + * @version   $Revision: 1.7 $
    * @created   August 16, 2001
    */
   public class Connection implements java.io.Serializable, javax.jms.Connection
  @@ -45,68 +46,72 @@
       * Description of the Field
       */
      public static ThreadGroup threadGroup = new ThreadGroup("JBossMQ Client 
Threads");
  -   /**
  -    * Description of the Field
  -    */
  -   protected final static long PING_INTERVAL = 1000 * 60;
  +
      static org.apache.log4j.Category cat = 
org.apache.log4j.Category.getInstance(Connection.class);
  -   //Maps a destination to a LinkedList of Subscriptions
  +   
      /**
  -    * Description of the Field
  +    * Maps a destination to a LinkedList of Subscriptions
       */
      public HashMap destinationSubscriptions = new HashMap();
  -   //Maps a a subsction id to a Subscription
  +   
      /**
  -    * Description of the Field
  +    * Maps a a subsction id to a Subscription
       */
      public HashMap subscriptions = new HashMap();
  -   //Is the connection stopped ?
      /**
  -    * Description of the Field
  +    * Is the connection stopped ?
       */
      public boolean modeStop;
      //////////////////////////////////////////////////////////////
      // Attributes
      //////////////////////////////////////////////////////////////
   
  -   // This is our connection to the JMS server
      /**
  -    * Description of the Field
  +    * This is our connection to the JMS server
       */
      protected ServerIL serverIL;
  -   //This is the clientID
  +   
      /**
  -    * Description of the Field
  +    * This is the clientID
       */
      protected String clientID;
  -   // The connection token is used to identify our connection
  -   // to the server.
      /**
  -    * Description of the Field
  +    * The connection token is used to identify our connection to the server.
       */
      protected ConnectionToken connectionToken;
   
  -   // The object that sets up the client IL
      /**
  -    * Description of the Field
  +    * The object that sets up the client IL
       */
      protected ClientILService clientILService;
   
  -   // The thread that pings the connection to see if it is 'alive'
      /**
  -    * Description of the Field
  +    * Manages the thread that pings the connection to see if it is 'alive'
       */
  -   protected Thread pingThread;
  +   static protected ClockDaemon clockDaemon = new ClockDaemon();
  +   
      /**
  -    * Description of the Field
  +    * How often to ping the connection
       */
  -   protected boolean ponged;
  +   protected long pingPeriod = 1000 * 60;
  +   
      /**
  -    * Description of the Field
  +    * This feild is reset when a ping is sent, set when ponged.
       */
  -   protected Semaphore pingTaskSemaphore = new Semaphore(0);
  +   protected boolean ponged=true;
  +   
      /**
  -    * Description of the Field
  +    * 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;
   
  @@ -171,22 +176,11 @@
   
         // Setup the XA Resource manager,
         spyXAResourceManager = new SpyXAResourceManager(this);
  -
  -      // Start the pingThread...
  -      pingThread = new Thread(threadGroup, new PingTask(), "Connection Monitor");
  -      pingThread.start();
  -      // Wait for the ping thread to start..
  -      try
  -      {
  -         pingTaskSemaphore.acquire();
  -      }
  -      catch (InterruptedException e)
  -      {
  -         Thread.currentThread().interrupt();
  -      }
   
  +      // Used to monitor the connection.
  +      startPingThread();
  +      
         cat.debug("Connection establishment successful");
  -
      }
   
      //////////////////////////////////////////////////////////////
  @@ -433,9 +427,6 @@
         cat.debug("Closing sessions, ClientID=" + connectionToken.getClientID());
         closing = true;
   
  -      // Stop the ping thread
  -      pingThread.interrupt();
  -
         //notify his sessions
         synchronized (createdSessions)
         {
  @@ -460,17 +451,9 @@
            throw new SpyJMSException("Cannot close properly the connection", e);
         }
   
  -      cat.debug("Waiting for ping thread to stop");
  -      // Try to wait for the ping thread to stop.
  -      try
  -      {
  -         pingTaskSemaphore.attempt(1000 * 10);
  -      }
  -      catch (InterruptedException e)
  -      {
  -         Thread.currentThread().interrupt();
  -      }
  -
  +      // Clean up after the ping thread..
  +      stopPingThread();
  +      
         cat.debug("Stoping the ClientIL service");
         stopILService();
         cat.debug("Disconnected from server");
  @@ -1006,41 +989,56 @@
          */
         public void run()
         {
  -
  -         // Ping until we are closed or interrupted.
  -         try
  -         {
  -            pingTaskSemaphore.release();
  -            while (!closed)
  +         try {
  +            pingTaskSemaphore.acquire();
  +         } catch ( InterruptedException e ) {
  +            return;
  +         }
  +         try {
  +            if (ponged == false)
               {
  -
  -               ponged = false;
  -               pingServer(System.currentTimeMillis());
  -               Thread.currentThread().sleep(PING_INTERVAL);
  -               // Wait for a ping time out..
  -
  -               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."));
  -               }
  -
  +               // 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();}
            }
  -         catch (InterruptedException e)
  -         {
  -         }
  -         finally
  -         {
  -            // Used to signal that we are done..
  -            cat.debug("PingTask closing.");
  -            pingTaskSemaphore.release();
  -         }
  +   }
  +
  +   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();
         }
      }
   }
  
  
  
  1.3       +7 -1      jbossmq/src/main/org/jboss/mq/GenericConnectionFactory.java
  
  Index: GenericConnectionFactory.java
  ===================================================================
  RCS file: 
/cvsroot/jboss/jbossmq/src/main/org/jboss/mq/GenericConnectionFactory.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- GenericConnectionFactory.java     2001/08/17 03:04:01     1.2
  +++ GenericConnectionFactory.java     2001/09/27 03:27:42     1.3
  @@ -19,7 +19,7 @@
    *
    * @author     Hiram Chirino ([EMAIL PROTECTED])
    * @created    August 16, 2001
  - * @version    $Revision: 1.2 $
  + * @version    $Revision: 1.3 $
    */
   public class GenericConnectionFactory implements java.io.Serializable {
   
  @@ -34,6 +34,7 @@
      // These are the keys we look for in the connection properties
      public final static String SERVER_IL_FACTORY_KEY = "ServerILFactory";
      public final static String CLIENT_IL_SERVICE_KEY = "ClientILService";
  +   public final static String PING_PERIOD_KEY = "PingPeriod";
   
      static org.apache.log4j.Category cat = org.apache.log4j.Category.getInstance( 
GenericConnectionFactory.class );
   
  @@ -64,6 +65,11 @@
       */
      public ClientILService createClientILService( Connection connection )
         throws Exception {
  +      // This is a good time to setup the PingPeriod
  +      String pingPeriod = connectionProperties.getProperty( PING_PERIOD_KEY, 
""+connection.pingPeriod ); 
  +      connection.pingPeriod = Long.parseLong(pingPeriod);
  +      
  +      // Setup the client connection.
         String clientILServiceCN = connectionProperties.getProperty( 
CLIENT_IL_SERVICE_KEY );
         ClientILService service = ( ClientILService )Class.forName( clientILServiceCN 
).newInstance();
         service.init( connection, connectionProperties );
  
  
  

_______________________________________________
Jboss-development mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/jboss-development

Reply via email to