camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject [2/6] camel git commit: CAMEL-8531: Make it easy to get the management mbean for a given process by its id
Date Tue, 24 Mar 2015 07:39:22 GMT
CAMEL-8531: Make it easy to get the management mbean for a given process by its id


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/4b4611dd
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/4b4611dd
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/4b4611dd

Branch: refs/heads/master
Commit: 4b4611dd493ed2741ef2be9bf9a61334585b5508
Parents: ab32a4b
Author: Claus Ibsen <davsclaus@apache.org>
Authored: Tue Mar 24 07:56:03 2015 +0100
Committer: Claus Ibsen <davsclaus@apache.org>
Committed: Tue Mar 24 08:41:23 2015 +0100

----------------------------------------------------------------------
 .../java/org/apache/camel/CamelContext.java     | 11 +++++++++++
 .../apache/camel/impl/DefaultCamelContext.java  | 20 ++++++++++++++++++++
 .../management/DefaultManagementAgent.java      |  9 +++++++++
 .../management/mbean/ManagedCamelContext.java   |  4 ++--
 .../camel/management/mbean/ManagedRoute.java    |  2 +-
 .../org/apache/camel/spi/ManagementAgent.java   |  9 +++++++++
 .../ManagedAggregateControllerTest.java         |  8 ++++++++
 .../ManagedRouteStopUsingMBeanAPITest.java      |  3 +--
 8 files changed, 61 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/4b4611dd/camel-core/src/main/java/org/apache/camel/CamelContext.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/CamelContext.java b/camel-core/src/main/java/org/apache/camel/CamelContext.java
index 0b7d8b6..adeb7c2 100644
--- a/camel-core/src/main/java/org/apache/camel/CamelContext.java
+++ b/camel-core/src/main/java/org/apache/camel/CamelContext.java
@@ -25,6 +25,7 @@ import java.util.Properties;
 import java.util.concurrent.ScheduledExecutorService;
 import java.util.concurrent.TimeUnit;
 
+import org.apache.camel.api.management.mbean.ManagedProcessorMBean;
 import org.apache.camel.builder.ErrorHandlerBuilder;
 import org.apache.camel.model.DataFormatDefinition;
 import org.apache.camel.model.ProcessorDefinition;
