public String getCamelContextStatsAsXml(String camelContextName, boolean fullStats, boolean includeProcessors) throws Exception { CamelContext context = this.getLocalCamelContext(camelContextName); if (context == null) { return null; } ManagementAgent agent = context.getManagementStrategy().getManagementAgent(); if (agent != null) { MBeanServer mBeanServer = agent.getMBeanServer(); ObjectName query = ObjectName.getInstance(agent.getMBeanObjectDomainName() + ":type=context,*"); Set<ObjectName> set = mBeanServer.queryNames(query, null); for (ObjectName contextMBean : set) { String camelId = (String) mBeanServer.getAttribute(contextMBean, "CamelId"); if (camelId != null && camelId.equals(context.getName())) { String xml = (String) mBeanServer.invoke(contextMBean, "dumpRoutesStatsAsXml", new Object[]{fullStats, includeProcessors}, new String[]{"boolean", "boolean"}); return xml; } } } return null; }
public void resetRouteStats(String camelContextName) throws Exception { CamelContext context = this.getLocalCamelContext(camelContextName); if (context == null) { return; } ManagementAgent agent = context.getManagementStrategy().getManagementAgent(); if (agent != null) { MBeanServer mBeanServer = agent.getMBeanServer(); // reset route mbeans ObjectName query = ObjectName.getInstance(agent.getMBeanObjectDomainName() + ":type=routes,*"); Set<ObjectName> set = mBeanServer.queryNames(query, null); for (ObjectName routeMBean : set) { String camelId = (String) mBeanServer.getAttribute(routeMBean, "CamelId"); if (camelId != null && camelId.equals(context.getName())) { mBeanServer.invoke(routeMBean, "reset", new Object[]{true}, new String[]{"boolean"}); } } } }
@Override public String getRouteStatsAsXml(String routeId, String camelContextName, boolean fullStats, boolean includeProcessors) throws Exception { CamelContext context = this.getLocalCamelContext(camelContextName); if (context == null) { return null; } ManagementAgent agent = context.getManagementStrategy().getManagementAgent(); if (agent != null) { MBeanServer mBeanServer = agent.getMBeanServer(); Set<ObjectName> set = mBeanServer.queryNames(new ObjectName(agent.getMBeanObjectDomainName() + ":type=routes,name=\"" + routeId + "\",*"), null); Iterator<ObjectName> iterator = set.iterator(); if (iterator.hasNext()) { ObjectName routeMBean = iterator.next(); // the route must be part of the camel context String camelId = (String) mBeanServer.getAttribute(routeMBean, "CamelId"); if (camelId != null && camelId.equals(camelContextName)) { String xml = (String) mBeanServer.invoke(routeMBean, "dumpRouteStatsAsXml", new Object[]{fullStats, includeProcessors}, new String[]{"boolean", "boolean"}); return xml; } } } return null; }
@Test public void testMonitor() throws Exception { final ManagementAgent managementAgent = context.getManagementStrategy().getManagementAgent(); assertNotNull(managementAgent); final MBeanServer mBeanServer = managementAgent.getMBeanServer(); assertNotNull(mBeanServer); final String mBeanServerDefaultDomain = managementAgent.getMBeanServerDefaultDomain(); assertEquals("org.apache.camel", mBeanServerDefaultDomain); final String managementName = context.getManagementName(); log.info("managementName = {}", managementName); getMockEndpoint("mock:result").expectedMessageCount(3); getMockEndpoint("mock:monitor").expectedMessageCount(4); // Send a couple of messages to get some route statistics template.sendBody("direct:start", "Hello Camel"); Thread.sleep(1000); template.sendBody("direct:start", "Camel Rocks!"); Thread.sleep(1000); template.sendBody("direct:start", "Camel Rocks!"); Thread.sleep(1000); }
@Test public void testMonitorSpring() throws Exception { final ManagementAgent managementAgent = context.getManagementStrategy().getManagementAgent(); assertNotNull(managementAgent); final MBeanServer mBeanServer = managementAgent.getMBeanServer(); assertNotNull(mBeanServer); final String mBeanServerDefaultDomain = managementAgent.getMBeanServerDefaultDomain(); assertEquals("org.apache.camel", mBeanServerDefaultDomain); final String managementName = context.getManagementName(); log.info("managementName = {}", managementName); getMockEndpoint("mock:result").expectedMessageCount(3); getMockEndpoint("mock:monitor").expectedMessageCount(4); // Send a couple of messages to get some route statistics template.sendBody("direct:start", "Hello Camel"); Thread.sleep(1000); template.sendBody("direct:start", "Camel Rocks!"); Thread.sleep(1000); template.sendBody("direct:start", "Camel Rocks!"); Thread.sleep(1000); }
@Test public void testObjectNameModification() throws JMException { MBeanServer mbeanServer = createStrictMock(MBeanServer.class); ObjectInstance instance = createStrictMock(ObjectInstance.class); ManagementAgent agent = new DefaultManagementAgent(); agent.setMBeanServer(mbeanServer); Object object = "object"; ObjectName sourceObjectName = new ObjectName("domain", "key", "value"); ObjectName registeredObjectName = new ObjectName("domain", "key", "otherValue"); // Register MBean and return different ObjectName expect(mbeanServer.isRegistered(sourceObjectName)).andReturn(false); expect(mbeanServer.registerMBean(object, sourceObjectName)).andReturn(instance); expect(instance.getObjectName()).andReturn(registeredObjectName); expect(mbeanServer.isRegistered(registeredObjectName)).andReturn(true); replay(mbeanServer, instance); agent.register(object, sourceObjectName); assertTrue(agent.isRegistered(sourceObjectName)); verify(mbeanServer, instance); reset(mbeanServer, instance); // ... and unregister it again expect(mbeanServer.isRegistered(registeredObjectName)).andReturn(true); mbeanServer.unregisterMBean(registeredObjectName); expect(mbeanServer.isRegistered(sourceObjectName)).andReturn(false); replay(mbeanServer); agent.unregister(sourceObjectName); assertFalse(agent.isRegistered(sourceObjectName)); verify(mbeanServer); }
@Test public void testShouldUseHostIPAddressWhenFlagisTrue() throws Exception { System.setProperty(JmxSystemPropertyKeys.USE_HOST_IP_ADDRESS, "true"); System.setProperty(JmxSystemPropertyKeys.CREATE_CONNECTOR, "true"); CamelContext ctx = new DefaultCamelContext(); ManagementAgent agent = new DefaultManagementAgent(ctx); agent.start(); assertTrue(agent.getUseHostIPAddress()); }
@Test public void shouldUseHostNameWhenFlagisFalse() throws Exception { System.setProperty(JmxSystemPropertyKeys.USE_HOST_IP_ADDRESS, "false"); System.setProperty(JmxSystemPropertyKeys.CREATE_CONNECTOR, "true"); CamelContext ctx = new DefaultCamelContext(); ManagementAgent agent = new DefaultManagementAgent(ctx); agent.start(); assertFalse(agent.getUseHostIPAddress()); }
@Override protected void doStart() throws Exception { if (metricsRegistry == null) { Registry camelRegistry = getCamelContext().getRegistry(); metricsRegistry = camelRegistry.lookupByNameAndType(MetricsComponent.METRIC_REGISTRY_NAME, MetricRegistry.class); // create a new metricsRegistry by default if (metricsRegistry == null) { metricsRegistry = new MetricRegistry(); } } if (useJmx) { ManagementAgent agent = getCamelContext().getManagementStrategy().getManagementAgent(); if (agent != null) { MBeanServer server = agent.getMBeanServer(); if (server != null) { reporter = JmxReporter.forRegistry(metricsRegistry).registerWith(server).inDomain(jmxDomain).build(); reporter.start(); } } else { throw new IllegalStateException("CamelContext has not enabled JMX"); } } // json mapper this.mapper = new ObjectMapper().registerModule(new MetricsModule(getRateUnit(), getDurationUnit(), false)); if (getRateUnit() == TimeUnit.SECONDS && getDurationUnit() == TimeUnit.SECONDS) { // they both use same units so reuse this.secondsMapper = this.mapper; } else { this.secondsMapper = new ObjectMapper().registerModule(new MetricsModule(TimeUnit.SECONDS, TimeUnit.SECONDS, false)); } }
public void setup() throws Exception { context = new DefaultCamelContext(); context.setNameStrategy(new ExplicitCamelContextNameStrategy("myCamelContextName")); final ManagementStrategy managementStrategy = context.getManagementStrategy(); final ManagementAgent managementAgent = managementStrategy.getManagementAgent(); managementAgent.setMBeanServerDefaultDomain("org.apache.camel"); final ManagementNameStrategy managementNameStrategy = context.getManagementNameStrategy(); managementNameStrategy.setNamePattern("CustomName-#name#"); // Add a simple test route context.addRoutes(new RouteBuilder() { @Override public void configure() throws Exception { from("direct:start") .routeId("first-route") .log(LoggingLevel.INFO, "First Route", "${body}") .to("mock:result"); from("direct:startOther") .routeId("other-route") .log(LoggingLevel.INFO, "Other Route", "${body}") .to("mock:resultOther"); } }); }
@Test public void testManagedResource() throws Exception { final ManagementAgent managementAgent = context.getManagementStrategy().getManagementAgent(); assertNotNull(managementAgent); final MBeanServer mBeanServer = managementAgent.getMBeanServer(); assertNotNull(mBeanServer); final String mBeanServerDefaultDomain = managementAgent.getMBeanServerDefaultDomain(); assertEquals("org.apache.camel", mBeanServerDefaultDomain); final String managementName = context.getManagementName(); assertNotNull("CamelContext should have a management name if JMX is enabled", managementName); LOG.info("managementName = {}", managementName); // Get the Camel Context MBean ObjectName onContext = ObjectName.getInstance(mBeanServerDefaultDomain + ":context=localhost/" + managementName + ",type=context,name=\"" + context.getName() + "\""); assertTrue("Should be registered", mBeanServer.isRegistered(onContext)); // Get myManagedBean ObjectName onManagedBean = ObjectName.getInstance(mBeanServerDefaultDomain + ":context=localhost/" + managementName + ",type=processors,name=\"myManagedBean\""); LOG.info("Canonical Name = {}", onManagedBean.getCanonicalName()); assertTrue("Should be registered", mBeanServer.isRegistered(onManagedBean)); // Send a couple of messages to get some route statistics template.sendBody("direct:start", "Hello Camel"); template.sendBody("direct:start", "Camel Rocks!"); // Get MBean attribute int camelsSeenCount = (Integer) mBeanServer.getAttribute(onManagedBean, "CamelsSeenCount"); assertEquals(2, camelsSeenCount); // Stop the route via JMX mBeanServer.invoke(onManagedBean, "resetCamelsSeenCount", null, null); camelsSeenCount = (Integer) mBeanServer.getAttribute(onManagedBean, "CamelsSeenCount"); assertEquals(0, camelsSeenCount); }
@SuppressWarnings("unchecked") public List<Map<String, Object>> browseInflightExchanges(String camelContextName, int limit, boolean sortByLongestDuration) throws Exception { CamelContext context = this.getLocalCamelContext(camelContextName); if (context == null) { return null; } List<Map<String, Object>> answer = new ArrayList<Map<String, Object>>(); ManagementAgent agent = context.getManagementStrategy().getManagementAgent(); if (agent != null) { MBeanServer mBeanServer = agent.getMBeanServer(); ObjectName on = new ObjectName(agent.getMBeanObjectDomainName() + ":type=services,name=DefaultInflightRepository,context=" + context.getManagementName()); if (mBeanServer.isRegistered(on)) { TabularData list = (TabularData) mBeanServer.invoke(on, "browse", new Object[]{limit, sortByLongestDuration}, new String[]{"int", "boolean"}); Collection<CompositeData> values = (Collection<CompositeData>) list.values(); for (CompositeData data : values) { Map<String, Object> row = new LinkedHashMap<String, Object>(); Object exchangeId = data.get("exchangeId"); if (exchangeId != null) { row.put("exchangeId", exchangeId); } Object fromRouteId = data.get("fromRouteId"); if (fromRouteId != null) { row.put("fromRouteId", fromRouteId); } Object routeId = data.get("routeId"); if (routeId != null) { row.put("routeId", routeId); } Object nodeId = data.get("nodeId"); if (nodeId != null) { row.put("nodeId", nodeId); } Object elapsed = data.get("elapsed"); if (elapsed != null) { row.put("elapsed", elapsed); } Object duration = data.get("duration"); if (duration != null) { row.put("duration", duration); } answer.add(row); } } } return answer; }
@Deprecated public ManagedManagementStrategy(ManagementAgent managementAgent) { setManagementAgent(managementAgent); }
public ManagedManagementStrategy(CamelContext camelContext, ManagementAgent managementAgent) { setCamelContext(camelContext); setManagementAgent(managementAgent); }
/** * Whether or not to register the mbean. * <p/> * The {@link ManagementAgent} has options which controls when to register. * This allows us to only register mbeans accordingly. For example by default any * dynamic endpoints is not registered. This avoids to register excessive mbeans, which * most often is not desired. * * @param service the object to register * @param route an optional route the mbean is associated with, can be <tt>null</tt> * @return <tt>true</tt> to register, <tt>false</tt> to skip registering */ protected boolean shouldRegister(Object service, Route route) { // the agent hasn't been started if (!initialized) { return false; } LOG.trace("Checking whether to register {} from route: {}", service, route); ManagementAgent agent = getManagementStrategy().getManagementAgent(); if (agent == null) { // do not register if no agent return false; } // always register if we are starting CamelContext if (getCamelContext().getStatus().isStarting()) { return true; } // always register if we are setting up routes if (getCamelContext().isSetupRoutes()) { return true; } // register if always is enabled if (agent.getRegisterAlways()) { return true; } // is it a known route then always accept if (route != null && knowRouteIds.contains(route.getId())) { return true; } // only register if we are starting a new route, and current thread is in starting routes mode if (agent.getRegisterNewRoutes()) { // no specific route, then fallback to see if this thread is starting routes // which is kept as state on the camel context return getCamelContext().isStartingRoutes(); } return false; }
public ManagementAgent getManagementAgent() { return managementAgent; }
public void setManagementAgent(ManagementAgent managementAgent) { this.managementAgent = managementAgent; }
@Test public void testManagedResource() throws Exception { // JMX tests dont work well on AIX CI servers (hangs them) if (isPlatform("aix")) { return; } final ManagementAgent managementAgent = context.getManagementStrategy().getManagementAgent(); TestCase.assertNotNull(managementAgent); final MBeanServer mBeanServer = managementAgent.getMBeanServer(); TestCase.assertNotNull(mBeanServer); final String mBeanServerDefaultDomain = managementAgent.getMBeanServerDefaultDomain(); TestCase.assertEquals("org.apache.camel", mBeanServerDefaultDomain); final String managementName = context.getManagementName(); TestCase.assertNotNull("CamelContext should have a management name if JMX is enabled", managementName); LOG.info("managementName = {}", managementName); // Get the Camel Context MBean ObjectName onContext = ObjectName.getInstance(mBeanServerDefaultDomain + ":context=" + managementName + ",type=context,name=\"" + context.getName() + "\""); TestCase.assertTrue("Should be registered", mBeanServer.isRegistered(onContext)); // Get myManagedBean ObjectName onManagedBean = ObjectName.getInstance(mBeanServerDefaultDomain + ":context=" + managementName + ",type=processors,name=\"myManagedBean\""); LOG.info("Canonical Name = {}", onManagedBean.getCanonicalName()); TestCase.assertTrue("Should be registered", mBeanServer.isRegistered(onManagedBean)); // Send a couple of messages to get some route statistics template.sendBody("direct:start", "Hello Camel"); template.sendBody("direct:start", "Camel Rocks!"); // Get MBean attribute int camelsSeenCount = (Integer) mBeanServer.getAttribute(onManagedBean, "CamelsSeenCount"); TestCase.assertEquals(2, camelsSeenCount); // Stop the route via JMX mBeanServer.invoke(onManagedBean, "resetCamelsSeenCount", null, null); camelsSeenCount = (Integer) mBeanServer.getAttribute(onManagedBean, "CamelsSeenCount"); TestCase.assertEquals(0, camelsSeenCount); String camelId = (String) mBeanServer.getAttribute(onManagedBean, "CamelId"); assertEquals(context.getName(), camelId); String state = (String) mBeanServer.getAttribute(onManagedBean, "State"); assertEquals("Started", state); String fqn = (String) mBeanServer.getAttribute(onManagedBean, "BeanClassName"); assertEquals(MyManagedBean.class.getCanonicalName(), fqn); }
public void testJmxConfiguration() throws Exception { ManagementAgent agent = getMandatoryBean(DefaultManagementAgent.class, "agent"); assertNotNull("SpringInstrumentationAgent must be configured for JMX support", agent); assertNotNull("MBeanServer must be configured for JMX support", agent.getMBeanServer()); assertEquals("org.apache.camel.test", agent.getMBeanServer().getDefaultDomain()); }
public void testEnableUseHostIPAddress() throws Exception { CamelContext ctx = createCamelContext(); ManagementAgent agent = ctx.getManagementStrategy().getManagementAgent(); agent.start(); assertTrue(agent.getUseHostIPAddress()); }
public static void main(String[] args) throws Exception { final CamelContext context = new DefaultCamelContext(); // Configure JMX settings final ManagementStrategy managementStrategy = context.getManagementStrategy(); /* managementStrategy.setStatisticsLevel(ManagementStatisticsLevel.All); managementStrategy.setLoadStatisticsEnabled(true); */ // TODO: double check this is right way to get and configure Management Agent final ManagementAgent managementAgent = managementStrategy.getManagementAgent(); managementAgent.setConnectorPort(1099); managementAgent.setServiceUrlPath("/jmxrmi/camel"); managementAgent.setCreateConnector(false); managementAgent.setUsePlatformMBeanServer(true); // TODO: check that level Extended is same/better as ALL managementAgent.setStatisticsLevel(ManagementStatisticsLevel.Extended); managementAgent.setLoadStatisticsEnabled(true); // Add a simple test route context.addRoutes(new RouteBuilder() { @Override public void configure() throws Exception { from("direct:start") .log("${body}") .to("mock:result"); } }); // Start the context context.start(); // Send a couple of messages to get some route statistics final ProducerTemplate template = context.createProducerTemplate(); template.sendBody("direct:start", "Hello Camel"); template.sendBody("direct:start", "Camel Rocks!"); // let the Camel runtime do its job for a while Thread.sleep(60000); // shutdown context.stop(); }
@Test public void testNamingPattern() throws Exception { final ManagementAgent managementAgent = context.getManagementStrategy().getManagementAgent(); assertNotNull(managementAgent); final MBeanServer mBeanServer = managementAgent.getMBeanServer(); assertNotNull(mBeanServer); final String mBeanServerDefaultDomain = managementAgent.getMBeanServerDefaultDomain(); assertEquals("org.apache.camel", mBeanServerDefaultDomain); final String managementName = context.getManagementName(); assertNotNull("CamelContext should have a management name if JMX is enabled", managementName); LOG.info("managementName = {}; name = {}", managementName, context.getName()); assertTrue(managementName.startsWith("CustomName")); // Get the Camel Context MBean ObjectName onContext = ObjectName.getInstance(mBeanServerDefaultDomain + ":context=localhost/" + managementName + ",type=context,name=\"" + context.getName() + "\""); assertTrue("Should be registered", mBeanServer.isRegistered(onContext)); // Get the first Route MBean by id ObjectName onRoute1 = ObjectName.getInstance(mBeanServerDefaultDomain + ":context=localhost/" + managementName + ",type=routes,name=\"first-route\""); LOG.info("Canonical Name = {}", onRoute1.getCanonicalName()); assertTrue("Should be registered", mBeanServer.isRegistered(onRoute1)); // Send a couple of messages to get some route statistics template.sendBody("direct:start", "Hello Camel"); template.sendBody("direct:start", "Camel Rocks!"); // Get an MBean attribute for the number of messages processed assertEquals(2L, mBeanServer.getAttribute(onRoute1, "ExchangesCompleted")); // Get the other Route MBean by id ObjectName onRoute2 = ObjectName.getInstance(mBeanServerDefaultDomain + ":context=localhost/" + managementName + ",type=routes,name=\"other-route\""); assertTrue("Should be registered", mBeanServer.isRegistered(onRoute2)); // Get an MBean attribute for the number of messages processed assertEquals(0L, mBeanServer.getAttribute(onRoute2, "ExchangesCompleted")); // Send some messages to the other route template.sendBody("direct:startOther", "Hello Other Camel"); template.sendBody("direct:startOther", "Other Camel Rocks!"); // Verify that the MBean statistics updated correctly assertEquals(2L, mBeanServer.getAttribute(onRoute2, "ExchangesCompleted")); // Check this routes running state assertEquals("Started", mBeanServer.getAttribute(onRoute2, "State")); // Stop the route via JMX mBeanServer.invoke(onRoute2, "stop", null, null); // verify the route now shows its state as stopped assertEquals("Stopped", mBeanServer.getAttribute(onRoute2, "State")); }
@Test public void testNamingContext() throws Exception { final ManagementAgent managementAgent = context.getManagementStrategy().getManagementAgent(); assertNotNull(managementAgent); final MBeanServer mBeanServer = managementAgent.getMBeanServer(); assertNotNull(mBeanServer); final String mBeanServerDefaultDomain = managementAgent.getMBeanServerDefaultDomain(); assertEquals("org.apache.camel", mBeanServerDefaultDomain); final String managementName = context.getManagementName(); assertNotNull("CamelContext should have a management name if JMX is enabled", managementName); LOG.info("managementName = {}", managementName); // Get the Camel Context MBean ObjectName onContext = ObjectName.getInstance(mBeanServerDefaultDomain + ":context=localhost/" + managementName + ",type=context,name=\"" + context.getName() + "\""); assertTrue("Should be registered", mBeanServer.isRegistered(onContext)); // Get the first Route MBean by id ObjectName onRoute1 = ObjectName.getInstance(mBeanServerDefaultDomain + ":context=localhost/" + managementName + ",type=routes,name=\"first-route\""); LOG.info("Canonical Name = {}", onRoute1.getCanonicalName()); assertTrue("Should be registered", mBeanServer.isRegistered(onRoute1)); // Send a couple of messages to get some route statistics template.sendBody("direct:start", "Hello Camel"); template.sendBody("direct:start", "Camel Rocks!"); // Get an MBean attribute for the number of messages processed assertEquals(2L, mBeanServer.getAttribute(onRoute1, "ExchangesCompleted")); // Get the other Route MBean by id ObjectName onRoute2 = ObjectName.getInstance(mBeanServerDefaultDomain + ":context=localhost/" + managementName + ",type=routes,name=\"other-route\""); assertTrue("Should be registered", mBeanServer.isRegistered(onRoute2)); // Get an MBean attribute for the number of messages processed assertEquals(0L, mBeanServer.getAttribute(onRoute2, "ExchangesCompleted")); // Send some messages to the other route template.sendBody("direct:startOther", "Hello Other Camel"); template.sendBody("direct:startOther", "Other Camel Rocks!"); // Verify that the MBean statistics updated correctly assertEquals(2L, mBeanServer.getAttribute(onRoute2, "ExchangesCompleted")); // Check this routes running state assertEquals("Started", mBeanServer.getAttribute(onRoute2, "State")); // Stop the route via JMX mBeanServer.invoke(onRoute2, "stop", null, null); // verify the route now shows its state as stopped assertEquals("Stopped", mBeanServer.getAttribute(onRoute2, "State")); }
@Test public void testNamingPatternSpring() throws Exception { final ManagementAgent managementAgent = context.getManagementStrategy().getManagementAgent(); assertNotNull(managementAgent); final MBeanServer mBeanServer = managementAgent.getMBeanServer(); assertNotNull(mBeanServer); final String mBeanServerDefaultDomain = managementAgent.getMBeanServerDefaultDomain(); assertEquals("org.apache.camel", mBeanServerDefaultDomain); final String managementName = context.getManagementName(); assertNotNull("CamelContext should have a management name if JMX is enabled", managementName); LOG.info("managementName = {}; name = {}", managementName, context.getName()); assertTrue(managementName.startsWith("CustomName")); // Get the Camel Context MBean ObjectName onContext = ObjectName.getInstance(mBeanServerDefaultDomain + ":context=localhost/" + managementName + ",type=context,name=\"" + context.getName() + "\""); assertTrue("Should be registered", mBeanServer.isRegistered(onContext)); // Get the first Route MBean by id ObjectName onRoute1 = ObjectName.getInstance(mBeanServerDefaultDomain + ":context=localhost/" + managementName + ",type=routes,name=\"first-route\""); LOG.info("Canonical Name = {}", onRoute1.getCanonicalName()); assertTrue("Should be registered", mBeanServer.isRegistered(onRoute1)); // Send a couple of messages to get some route statistics template.sendBody("direct:start", "Hello Camel"); template.sendBody("direct:start", "Camel Rocks!"); // Get an MBean attribute for the number of messages processed assertEquals(2L, mBeanServer.getAttribute(onRoute1, "ExchangesCompleted")); // Get the other Route MBean by id ObjectName onRoute2 = ObjectName.getInstance(mBeanServerDefaultDomain + ":context=localhost/" + managementName + ",type=routes,name=\"other-route\""); assertTrue("Should be registered", mBeanServer.isRegistered(onRoute2)); // Get an MBean attribute for the number of messages processed assertEquals(0L, mBeanServer.getAttribute(onRoute2, "ExchangesCompleted")); // Send some messages to the other route template.sendBody("direct:startOther", "Hello Other Camel"); template.sendBody("direct:startOther", "Other Camel Rocks!"); // Verify that the MBean statistics updated correctly assertEquals(2L, mBeanServer.getAttribute(onRoute2, "ExchangesCompleted")); // Check this routes running state assertEquals("Started", mBeanServer.getAttribute(onRoute2, "State")); // Stop the route via JMX mBeanServer.invoke(onRoute2, "stop", null, null); // verify the route now shows its state as stopped assertEquals("Stopped", mBeanServer.getAttribute(onRoute2, "State")); }
@Test public void testNamingContextSpring() throws Exception { final ManagementAgent managementAgent = context.getManagementStrategy().getManagementAgent(); assertNotNull(managementAgent); final MBeanServer mBeanServer = managementAgent.getMBeanServer(); assertNotNull(mBeanServer); final String mBeanServerDefaultDomain = managementAgent.getMBeanServerDefaultDomain(); assertEquals("org.apache.camel", mBeanServerDefaultDomain); final String managementName = context.getManagementName(); assertNotNull("CamelContext should have a management name if JMX is enabled", managementName); LOG.info("managementName = {}", managementName); // Get the Camel Context MBean ObjectName onContext = ObjectName.getInstance(mBeanServerDefaultDomain + ":context=localhost/" + managementName + ",type=context,name=\"" + context.getName() + "\""); assertTrue("Should be registered", mBeanServer.isRegistered(onContext)); // Get the first Route MBean by id ObjectName onRoute1 = ObjectName.getInstance(mBeanServerDefaultDomain + ":context=localhost/" + managementName + ",type=routes,name=\"first-route\""); LOG.info("Canonical Name = {}", onRoute1.getCanonicalName()); assertTrue("Should be registered", mBeanServer.isRegistered(onRoute1)); // Send a couple of messages to get some route statistics template.sendBody("direct:start", "Hello Camel"); template.sendBody("direct:start", "Camel Rocks!"); // Get an MBean attribute for the number of messages processed assertEquals(2L, mBeanServer.getAttribute(onRoute1, "ExchangesCompleted")); // Get the other Route MBean by id ObjectName onRoute2 = ObjectName.getInstance(mBeanServerDefaultDomain + ":context=localhost/" + managementName + ",type=routes,name=\"other-route\""); assertTrue("Should be registered", mBeanServer.isRegistered(onRoute2)); // Get an MBean attribute for the number of messages processed assertEquals(0L, mBeanServer.getAttribute(onRoute2, "ExchangesCompleted")); // Send some messages to the other route template.sendBody("direct:startOther", "Hello Other Camel"); template.sendBody("direct:startOther", "Other Camel Rocks!"); // Verify that the MBean statistics updated correctly assertEquals(2L, mBeanServer.getAttribute(onRoute2, "ExchangesCompleted")); // Check this routes running state assertEquals("Started", mBeanServer.getAttribute(onRoute2, "State")); // Stop the route via JMX mBeanServer.invoke(onRoute2, "stop", null, null); // verify the route now shows its state as stopped assertEquals("Stopped", mBeanServer.getAttribute(onRoute2, "State")); }