use of org.apache.camel.impl.ConsumerCache in project camel by apache.
the class DefaultManagementLifecycleStrategy method getManagedObjectForService.
@SuppressWarnings("unchecked")
private Object getManagedObjectForService(CamelContext context, Service service, Route route) {
// skip channel, UoW and dont double wrap instrumentation
if (service instanceof Channel || service instanceof UnitOfWork || service instanceof InstrumentationProcessor) {
return null;
}
// skip non managed services
if (service instanceof NonManagedService) {
return null;
}
Object answer = null;
if (service instanceof ManagementAware) {
return ((ManagementAware<Service>) service).getManagedObject(service);
} else if (service instanceof Tracer) {
// special for tracer
Tracer tracer = (Tracer) service;
ManagedTracer mt = managedTracers.get(tracer);
if (mt == null) {
mt = new ManagedTracer(context, tracer);
mt.init(getManagementStrategy());
managedTracers.put(tracer, mt);
}
return mt;
} else if (service instanceof BacklogTracer) {
// special for backlog tracer
BacklogTracer backlogTracer = (BacklogTracer) service;
ManagedBacklogTracer mt = managedBacklogTracers.get(backlogTracer);
if (mt == null) {
mt = new ManagedBacklogTracer(context, backlogTracer);
mt.init(getManagementStrategy());
managedBacklogTracers.put(backlogTracer, mt);
}
return mt;
} else if (service instanceof BacklogDebugger) {
// special for backlog debugger
BacklogDebugger backlogDebugger = (BacklogDebugger) service;
ManagedBacklogDebugger md = managedBacklogDebuggers.get(backlogDebugger);
if (md == null) {
md = new ManagedBacklogDebugger(context, backlogDebugger);
md.init(getManagementStrategy());
managedBacklogDebuggers.put(backlogDebugger, md);
}
return md;
} else if (service instanceof DataFormat) {
answer = getManagementObjectStrategy().getManagedObjectForDataFormat(context, (DataFormat) service);
} else if (service instanceof Producer) {
answer = getManagementObjectStrategy().getManagedObjectForProducer(context, (Producer) service);
} else if (service instanceof Consumer) {
answer = getManagementObjectStrategy().getManagedObjectForConsumer(context, (Consumer) service);
} else if (service instanceof Processor) {
// special for processors as we need to do some extra work
return getManagedObjectForProcessor(context, (Processor) service, route);
} else if (service instanceof ThrottlingInflightRoutePolicy) {
answer = new ManagedThrottlingInflightRoutePolicy(context, (ThrottlingInflightRoutePolicy) service);
} else if (service instanceof ThrottlingExceptionRoutePolicy) {
answer = new ManagedThrottlingExceptionRoutePolicy(context, (ThrottlingExceptionRoutePolicy) service);
} else if (service instanceof ConsumerCache) {
answer = new ManagedConsumerCache(context, (ConsumerCache) service);
} else if (service instanceof ProducerCache) {
answer = new ManagedProducerCache(context, (ProducerCache) service);
} else if (service instanceof DefaultEndpointRegistry) {
answer = new ManagedEndpointRegistry(context, (DefaultEndpointRegistry) service);
} else if (service instanceof TypeConverterRegistry) {
answer = new ManagedTypeConverterRegistry(context, (TypeConverterRegistry) service);
} else if (service instanceof RestRegistry) {
answer = new ManagedRestRegistry(context, (RestRegistry) service);
} else if (service instanceof InflightRepository) {
answer = new ManagedInflightRepository(context, (InflightRepository) service);
} else if (service instanceof AsyncProcessorAwaitManager) {
answer = new ManagedAsyncProcessorAwaitManager(context, (AsyncProcessorAwaitManager) service);
} else if (service instanceof RuntimeEndpointRegistry) {
answer = new ManagedRuntimeEndpointRegistry(context, (RuntimeEndpointRegistry) service);
} else if (service instanceof StreamCachingStrategy) {
answer = new ManagedStreamCachingStrategy(context, (StreamCachingStrategy) service);
} else if (service instanceof EventNotifier) {
answer = getManagementObjectStrategy().getManagedObjectForEventNotifier(context, (EventNotifier) service);
} else if (service instanceof TransformerRegistry) {
answer = new ManagedTransformerRegistry(context, (TransformerRegistry) service);
} else if (service instanceof ValidatorRegistry) {
answer = new ManagedValidatorRegistry(context, (ValidatorRegistry) service);
} else if (service instanceof RuntimeCamelCatalog) {
answer = new ManagedRuntimeCamelCatalog(context, (RuntimeCamelCatalog) service);
} else if (service != null) {
// fallback as generic service
answer = getManagementObjectStrategy().getManagedObjectForService(context, service);
}
if (answer != null && answer instanceof ManagedService) {
ManagedService ms = (ManagedService) answer;
ms.setRoute(route);
ms.init(getManagementStrategy());
}
return answer;
}
use of org.apache.camel.impl.ConsumerCache in project camel by apache.
the class PollEnricher method doStart.
protected void doStart() throws Exception {
if (consumerCache == null) {
// create consumer cache if we use dynamic expressions for computing the endpoints to poll
if (cacheSize < 0) {
consumerCache = new EmptyConsumerCache(this, camelContext);
LOG.debug("PollEnrich {} is not using ConsumerCache", this);
} else if (cacheSize == 0) {
consumerCache = new ConsumerCache(this, camelContext);
LOG.debug("PollEnrich {} using ConsumerCache with default cache size", this);
} else {
consumerCache = new ConsumerCache(this, camelContext, cacheSize);
LOG.debug("PollEnrich {} using ConsumerCache with cacheSize={}", this, cacheSize);
}
}
ServiceHelper.startServices(consumerCache, aggregationStrategy);
}
use of org.apache.camel.impl.ConsumerCache in project camel by apache.
the class ManagedConsumerCacheHitsTest method testManageConsumerCache.
public void testManageConsumerCache() throws Exception {
// JMX tests dont work well on AIX CI servers (hangs them)
if (isPlatform("aix")) {
return;
}
// always register services in JMX so we can enlist our consumer template/cache
context.getManagementStrategy().getManagementAgent().setRegisterAlways(true);
ConsumerCache cache = new ConsumerCache(this, context);
context.addService(cache);
template.sendBody("seda:a", "Hello World");
Exchange out = cache.getConsumer(context.getEndpoint("seda:a")).receive(3000);
assertNotNull("Should got an exchange", out);
assertEquals("Hello World", out.getIn().getBody());
// get the stats for the route
MBeanServer mbeanServer = getMBeanServer();
Set<ObjectName> set = mbeanServer.queryNames(new ObjectName("*:type=services,*"), null);
List<ObjectName> list = new ArrayList<ObjectName>(set);
ObjectName on = null;
for (ObjectName name : list) {
if (name.getCanonicalName().contains("ConsumerCache")) {
on = name;
break;
}
}
assertNotNull("Should have found ConsumerCache", on);
Integer max = (Integer) mbeanServer.getAttribute(on, "MaximumCacheSize");
assertEquals(1000, max.intValue());
Integer current = (Integer) mbeanServer.getAttribute(on, "Size");
assertEquals(1, current.intValue());
// since we only send 1 message we should have 0 hits and 1 miss
Long hits = (Long) mbeanServer.getAttribute(on, "Hits");
assertEquals(0, hits.longValue());
Long misses = (Long) mbeanServer.getAttribute(on, "Misses");
assertEquals(1, misses.longValue());
// send more
// ---------
template.sendBody("seda:b", "Hello World");
template.sendBody("seda:c", "Hello World");
out = cache.getConsumer(context.getEndpoint("seda:b")).receive(3000);
assertNotNull(out);
out = cache.getConsumer(context.getEndpoint("seda:c")).receive(3000);
assertNotNull(out);
// we have only consumed from 3 different endpoints so all is misses
hits = (Long) mbeanServer.getAttribute(on, "Hits");
assertEquals(0, hits.longValue());
misses = (Long) mbeanServer.getAttribute(on, "Misses");
assertEquals(3, misses.longValue());
// send to same endpoints
// ----------------------
template.sendBody("seda:a", "Bye World");
template.sendBody("seda:b", "Bye World");
out = cache.getConsumer(context.getEndpoint("seda:a")).receive(3000);
assertNotNull(out);
out = cache.getConsumer(context.getEndpoint("seda:b")).receive(3000);
assertNotNull(out);
// we should have hits now
hits = (Long) mbeanServer.getAttribute(on, "Hits");
assertEquals(2, hits.longValue());
misses = (Long) mbeanServer.getAttribute(on, "Misses");
assertEquals(3, misses.longValue());
// reset statistics
// ----------------
mbeanServer.invoke(on, "resetStatistics", null, null);
hits = (Long) mbeanServer.getAttribute(on, "Hits");
assertEquals(0, hits.longValue());
misses = (Long) mbeanServer.getAttribute(on, "Misses");
assertEquals(0, misses.longValue());
}
use of org.apache.camel.impl.ConsumerCache in project camel by apache.
the class ManagedConsumerCacheTest method testManageConsumerCache.
public void testManageConsumerCache() throws Exception {
// JMX tests dont work well on AIX CI servers (hangs them)
if (isPlatform("aix")) {
return;
}
// always register services in JMX so we can enlist our consumer template/cache
context.getManagementStrategy().getManagementAgent().setRegisterAlways(true);
ConsumerCache cache = new ConsumerCache(this, context);
context.addService(cache);
template.sendBody("direct:start", "Hello World");
Endpoint endpoint = context.getEndpoint("seda:queue");
PollingConsumer consumer = cache.getConsumer(endpoint);
Exchange out = consumer.receive(3000);
assertNotNull("Should got an exchange", out);
assertEquals("Hello World", out.getIn().getBody());
// get the stats for the route
MBeanServer mbeanServer = getMBeanServer();
Set<ObjectName> set = mbeanServer.queryNames(new ObjectName("*:type=services,*"), null);
List<ObjectName> list = new ArrayList<ObjectName>(set);
ObjectName on = null;
for (ObjectName name : list) {
if (name.getCanonicalName().contains("ConsumerCache")) {
on = name;
break;
}
}
assertNotNull("Should have found ConsumerCache", on);
Integer max = (Integer) mbeanServer.getAttribute(on, "MaximumCacheSize");
assertEquals(1000, max.intValue());
Integer current = (Integer) mbeanServer.getAttribute(on, "Size");
assertEquals(1, current.intValue());
String source = (String) mbeanServer.getAttribute(on, "Source");
assertNotNull(source);
assertTrue(source.contains("testManageConsumerCache"));
// purge
mbeanServer.invoke(on, "purge", null, null);
current = (Integer) mbeanServer.getAttribute(on, "Size");
assertEquals(0, current.intValue());
// stop the consumer as it was purged from the cache
// so we need to manually stop it
consumer.stop();
cache.stop();
}
Aggregations