You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicemix.apache.org by ge...@apache.org on 2008/10/24 15:22:02 UTC

svn commit: r707628 - in /servicemix/smx3/trunk: ./ core/servicemix-core/ core/servicemix-core/src/main/java/org/apache/servicemix/jbi/monitoring/ core/servicemix-core/src/test/java/org/apache/servicemix/jbi/monitoring/

Author: gertv
Date: Fri Oct 24 06:22:02 2008
New Revision: 707628

URL: http://svn.apache.org/viewvc?rev=707628&view=rev
Log:
SM-1456: Statistics Service throws NPE on startup.

Added:
    servicemix/smx3/trunk/core/servicemix-core/src/test/java/org/apache/servicemix/jbi/monitoring/
    servicemix/smx3/trunk/core/servicemix-core/src/test/java/org/apache/servicemix/jbi/monitoring/StatisticsServiceTest.java
Modified:
    servicemix/smx3/trunk/core/servicemix-core/pom.xml
    servicemix/smx3/trunk/core/servicemix-core/src/main/java/org/apache/servicemix/jbi/monitoring/StatisticsService.java
    servicemix/smx3/trunk/pom.xml

Modified: servicemix/smx3/trunk/core/servicemix-core/pom.xml
URL: http://svn.apache.org/viewvc/servicemix/smx3/trunk/core/servicemix-core/pom.xml?rev=707628&r1=707627&r2=707628&view=diff
==============================================================================
--- servicemix/smx3/trunk/core/servicemix-core/pom.xml (original)
+++ servicemix/smx3/trunk/core/servicemix-core/pom.xml Fri Oct 24 06:22:02 2008
@@ -236,7 +236,6 @@
     <dependency>
       <groupId>org.easymock</groupId>
       <artifactId>easymock</artifactId>
-      <version>2.0</version>
       <scope>test</scope>
     </dependency>    
   </dependencies>

Modified: servicemix/smx3/trunk/core/servicemix-core/src/main/java/org/apache/servicemix/jbi/monitoring/StatisticsService.java
URL: http://svn.apache.org/viewvc/servicemix/smx3/trunk/core/servicemix-core/src/main/java/org/apache/servicemix/jbi/monitoring/StatisticsService.java?rev=707628&r1=707627&r2=707628&view=diff
==============================================================================
--- servicemix/smx3/trunk/core/servicemix-core/src/main/java/org/apache/servicemix/jbi/monitoring/StatisticsService.java (original)
+++ servicemix/smx3/trunk/core/servicemix-core/src/main/java/org/apache/servicemix/jbi/monitoring/StatisticsService.java Fri Oct 24 06:22:02 2008
@@ -43,6 +43,7 @@
 import org.apache.servicemix.jbi.event.ExchangeEvent;
 import org.apache.servicemix.jbi.event.ExchangeListener;
 import org.apache.servicemix.jbi.framework.ComponentMBeanImpl;
+import org.apache.servicemix.jbi.framework.Endpoint;
 import org.apache.servicemix.jbi.management.AttributeInfoHelper;
 import org.apache.servicemix.jbi.management.BaseSystemService;
 import org.apache.servicemix.jbi.management.ManagementContext;
@@ -156,28 +157,8 @@
     }
 
     public void init(JBIContainer container) throws JBIException {
-        endpointListener = new EndpointAdapter() {
-            public void internalEndpointRegistered(EndpointEvent event) {
-                onEndpointRegistered(event);
-            }
-            public void internalEndpointUnregistered(EndpointEvent event) {
-                onEndpointUnregistered(event);
-            }
-            public void externalEndpointRegistered(EndpointEvent event) {
-                onEndpointRegistered(event);
-            }
-            public void externalEndpointUnregistered(EndpointEvent event) {
-                onEndpointUnregistered(event);
-            }
-        };
-        componentListener = new ComponentAdapter() {
-            public void componentInitialized(ComponentEvent event) {
-                onComponentInitialized(event);
-            }
-            public void componentShutDown(ComponentEvent event) {
-                onComponentShutDown(event);
-            }
-        };
+        initComponentListener(container);
+        initEndpointListener(container);
         exchangeListener = new ExchangeListener() {
             public void exchangeSent(ExchangeEvent event) {
                 onExchangeSent(event);
@@ -186,82 +167,47 @@
                 onExchangeAccepted(event);
             }
         };
-        container.addListener(componentListener);
-        container.addListener(endpointListener);
         super.init(container);
     }
