You are viewing a plain text version of this content. The canonical link for it is here.
Posted to scm@excalibur.apache.org by le...@apache.org on 2004/07/27 20:50:49 UTC

svn commit: rev 30795 - in excalibur/trunk/containerkit/instrument-client/src/java/org/apache/excalibur/instrument/client: . http

Author: leif
Date: Tue Jul 27 11:50:48 2004
New Revision: 30795

Modified:
   excalibur/trunk/containerkit/instrument-client/src/java/org/apache/excalibur/instrument/client/InstrumentClientFrame.java
   excalibur/trunk/containerkit/instrument-client/src/java/org/apache/excalibur/instrument/client/InstrumentManagerConnection.java
   excalibur/trunk/containerkit/instrument-client/src/java/org/apache/excalibur/instrument/client/InstrumentManagerConnectionListener.java
   excalibur/trunk/containerkit/instrument-client/src/java/org/apache/excalibur/instrument/client/http/HTTPInstrumentManagerConnection.java
Log:
Give each connection its own thread to avoid timeouts on one server from affecting the performance of the connection to another.

Modified: excalibur/trunk/containerkit/instrument-client/src/java/org/apache/excalibur/instrument/client/InstrumentClientFrame.java
==============================================================================
--- excalibur/trunk/containerkit/instrument-client/src/java/org/apache/excalibur/instrument/client/InstrumentClientFrame.java	(original)
+++ excalibur/trunk/containerkit/instrument-client/src/java/org/apache/excalibur/instrument/client/InstrumentClientFrame.java	Tue Jul 27 11:50:48 2004
@@ -69,7 +69,7 @@
  */
 class InstrumentClientFrame
     extends JFrame
-    implements Runnable, InstrumentManagerConnectionListener, LogEnabled
+    implements InstrumentManagerConnectionListener, LogEnabled
 {
     protected static final String MEDIA_PATH = "org/apache/excalibur/instrument/client/media/";
     
@@ -92,7 +92,6 @@
     /** Shutdown hook */
     private Thread m_hook;
     
-    private Thread m_runner;
     private Logger m_logger;
     
     /*---------------------------------------------------------------
@@ -114,84 +113,11 @@
     {
         init();
         
-        m_runner = new Thread( this, "InstrumentClientFrameRunner" );
-        m_runner.start();
-        
         ClassLoader cl = this.getClass().getClassLoader();
         setIconImage( new ImageIcon( cl.getResource( MEDIA_PATH + "client.gif") ).getImage() );
     }
     
     /*---------------------------------------------------------------
-     * Runnable Methods
-     *-------------------------------------------------------------*/
-    public void run()
-    {
-        while( m_runner != null )
-        {
-            try
-            {
-                try
-                {
-                    Thread.sleep( 1000 );
-                }
-                catch( InterruptedException e )
-                {
-                    if ( m_runner == null )
-                    {
-                        return;
-                    }
-                }
-                
-                // Update each of the InstrumentManagerConnections.
-                InstrumentManagerConnection[] connections = getConnections();
-                for ( int i = 0; i < connections.length; i++ )
-                {
-                    InstrumentManagerConnection connection = connections[i];
-                    connection.update();
-                    
-                    // Update the tab title.
-                    int tabIndex = m_connectionsPane.indexOfComponent( connection );
-                    if ( tabIndex >= 0 )
-                    {
-                        m_connectionsPane.setTitleAt( tabIndex, connection.getTabTitle() );
-                        m_connectionsPane.setToolTipTextAt( tabIndex, connection.getTabTooltip() );
-                    }
-                }
-                
-                // Update each of the InstrumentSampleFrames.
-                JInternalFrame[] frames = m_desktopPane.getAllFrames();
-                for( int i = 0; i < frames.length; i++ )
-                {
-                    JInternalFrame frame = frames[ i ];
-                    
-                    if( frame instanceof InstrumentSampleFrame )
-                    {
-                        ( (InstrumentSampleFrame)frame ).update();
-                    }
-                }
-            }
-            catch( Throwable t )
-            {
-                // Should not get here, but we want to make sure that this never happens.
-                getLogger().error( "Unexpected error caught in InstrumentClientFrame runner:", t );
-                
-                // Avoid thrashing.
-                try
-                {
-                    Thread.sleep( 5000 );
-                }
-                catch ( InterruptedException e )
-                {
-                    if ( m_runner == null )
-                    {
-                        return;
-                    }
-                }
-            }
-        }
-    }
-    
-    /*---------------------------------------------------------------
      * InstrumentManagerConnectionListener Methods
      *-------------------------------------------------------------*/
     /**
@@ -202,13 +128,8 @@
      */
     public void opened( InstrumentManagerConnection connection )
     {
-        // Remove the tab
-        int tabIndex = m_connectionsPane.indexOfComponent( connection );
-        if ( tabIndex >= 0 )
-        {
-            m_connectionsPane.setTitleAt( tabIndex, connection.getTabTitle() );
-            m_connectionsPane.setToolTipTextAt( tabIndex, connection.getTabTooltip() );
-        }
+        getLogger().debug( "opened: " + connection.getKey() );
+        updateConnectionTab( connection );
     }
     
     /**
@@ -219,13 +140,8 @@
      */
     public void closed( InstrumentManagerConnection connection )
     {
-        // Remove the tab
-        int tabIndex = m_connectionsPane.indexOfComponent( connection );
-        if ( tabIndex >= 0 )
-        {
-            m_connectionsPane.setTitleAt( tabIndex, connection.getTabTitle() );
-            m_connectionsPane.setToolTipTextAt( tabIndex, connection.getTabTooltip() );
-        }
+        getLogger().debug( "closed: " + connection.getKey() );
+        updateConnectionTab( connection );
     }
     
     /**
@@ -235,6 +151,7 @@
      */
     public void deleted( InstrumentManagerConnection connection )
     {
+        getLogger().debug( "deleted: " + connection.getKey() );
         // Remove the tab
         int tabIndex = m_connectionsPane.indexOfComponent( connection );
         if ( tabIndex >= 0 )
@@ -468,14 +385,13 @@
         }
         
         
-        // Build up a list of existing frames.
-        List oldFrames = new ArrayList();
+        // Always hide all existing frames as new ones will be created.
         JInternalFrame frames[] = m_desktopPane.getAllFrames();
         for ( int i = 0; i < frames.length; i++ )
         {
             if ( frames[i] instanceof AbstractInternalFrame )
             {
-                oldFrames.add( frames[i] );
+                ((AbstractInternalFrame)frames[i]).hideFrame();
             }
         }
         
@@ -519,8 +435,7 @@
                     // Let the connection load the frame.
                     try
                     {
-                        InstrumentSampleFrame sampleFrame = connection.loadSampleFrame( frameConf );
-                        oldFrames.remove( sampleFrame );
+                        connection.loadSampleFrame( frameConf );
                     }
                     catch ( ConfigurationException e )
                     {
@@ -545,13 +460,6 @@
                 getLogger().warn( "Not loading inner frame due to unknown type: " + type );
             }
         }
