Search in sources :

Example 61 with RouteBuilder

use of org.apache.camel.builder.RouteBuilder in project camel by apache.

the class BasicPublisherTest method testOnlyOneCamelProducerPerPublisher.

@Test(expected = IllegalStateException.class)
public void testOnlyOneCamelProducerPerPublisher() throws Exception {
    new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            from("direct:one").to("reactive-streams:stream");
            from("direct:two").to("reactive-streams:stream");
        }
    }.addRoutesToCamelContext(context);
    context.start();
}
Also used : RouteBuilder(org.apache.camel.builder.RouteBuilder) Test(org.junit.Test)

Example 62 with RouteBuilder

use of org.apache.camel.builder.RouteBuilder in project camel by apache.

the class ShiroAuthorizationTest method createRouteBuilders.

@Override
protected RouteBuilder[] createRouteBuilders() throws Exception {
    return new RouteBuilder[] { new RouteBuilder() {

        public void configure() {
            List<Permission> permissionsList = new ArrayList<Permission>();
            Permission permission = new WildcardPermission("zone1:readwrite:*");
            permissionsList.add(permission);
            final ShiroSecurityPolicy securityPolicy = new ShiroSecurityPolicy("src/test/resources/securityconfig.ini", passPhrase, true, permissionsList);
            onException(CamelAuthorizationException.class).to("mock:authorizationException");
            from("direct:secureEndpoint").policy(securityPolicy).to("log:incoming payload").to("mock:success");
        }
    }, new RouteBuilder() {

        public void configure() {
            List<Permission> permissionsList = new ArrayList<Permission>();
            Permission permission = new WildcardPermission("zone1:readonly:*");
            permissionsList.add(permission);
            permission = new WildcardPermission("zone1:writeonly:*");
            permissionsList.add(permission);
            final ShiroSecurityPolicy securityPolicy = new ShiroSecurityPolicy("src/test/resources/securityconfig.ini", passPhrase, true, permissionsList);
            onException(CamelAuthorizationException.class).to("mock:authorizationException");
            from("direct:secureAnyEndpoint").policy(securityPolicy).to("log:incoming payload").to("mock:success");
        }
    }, new RouteBuilder() {

        public void configure() {
            List<Permission> permissionsList = new ArrayList<Permission>();
            Permission permission = new WildcardPermission("zone1:readonly:*");
            permissionsList.add(permission);
            permission = new WildcardPermission("zone1:writeonly:*");
            permissionsList.add(permission);
            final ShiroSecurityPolicy securityPolicy = new ShiroSecurityPolicy("src/test/resources/securityconfig.ini", passPhrase, true, permissionsList);
            securityPolicy.setAllPermissionsRequired(true);
            onException(CamelAuthorizationException.class).to("mock:authorizationException");
            from("direct:secureAllEndpoint").policy(securityPolicy).to("log:incoming payload").to("mock:success");
        }
    } };
}
Also used : CamelAuthorizationException(org.apache.camel.CamelAuthorizationException) RouteBuilder(org.apache.camel.builder.RouteBuilder) ArrayList(java.util.ArrayList) Permission(org.apache.shiro.authz.Permission) WildcardPermission(org.apache.shiro.authz.permission.WildcardPermission) WildcardPermission(org.apache.shiro.authz.permission.WildcardPermission)

Example 63 with RouteBuilder

use of org.apache.camel.builder.RouteBuilder in project camel by apache.

the class CamelServletContextListener method extractRoutes.

/**
     * Extract the routes from the parameters.
     *
     * @param map parameters
     * @return a list of routes, which can be of different types. See source code for more details.
     */
private List<Object> extractRoutes(Map<String, Object> map) {
    List<Object> answer = new ArrayList<Object>();
    List<String> names = new ArrayList<String>();
    for (Map.Entry<String, Object> entry : map.entrySet()) {
        if (entry.getKey().toLowerCase(Locale.UK).startsWith("routebuilder")) {
            names.add(entry.getKey());
            // we can have multiple values assigned, separated by comma, so create an iterator
            String value = (String) entry.getValue();
            Iterator<Object> it = ObjectHelper.createIterator(value);
            while (it.hasNext()) {
                value = (String) it.next();
                if (ObjectHelper.isNotEmpty(value)) {
                    // trim value before usage, as people can indent the values
                    value = value.trim();
                    Object target = null;
                    if (value.startsWith("#")) {
                        // a reference lookup in jndi
                        value = value.substring(1);
                        target = lookupRegistryByName(value);
                    } else if (ResourceHelper.hasScheme(value)) {
                        // XML resource from classpath or file system
                        InputStream is = null;
                        try {
                            is = ResourceHelper.resolveMandatoryResourceAsInputStream(camelContext, value);
                            target = camelContext.loadRoutesDefinition(is);
                        } catch (Exception e) {
                            throw new RuntimeException("Error loading routes from resource: " + value, e);
                        } finally {
                            IOHelper.close(is, entry.getKey(), LOG);
                        }
                    } else if (value.startsWith("packagescan:")) {
                        // using package scanning
                        String path = value.substring(12);
                        Set<Class<?>> classes = camelContext.getPackageScanClassResolver().findImplementations(RouteBuilder.class, path);
                        if (!classes.isEmpty()) {
                            Set<RouteBuilder> builders = new LinkedHashSet<RouteBuilder>();
                            target = builders;
                            for (Class<?> clazz : classes) {
                                try {
                                    RouteBuilder route = (RouteBuilder) camelContext.getInjector().newInstance(clazz);
                                    builders.add(route);
                                } catch (Exception e) {
                                    throw new RuntimeException("Error creating RouteBuilder " + clazz, e);
                                }
                            }
                        }
                    } else {
                        // assume its a FQN classname for a RouteBuilder class
                        try {
                            Class<RouteBuilder> clazz = camelContext.getClassResolver().resolveMandatoryClass(value, RouteBuilder.class);
                            target = camelContext.getInjector().newInstance(clazz);
                        } catch (Exception e) {
                            throw new RuntimeException("Error creating RouteBuilder " + value, e);
                        }
                    }
                    if (target != null) {
                        answer.add(target);
                    }
                }
            }
        }
    }
    // after adding the route builders we should remove them from the map
    for (String name : names) {
        map.remove(name);
    }
    return answer;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) RouteBuilder(org.apache.camel.builder.RouteBuilder) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 64 with RouteBuilder