@@ -532,6 +533,16 @@ public interface CamelContext extends SuspendableService, RuntimeConfiguration
{
     <T extends Processor> T getProcessor(String id, Class<T> type);
 
     /**
+     * Gets the managed processor from any of the routes which with the given id
+     *
+     * @param id id of the processor
+     * @param type the managed processor type from the {@link org.apache.camel.api.management.mbean}
package.
+     * @return the processor or <tt>null</tt> if not found
+     * @throws java.lang.ClassCastException is thrown if the type is not correct type
+     */
+    <T extends ManagedProcessorMBean> T getManagedProcessor(String id, Class<T>
type);
+
+    /**
      * Gets the processor definition from any of the routes which with the given id
      *
      * @param id id of the processor definition

http://git-wip-us.apache.org/repos/asf/camel/blob/4b4611dd/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java b/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java
index b0874a5..4053a1e 100644
--- a/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java
+++ b/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java
@@ -38,6 +38,8 @@ import java.util.concurrent.CopyOnWriteArrayList;
 import java.util.concurrent.ScheduledExecutorService;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.atomic.AtomicInteger;
+import javax.management.MalformedObjectNameException;
+import javax.management.ObjectName;
 import javax.naming.Context;
 import javax.xml.bind.JAXBContext;
 import javax.xml.bind.Unmarshaller;
@@ -72,6 +74,7 @@ import org.apache.camel.StatefulService;
 import org.apache.camel.SuspendableService;
 import org.apache.camel.TypeConverter;
 import org.apache.camel.VetoCamelContextStartException;
+import org.apache.camel.api.management.mbean.ManagedProcessorMBean;
 import org.apache.camel.builder.ErrorHandlerBuilder;
 import org.apache.camel.builder.ErrorHandlerBuilderSupport;
 import org.apache.camel.component.properties.PropertiesComponent;
@@ -727,6 +730,23 @@ public class DefaultCamelContext extends ServiceSupport implements ModelCamelCon
         return null;
     }
 
+    public <T extends ManagedProcessorMBean> T getManagedProcessor(String id, Class<T>
type) {
+        Processor processor = getProcessor(id);
+        ProcessorDefinition def = getProcessorDefinition(id);
+
+        if (processor != null && def != null) {
+            // lookup the processor in mbean
+            try {
+                ObjectName on = getManagementStrategy().getManagementNamingStrategy().getObjectNameForProcessor(this,
processor, def);
+                return getManagementStrategy().getManagementAgent().newProxyClient(on, type);
+            } catch (MalformedObjectNameException e) {
+                throw ObjectHelper.wrapRuntimeCamelException(e);
+            }
+        }
+
+        return null;
+    }
+
     public ProcessorDefinition getProcessorDefinition(String id) {
         for (RouteDefinition route : getRouteDefinitions()) {
             Iterator<ProcessorDefinition> it = ProcessorDefinitionHelper.filterTypeInOutputs(route.getOutputs(),
ProcessorDefinition.class);

http://git-wip-us.apache.org/repos/asf/camel/blob/4b4611dd/camel-core/src/main/java/org/apache/camel/management/DefaultManagementAgent.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/management/DefaultManagementAgent.java
b/camel-core/src/main/java/org/apache/camel/management/DefaultManagementAgent.java
index 4c9f768..dcad99f 100644
--- a/camel-core/src/main/java/org/apache/camel/management/DefaultManagementAgent.java
+++ b/camel-core/src/main/java/org/apache/camel/management/DefaultManagementAgent.java
@@ -31,6 +31,7 @@ import java.util.concurrent.ConcurrentMap;
 import javax.management.JMException;
 import javax.management.MBeanServer;
 import javax.management.MBeanServerFactory;
+import javax.management.MBeanServerInvocationHandler;
 import javax.management.NotCompliantMBeanException;
 import javax.management.ObjectInstance;
 import javax.management.ObjectName;
@@ -276,6 +277,14 @@ public class DefaultManagementAgent extends ServiceSupport implements
Management
                 || server.isRegistered(name);
     }
 
+    public <T> T newProxyClient(ObjectName name, Class<T> mbean) {
+        if (server.isRegistered(name)) {
+            return MBeanServerInvocationHandler.newProxyInstance(server, name, mbean, false);
+        } else {
+            return null;
+        }
+    }
+
     protected void doStart() throws Exception {
         ObjectHelper.notNull(camelContext, "CamelContext");
 

http://git-wip-us.apache.org/repos/asf/camel/blob/4b4611dd/camel-core/src/main/java/org/apache/camel/management/mbean/ManagedCamelContext.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/management/mbean/ManagedCamelContext.java
b/camel-core/src/main/java/org/apache/camel/management/mbean/ManagedCamelContext.java
index 2314da6..598c340 100644
--- a/camel-core/src/main/java/org/apache/camel/management/mbean/ManagedCamelContext.java
+++ b/camel-core/src/main/java/org/apache/camel/management/mbean/ManagedCamelContext.java
@@ -391,7 +391,7 @@ public class ManagedCamelContext extends ManagedPerformanceCounter implements
Ti
                 query = ObjectName.getInstance("org.apache.camel:context=" + prefix + getContext().getManagementName()
+ ",type=processors,*");
                 Set<ObjectName> names = server.queryNames(query, null);
                 for (ObjectName on : names) {
-                    ManagedProcessorMBean processor = MBeanServerInvocationHandler.newProxyInstance(server,
on, ManagedProcessorMBean.class, true);
+                    ManagedProcessorMBean processor = context.getManagementStrategy().getManagementAgent().newProxyClient(on,
ManagedProcessorMBean.class);
                     processors.add(processor);
                 }
             }
@@ -400,7 +400,7 @@ public class ManagedCamelContext extends ManagedPerformanceCounter implements
Ti
             // loop the routes, and append the processor stats if needed
             sb.append("  <routeStats>\n");
             for (ObjectName on : routes) {
-                ManagedRouteMBean route = MBeanServerInvocationHandler.newProxyInstance(server,
on, ManagedRouteMBean.class, true);
+                ManagedRouteMBean route = context.getManagementStrategy().getManagementAgent().newProxyClient(on,
ManagedRouteMBean.class);
                 sb.append("    <routeStat").append(String.format(" id=\"%s\" state=\"%s\"",
route.getRouteId(), route.getState()));
                 // use substring as we only want the attributes
                 stat = route.dumpStatsAsXml(fullStats);

http://git-wip-us.apache.org/repos/asf/camel/blob/4b4611dd/camel-core/src/main/java/org/apache/camel/management/mbean/ManagedRoute.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/management/mbean/ManagedRoute.java
b/camel-core/src/main/java/org/apache/camel/management/mbean/ManagedRoute.java
index 3bd1dad..d31e4e5 100644
--- a/camel-core/src/main/java/org/apache/camel/management/mbean/ManagedRoute.java
+++ b/camel-core/src/main/java/org/apache/camel/management/mbean/ManagedRoute.java
@@ -288,7 +288,7 @@ public class ManagedRoute extends ManagedPerformanceCounter implements
TimerList
                 Set<ObjectName> names = server.queryNames(query, null);
                 List<ManagedProcessorMBean> mps = new ArrayList<ManagedProcessorMBean>();
                 for (ObjectName on : names) {
-                    ManagedProcessorMBean processor = MBeanServerInvocationHandler.newProxyInstance(server,
on, ManagedProcessorMBean.class, true);
+                    ManagedProcessorMBean processor = context.getManagementStrategy().getManagementAgent().newProxyClient(on,
ManagedProcessorMBean.class);
 
                     // the processor must belong to this route
                     if (getRouteId().equals(processor.getRouteId())) {

http://git-wip-us.apache.org/repos/asf/camel/blob/4b4611dd/camel-core/src/main/java/org/apache/camel/spi/ManagementAgent.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/spi/ManagementAgent.java b/camel-core/src/main/java/org/apache/camel/spi/ManagementAgent.java
index bec9b8b..df1f183 100644
--- a/camel-core/src/main/java/org/apache/camel/spi/ManagementAgent.java
+++ b/camel-core/src/main/java/org/apache/camel/spi/ManagementAgent.java
@@ -66,6 +66,15 @@ public interface ManagementAgent extends Service {
     boolean isRegistered(ObjectName name);
 
     /**
+     * Creates a new proxy client
+     *
+     * @param name   the mbean name
+     * @param mbean  the client interface, such as from the {@link org.apache.camel.api.management.mbean}
package.
+     * @return the client or <tt>null</tt> if mbean does not exists
+     */
+    <T> T newProxyClient(ObjectName name, Class<T> mbean);
+
+    /**
      * Get the MBeanServer which hosts managed objects.
      * <p/>
      * <b>Notice:</b> If the JMXEnabled configuration is not set to <tt>true</tt>,

http://git-wip-us.apache.org/repos/asf/camel/blob/4b4611dd/camel-core/src/test/java/org/apache/camel/management/ManagedAggregateControllerTest.java
----------------------------------------------------------------------
diff --git a/camel-core/src/test/java/org/apache/camel/management/ManagedAggregateControllerTest.java
b/camel-core/src/test/java/org/apache/camel/management/ManagedAggregateControllerTest.java
index 2d783f6..ba14824 100644
--- a/camel-core/src/test/java/org/apache/camel/management/ManagedAggregateControllerTest.java
+++ b/camel-core/src/test/java/org/apache/camel/management/ManagedAggregateControllerTest.java
@@ -20,6 +20,7 @@ import javax.management.MBeanServer;
 import javax.management.ObjectName;
 
 import org.apache.camel.Exchange;
+import org.apache.camel.api.management.mbean.ManagedAggregateProcessorMBean;
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.processor.aggregate.AggregateController;
 import org.apache.camel.processor.aggregate.AggregationStrategy;
@@ -151,6 +152,13 @@ public class ManagedAggregateControllerTest extends ManagementTestSupport
{
 
         pending = (Integer) mbeanServer.invoke(on, "aggregationRepositoryGroups", null, null);
         assertEquals(1, pending.intValue());
+
+        // we can also use the client mbean
+        ManagedAggregateProcessorMBean client = context.getManagedProcessor("myAggregator",
ManagedAggregateProcessorMBean.class);
+        assertNotNull(client);
+
+        assertEquals(1, client.getCompletedByForce());
+        assertEquals(4, client.getTotalIn());
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/camel/blob/4b4611dd/camel-core/src/test/java/org/apache/camel/management/ManagedRouteStopUsingMBeanAPITest.java
----------------------------------------------------------------------
diff --git a/camel-core/src/test/java/org/apache/camel/management/ManagedRouteStopUsingMBeanAPITest.java
b/camel-core/src/test/java/org/apache/camel/management/ManagedRouteStopUsingMBeanAPITest.java
index b9de9c4..e22a631 100644
--- a/camel-core/src/test/java/org/apache/camel/management/ManagedRouteStopUsingMBeanAPITest.java
+++ b/camel-core/src/test/java/org/apache/camel/management/ManagedRouteStopUsingMBeanAPITest.java
@@ -18,7 +18,6 @@ package org.apache.camel.management;
 
 import java.util.Set;
 import javax.management.MBeanServer;
-import javax.management.MBeanServerInvocationHandler;
 import javax.management.ObjectName;
 
 import org.apache.camel.ServiceStatus;
@@ -48,7 +47,7 @@ public class ManagedRouteStopUsingMBeanAPITest extends ManagementTestSupport
{
 
         ObjectName on = set.iterator().next();
 
-        ManagedRouteMBean mbean = MBeanServerInvocationHandler.newProxyInstance(mbeanServer,
on, ManagedRouteMBean.class, false);
+        ManagedRouteMBean mbean = context.getManagementStrategy().getManagementAgent().newProxyClient(on,
ManagedRouteMBean.class);
 
         // the route has this starting endpoint uri
         assertEquals("direct://start", mbean.getEndpointUri());


Mime
View raw message