-        
-        // Any old frames left need to be deleted.
-        for ( Iterator iter = oldFrames.iterator(); iter.hasNext(); )
-        {
-            AbstractInternalFrame frame = (AbstractInternalFrame)iter.next();
-            frame.hideFrame();
-        }
     }
 
     /**
@@ -763,6 +671,17 @@
         setLocation( screenWidth / 20, screenHeight / 20 );
         setSize( screenWidth * 9 / 10, screenHeight * 8 / 10 );
     }
+
+    void updateConnectionTab( InstrumentManagerConnection connection )
+    {
+        // Update the tab title.
+        int tabIndex = m_connectionsPane.indexOfComponent( connection );
+        if ( tabIndex >= 0 )
+        {
+            m_connectionsPane.setTitleAt( tabIndex, connection.getTabTitle() );
+            m_connectionsPane.setToolTipTextAt( tabIndex, connection.getTabTooltip() );
+        }
+    }
     
     private void updateTitle()
     {
@@ -1052,11 +971,11 @@
             }
         }
         
-        // Stop the runner.
-        if ( m_runner != null )
+        // Delete all of the connections.
+        InstrumentManagerConnection[] connections = getConnections();
+        for ( int i = 0; i < connections.length; i++ )
         {
-            m_runner.interrupt();
-            m_runner = null;
+            connections[i].delete();
         }
         
         if ( !fallThrough )

Modified: excalibur/trunk/containerkit/instrument-client/src/java/org/apache/excalibur/instrument/client/InstrumentManagerConnection.java
==============================================================================
--- excalibur/trunk/containerkit/instrument-client/src/java/org/apache/excalibur/instrument/client/InstrumentManagerConnection.java	(original)
+++ excalibur/trunk/containerkit/instrument-client/src/java/org/apache/excalibur/instrument/client/InstrumentManagerConnection.java	Tue Jul 27 11:50:48 2004
@@ -52,7 +52,7 @@
  */
 public abstract class InstrumentManagerConnection
     extends JComponent