-    
-    protected void onComponentInitialized(ComponentEvent event) {
-        ComponentMBeanImpl component = event.getComponent();
-        String key = component.getName();
-        ComponentStats stats = new ComponentStats(component);
-        componentStats.putIfAbsent(key, stats);
-        // Register MBean
-        ManagementContext context = container.getManagementContext();
-        try {
-            context.registerMBean(context.createObjectName(context.createObjectNameProps(stats, true)), 
-                    stats, 
-                    ComponentStatsMBean.class);
-        } catch (Exception e) {
-            LOG.info("Unable to register component statistics MBean: " + e.getMessage());
-            if (LOG.isDebugEnabled()) {
-                LOG.debug("Unable to register component statistics MBean", e);
+
+    private void initComponentListener(final JBIContainer container) {
+        componentListener = new ComponentAdapter() {
+            public void componentInitialized(ComponentEvent event) {
+                createComponentStats(container, event.getComponent());
             }
-        }
-    }
-    
-    protected void onComponentShutDown(ComponentEvent event) {
-        ComponentMBeanImpl component = event.getComponent();
-        String key = component.getName();
-        ComponentStats stats = componentStats.remove(key);
-        if (stats == null) {
-            return;
-        }
-        // Register MBean
-        ManagementContext context = container.getManagementContext();
-        try {
-            context.unregisterMBean(context.createObjectName(context.createObjectNameProps(stats, true)));
-        } catch (Exception e) {
-            LOG.info("Unable to unregister component statistics MBean: " + e);
-            if (LOG.isDebugEnabled()) {
-                LOG.debug("Unable to unregister component statistics MBean", e);
+            public void componentShutDown(ComponentEvent event) {
+                removeComponentStats(container, event.getComponent());
             }
+        };
+        container.addListener(componentListener);
+        // add components that were initialized/started before we added the listener
+        for (ComponentMBeanImpl component : container.getRegistry().getComponentRegistry().getComponents()) {
+            createComponentStats(container, component);
         }
     }
-    
-    protected void onEndpointRegistered(EndpointEvent event) {
-        AbstractServiceEndpoint endpoint = (AbstractServiceEndpoint) event.getEndpoint();
-        String key = EndpointSupport.getUniqueKey(endpoint);
-        ComponentStats compStats = componentStats.get(endpoint.getComponentNameSpace().getName()); 
-        EndpointStats stats = new EndpointStats(endpoint, compStats.getMessagingStats());
-        endpointStats.putIfAbsent(key, stats);
-        // Register MBean
-        ManagementContext context = container.getManagementContext();
-        try {
-            context.registerMBean(context.createObjectName(context.createObjectNameProps(stats, true)), 
-                                  stats, 
-                                  EndpointStatsMBean.class);
-        } catch (Exception e) {
-            LOG.info("Unable to register endpoint statistics MBean: " + e.getMessage());
-            if (LOG.isDebugEnabled()) {
-                LOG.debug("Unable to register endpoint statistics MBean", e);
+
+    private void initEndpointListener(final JBIContainer container) {
+        endpointListener = new EndpointAdapter() {
+            public void internalEndpointRegistered(EndpointEvent event) {
+                createEndpointStats(container, (AbstractServiceEndpoint) event.getEndpoint());
             }
-        }
-    }
-    
-    protected void onEndpointUnregistered(EndpointEvent event) {
-        AbstractServiceEndpoint endpoint = (AbstractServiceEndpoint) event.getEndpoint();
-        String key = EndpointSupport.getUniqueKey(endpoint);
-        EndpointStats stats = endpointStats.remove(key);
-        // Register MBean
-        ManagementContext context = container.getManagementContext();
-        try {
-            context.unregisterMBean(context.createObjectName(context.createObjectNameProps(stats, true)));
-        } catch (Exception e) {
-            LOG.info("Unable to unregister endpoint statistics MBean: " + e.getMessage());
-            if (LOG.isDebugEnabled()) {
-                LOG.debug("Unable to unregister endpoint statistics MBean", e);
+            public void internalEndpointUnregistered(EndpointEvent event) {
+                removeEndpointStats(container, (AbstractServiceEndpoint) event.getEndpoint());
+            }
+            public void externalEndpointRegistered(EndpointEvent event) {
+                createEndpointStats(container, (AbstractServiceEndpoint) event.getEndpoint());
             }
+            public void externalEndpointUnregistered(EndpointEvent event) {
+                removeEndpointStats(container, (AbstractServiceEndpoint) event.getEndpoint());
+            }
+        };
+        container.addListener(endpointListener);
+        // add endpoints that were registered before we added the listener
+        for (Endpoint mbean : container.getDefaultBroker().getRegistry().getEndpointRegistry().getEndpointMBeans()) {
+            AbstractServiceEndpoint endpoint = 
+                (AbstractServiceEndpoint) container.getEndpoint(container.getComponent(mbean.getComponentName()).getContext(), 
+                                                                mbean.getServiceName(), mbean.getEndpointName());
+            createEndpointStats(container, endpoint);
         }
     }
 
@@ -354,5 +300,104 @@
         helper.addOperation(getObjectToManage(), "resetAllStats", "reset all statistics");
         return OperationInfoHelper.join(super.getOperationInfos(), helper.getOperationInfos());
     }
+    
+    /*
+     * Creates a {@link ComponentStats} instance for a component and adds it to the Map
+     */
+    private void createComponentStats(JBIContainer container, ComponentMBeanImpl component) {
+        String key = component.getName();
+        ComponentStats stats = new ComponentStats(component);
+        componentStats.putIfAbsent(key, stats);
+        // Register MBean
+        ManagementContext context = container.getManagementContext();
+        try {
+            context.registerMBean(context.createObjectName(context.createObjectNameProps(stats, true)), 
+                    stats, 
+                    ComponentStatsMBean.class);
+        } catch (Exception e) {
+            LOG.info("Unable to register component statistics MBean: " + e.getMessage());
+            if (LOG.isDebugEnabled()) {
+                LOG.debug("Unable to register component statistics MBean", e);
+            }
+        }
+    }
 
+    /*
+     * Removes the {@link ComponentStats} for this component from the Map
+     */
+    private void removeComponentStats(JBIContainer container, ComponentMBeanImpl component) {
+        String key = component.getName();
+        ComponentStats stats = componentStats.remove(key);
+        if (stats == null) {
+            return;
+        }
+        // Register MBean
+        ManagementContext context = container.getManagementContext();
+        try {
+            context.unregisterMBean(context.createObjectName(context.createObjectNameProps(stats, true)));
+        } catch (Exception e) {
+            LOG.info("Unable to unregister component statistics MBean: " + e);
+            if (LOG.isDebugEnabled()) {
+                LOG.debug("Unable to unregister component statistics MBean", e);
+            }
+        }
+    }
+    
+    /*
+     * Create an {@link EndpointStats} instance for the endpoint and adds it to the Map
+     */
+    private void createEndpointStats(JBIContainer container, AbstractServiceEndpoint endpoint) {
+        String key = EndpointSupport.getUniqueKey(endpoint);
+        ComponentStats compStats = componentStats.get(endpoint.getComponentNameSpace().getName()); 
+        EndpointStats stats = new EndpointStats(endpoint, compStats.getMessagingStats());
+        endpointStats.putIfAbsent(key, stats);
+        // Register MBean
+        ManagementContext context = container.getManagementContext();
+        try {
+            context.registerMBean(context.createObjectName(context.createObjectNameProps(stats, true)), 
+                                  stats, 
+                                  EndpointStatsMBean.class);
+        } catch (Exception e) {
+            LOG.info("Unable to register endpoint statistics MBean: " + e.getMessage());
+            if (LOG.isDebugEnabled()) {
+                LOG.debug("Unable to register endpoint statistics MBean", e);
+            }
+        }
+    }
+
+    /*
+     * Removes the {@link EndpointStats} instance for the endpoint from the Map
+     */
+    private void removeEndpointStats(JBIContainer containner, AbstractServiceEndpoint endpoint) {
+        String key = EndpointSupport.getUniqueKey(endpoint);
+        EndpointStats stats = endpointStats.remove(key);
+        // Register MBean
+        ManagementContext context = container.getManagementContext();
+        try {
+            context.unregisterMBean(context.createObjectName(context.createObjectNameProps(stats, true)));
+        } catch (Exception e) {
+            LOG.info("Unable to unregister endpoint statistics MBean: " + e.getMessage());
+            if (LOG.isDebugEnabled()) {
+                LOG.debug("Unable to unregister endpoint statistics MBean", e);
+            }
+        }
+    }
+    
+    /**
+     * Access the {@link EndpointStats} for all the endpoints that are currently registered
+     * 
+     * @return the Map of {@link EndpointStats}
+     */
+    protected ConcurrentHashMap<String, EndpointStats> getEndpointStats() {
+        return endpointStats;
+    }
+    
+    /**
+     * Access the {@link ComponentStats} for all the endpoints that are currently initialized/started 
+     * 
+     * @return the Map of {@link ComponentStats}
+     */    
+    protected ConcurrentHashMap<String, ComponentStats> getComponentStats() {
+        return componentStats;
+    }
 }

Added: servicemix/smx3/trunk/core/servicemix-core/src/test/java/org/apache/servicemix/jbi/monitoring/StatisticsServiceTest.java
URL: http://svn.apache.org/viewvc/servicemix/smx3/trunk/core/servicemix-core/src/test/java/org/apache/servicemix/jbi/monitoring/StatisticsServiceTest.java?rev=707628&view=auto
==============================================================================
--- servicemix/smx3/trunk/core/servicemix-core/src/test/java/org/apache/servicemix/jbi/monitoring/StatisticsServiceTest.java (added)
+++ servicemix/smx3/trunk/core/servicemix-core/src/test/java/org/apache/servicemix/jbi/monitoring/StatisticsServiceTest.java Fri Oct 24 06:22:02 2008
@@ -0,0 +1,85 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.servicemix.jbi.monitoring;
+
+import javax.jbi.JBIException;
+import javax.jbi.servicedesc.ServiceEndpoint;
+import javax.xml.namespace.QName;
+
+import junit.framework.TestCase;
+
+import org.apache.servicemix.jbi.container.ActivationSpec;
+import org.apache.servicemix.jbi.container.JBIContainer;
+import org.apache.servicemix.jbi.framework.ComponentContextImpl;
+import org.apache.servicemix.jbi.messaging.DeliveryChannelImplTest.TestComponent;
+import org.apache.servicemix.jbi.servicedesc.EndpointSupport;
+
+/**
+ * Test case for {@link StatisticsService}
+ */
+public class StatisticsServiceTest extends TestCase {
+    
+    private static final String COMPONENT = "component";
+    private static final String ENDPOINT = "endpoint";
+    private static final QName SERVICE = new QName("service");
+
+    protected JBIContainer container;
+    private StatisticsService service;
+
+    protected void setUp() throws Exception {
+        // set up a test container instance
+        container = new JBIContainer();
+        container.setEmbedded(true);
+        container.init();
+        container.start();
+        // and a test StatisticsService
+        service = new StatisticsService();
+    }
+    
+    public void testAddEndpointStatsByListener() throws JBIException {
+        // initialize and start the StatisticsService
+        service.init(container);
+        service.start();
+
+        // now register a new endpoint
+        ServiceEndpoint endpoint = registerEndpoint();
+
+        // StatisticsService should know about the endpoint/component through listener callbacks
+        assertNotNull(service.getComponentStats().get(COMPONENT));
+        assertNotNull(service.getEndpointStats().get(EndpointSupport.getUniqueKey(endpoint)));
+    }
+    
+    public void testAddEndpointStatsAtStartup() throws JBIException {
+        // first register the endpoint
+        ServiceEndpoint endpoint = registerEndpoint();
+        
+        // initialize and start the StatisticsService
+        service.init(container);
+        service.start();
+
+        // StatisticsService should have learn about existing endpoints/components at startup
+        assertNotNull(service.getComponentStats().get(COMPONENT));
+        assertNotNull(service.getEndpointStats().get(EndpointSupport.getUniqueKey(endpoint)));
+    }
+
+    private ServiceEndpoint registerEndpoint() throws JBIException {
+        TestComponent component = new TestComponent(SERVICE, ENDPOINT);
+        container.activateComponent(new ActivationSpec(COMPONENT, component));
+        return container.getEndpoint((ComponentContextImpl)component.getContext(), SERVICE, ENDPOINT);
+    }
+
+}

Modified: servicemix/smx3/trunk/pom.xml
URL: http://svn.apache.org/viewvc/servicemix/smx3/trunk/pom.xml?rev=707628&r1=707627&r2=707628&view=diff
==============================================================================
--- servicemix/smx3/trunk/pom.xml (original)
+++ servicemix/smx3/trunk/pom.xml Fri Oct 24 06:22:02 2008
@@ -1053,11 +1053,6 @@
                 <version>2.1</version>
             </dependency>
             <dependency>
-                <groupId>easymock</groupId>
-                <artifactId>easymock</artifactId>
-                <version>1.2_Java1.3</version>
-            </dependency>
-            <dependency>
                 <groupId>emberio</groupId>
                 <artifactId>emberio</artifactId>
                 <version>0.3-alpha</version>
@@ -1803,6 +1798,11 @@
                     </exclusion>
                 </exclusions>
             </dependency>
+            <dependency>
+                <groupId>org.easymock</groupId>
+                <artifactId>easymock</artifactId>
+                <version>2.0</version>
+            </dependency>
         </dependencies>
     </dependencyManagement>