Search in sources :

Example 1 with ServiceSupport

use of org.apache.camel.support.ServiceSupport 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());
    }
}
Also used : CamelContext(org.apache.camel.CamelContext) ServiceSupport(org.apache.camel.support.ServiceSupport) LifecycleStrategySupport(org.apache.camel.support.LifecycleStrategySupport) RouteBuilder(org.apache.camel.builder.RouteBuilder) HashMap(java.util.HashMap) Service(org.apache.camel.Service) DefaultEndpoint(org.apache.camel.impl.DefaultEndpoint) Endpoint(org.apache.camel.Endpoint) AtomicLong(java.util.concurrent.atomic.AtomicLong) Route(org.apache.camel.Route)

Example 2 with ServiceSupport

use of org.apache.camel.support.ServiceSupport in project camel by apache.

the class QuartzEndpoint method onJobExecute.

/**
     * This method is invoked when a Quartz job is fired.
     *
     * @param jobExecutionContext the Quartz Job context
     */
public void onJobExecute(final JobExecutionContext jobExecutionContext) throws JobExecutionException {
    boolean run = true;
    LoadBalancer balancer = getLoadBalancer();
    if (balancer instanceof ServiceSupport) {
        run = ((ServiceSupport) balancer).isRunAllowed();
    }
    if (!run) {
        // quartz scheduler could potential trigger during a route has been shutdown
        LOG.warn("Cannot execute Quartz Job with context: " + jobExecutionContext + " because processor is not started: " + balancer);
        return;
    }
    LOG.debug("Firing Quartz Job with context: {}", jobExecutionContext);
    Exchange exchange = createExchange(jobExecutionContext);
    try {
        balancer.process(exchange);
        if (exchange.getException() != null) {
            // propagate the exception back to Quartz
            throw new JobExecutionException(exchange.getException());
        }
    } catch (Exception e) {
        // log the error
        LOG.error(CamelExchangeException.createExceptionMessage("Error processing exchange", exchange, e));
        // and rethrow to let quartz handle it
        if (e instanceof JobExecutionException) {
            throw (JobExecutionException) e;
        }
        throw new JobExecutionException(e);
    }
}
Also used : Exchange(org.apache.camel.Exchange) ServiceSupport(org.apache.camel.support.ServiceSupport) JobExecutionException(org.quartz.JobExecutionException) LoadBalancer(org.apache.camel.processor.loadbalancer.LoadBalancer) RoundRobinLoadBalancer(org.apache.camel.processor.loadbalancer.RoundRobinLoadBalancer) JobExecutionException(org.quartz.JobExecutionException) SchedulerException(org.quartz.SchedulerException) CamelExchangeException(org.apache.camel.CamelExchangeException)

Aggregations

ServiceSupport (org.apache.camel.support.ServiceSupport)2 HashMap (java.util.HashMap)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 CamelContext (org.apache.camel.CamelContext)1 CamelExchangeException (org.apache.camel.CamelExchangeException)1 Endpoint (org.apache.camel.Endpoint)1 Exchange (org.apache.camel.Exchange)1 Route (org.apache.camel.Route)1 Service (org.apache.camel.Service)1 RouteBuilder (org.apache.camel.builder.RouteBuilder)1 DefaultEndpoint (org.apache.camel.impl.DefaultEndpoint)1 LoadBalancer (org.apache.camel.processor.loadbalancer.LoadBalancer)1 RoundRobinLoadBalancer (org.apache.camel.processor.loadbalancer.RoundRobinLoadBalancer)1 LifecycleStrategySupport (org.apache.camel.support.LifecycleStrategySupport)1 JobExecutionException (org.quartz.JobExecutionException)1 SchedulerException (org.quartz.SchedulerException)1