-    implements LogEnabled
+    implements LogEnabled, Runnable
 {
     private Logger m_logger;
     private InstrumentManagerTreeModel m_treeModel;
@@ -61,6 +61,7 @@
     private InstrumentClientFrame m_frame;
     
     private boolean m_deleted;
+    private Thread m_runner;
     
     private JLabel m_descriptionLabel;
 
@@ -73,6 +74,7 @@
 
     /** Maintain a list of all sample frames which are viewing data in this connection. */
     private Map m_sampleFrames = new HashMap();
+    private InstrumentSampleFrame[] m_sampleFrameArray = null;
     
     /*---------------------------------------------------------------
      * Constructors
@@ -99,6 +101,71 @@
     }
 
     /*---------------------------------------------------------------
+     * Runnable Methods
+     *-------------------------------------------------------------*/
+    public void run()
+    {
+        getLogger().debug( "Started " + Thread.currentThread().getName() );
+        try
+        {
+            while( m_runner != null )
+            {
+                try
+                {
+                    try
+                    {
+                        Thread.sleep( 1000 );
+                    }
+                    catch( InterruptedException e )
+                    {
+                        if ( m_runner == null )
+                        {
+                            return;
+                        }
+                    }
+                    
+                    update();
+                    
+                    m_frame.updateConnectionTab( this );
+                    
+                    // Update each of the InstrumentSampleFrames belonging to this connection.
+                    if ( isConnected() )
+                    {
+                        InstrumentSampleFrame[] frames = getSampleFrameArray();
+                        for( int i = 0; i < frames.length; i++ )
+                        {
+                            frames[i].update();
+                        }
+                    }
+                }
+                catch( Throwable t )
+                {
+                    // Should not get here, but we want to make sure that this never happens.
+                    getLogger().error(
+                        "Unexpected error caught in " + Thread.currentThread().getName(), t );
+                    
+                    // Avoid thrashing.
+                    try
+                    {
+                        Thread.sleep( 5000 );
+                    }
+                    catch ( InterruptedException e )
+                    {
+                        if ( m_runner == null )
+                        {
+                            return;
+                        }
+                    }
+                }
+            }
+        }
+        finally
+        {
+            getLogger().debug( "Stopped " + Thread.currentThread().getName() );
+        }
+    }
+    
+    /*---------------------------------------------------------------
      * Methods
      *-------------------------------------------------------------*/
     /**
@@ -216,6 +283,9 @@
         // Tree Pane
         m_tree = new InstrumentManagerTree( this );
         add( m_tree, BorderLayout.CENTER );
+        
+        m_runner = new Thread( this, "InstrumentManagerConnection[" + getKey() + "]" );
+        m_runner.start();
     }
     
     /**
@@ -433,7 +503,7 @@
      *
      * @return An array of the currently registered listeners
      */
-    private InstrumentManagerConnectionListener[] getListenerArray()
+    protected InstrumentManagerConnectionListener[] getListenerArray()
     {
         InstrumentManagerConnectionListener[] listenerArray = m_listenerArray;
         if ( listenerArray == null )
@@ -489,18 +559,48 @@
         return (InstrumentSampleFrame)m_sampleFrames.get( sampleName );
     }
 
+    /**
+     * Adds a single sample frame.
+     * Caller must synchronize on this connection before calling.
+     */
     private void addSampleFrame( String sampleName, InstrumentSampleFrame sampleFrame )
     {
         getLogger().debug("InstrumentManagerConnection.addSampleFrame(" + sampleName + ", frame)");
         // Assumes "this" is synchronized.
         m_sampleFrames.put( sampleName, sampleFrame );
+        m_sampleFrameArray = null;
     }
 
+    /**
+     * Removes a single sample frame.
+     * Caller must synchronize on this connection before calling.
+     */
     private void removeSampleFrame( String sampleName )
     {
         getLogger().debug("InstrumentManagerConnection.removeSampleFrame(" + sampleName + ")");
         // Assumes "this" is synchronized.
         m_sampleFrames.remove( sampleName );
+        m_sampleFrameArray = null;
+    }
+
+    /**
+     * Returns a thread save array representation of the InstrumentSampleFrames.
+     *
+     * @return A thread save array representation of the InstrumentSampleFrames.
+     */
+    private InstrumentSampleFrame[] getSampleFrameArray()
+    {
+        InstrumentSampleFrame[] array = m_sampleFrameArray;
+        if ( array == null )
+        {
+            synchronized(this)
+            {
+                m_sampleFrameArray = new InstrumentSampleFrame[m_sampleFrames.size()];
+                m_sampleFrames.values().toArray( m_sampleFrameArray );
+                array = m_sampleFrameArray;
+            }
+        }
+        return array;
     }
 
     /**
@@ -508,11 +608,9 @@
      *
      * @param sampleFrameState Saved state of the frame to load.
      *
-     * @returns The sample frame.
-     *
      * @throws ConfigurationException If there are any problems with the state.
      */
-    InstrumentSampleFrame loadSampleFrame( Configuration sampleFrameState )
+    void loadSampleFrame( Configuration sampleFrameState )
         throws ConfigurationException
     {
         // Get the sample name
@@ -538,8 +636,6 @@
             sampleFrame.addToDesktop( m_frame.getDesktopPane() );
         }
         sampleFrame.show();  // Outside of synchronization to avoid deadlocks.
-        
-        return sampleFrame;
     }
     
     /**
@@ -603,6 +699,20 @@
         getLogger().debug( "delete()" );
 
         m_deleted = true;
+        
+        Thread runner = m_runner;
+        if ( runner != null )
+        {
+            m_runner = null;
+            runner.interrupt();
+        }
+        
+        // Hide any of our own sample frames.
+        InstrumentSampleFrame[] frames = getSampleFrameArray();
+        for ( int i = 0; i < frames.length; i++ )
+        {
+            frames[i].hideFrame();
+        }
 
         // Notify the listeners.
         InstrumentManagerConnectionListener[] listenerArray = getListenerArray();

Modified: excalibur/trunk/containerkit/instrument-client/src/java/org/apache/excalibur/instrument/client/InstrumentManagerConnectionListener.java
==============================================================================
--- excalibur/trunk/containerkit/instrument-client/src/java/org/apache/excalibur/instrument/client/InstrumentManagerConnectionListener.java	(original)
+++ excalibur/trunk/containerkit/instrument-client/src/java/org/apache/excalibur/instrument/client/InstrumentManagerConnectionListener.java	Tue Jul 27 11:50:48 2004
@@ -22,7 +22,7 @@
  * @version CVS $Revision: 1.4 $ $Date: 2004/02/28 11:47:23 $
  * @since 4.1
  */
