Search in sources :

Example 36 with ProducerTemplate

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

the class XmlSignatureTest method doSignatureRouteTest.

public Exchange doSignatureRouteTest(RouteBuilder builder, Exchange e, Map<String, Object> headers) throws Exception {
    CamelContext context = new DefaultCamelContext();
    try {
        context.addRoutes(builder);
        context.start();
        MockEndpoint mock = context.getEndpoint("mock:result", MockEndpoint.class);
        mock.setExpectedMessageCount(1);
        ProducerTemplate template = context.createProducerTemplate();
        if (e != null) {
            template.send("direct:in", e);
        } else {
            template.sendBodyAndHeaders("direct:in", payload, headers);
        }
        assertMockEndpointsSatisfied();
        return mock.getReceivedExchanges().get(0);
    } finally {
        context.stop();
    }
}
Also used : DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) CamelContext(org.apache.camel.CamelContext) ProducerTemplate(org.apache.camel.ProducerTemplate) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext)

Example 37 with ProducerTemplate

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

the class TestHelper method sendText.

protected void sendText(final String fragment, CamelContext context) throws Exception {
    ProducerTemplate template = context.createProducerTemplate();
    template.start();
    template.send("direct:start", new Processor() {

        public void process(Exchange exchange) throws Exception {
            // Set the property of the charset encoding
            exchange.setProperty(Exchange.CHARSET_NAME, "UTF-8");
            Message in = exchange.getIn();
            in.setBody(fragment);
            log.info("xmlFragment: {}", fragment);
        }
    });
}
Also used : Exchange(org.apache.camel.Exchange) ProducerTemplate(org.apache.camel.ProducerTemplate) Processor(org.apache.camel.Processor) Message(org.apache.camel.Message) XMLEncryptionException(org.apache.xml.security.encryption.XMLEncryptionException)

Example 38 with ProducerTemplate

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

the class CamelJmsToFileExample method main.

public static void main(String[] args) throws Exception {
    // START SNIPPET: e1
    CamelContext context = new DefaultCamelContext();
    // END SNIPPET: e1
    // Set up the ActiveMQ JMS Components
    // START SNIPPET: e2
    ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("vm://localhost?broker.persistent=false");
    // Note we can explicit name the component
    context.addComponent("test-jms", JmsComponent.jmsComponentAutoAcknowledge(connectionFactory));
    // END SNIPPET: e2
    // Add some configuration by hand ...
    // START SNIPPET: e3
    context.addRoutes(new RouteBuilder() {

        public void configure() {
            from("test-jms:queue:test.queue").to("file://test");
        }
    });
    // END SNIPPET: e3
    // Camel template - a handy class for kicking off exchanges
    // START SNIPPET: e4
    ProducerTemplate template = context.createProducerTemplate();
    // END SNIPPET: e4
    // Now everything is set up - lets start the context
    context.start();
    // START SNIPPET: e5
    for (int i = 0; i < 10; i++) {
        template.sendBody("test-jms:queue:test.queue", "Test Message: " + i);
    }
    // END SNIPPET: e5
    // wait a bit and then stop
    Thread.sleep(1000);
    context.stop();
}
Also used : CamelContext(org.apache.camel.CamelContext) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) ActiveMQConnectionFactory(org.apache.activemq.ActiveMQConnectionFactory) ProducerTemplate(org.apache.camel.ProducerTemplate) ConnectionFactory(javax.jms.ConnectionFactory) ActiveMQConnectionFactory(org.apache.activemq.ActiveMQConnectionFactory) RouteBuilder(org.apache.camel.builder.RouteBuilder) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext)

Example 39 with ProducerTemplate

use of org.apache.camel.ProducerTemplate 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 40 with ProducerTemplate

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

the class CxfEndpointBeansTest method testCxfBusConfiguration.

@Test
public void testCxfBusConfiguration() throws Exception {
    // get the camelContext from application context
    ProducerTemplate template = context.createProducerTemplate();
    Exchange reply = template.request("cxf:bean:serviceEndpoint", new Processor() {

        public void process(final Exchange exchange) {
            final List<String> params = new ArrayList<String>();
            params.add("hello");
            exchange.getIn().setBody(params);
            exchange.getIn().setHeader(CxfConstants.OPERATION_NAME, "echo");
        }
    });
    Exception ex = reply.getException();
    assertTrue("Should get the fault here", ex instanceof org.apache.cxf.interceptor.Fault || ex instanceof HTTPException);
}
Also used : Exchange(org.apache.camel.Exchange) ProducerTemplate(org.apache.camel.ProducerTemplate) Processor(org.apache.camel.Processor) HTTPException(org.apache.cxf.transport.http.HTTPException) ArrayList(java.util.ArrayList) List(java.util.List) HTTPException(org.apache.cxf.transport.http.HTTPException) Test(org.junit.Test)

Aggregations

ProducerTemplate (org.apache.camel.ProducerTemplate)130 Test (org.junit.Test)58 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)53 CamelContext (org.apache.camel.CamelContext)48 Exchange (org.apache.camel.Exchange)36 DefaultCamelContext (org.apache.camel.impl.DefaultCamelContext)27 Deployment (org.activiti.engine.test.Deployment)16 RouteBuilder (org.apache.camel.builder.RouteBuilder)16 Processor (org.apache.camel.Processor)8 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)8 HashMap (java.util.HashMap)7 Task (org.activiti.engine.task.Task)7 Endpoint (org.apache.camel.Endpoint)7 File (java.io.File)6 ArrayList (java.util.ArrayList)5 Message (org.apache.camel.Message)4 List (java.util.List)3 ExecutorService (java.util.concurrent.ExecutorService)3 HistoricVariableInstance (org.activiti.engine.history.HistoricVariableInstance)3 ConsumerTemplate (org.apache.camel.ConsumerTemplate)3