use of org.apache.camel.spi.ManagementAgent in project camel by apache.
the class MetricsMessageHistoryService method doStart.
@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));
}
}
use of org.apache.camel.spi.ManagementAgent in project camel by apache.
the class JMXAgentPropertiesTest method testEnableUseHostIPAddress.
public void testEnableUseHostIPAddress() throws Exception {
CamelContext ctx = createCamelContext();
ManagementAgent agent = ctx.getManagementStrategy().getManagementAgent();
agent.start();
assertTrue(agent.getUseHostIPAddress());
}
use of org.apache.camel.spi.ManagementAgent in project camel by apache.
the class AbstractLocalCamelController method getRouteStatsAsXml.
@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;
}
use of org.apache.camel.spi.ManagementAgent in project camel by apache.
the class AbstractLocalCamelController method getCamelContextStatsAsXml.
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;
}
use of org.apache.camel.spi.ManagementAgent in project camel by apache.
the class ManagedResourceTest method testManagedResource.
@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);
}
Aggregations