use of org.apache.camel.builder.RouteBuilder in project camel by apache.

the class SjmsBatchConsumerTest method testConsumptionCompletionInterval.

@Test
public void testConsumptionCompletionInterval() throws Exception {
    final int completionInterval = 2000;
    // timeout-based only
    final int completionSize = -1;
    final String queueName = getQueueName();
    context.addRoutes(new TransactedSendHarness(queueName));
    context.addRoutes(new RouteBuilder() {

        public void configure() throws Exception {
            fromF("sjms-batch:%s?completionInterval=%s&completionSize=%s&aggregationStrategy=#testStrategy", queueName, completionInterval, completionSize).routeId("batchConsumer").startupOrder(10).to("mock:batches");
        }
    });
    context.start();
    int messageCount = 50;
    assertTrue(messageCount < SjmsBatchEndpoint.DEFAULT_COMPLETION_SIZE);
    MockEndpoint mockBatches = getMockEndpoint("mock:batches");
    // everything ought to be batched together but the interval may trigger in between and we get 2 etc
    mockBatches.expectedMinimumMessageCount(1);
    template.sendBody("direct:in", generateStrings(messageCount));
    mockBatches.assertIsSatisfied();
}
Also used : RouteBuilder(org.apache.camel.builder.RouteBuilder) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) Test(org.junit.Test)

Example 65 with RouteBuilder

use of org.apache.camel.builder.RouteBuilder in project camel by apache.

the class SjmsBatchConsumerTest method testConsumption.

@Test
public void testConsumption() throws Exception {
    final int messageCount = 10000;
    final int consumerCount = 5;
    final String queueName = getQueueName();
    context.addRoutes(new TransactedSendHarness(queueName));
    context.addRoutes(new RouteBuilder() {

        public void configure() throws Exception {
            int completionTimeout = 1000;
            int completionSize = 200;
            fromF("sjms-batch:%s?completionTimeout=%s&completionSize=%s&consumerCount=%s&aggregationStrategy=#testStrategy", queueName, completionTimeout, completionSize, consumerCount).routeId("batchConsumer").startupOrder(10).autoStartup(false).split(body()).to("mock:split");
        }
    });
    context.start();
    MockEndpoint mockBefore = getMockEndpoint("mock:before");
    mockBefore.setExpectedMessageCount(messageCount);
    MockEndpoint mockSplit = getMockEndpoint("mock:split");
    mockSplit.setExpectedMessageCount(messageCount);
    LOG.info("Sending messages");
    template.sendBody("direct:in", generateStrings(messageCount));
    LOG.info("Send complete");
    StopWatch stopWatch = new StopWatch();
    context.startRoute("batchConsumer");
    assertMockEndpointsSatisfied();
    long time = stopWatch.stop();
    LOG.info("Processed {} messages in {} ms", messageCount, time);
    LOG.info("Average throughput {} msg/s", (long) (messageCount / (time / 1000d)));
}
Also used : RouteBuilder(org.apache.camel.builder.RouteBuilder) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) StopWatch(org.apache.camel.util.StopWatch) Test(org.junit.Test)

Aggregations

RouteBuilder (org.apache.camel.builder.RouteBuilder)1759 Exchange (org.apache.camel.Exchange)628 Processor (org.apache.camel.Processor)545 Test (org.junit.Test)476 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)341 CamelExecutionException (org.apache.camel.CamelExecutionException)135 FailedToCreateRouteException (org.apache.camel.FailedToCreateRouteException)119 DefaultCamelContext (org.apache.camel.impl.DefaultCamelContext)104 File (java.io.File)68 CamelContext (org.apache.camel.CamelContext)64 IOException (java.io.IOException)61 ResolveEndpointFailedException (org.apache.camel.ResolveEndpointFailedException)42 HashMap (java.util.HashMap)35 Path (org.apache.hadoop.fs.Path)34 CountDownLatch (java.util.concurrent.CountDownLatch)32 Configuration (org.apache.hadoop.conf.Configuration)32 Endpoint (org.apache.camel.Endpoint)30 ArrayFile (org.apache.hadoop.io.ArrayFile)30 SequenceFile (org.apache.hadoop.io.SequenceFile)30 RuntimeCamelException (org.apache.camel.RuntimeCamelException)26