Search in sources :

Example 66 with Consumer

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

the class CoAPComponent method createConsumer.

@Override
public Consumer createConsumer(CamelContext camelContext, Processor processor, String verb, String basePath, String uriTemplate, String consumes, String produces, RestConfiguration configuration, Map<String, Object> parameters) throws Exception {
    RestConfiguration config = configuration;
    if (config == null) {
        config = getCamelContext().getRestConfiguration("coap", true);
    }
    String host = config.getHost();
    if (ObjectHelper.isEmpty(host)) {
        if (config.getRestHostNameResolver() == RestConfiguration.RestHostNameResolver.allLocalIp) {
            host = "0.0.0.0";
        } else if (config.getRestHostNameResolver() == RestConfiguration.RestHostNameResolver.localHostName) {
            host = HostUtils.getLocalHostName();
        } else if (config.getRestHostNameResolver() == RestConfiguration.RestHostNameResolver.localIp) {
            host = HostUtils.getLocalIp();
        }
    }
    Map<String, Object> map = new HashMap<String, Object>();
    // setup endpoint options
    if (config.getEndpointProperties() != null && !config.getEndpointProperties().isEmpty()) {
        map.putAll(config.getEndpointProperties());
    }
    // allow HTTP Options as we want to handle CORS in rest-dsl
    boolean cors = config.isEnableCORS();
    String query = URISupport.createQueryString(map);
    String url = (config.getScheme() == null ? "coap" : config.getScheme()) + "://" + host;
    if (config.getPort() != -1) {
        url += ":" + config.getPort();
    }
    String restrict = verb.toUpperCase(Locale.US);
    if (cors) {
        restrict += ",OPTIONS";
    }
    if (uriTemplate == null) {
        uriTemplate = "";
    }
    url += basePath + uriTemplate + "?coapMethod=" + restrict;
    if (!query.isEmpty()) {
        url += "&" + query;
    }
    CoAPEndpoint endpoint = camelContext.getEndpoint(url, CoAPEndpoint.class);
    setProperties(endpoint, parameters);
    // configure consumer properties
    Consumer consumer = endpoint.createConsumer(processor);
    if (config.getConsumerProperties() != null && !config.getConsumerProperties().isEmpty()) {
        setProperties(consumer, config.getConsumerProperties());
    }
    return consumer;
}
Also used : Consumer(org.apache.camel.Consumer) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) RestConfiguration(org.apache.camel.spi.RestConfiguration)

Example 67 with Consumer

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

the class DefaultCamelContext method doStartOrResumeRouteConsumers.

