Search in sources :

Example 66 with CamelContext

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

the class MessageConsumerClient method main.

public static void main(String[] args) throws Exception {
    LOG.info("About to run Kafka-camel integration...");
    CamelContext camelContext = new DefaultCamelContext();
    // Add route to send messages to Kafka
    camelContext.addRoutes(new RouteBuilder() {

        public void configure() {
            PropertiesComponent pc = getContext().getComponent("properties", PropertiesComponent.class);
            pc.setLocation("classpath:application.properties");
            log.info("About to start route: Kafka Server -> Log ");
            from("kafka:{{consumer.topic}}?brokers={{kafka.host}}:{{kafka.port}}" + "&maxPollRecords={{consumer.maxPollRecords}}" + "&consumersCount={{consumer.consumersCount}}" + "&seekTo={{consumer.seekTo}}" + "&groupId={{consumer.group}}").routeId("FromKafka").log("${body}");
        }
    });
    camelContext.start();
    // let it run for 5 minutes before shutting down
    Thread.sleep(5 * 60 * 1000);
    camelContext.stop();
}
Also used : CamelContext(org.apache.camel.CamelContext) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) RouteBuilder(org.apache.camel.builder.RouteBuilder) PropertiesComponent(org.apache.camel.component.properties.PropertiesComponent) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext)

Example 67 with CamelContext

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

the class MessagePublisherClient method main.

public static void main(String[] args) throws Exception {
    LOG.info("About to run Kafka-camel integration...");
    String testKafkaMessage = "Test Message from  MessagePublisherClient " + Calendar.getInstance().getTime();
    CamelContext camelContext = new DefaultCamelContext();
    // Add route to send messages to Kafka
    camelContext.addRoutes(new RouteBuilder() {

        public void configure() {
            PropertiesComponent pc = getContext().getComponent("properties", PropertiesComponent.class);
            pc.setLocation("classpath:application.properties");
            // setup kafka component with the brokers
            KafkaComponent kafka = new KafkaComponent();
            kafka.setBrokers("{{kafka.host}}:{{kafka.port}}");
            camelContext.addComponent("kafka", kafka);
            from("direct:kafkaStart").routeId("DirectToKafka").to("kafka:{{producer.topic}}").log("${headers}");
            // Topic can be set in header as well.
            from("direct:kafkaStartNoTopic").routeId("kafkaStartNoTopic").to("kafka:dummy").log("${headers}");
            // Use custom partitioner based on the key.
            from("direct:kafkaStartWithPartitioner").routeId("kafkaStartWithPartitioner").to("kafka:{{producer.topic}}?partitioner={{producer.partitioner}}").log("${headers}");
            // Takes input from the command line.
            from("stream:in").setHeader(KafkaConstants.PARTITION_KEY, simple("0")).setHeader(KafkaConstants.KEY, simple("1")).to("direct:kafkaStart");
        }
    });
    ProducerTemplate producerTemplate = camelContext.createProducerTemplate();
    camelContext.start();
    Map<String, Object> headers = new HashMap<String, Object>();
    headers.put(KafkaConstants.PARTITION_KEY, 0);
    headers.put(KafkaConstants.KEY, "1");
    producerTemplate.sendBodyAndHeaders("direct:kafkaStart", testKafkaMessage, headers);
    // Send with topicName in header
    testKafkaMessage = "TOPIC " + testKafkaMessage;
    headers.put(KafkaConstants.KEY, "2");
    headers.put(KafkaConstants.TOPIC, "TestLog");
    producerTemplate.sendBodyAndHeaders("direct:kafkaStartNoTopic", testKafkaMessage, headers);
    testKafkaMessage = "PART 0 :  " + testKafkaMessage;
    Map<String, Object> newHeader = new HashMap<String, Object>();
    // This should go to partition 0
    newHeader.put(KafkaConstants.KEY, "AB");
    producerTemplate.sendBodyAndHeaders("direct:kafkaStartWithPartitioner", testKafkaMessage, newHeader);
    testKafkaMessage = "PART 1 :  " + testKafkaMessage;
    // This should go to partition 1
    newHeader.put(KafkaConstants.KEY, "ABC");
    producerTemplate.sendBodyAndHeaders("direct:kafkaStartWithPartitioner", testKafkaMessage, newHeader);
    LOG.info("Successfully published event to Kafka.");
    System.out.println("Enter text on the line below : [Press Ctrl-C to exit.] ");
    Thread.sleep(5 * 60 * 1000);
    camelContext.stop();
}
Also used : CamelContext(org.apache.camel.CamelContext) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) ProducerTemplate(org.apache.camel.ProducerTemplate) RouteBuilder(org.apache.camel.builder.RouteBuilder) KafkaComponent(org.apache.camel.component.kafka.KafkaComponent) HashMap(java.util.HashMap) PropertiesComponent(org.apache.camel.component.properties.PropertiesComponent) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext)

Example 68 with CamelContext

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

the class CamelCdiTest method assertCamelContext.

