use of javax.management.MBeanServer in project camel by apache.
the class ManagedRoute method reset.
public void reset(boolean includeProcessors) throws Exception {
reset();
// and now reset all processors for this route
if (includeProcessors) {
MBeanServer server = getContext().getManagementStrategy().getManagementAgent().getMBeanServer();
if (server != null) {
// get all the processor mbeans and sort them accordingly to their index
String prefix = getContext().getManagementStrategy().getManagementAgent().getIncludeHostName() ? "*/" : "";
ObjectName query = ObjectName.getInstance(jmxDomain + ":context=" + prefix + getContext().getManagementName() + ",type=processors,*");
QueryExp queryExp = Query.match(new AttributeValueExp("RouteId"), new StringValueExp(getRouteId()));
Set<ObjectName> names = server.queryNames(query, queryExp);
for (ObjectName name : names) {
server.invoke(name, "reset", null, null);
}
}
}
}
use of javax.management.MBeanServer in project camel by apache.
the class ManagedRoute method dumpRouteStatsAsXml.
public String dumpRouteStatsAsXml(boolean fullStats, boolean includeProcessors) throws Exception {
// in this logic we need to calculate the accumulated processing time for the processor in the route
// and hence why the logic is a bit more complicated to do this, as we need to calculate that from
// the bottom -> top of the route but this information is valuable for profiling routes
StringBuilder sb = new StringBuilder();
// need to calculate this value first, as we need that value for the route stat
Long processorAccumulatedTime = 0L;
// gather all the processors for this route, which requires JMX
if (includeProcessors) {
sb.append(" <processorStats>\n");
MBeanServer server = getContext().getManagementStrategy().getManagementAgent().getMBeanServer();
if (server != null) {
// get all the processor mbeans and sort them accordingly to their index
String prefix = getContext().getManagementStrategy().getManagementAgent().getIncludeHostName() ? "*/" : "";
ObjectName query = ObjectName.getInstance(jmxDomain + ":context=" + prefix + getContext().getManagementName() + ",type=processors,*");
Set<ObjectName> names = server.queryNames(query, null);
List<ManagedProcessorMBean> mps = new ArrayList<ManagedProcessorMBean>();
for (ObjectName on : names) {
ManagedProcessorMBean processor = context.getManagementStrategy().getManagementAgent().newProxyClient(on, ManagedProcessorMBean.class);
// the processor must belong to this route
if (getRouteId().equals(processor.getRouteId())) {
mps.add(processor);
}
}
mps.sort(new OrderProcessorMBeans());
// walk the processors in reverse order, and calculate the accumulated total time
Map<String, Long> accumulatedTimes = new HashMap<String, Long>();
Collections.reverse(mps);
for (ManagedProcessorMBean processor : mps) {
processorAccumulatedTime += processor.getTotalProcessingTime();
accumulatedTimes.put(processor.getProcessorId(), processorAccumulatedTime);
}
// and reverse back again
Collections.reverse(mps);
// and now add the sorted list of processors to the xml output
for (ManagedProcessorMBean processor : mps) {
sb.append(" <processorStat").append(String.format(" id=\"%s\" index=\"%s\" state=\"%s\"", processor.getProcessorId(), processor.getIndex(), processor.getState()));
// do we have an accumulated time then append that
Long accTime = accumulatedTimes.get(processor.getProcessorId());
if (accTime != null) {
sb.append(" accumulatedProcessingTime=\"").append(accTime).append("\"");
}
// use substring as we only want the attributes
sb.append(" ").append(processor.dumpStatsAsXml(fullStats).substring(7)).append("\n");
}
}
sb.append(" </processorStats>\n");
}
// route self time is route total - processor accumulated total)
long routeSelfTime = getTotalProcessingTime() - processorAccumulatedTime;
if (routeSelfTime < 0) {
// ensure we don't calculate that as negative
routeSelfTime = 0;
}
StringBuilder answer = new StringBuilder();
answer.append("<routeStat").append(String.format(" id=\"%s\"", route.getId())).append(String.format(" state=\"%s\"", getState()));
// use substring as we only want the attributes
String stat = dumpStatsAsXml(fullStats);
answer.append(" exchangesInflight=\"").append(getInflightExchanges()).append("\"");
answer.append(" selfProcessingTime=\"").append(routeSelfTime).append("\"");
InFlightKey oldestInflightEntry = getOldestInflightEntry();
if (oldestInflightEntry == null) {
answer.append(" oldestInflightExchangeId=\"\"");
answer.append(" oldestInflightDuration=\"\"");
} else {
answer.append(" oldestInflightExchangeId=\"").append(oldestInflightEntry.exchangeId).append("\"");
answer.append(" oldestInflightDuration=\"").append(System.currentTimeMillis() - oldestInflightEntry.timeStamp).append("\"");
}
answer.append(" ").append(stat.substring(7, stat.length() - 2)).append(">\n");
if (includeProcessors) {
answer.append(sb);
}
answer.append("</routeStat>");
return answer.toString();
}
use of javax.management.MBeanServer in project camel by apache.
the class ManagedStreamCachingStrategyTest method testStreamCachingStrategy.
public void testStreamCachingStrategy() throws Exception {
// JMX tests dont work well on AIX CI servers (hangs them)
if (isPlatform("aix")) {
return;
}
MBeanServer mbeanServer = getMBeanServer();
ObjectName on = ObjectName.getInstance("org.apache.camel:context=myCamel,type=services,*");
// number of services
Set<ObjectName> names = mbeanServer.queryNames(on, null);
ObjectName name = null;
for (ObjectName service : names) {
if (service.toString().contains("DefaultStreamCachingStrategy")) {
name = service;
break;
}
}
assertNotNull("Cannot find DefaultStreamCachingStrategy", name);
Boolean enabled = (Boolean) mbeanServer.getAttribute(name, "Enabled");
assertEquals(Boolean.TRUE, enabled);
String dir = (String) mbeanServer.getAttribute(name, "SpoolDirectory");
assertEquals(normalizePath("target/cachedir/myCamel"), normalizePath(dir));
Long threshold = (Long) mbeanServer.getAttribute(name, "SpoolThreshold");
assertEquals(StreamCache.DEFAULT_SPOOL_THRESHOLD, threshold.longValue());
Integer size = (Integer) mbeanServer.getAttribute(name, "BufferSize");
assertEquals(IOHelper.DEFAULT_BUFFER_SIZE, size.intValue());
Long counter = (Long) mbeanServer.getAttribute(name, "CacheMemoryCounter");
assertEquals(0, counter.longValue());
counter = (Long) mbeanServer.getAttribute(name, "CacheSpoolCounter");
assertEquals(0, counter.longValue());
Long cacheSize = (Long) mbeanServer.getAttribute(name, "CacheMemorySize");
assertEquals(0, cacheSize.longValue());
cacheSize = (Long) mbeanServer.getAttribute(name, "CacheSpoolSize");
assertEquals(0, cacheSize.longValue());
String chiper = (String) mbeanServer.getAttribute(name, "SpoolChiper");
assertNull(chiper);
Boolean remove = (Boolean) mbeanServer.getAttribute(name, "RemoveSpoolDirectoryWhenStopping");
assertEquals(Boolean.TRUE, remove);
}
use of javax.management.MBeanServer in project camel by apache.
the class ManagedSuspendedServiceTest method testConsumeSuspendAndResumeFile.
public void testConsumeSuspendAndResumeFile() throws Exception {
// JMX tests dont work well on AIX CI servers (hangs them)
if (isPlatform("aix")) {
return;
}
MBeanServer mbeanServer = getMBeanServer();
Set<ObjectName> set = mbeanServer.queryNames(new ObjectName("*:type=consumers,*"), null);
assertEquals(1, set.size());
ObjectName on = set.iterator().next();
boolean registered = mbeanServer.isRegistered(on);
assertEquals("Should be registered", true, registered);
Boolean ss = (Boolean) mbeanServer.getAttribute(on, "SupportSuspension");
assertEquals(true, ss.booleanValue());
Boolean suspended = (Boolean) mbeanServer.getAttribute(on, "Suspended");
assertEquals(false, suspended.booleanValue());
MockEndpoint mock = getMockEndpoint("mock:result");
mock.expectedMessageCount(1);
template.sendBodyAndHeader("file://target/suspended", "Bye World", Exchange.FILE_NAME, "bye.txt");
template.sendBodyAndHeader("file://target/suspended", "Hello World", Exchange.FILE_NAME, "hello.txt");
assertMockEndpointsSatisfied();
Thread.sleep(1000);
// now its suspended by the policy
suspended = (Boolean) mbeanServer.getAttribute(on, "Suspended");
assertEquals(true, suspended.booleanValue());
// the route is suspended by the policy so we should only receive one
String[] files = new File("target/suspended/").list();
assertNotNull(files);
assertEquals("The file should exists", 1, files.length);
// reset mock
mock.reset();
mock.expectedMessageCount(1);
// now resume it
mbeanServer.invoke(on, "resume", null, null);
assertMockEndpointsSatisfied();
suspended = (Boolean) mbeanServer.getAttribute(on, "Suspended");
assertEquals(false, suspended.booleanValue());
Thread.sleep(500);
// and the file is now deleted
files = new File("target/suspended/").list();
assertNotNull(files);
assertEquals("The file should exists", 0, files.length);
}
use of javax.management.MBeanServer in project camel by apache.
the class ManagedThrottlerTest method testThrottleAsyncExceptionVisableViaJmx.
public void testThrottleAsyncExceptionVisableViaJmx() throws Exception {
// JMX tests dont work well on AIX CI servers (hangs them)
if (isPlatform("aix")) {
return;
}
if (isPlatform("windows")) {
// windows needs more sleep to read updated jmx values so we skip as we dont want further delays in core tests
return;
}
// get the stats for the route
MBeanServer mbeanServer = getMBeanServer();
// get the object name for the delayer
ObjectName throttlerName = ObjectName.getInstance("org.apache.camel:context=camel-1,type=processors,name=\"mythrottler4\"");
// use route to get the total time
ObjectName routeName = ObjectName.getInstance("org.apache.camel:context=camel-1,type=routes,name=\"route4\"");
// reset the counters
mbeanServer.invoke(routeName, "reset", null, null);
getMockEndpoint("mock:endAsyncException").expectedMessageCount(10);
NotifyBuilder notifier = new NotifyBuilder(context).from("seda:throttleCountAsyncException").whenReceived(5).create();
for (int i = 0; i < 10; i++) {
template.sendBody("seda:throttleCountAsyncException", "Message " + i);
}
assertTrue(notifier.matches(2, TimeUnit.SECONDS));
assertMockEndpointsSatisfied();
// give a sec for exception handling to finish..
Thread.sleep(500);
// since all exchanges ended w/ exception, they are not completed
Long completed = (Long) mbeanServer.getAttribute(routeName, "ExchangesCompleted");
assertEquals(0, completed.longValue());
}
Aggregations