private void doStartOrResumeRouteConsumers(Map<Integer, DefaultRouteStartupOrder> inputs, boolean resumeOnly, boolean addingRoute) throws Exception {
    List<Endpoint> routeInputs = new ArrayList<Endpoint>();
    for (Map.Entry<Integer, DefaultRouteStartupOrder> entry : inputs.entrySet()) {
        Integer order = entry.getKey();
        Route route = entry.getValue().getRoute();
        RouteService routeService = entry.getValue().getRouteService();
        // if we are starting camel, then skip routes which are configured to not be auto started
        boolean autoStartup = routeService.getRouteDefinition().isAutoStartup(this) && this.isAutoStartup();
        if (addingRoute && !autoStartup) {
            log.info("Skipping starting of route " + routeService.getId() + " as its configured with autoStartup=false");
            continue;
        }
        // start the service
        for (Consumer consumer : routeService.getInputs().values()) {
            Endpoint endpoint = consumer.getEndpoint();
            // check multiple consumer violation, with the other routes to be started
            if (!doCheckMultipleConsumerSupportClash(endpoint, routeInputs)) {
                throw new FailedToStartRouteException(routeService.getId(), "Multiple consumers for the same endpoint is not allowed: " + endpoint);
            }
            // check for multiple consumer violations with existing routes which
            // have already been started, or is currently starting
            List<Endpoint> existingEndpoints = new ArrayList<Endpoint>();
            for (Route existingRoute : getRoutes()) {
                if (route.getId().equals(existingRoute.getId())) {
                    // skip ourselves
                    continue;
                }
                Endpoint existing = existingRoute.getEndpoint();
                ServiceStatus status = getRouteStatus(existingRoute.getId());
                if (status != null && (status.isStarted() || status.isStarting())) {
                    existingEndpoints.add(existing);
                }
            }
            if (!doCheckMultipleConsumerSupportClash(endpoint, existingEndpoints)) {
                throw new FailedToStartRouteException(routeService.getId(), "Multiple consumers for the same endpoint is not allowed: " + endpoint);
            }
            // start the consumer on the route
            log.debug("Route: {} >>> {}", route.getId(), route);
            if (resumeOnly) {
                log.debug("Resuming consumer (order: {}) on route: {}", order, route.getId());
            } else {
                log.debug("Starting consumer (order: {}) on route: {}", order, route.getId());
            }
            if (resumeOnly && route.supportsSuspension()) {
                // if we are resuming and the route can be resumed
                ServiceHelper.resumeService(consumer);
                log.info("Route: " + route.getId() + " resumed and consuming from: " + endpoint);
            } else {
                // when starting we should invoke the lifecycle strategies
                for (LifecycleStrategy strategy : lifecycleStrategies) {
                    strategy.onServiceAdd(this, consumer, route);
                }
                startService(consumer);
                log.info("Route: " + route.getId() + " started and consuming from: " + endpoint);
            }
            routeInputs.add(endpoint);
            // add to the order which they was started, so we know how to stop them in reverse order
            // but only add if we haven't already registered it before (we dont want to double add when restarting)
            boolean found = false;
            for (RouteStartupOrder other : routeStartupOrder) {
                if (other.getRoute().getId().equals(route.getId())) {
                    found = true;
                    break;
                }
            }
            if (!found) {
                routeStartupOrder.add(entry.getValue());
            }
        }
        if (resumeOnly) {
            routeService.resume();
        } else {
            // and start the route service (no need to start children as they are already warmed up)
            routeService.start(false);
        }
    }
}
Also used : CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) RouteStartupOrder(org.apache.camel.spi.RouteStartupOrder) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) FailedToStartRouteException(org.apache.camel.FailedToStartRouteException) Endpoint(org.apache.camel.Endpoint) PollingConsumer(org.apache.camel.PollingConsumer) Consumer(org.apache.camel.Consumer) LifecycleStrategy(org.apache.camel.spi.LifecycleStrategy) ServiceStatus(org.apache.camel.ServiceStatus) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) TreeMap(java.util.TreeMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ShutdownRoute(org.apache.camel.ShutdownRoute) Route(org.apache.camel.Route)

Example 68 with Consumer

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

the class FileConsumerAutoCreateDirectoryTest method testCreateDirectory.

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

        public void process(Exchange exchange) throws Exception {
        // noop
        }
    });
    consumer.start();
    consumer.stop();
    // the directory should now exists
    File dir = new File("target/file/foo");
    assertTrue("Directory should be created", dir.exists());
    assertTrue("Directory should be a directory", dir.isDirectory());
}
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 69 with Consumer

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

the class FileConsumerAutoCreateDirectoryTest method testAutoCreateDirectoryWithDot.

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

        public void process(Exchange exchange) throws Exception {
        // noop
        }
    });
    consumer.start();
    consumer.stop();
    // the directory should exist
    File dir = new File("target/file/foo.bar");
    assertTrue("Directory should be created", dir.exists());
    assertTrue("Directory should be a directory", dir.isDirectory());
}
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 70 with Consumer

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

the class FileConsumerAutoCreateDirectoryTest method testCreateAbsoluteDirectory.

public void testCreateAbsoluteDirectory() throws Exception {
    deleteDirectory("target/file/foo");
    // use current dir as base as absolute path
    String base = new File("").getAbsolutePath() + "/target/file/foo";
    Endpoint endpoint = context.getEndpoint("file://" + base);
    Consumer consumer = endpoint.createConsumer(new Processor() {

        public void process(Exchange exchange) throws Exception {
        // noop
        }
    });
    consumer.start();
    consumer.stop();
    // the directory should now exists
    File dir = new File(base);
    assertTrue("Directory should be created", dir.exists());
    assertTrue("Directory should be a directory", dir.isDirectory());
}
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)

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