protected CamelContext assertCamelContext(String contextName) {
    CamelContext answer = camelContexts.select(ContextName.Literal.of(contextName)).get();
    assertTrue("CamelContext '" + contextName + "' is not started", answer.getStatus().isStarted());
    return answer;
}
Also used : CamelContext(org.apache.camel.CamelContext)

Example 69 with CamelContext

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

the class CamelCdiTest method checkContextsHaveCorrectEndpointsAndRoutes.

@Test
public void checkContextsHaveCorrectEndpointsAndRoutes() throws Exception {
    assertNotNull("camelContexts not injected!", camelContexts);
    for (CamelContext camelContext : camelContexts) {
        LOG.info("CamelContext " + camelContext + " has endpoints: " + camelContext.getEndpointMap().keySet());
        camelContext.start();
    }
    CamelContext contextA = assertCamelContext("contextA");
    assertHasEndpoints(contextA, "seda://A.a", "mock://A.b");
    MockEndpoint mockEndpoint = routesA.b;
    mockEndpoint.expectedBodiesReceived(Constants.EXPECTED_BODIES_A);
    routesA.sendMessages();
    mockEndpoint.assertIsSatisfied();
    CamelContext contextB = assertCamelContext("contextB");
    assertHasEndpoints(contextB, "seda://B.a", "mock://B.b");
    MockEndpoint mockEndpointB = routesB.b;
    mockEndpointB.expectedBodiesReceived(Constants.EXPECTED_BODIES_B);
    routesB.sendMessages();
    mockEndpointB.assertIsSatisfied();
    CamelContext contextC = assertCamelContext("contextC");
    assertHasEndpoints(contextC, "seda://C.a", "mock://C.b");
    MockEndpoint mockEndpointC = routesC.b;
    mockEndpointC.expectedBodiesReceived(Constants.EXPECTED_BODIES_C);
    routesC.sendMessages();
    mockEndpointC.assertIsSatisfied();
    CamelContext contextD = assertCamelContext("contextD");
    assertHasEndpoints(contextD, "seda://D.a", "mock://D.b");
    MockEndpoint mockEndpointD = routesD.b;
    mockEndpointD.expectedBodiesReceived(Constants.EXPECTED_BODIES_D);
    routesD.sendMessages();
    mockEndpointD.assertIsSatisfied();
    CamelContext contextE = assertCamelContext("contextD");
    assertHasEndpoints(contextE, "seda://D.a", "mock://D.b");
    MockEndpoint mockDb = CamelContextHelper.getMandatoryEndpoint(contextE, "mock://D.b", MockEndpoint.class);
    mockDb.reset();
    mockDb.expectedBodiesReceived(Constants.EXPECTED_BODIES_D_A);
    for (Object body : Constants.EXPECTED_BODIES_D_A) {
        producerD.sendBody("seda:D.a", body);
    }
    mockDb.assertIsSatisfied();
}
Also used : CamelContext(org.apache.camel.CamelContext) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) Test(org.junit.Test)

Example 70 with CamelContext

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

the class CamelCoreTest method testCamelCore.

@Test
public void testCamelCore() throws Exception {
    // install the camel blueprint xml file we use in this test
    URL url = ObjectHelper.loadResourceAsURL("org/apache/camel/itest/CamelCoreTest.xml", CamelCoreTest.class.getClassLoader());
    installBlueprintAsBundle("CamelCoreTest", url, true);
    // lookup Camel from OSGi
    CamelContext camel = getOsgiService(bundleContext, CamelContext.class);
    // test camel
    MockEndpoint mock = camel.getEndpoint("mock:result", MockEndpoint.class);
    mock.expectedBodiesReceived("Hello World");
    camel.createProducerTemplate().sendBody("direct:start", "World");
    mock.assertIsSatisfied();
}
Also used : CamelContext(org.apache.camel.CamelContext) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) URL(java.net.URL) AbstractFeatureTest(org.apache.camel.test.karaf.AbstractFeatureTest) Test(org.junit.Test)

Aggregations

CamelContext (org.apache.camel.CamelContext)1478 Test (org.junit.Test)691 DefaultCamelContext (org.apache.camel.impl.DefaultCamelContext)684 RouteBuilder (org.apache.camel.builder.RouteBuilder)448 ProducerTemplate (org.apache.camel.ProducerTemplate)434 ConnectionFactory (javax.jms.ConnectionFactory)220 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)210 Exchange (org.apache.camel.Exchange)109 HashMap (java.util.HashMap)93 Endpoint (org.apache.camel.Endpoint)52 DefaultExchange (org.apache.camel.impl.DefaultExchange)50 IOException (java.io.IOException)46 Map (java.util.Map)45 SimpleRegistry (org.apache.camel.impl.SimpleRegistry)44 CountDownLatch (java.util.concurrent.CountDownLatch)42 SpringCamelContext (org.apache.camel.spring.SpringCamelContext)42 ArrayList (java.util.ArrayList)41 Processor (org.apache.camel.Processor)40 PropertiesComponent (org.apache.camel.component.properties.PropertiesComponent)37 CamelExecutionException (org.apache.camel.CamelExecutionException)33