use of org.apache.camel.support.LifecycleStrategySupport in project camel by apache.
the class ServicePoolAwareLeakyTest method testForMemoryLeak.
public void testForMemoryLeak() throws Exception {
registerLeakyComponent();
final Map<String, AtomicLong> references = new HashMap<String, AtomicLong>();
// track LeakySieveProducer lifecycle
context.addLifecycleStrategy(new LifecycleStrategySupport() {
@Override
public void onServiceAdd(CamelContext context, Service service, Route route) {
if (service instanceof LeakySieveProducer) {
String key = ((LeakySieveProducer) service).getEndpoint().getEndpointKey();
AtomicLong num = references.get(key);
if (num == null) {
num = new AtomicLong();
references.put(key, num);
}
num.incrementAndGet();
}
}
@Override
public void onServiceRemove(CamelContext context, Service service, Route route) {
if (service instanceof LeakySieveProducer) {
String key = ((LeakySieveProducer) service).getEndpoint().getEndpointKey();
AtomicLong num = references.get(key);
if (num == null) {
num = new AtomicLong();
references.put(key, num);
}
num.decrementAndGet();
}
}
});
context.addRoutes(new RouteBuilder() {
@Override
public void configure() throws Exception {
from("direct:sieve-transient").id("sieve-transient").to(LEAKY_SIEVE_TRANSIENT);
from("direct:sieve-stable").id("sieve-stable").to(LEAKY_SIEVE_STABLE);
}
});
context.start();
for (int i = 0; i < 1000; i++) {
ServiceSupport service = (ServiceSupport) context.getProducerServicePool();
assertEquals(ServiceStatus.Started, service.getStatus());
if (isFailFast()) {
assertEquals(2, context.getProducerServicePool().size());
assertEquals(1, references.get(LEAKY_SIEVE_TRANSIENT).get());
assertEquals(1, references.get(LEAKY_SIEVE_STABLE).get());
}
context.stopRoute("sieve-transient");
if (isFailFast()) {
assertEquals("Expected no service references to remain", 0, references.get(LEAKY_SIEVE_TRANSIENT));
}
if (isFailFast()) {
if (isVerifyProducerServicePoolRemainsStarted()) {
assertEquals(ServiceStatus.Started, service.getStatus());
}
assertEquals("Expected one stable producer to remain pooled", 1, context.getProducerServicePool().size());
assertEquals("Expected one stable producer to remain as service", 1, references.get(LEAKY_SIEVE_STABLE).get());
}
// Send a body to verify behaviour of send producer after another route has been stopped
sendBody("direct:sieve-stable", "");
if (isFailFast()) {
// shared pool is used despite being 'Stopped'
if (isVerifyProducerServicePoolRemainsStarted()) {
assertEquals(ServiceStatus.Started, service.getStatus());
}
assertEquals("Expected only stable producer in pool", 1, context.getProducerServicePool().size());
assertEquals("Expected no references to transient producer", 0, references.get(LEAKY_SIEVE_TRANSIENT).get());
assertEquals("Expected reference to stable producer", 1, references.get(LEAKY_SIEVE_STABLE).get());
}
context.startRoute("sieve-transient");
// ok, back to normal
assertEquals(ServiceStatus.Started, service.getStatus());
if (isFailFast()) {
assertEquals("Expected both producers in pool", 2, context.getProducerServicePool().size());
assertEquals("Expected one transient producer as service", 1, references.get(LEAKY_SIEVE_TRANSIENT).get());
assertEquals("Expected one stable producer as service", 1, references.get(LEAKY_SIEVE_STABLE).get());
}
}
if (!isFailFast()) {
assertEquals("Expected both producers in pool", 2, context.getProducerServicePool().size());
// if not fixed these will equal the number of iterations in the loop + 1
assertEquals("Expected one transient producer as service", 1, references.get(LEAKY_SIEVE_TRANSIENT).get());
assertEquals("Expected one stable producer as service", 1, references.get(LEAKY_SIEVE_STABLE).get());
}
}
use of org.apache.camel.support.LifecycleStrategySupport in project ignite by apache.
the class IgniteCamelStreamerTest method testUserSpecifiedCamelContext.
/**
* @throws Exception
*/
public void testUserSpecifiedCamelContext() throws Exception {
final AtomicInteger cnt = new AtomicInteger();
// Create a CamelContext with a probe that'll help us know if it has been used.
CamelContext context = new DefaultCamelContext();
context.setTracing(true);
context.addLifecycleStrategy(new LifecycleStrategySupport() {
@Override
public void onEndpointAdd(Endpoint endpoint) {
cnt.incrementAndGet();
}
});
streamer.setSingleTupleExtractor(singleTupleExtractor());
streamer.setCamelContext(context);
// Subscribe to cache PUT events.
CountDownLatch latch = subscribeToPutEvents(50);
// Action time.
streamer.start();
// Send messages.
sendMessages(0, 50, false);
// Assertions.
assertTrue(latch.await(10, TimeUnit.SECONDS));
assertCacheEntriesLoaded(50);
assertTrue(cnt.get() > 0);
}
Aggregations