Search in sources :

Example 11 with Consumer

use of org.apache.camel.Consumer in project camel by apache.

the class RouteService method doWarmUp.

protected synchronized void doWarmUp() throws Exception {
    if (endpointDone.compareAndSet(false, true)) {
        // and whatnot, thus their lifecycle is to start once, and only to stop when Camel shutdown
        for (Route route : routes) {
            // ensure endpoint is started first (before the route services, such as the consumer)
            ServiceHelper.startService(route.getEndpoint());
        }
    }
    if (warmUpDone.compareAndSet(false, true)) {
        for (Route route : routes) {
            try (MDCHelper mdcHelper = new MDCHelper(route.getId())) {
                // warm up the route first
                route.warmUp();
                LOG.debug("Starting services on route: {}", route.getId());
                List<Service> services = route.getServices();
                // callback that we are staring these services
                route.onStartingServices(services);
                // gather list of services to start as we need to start child services as well
                Set<Service> list = new LinkedHashSet<Service>();
                for (Service service : services) {
                    list.addAll(ServiceHelper.getChildServices(service));
                }
                // split into consumers and child services as we need to start the consumers
                // afterwards to avoid them being active while the others start
                List<Service> childServices = new ArrayList<Service>();
                for (Service service : list) {
                    // inject the route
                    if (service instanceof RouteAware) {
                        ((RouteAware) service).setRoute(route);
                    }
                    if (service instanceof Consumer) {
                        inputs.put(route, (Consumer) service);
                    } else {
                        childServices.add(service);
                    }
                }
                startChildService(route, childServices);
                // fire event
                EventHelper.notifyRouteAdded(camelContext, route);
            }
        }
        // ensure lifecycle strategy is invoked which among others enlist the route in JMX
        for (LifecycleStrategy strategy : camelContext.getLifecycleStrategies()) {
            strategy.onRoutesAdd(routes);
        }
        // add routes to camel context
        camelContext.addRouteCollection(routes);
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Consumer(org.apache.camel.Consumer) LifecycleStrategy(org.apache.camel.spi.LifecycleStrategy) RouteAware(org.apache.camel.RouteAware) ArrayList(java.util.ArrayList) Service(org.apache.camel.Service) Route(org.apache.camel.Route)

Example 12 with Consumer

use of org.apache.camel.Consumer in project camel by apache.

the class ThrottlingInflightRoutePolicy method throttle.

/**
     * Throttles the route when {@link Exchange}s is done.
     *
     * @param route  the route
     * @param exchange the exchange
     */
protected void throttle(Route route, Exchange exchange) {
    // this works the best when this logic is executed when the exchange is done
    Consumer consumer = route.getConsumer();
    int size = getSize(route, exchange);
    boolean stop = maxInflightExchanges > 0 && size > maxInflightExchanges;
    if (log.isTraceEnabled()) {
        log.trace("{} > 0 && {} > {} evaluated as {}", new Object[] { maxInflightExchanges, size, maxInflightExchanges, stop });
    }
    if (stop) {
        try {
            lock.lock();
            stopConsumer(size, consumer);
        } catch (Exception e) {
            handleException(e);
        } finally {
            lock.unlock();
        }
    }
    // reload size in case a race condition with too many at once being invoked
    // so we need to ensure that we read the most current size and start the consumer if we are already to low
    size = getSize(route, exchange);
    boolean start = size <= resumeInflightExchanges;
    if (log.isTraceEnabled()) {
        log.trace("{} <= {} evaluated as {}", new Object[] { size, resumeInflightExchanges, start });
    }
    if (start) {
        try {
            lock.lock();
            startConsumer(size, consumer);
        } catch (Exception e) {
            handleException(e);
        } finally {
            lock.unlock();
        }
    }
}
Also used : Consumer(org.apache.camel.Consumer)

Example 13 with Consumer

use of org.apache.camel.Consumer in project camel by apache.

the class PollEnricher method process.

/**
     * Enriches the input data (<code>exchange</code>) by first obtaining
     * additional data from an endpoint represented by an endpoint
     * <code>producer</code> and second by aggregating input data and additional
     * data. Aggregation of input data and additional data is delegated to an
     * {@link org.apache.camel.processor.aggregate.AggregationStrategy} object set at construction time. If the
     * message exchange with the resource endpoint fails then no aggregation
     * will be done and the failed exchange content is copied over to the
     * original message exchange.
     *
     * @param exchange input data.
     */
@Override
public boolean process(Exchange exchange, AsyncCallback callback) {
    try {
        preCheckPoll(exchange);
    } catch (Exception e) {
        exchange.setException(new CamelExchangeException("Error during pre poll check", exchange, e));
        callback.done(true);
        return true;
    }
    // which consumer to use
    PollingConsumer consumer;
    Endpoint endpoint;
    // use dynamic endpoint so calculate the endpoint to use
    Object recipient = null;
    try {
        recipient = expression.evaluate(exchange, Object.class);
        endpoint = resolveEndpoint(exchange, recipient);
        // acquire the consumer from the cache
        consumer = consumerCache.acquirePollingConsumer(endpoint);
    } catch (Throwable e) {
        if (isIgnoreInvalidEndpoint()) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Endpoint uri is invalid: " + recipient + ". This exception will be ignored.", e);
            }
        } else {
            exchange.setException(e);
        }
        callback.done(true);
        return true;
    }
    // grab the real delegate consumer that performs the actual polling
    Consumer delegate = consumer;
    if (consumer instanceof EventDrivenPollingConsumer) {
        delegate = ((EventDrivenPollingConsumer) consumer).getDelegateConsumer();
    }
    // is the consumer bridging the error handler?
    boolean bridgeErrorHandler = false;
    if (delegate instanceof DefaultConsumer) {
        ExceptionHandler handler = ((DefaultConsumer) delegate).getExceptionHandler();
        if (handler != null && handler instanceof BridgeExceptionHandlerToErrorHandler) {
            bridgeErrorHandler = true;
        }
    }
    Exchange resourceExchange;
    try {
        if (timeout < 0) {
            LOG.debug("Consumer receive: {}", consumer);
            resourceExchange = consumer.receive();
        } else if (timeout == 0) {
            LOG.debug("Consumer receiveNoWait: {}", consumer);
            resourceExchange = consumer.receiveNoWait();
        } else {
            LOG.debug("Consumer receive with timeout: {} ms. {}", timeout, consumer);
            resourceExchange = consumer.receive(timeout);
        }
        if (resourceExchange == null) {
            LOG.debug("Consumer received no exchange");
        } else {
            LOG.debug("Consumer received: {}", resourceExchange);
        }
    } catch (Exception e) {
        exchange.setException(new CamelExchangeException("Error during poll", exchange, e));
        callback.done(true);
        return true;
    } finally {
        // return the consumer back to the cache
        consumerCache.releasePollingConsumer(endpoint, consumer);
    }
    // remember current redelivery stats
    Object redeliveried = exchange.getIn().getHeader(Exchange.REDELIVERED);
    Object redeliveryCounter = exchange.getIn().getHeader(Exchange.REDELIVERY_COUNTER);
    Object redeliveryMaxCounter = exchange.getIn().getHeader(Exchange.REDELIVERY_MAX_COUNTER);
    // if we are bridging error handler and failed then remember the caused exception
    Throwable cause = null;
    if (resourceExchange != null && bridgeErrorHandler) {
        cause = resourceExchange.getException();
    }
    try {
        if (!isAggregateOnException() && (resourceExchange != null && resourceExchange.isFailed())) {
            // copy resource exchange onto original exchange (preserving pattern)
            // and preserve redelivery headers
            copyResultsPreservePattern(exchange, resourceExchange);
        } else {
            prepareResult(exchange);
            // prepare the exchanges for aggregation
            ExchangeHelper.prepareAggregation(exchange, resourceExchange);
            // must catch any exception from aggregation
            Exchange aggregatedExchange = aggregationStrategy.aggregate(exchange, resourceExchange);
            if (aggregatedExchange != null) {
                // copy aggregation result onto original exchange (preserving pattern)
                copyResultsPreservePattern(exchange, aggregatedExchange);
                // handover any synchronization
                if (resourceExchange != null) {
                    resourceExchange.handoverCompletions(exchange);
                }
            }
        }
        // if we failed then restore caused exception
        if (cause != null) {
            // restore caused exception
            exchange.setException(cause);
            // remove the exhausted marker as we want to be able to perform redeliveries with the error handler
            exchange.removeProperties(Exchange.REDELIVERY_EXHAUSTED);
            // preserve the redelivery stats
            if (redeliveried != null) {
                if (exchange.hasOut()) {
                    exchange.getOut().setHeader(Exchange.REDELIVERED, redeliveried);
                } else {
                    exchange.getIn().setHeader(Exchange.REDELIVERED, redeliveried);
                }
            }
            if (redeliveryCounter != null) {
                if (exchange.hasOut()) {
                    exchange.getOut().setHeader(Exchange.REDELIVERY_COUNTER, redeliveryCounter);
                } else {
                    exchange.getIn().setHeader(Exchange.REDELIVERY_COUNTER, redeliveryCounter);
                }
            }
            if (redeliveryMaxCounter != null) {
                if (exchange.hasOut()) {
                    exchange.getOut().setHeader(Exchange.REDELIVERY_MAX_COUNTER, redeliveryMaxCounter);
                } else {
                    exchange.getIn().setHeader(Exchange.REDELIVERY_MAX_COUNTER, redeliveryMaxCounter);
                }
            }
        }
        // set header with the uri of the endpoint enriched so we can use that for tracing etc
        if (exchange.hasOut()) {
            exchange.getOut().setHeader(Exchange.TO_ENDPOINT, consumer.getEndpoint().getEndpointUri());
        } else {
            exchange.getIn().setHeader(Exchange.TO_ENDPOINT, consumer.getEndpoint().getEndpointUri());
        }
    } catch (Throwable e) {
        exchange.setException(new CamelExchangeException("Error occurred during aggregation", exchange, e));
        callback.done(true);
        return true;
    }
    callback.done(true);
    return true;
}
Also used : CamelExchangeException(org.apache.camel.CamelExchangeException) PollingConsumer(org.apache.camel.PollingConsumer) EventDrivenPollingConsumer(org.apache.camel.impl.EventDrivenPollingConsumer) DefaultConsumer(org.apache.camel.impl.DefaultConsumer) EventDrivenPollingConsumer(org.apache.camel.impl.EventDrivenPollingConsumer) CamelExchangeException(org.apache.camel.CamelExchangeException) BridgeExceptionHandlerToErrorHandler(org.apache.camel.impl.BridgeExceptionHandlerToErrorHandler) ExceptionHandler(org.apache.camel.spi.ExceptionHandler) Exchange(org.apache.camel.Exchange) Endpoint(org.apache.camel.Endpoint) PollingConsumer(org.apache.camel.PollingConsumer) Consumer(org.apache.camel.Consumer) EventDrivenPollingConsumer(org.apache.camel.impl.EventDrivenPollingConsumer) DefaultConsumer(org.apache.camel.impl.DefaultConsumer)

