Search in sources :

Example 1 with InterceptSendToEndpoint

use of org.apache.camel.impl.InterceptSendToEndpoint in project camel by apache.

the class MockEndpoint method assertIsSatisfied.

/**
     * Asserts that all the expectations on any {@link MockEndpoint} instances registered
     * in the given context are valid
     *
     * @param context the camel context used to find all the available endpoints to be asserted
     * @param timeout timeout
     * @param unit    time unit
     */
public static void assertIsSatisfied(CamelContext context, long timeout, TimeUnit unit) throws InterruptedException {
    ObjectHelper.notNull(context, "camelContext");
    ObjectHelper.notNull(unit, "unit");
    Collection<Endpoint> endpoints = context.getEndpoints();
    long millis = unit.toMillis(timeout);
    for (Endpoint endpoint : endpoints) {
        // if the endpoint was intercepted we should get the delegate
        if (endpoint instanceof InterceptSendToEndpoint) {
            endpoint = ((InterceptSendToEndpoint) endpoint).getDelegate();
        }
        if (endpoint instanceof MockEndpoint) {
            MockEndpoint mockEndpoint = (MockEndpoint) endpoint;
            mockEndpoint.setResultWaitTime(millis);
            mockEndpoint.assertIsSatisfied();
        }
    }
}
Also used : InterceptSendToEndpoint(org.apache.camel.impl.InterceptSendToEndpoint) Endpoint(org.apache.camel.Endpoint) UriEndpoint(org.apache.camel.spi.UriEndpoint) DefaultEndpoint(org.apache.camel.impl.DefaultEndpoint) InterceptSendToEndpoint(org.apache.camel.impl.InterceptSendToEndpoint) BrowsableEndpoint(org.apache.camel.spi.BrowsableEndpoint)

Example 2 with InterceptSendToEndpoint

use of org.apache.camel.impl.InterceptSendToEndpoint in project camel by apache.

the class InterceptSendToEndpointDefinition method createProcessor.

@Override
public Processor createProcessor(final RouteContext routeContext) throws Exception {
    // create the detour
    final Processor detour = this.createChildProcessor(routeContext, true);
    final String matchURI = getUri();
    // register endpoint callback so we can proxy the endpoint
    routeContext.getCamelContext().addRegisterEndpointCallback(new EndpointStrategy() {

        public Endpoint registerEndpoint(String uri, Endpoint endpoint) {
            if (endpoint instanceof InterceptSendToEndpoint) {
                // endpoint already decorated
                return endpoint;
            } else if (matchURI == null || matchPattern(routeContext.getCamelContext(), uri, matchURI)) {
                // only proxy if the uri is matched decorate endpoint with our proxy
                // should be false by default
                boolean skip = getSkipSendToOriginalEndpoint() != null && getSkipSendToOriginalEndpoint();
                InterceptSendToEndpoint proxy = new InterceptSendToEndpoint(endpoint, skip);
                proxy.setDetour(detour);
                return proxy;
            } else {
                // no proxy so return regular endpoint
                return endpoint;
            }
        }
    });
    // remove the original intercepted route from the outputs as we do not intercept as the regular interceptor
    // instead we use the proxy endpoints producer do the triggering. That is we trigger when someone sends
    // an exchange to the endpoint, see InterceptSendToEndpoint for details.
    RouteDefinition route = routeContext.getRoute();
    List<ProcessorDefinition<?>> outputs = route.getOutputs();
    outputs.remove(this);
    return new InterceptEndpointProcessor(matchURI, detour);
}
Also used : InterceptSendToEndpoint(org.apache.camel.impl.InterceptSendToEndpoint) InterceptEndpointProcessor(org.apache.camel.processor.InterceptEndpointProcessor) Processor(org.apache.camel.Processor) InterceptSendToEndpoint(org.apache.camel.impl.InterceptSendToEndpoint) Endpoint(org.apache.camel.Endpoint) EndpointStrategy(org.apache.camel.spi.EndpointStrategy) InterceptEndpointProcessor(org.apache.camel.processor.InterceptEndpointProcessor)

Example 3 with InterceptSendToEndpoint

use of org.apache.camel.impl.InterceptSendToEndpoint in project camel by apache.

the class SendProcessor method doStart.

