Search in sources :

Example 1 with KafkaComponent

use of org.apache.camel.component.kafka.KafkaComponent 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 2 with KafkaComponent

use of org.apache.camel.component.kafka.KafkaComponent in project camel by apache.

the class KafkaComponentAutoConfiguration method configureKafkaComponent.

@Lazy
@Bean(name = "kafka-component")
@ConditionalOnClass(CamelContext.class)
@ConditionalOnMissingBean(KafkaComponent.class)
public KafkaComponent configureKafkaComponent(CamelContext camelContext, KafkaComponentConfiguration configuration) throws Exception {
    KafkaComponent component = new KafkaComponent();
    component.setCamelContext(camelContext);
    Map<String, Object> parameters = new HashMap<>();
    IntrospectionSupport.getProperties(configuration, parameters, null, false);
    for (Map.Entry<String, Object> entry : parameters.entrySet()) {
        Object value = entry.getValue();
        Class<?> paramClass = value.getClass();
        if (paramClass.getName().endsWith("NestedConfiguration")) {
            Class nestedClass = null;
            try {
                nestedClass = (Class) paramClass.getDeclaredField("CAMEL_NESTED_CLASS").get(null);
                HashMap<String, Object> nestedParameters = new HashMap<>();
                IntrospectionSupport.getProperties(value, nestedParameters, null, false);
                Object nestedProperty = nestedClass.newInstance();
                IntrospectionSupport.setProperties(camelContext, camelContext.getTypeConverter(), nestedProperty, nestedParameters);
                entry.setValue(nestedProperty);
            } catch (NoSuchFieldException e) {
            }
        }
    }
    IntrospectionSupport.setProperties(camelContext, camelContext.getTypeConverter(), component, parameters);
    return component;
}
Also used : KafkaComponent(org.apache.camel.component.kafka.KafkaComponent) HashMap(java.util.HashMap) ConditionalOnClass(org.springframework.boot.autoconfigure.condition.ConditionalOnClass) HashMap(java.util.HashMap) Map(java.util.Map) Lazy(org.springframework.context.annotation.Lazy) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) ConditionalOnClass(org.springframework.boot.autoconfigure.condition.ConditionalOnClass) ConditionalOnBean(org.springframework.boot.autoconfigure.condition.ConditionalOnBean) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) Bean(org.springframework.context.annotation.Bean)

Aggregations

HashMap (java.util.HashMap)2 KafkaComponent (org.apache.camel.component.kafka.KafkaComponent)2 Map (java.util.Map)1 CamelContext (org.apache.camel.CamelContext)1 ProducerTemplate (org.apache.camel.ProducerTemplate)1 RouteBuilder (org.apache.camel.builder.RouteBuilder)1 PropertiesComponent (org.apache.camel.component.properties.PropertiesComponent)1 DefaultCamelContext (org.apache.camel.impl.DefaultCamelContext)1 ConditionalOnBean (org.springframework.boot.autoconfigure.condition.ConditionalOnBean)1 ConditionalOnClass (org.springframework.boot.autoconfigure.condition.ConditionalOnClass)1 ConditionalOnMissingBean (org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean)1 Bean (org.springframework.context.annotation.Bean)1 Lazy (org.springframework.context.annotation.Lazy)1