Example 14 with Consumer

use of org.apache.camel.Consumer in project camel by apache.

the class FileConsumerAutoCreateDirectoryTest method testDoNotCreateDirectory.

public void testDoNotCreateDirectory() throws Exception {
    deleteDirectory("target/file/foo");
    Endpoint endpoint = context.getEndpoint("file://target/file/foo?autoCreate=false");
    Consumer consumer = endpoint.createConsumer(new Processor() {

        public void process(Exchange exchange) throws Exception {
        // noop
        }
    });
    consumer.start();
    consumer.stop();
    // the directory should NOT exists
    File dir = new File("target/file/foo");
    assertFalse("Directory should NOT be created", dir.exists());
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) Endpoint(org.apache.camel.Endpoint) Consumer(org.apache.camel.Consumer) File(java.io.File) FileNotFoundException(java.io.FileNotFoundException)

Example 15 with Consumer

use of org.apache.camel.Consumer in project camel by apache.

the class RefComponentTest method bindToRegistry.

private void bindToRegistry(JndiRegistry jndi) throws Exception {
    Component comp = new DirectComponent();
    comp.setCamelContext(context);
    Endpoint slow = comp.createEndpoint("direct:somename");
    Consumer consumer = slow.createConsumer(new Processor() {

        public void process(Exchange exchange) throws Exception {
            template.send("mock:result", exchange);
        }
    });
    consumer.start();
    // bind our endpoint to the registry for ref to lookup
    jndi.bind("foo", slow);
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) Endpoint(org.apache.camel.Endpoint) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) Consumer(org.apache.camel.Consumer) DirectComponent(org.apache.camel.component.direct.DirectComponent) Component(org.apache.camel.Component) DirectComponent(org.apache.camel.component.direct.DirectComponent)

Aggregations

Consumer (org.apache.camel.Consumer)76 Endpoint (org.apache.camel.Endpoint)27 Processor (org.apache.camel.Processor)19 Exchange (org.apache.camel.Exchange)18 Test (org.junit.Test)18 HashMap (java.util.HashMap)10 RestConfiguration (org.apache.camel.spi.RestConfiguration)10 Producer (org.apache.camel.Producer)7 PollingConsumer (org.apache.camel.PollingConsumer)6 File (java.io.File)5 PollingConsumerPollStrategy (org.apache.camel.spi.PollingConsumerPollStrategy)5 FileNotFoundException (java.io.FileNotFoundException)4 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)4 SuspendableService (org.apache.camel.SuspendableService)4 ArrayList (java.util.ArrayList)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 CamelContext (org.apache.camel.CamelContext)3 Component (org.apache.camel.Component)3 NoSuchBeanException (org.apache.camel.NoSuchBeanException)3 Route (org.apache.camel.Route)3