protected void doStart() throws Exception {
    if (producerCache == null) {
        // use a single producer cache as we need to only hold reference for one destination
        // and use a regular HashMap as we do not want a soft reference store that may get re-claimed when low on memory
        // as we want to ensure the producer is kept around, to ensure its lifecycle is fully managed,
        // eg stopping the producer when we stop etc.
        producerCache = new ProducerCache(this, camelContext, new HashMap<String, Producer>(1));
    // do not add as service as we do not want to manage the producer cache
    }
    ServiceHelper.startService(producerCache);
    // the destination could since have been intercepted by a interceptSendToEndpoint so we got to
    // lookup this before we can use the destination
    Endpoint lookup = camelContext.hasEndpoint(destination.getEndpointKey());
    if (lookup instanceof InterceptSendToEndpoint) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Intercepted sending to {} -> {}", URISupport.sanitizeUri(destination.getEndpointUri()), URISupport.sanitizeUri(lookup.getEndpointUri()));
        }
        destination = lookup;
    }
    // warm up the producer by starting it so we can fail fast if there was a problem
    // however must start endpoint first
    ServiceHelper.startService(destination);
    // this SendProcessor is used a lot in Camel (eg every .to in the route DSL) and therefore we
    // want to optimize for regular producers, by using the producer directly instead of the ProducerCache
    // Only for pooled and non singleton producers we have to use the ProducerCache as it supports these
    // kind of producer better (though these kind of producer should be rare)
    Producer producer = producerCache.acquireProducer(destination);
    if (producer instanceof ServicePoolAware || !producer.isSingleton()) {
        // no we cannot optimize it - so release the producer back to the producer cache
        // and use the producer cache for sending
        producerCache.releaseProducer(destination, producer);
    } else {
        // yes we can optimize and use the producer directly for sending
        this.producer = AsyncProcessorConverterHelper.convert(producer);
    }
}
Also used : InterceptSendToEndpoint(org.apache.camel.impl.InterceptSendToEndpoint) Endpoint(org.apache.camel.Endpoint) InterceptSendToEndpoint(org.apache.camel.impl.InterceptSendToEndpoint) Producer(org.apache.camel.Producer) HashMap(java.util.HashMap) ServicePoolAware(org.apache.camel.ServicePoolAware) ProducerCache(org.apache.camel.impl.ProducerCache)

Example 4 with InterceptSendToEndpoint

use of org.apache.camel.impl.InterceptSendToEndpoint in project camel by apache.

the class MockEndpoint method setAssertPeriod.

/**
     * Sets the assert period on all the expectations on any {@link MockEndpoint} instances registered
     * in the given context.
     *
     * @param context the camel context used to find all the available endpoints
     * @param period the period in millis
     */
public static void setAssertPeriod(CamelContext context, long period) {
    ObjectHelper.notNull(context, "camelContext");
    Collection<Endpoint> endpoints = context.getEndpoints();
    for (Endpoint endpoint : endpoints) {
        // if the endpoint was intercepted we should get the delegate
        if (endpoint instanceof InterceptSendToEndpoint) {
            endpoint = ((InterceptSendToEndpoint) endpoint).getDelegate();
        }
        if (endpoint instanceof MockEndpoint) {
            MockEndpoint mockEndpoint = (MockEndpoint) endpoint;
            mockEndpoint.setAssertPeriod(period);
        }
    }
}
Also used : InterceptSendToEndpoint(org.apache.camel.impl.InterceptSendToEndpoint) Endpoint(org.apache.camel.Endpoint) UriEndpoint(org.apache.camel.spi.UriEndpoint) DefaultEndpoint(org.apache.camel.impl.DefaultEndpoint) InterceptSendToEndpoint(org.apache.camel.impl.InterceptSendToEndpoint) BrowsableEndpoint(org.apache.camel.spi.BrowsableEndpoint)

Example 5 with InterceptSendToEndpoint

use of org.apache.camel.impl.InterceptSendToEndpoint in project camel by apache.

the class MockEndpoint method assertIsSatisfied.

/**
     * Asserts that all the expectations on any {@link MockEndpoint} instances registered
     * in the given context are valid
     *
     * @param context the camel context used to find all the available endpoints to be asserted
     */
public static void assertIsSatisfied(CamelContext context) throws InterruptedException {
    ObjectHelper.notNull(context, "camelContext");
    Collection<Endpoint> endpoints = context.getEndpoints();
    for (Endpoint endpoint : endpoints) {
        // if the endpoint was intercepted we should get the delegate
        if (endpoint instanceof InterceptSendToEndpoint) {
            endpoint = ((InterceptSendToEndpoint) endpoint).getDelegate();
        }
        if (endpoint instanceof MockEndpoint) {
            MockEndpoint mockEndpoint = (MockEndpoint) endpoint;
            mockEndpoint.assertIsSatisfied();
        }
    }
}
Also used : InterceptSendToEndpoint(org.apache.camel.impl.InterceptSendToEndpoint) Endpoint(org.apache.camel.Endpoint) UriEndpoint(org.apache.camel.spi.UriEndpoint) DefaultEndpoint(org.apache.camel.impl.DefaultEndpoint) InterceptSendToEndpoint(org.apache.camel.impl.InterceptSendToEndpoint) BrowsableEndpoint(org.apache.camel.spi.BrowsableEndpoint)

Aggregations

InterceptSendToEndpoint (org.apache.camel.impl.InterceptSendToEndpoint)7 Endpoint (org.apache.camel.Endpoint)6 DefaultEndpoint (org.apache.camel.impl.DefaultEndpoint)4 BrowsableEndpoint (org.apache.camel.spi.BrowsableEndpoint)4 UriEndpoint (org.apache.camel.spi.UriEndpoint)4 Producer (org.apache.camel.Producer)2 HashMap (java.util.HashMap)1 Processor (org.apache.camel.Processor)1 ServicePoolAware (org.apache.camel.ServicePoolAware)1 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)1 ProducerCache (org.apache.camel.impl.ProducerCache)1 InterceptEndpointProcessor (org.apache.camel.processor.InterceptEndpointProcessor)1 EndpointStrategy (org.apache.camel.spi.EndpointStrategy)1 ObjectHelper.wrapRuntimeCamelException (org.apache.camel.util.ObjectHelper.wrapRuntimeCamelException)1