Search in sources :

Example 31 with PropertiesComponent

use of org.apache.camel.component.properties.PropertiesComponent 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 32 with PropertiesComponent

use of org.apache.camel.component.properties.PropertiesComponent 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 33 with PropertiesComponent

use of org.apache.camel.component.properties.PropertiesComponent in project camel by apache.

the class MyFtpClientRouteBuilder method configure.

@Override
public void configure() throws Exception {
    // configure properties component
    PropertiesComponent pc = getContext().getComponent("properties", PropertiesComponent.class);
    pc.setLocation("classpath:ftp.properties");
    // lets shutdown faster in case of in-flight messages stack up
    getContext().getShutdownStrategy().setTimeout(10);
    from("file:target/upload?moveFailed=../error").log("Uploading file ${file:name}").to("{{ftp.client}}").log("Uploaded file ${file:name} complete.");
    // use system out so it stand out
    System.out.println("*********************************************************************************");
    System.out.println("Camel will route files from target/upload directory to the FTP server: " + getContext().resolvePropertyPlaceholders("{{ftp.server}}"));
    System.out.println("You can configure the location of the ftp server in the src/main/resources/ftp.properties file.");
    System.out.println("If the file upload fails, then the file is moved to the target/error directory.");
    System.out.println("Use ctrl + c to stop this application.");
    System.out.println("*********************************************************************************");
}
Also used : PropertiesComponent(org.apache.camel.component.properties.PropertiesComponent)

Example 34 with PropertiesComponent

use of org.apache.camel.component.properties.PropertiesComponent in project camel by apache.

the class Application method properties.

@Produces
@ApplicationScoped
@Named("properties")
// "properties" component bean that Camel uses to lookup properties
PropertiesComponent properties(PropertiesParser parser) {
    PropertiesComponent component = new PropertiesComponent();
    // Use DeltaSpike as configuration source for Camel CDI
    component.setPropertiesParser(parser);
    return component;
}
Also used : PropertiesComponent(org.apache.camel.component.properties.PropertiesComponent) Named(javax.inject.Named) Produces(javax.enterprise.inject.Produces) ApplicationScoped(javax.enterprise.context.ApplicationScoped)

Example 35 with PropertiesComponent

use of org.apache.camel.component.properties.PropertiesComponent in project camel by apache.

the class Application method properties.

@Produces
@ApplicationScoped
@Named("properties")
// "properties" component bean that Camel uses to lookup properties
PropertiesComponent properties() {
    PropertiesComponent component = new PropertiesComponent();
    component.setLocation("classpath:application.properties");
    return component;
}
Also used : PropertiesComponent(org.apache.camel.component.properties.PropertiesComponent) Named(javax.inject.Named) Produces(javax.enterprise.inject.Produces) ApplicationScoped(javax.enterprise.context.ApplicationScoped)

Aggregations

PropertiesComponent (org.apache.camel.component.properties.PropertiesComponent)83 CamelContext (org.apache.camel.CamelContext)35 Properties (java.util.Properties)17 ApplicationScoped (javax.enterprise.context.ApplicationScoped)12 Produces (javax.enterprise.inject.Produces)12 Named (javax.inject.Named)12 RouteBuilder (org.apache.camel.builder.RouteBuilder)11 DefaultCamelContext (org.apache.camel.impl.DefaultCamelContext)8 PropertiesLocation (org.apache.camel.component.properties.PropertiesLocation)7 SpringCamelContext (org.apache.camel.spring.SpringCamelContext)6 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)5 Test (org.junit.Test)5 HashMap (java.util.HashMap)3 Component (org.apache.camel.Component)3 AnnotationConfigApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext)3 Method (java.lang.reflect.Method)2 ArrayList (java.util.ArrayList)2 LinkedList (java.util.LinkedList)2 Map (java.util.Map)2 Exchange (org.apache.camel.Exchange)2