-interface InstrumentManagerConnectionListener
+public interface InstrumentManagerConnectionListener
 {
     /**
      * Called when the connection is opened.  May be called more than once if 

Modified: excalibur/trunk/containerkit/instrument-client/src/java/org/apache/excalibur/instrument/client/http/HTTPInstrumentManagerConnection.java
==============================================================================
--- excalibur/trunk/containerkit/instrument-client/src/java/org/apache/excalibur/instrument/client/http/HTTPInstrumentManagerConnection.java	(original)
+++ excalibur/trunk/containerkit/instrument-client/src/java/org/apache/excalibur/instrument/client/http/HTTPInstrumentManagerConnection.java	Tue Jul 27 11:50:48 2004
@@ -34,6 +34,7 @@
 
 import org.apache.excalibur.instrument.client.InstrumentableData;
 import org.apache.excalibur.instrument.client.InstrumentManagerConnection;
+import org.apache.excalibur.instrument.client.InstrumentManagerConnectionListener;
 import org.apache.excalibur.instrument.client.InstrumentManagerData;
 
 /**
@@ -243,7 +244,17 @@
         {
             HttpURLConnection conn = (HttpURLConnection)url.openConnection();
             
+            boolean oldConnected = m_connected;
             m_connected = true;
+            if ( !oldConnected )
+            {
+                // Notify the listeners.
+                InstrumentManagerConnectionListener[] listenerArray = getListenerArray();
+                for ( int i = 0; i < listenerArray.length; i++ )
+                {
+                    listenerArray[i].opened( this );
+                }
+            }
             
             if ( conn.getResponseCode() == conn.HTTP_OK )
             {
@@ -306,7 +317,19 @@
                 getLogger().debug( "Request failed.  URL: " + url + "  Error: ", e );
             }
             
+            
+            boolean oldConnected = m_connected;
             m_connected = false;
+            if ( oldConnected )
+            {
+                // Notify the listeners.
+                InstrumentManagerConnectionListener[] listenerArray = getListenerArray();
+                for ( int i = 0; i < listenerArray.length; i++ )
+                {
+                    listenerArray[i].closed( this );
+                }
+            }
+            
             return null;
         }
     }

---------------------------------------------------------------------
To unsubscribe, e-mail: scm-unsubscribe@excalibur.apache.org
For additional commands, e-mail: scm-help@excalibur.apache.org