Search in sources :

Example 81 with ProducerTemplate

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

the class SqlProducerOutputTypeSelectOneTest method testSelectOneSingleColumn.

@Test
public void testSelectOneSingleColumn() throws Exception {
    camel1.addRoutes(new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            from("direct:start").to("sql:select project from projects where id=3?outputType=SelectOne").to("mock:result");
        }
    });
    camel1.start();
    ProducerTemplate template = camel1.createProducerTemplate();
    MockEndpoint mock = (MockEndpoint) camel1.getEndpoint("mock:result");
    mock.expectedMessageCount(1);
    template.sendBody("direct:start", "testmsg");
    mock.assertIsSatisfied(2000);
    List<Exchange> exchanges = mock.getReceivedExchanges();
    Assert.assertThat(exchanges.size(), CoreMatchers.is(1));
    String result = exchanges.get(0).getIn().getBody(String.class);
    Assert.assertThat(result, CoreMatchers.is("Linux"));
}
Also used : Exchange(org.apache.camel.Exchange) ProducerTemplate(org.apache.camel.ProducerTemplate) RouteBuilder(org.apache.camel.builder.RouteBuilder) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) Test(org.junit.Test)

Example 82 with ProducerTemplate

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

the class CamelClient method main.

public static void main(final String[] args) throws Exception {
    File csvLogFile = new File(CSV_PATH);
    if (csvLogFile.exists()) {
        LOG.info("---> Removing log file '{}'...", csvLogFile.getAbsolutePath());
        csvLogFile.delete();
    }
    // START SNIPPET: e1
    LOG.info("---> Starting 'order' camel route...");
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("META-INF/spring/camel-context.xml");
    context.start();
    CamelContext camelContext = context.getBean("order", CamelContext.class);
    ProducerTemplate producer = camelContext.createProducerTemplate();
    // END SNIPPET: e1
    Thread.sleep(1000);
    Order order = new Order().setOrderId("Order-Java-0001").setItemId("MILK").setQuantity(3);
    LOG.info("---> Sending '{}' to 'direct:java'", order);
    OrderResponse response = producer.requestBody("direct:java", order, OrderResponse.class);
    Thread.sleep(1000);
    LOG.info("---> Received '{}'", response);
    LOG.info("---> CSV log now contains:{}", getCsvLog());
    Thread.sleep(1000);
    String orderXml = "<order orderId=\"Order-XML-0001\" itemId=\"MIKAN\" quantity=\"365\"/>";
    LOG.info("---> Sending '{}' to 'direct:xml'", orderXml);
    Exchange answerXml = producer.send("direct:xml", ex -> {
        ((DataTypeAware) ex.getIn()).setBody(orderXml, new DataType("xml:XMLOrder"));
    });
    Thread.sleep(1000);
    LOG.info("---> Received '{}'", answerXml.getOut().getBody(String.class));
    LOG.info("---> CSV log now contains:{}", getCsvLog());
    Thread.sleep(1000);
    String orderJson = "{\"orderId\":\"Order-JSON-0001\", \"itemId\":\"MIZUYO-KAN\", \"quantity\":\"16350\"}";
    LOG.info("---> Sending '{}' to 'direct:json'", orderJson);
    Exchange answerJson = producer.send("direct:json", ex -> {
        ((DataTypeAware) ex.getIn()).setBody(orderJson, new DataType("json"));
    });
    Thread.sleep(1000);
    LOG.info("---> Received '{}'", answerJson.getOut().getBody(String.class));
    LOG.info("---> CSV log now contains:{}", getCsvLog());
    Thread.sleep(1000);
    context.stop();
}
Also used : CamelContext(org.apache.camel.CamelContext) Order(org.apache.camel.example.transformer.demo.Order) Exchange(org.apache.camel.Exchange) ProducerTemplate(org.apache.camel.ProducerTemplate) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) DataTypeAware(org.apache.camel.spi.DataTypeAware) DataType(org.apache.camel.spi.DataType) OrderResponse(org.apache.camel.example.transformer.demo.OrderResponse) File(java.io.File)

Example 83 with ProducerTemplate

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

the class JettyFileMulticastTest method testMulticastEndpoint.

@Test
public void testMulticastEndpoint() throws Exception {
    assertNotNull(resultEndpoint);
    assertNotNull(fileEndpoint);
    resultEndpoint.reset();
    fileEndpoint.reset();
    ProducerTemplate template = camelContext.createProducerTemplate();
    template.sendBodyAndHeader("direct:start", "Hello form Willem", "Operation", "greetMe");
    // Sleep a while and wait for the message whole processing
    Thread.sleep(2000);
    template.stop();
    MockEndpoint.assertIsSatisfied(camelContext);
    List<Exchange> resultExchanges = resultEndpoint.getReceivedExchanges();
    assertEquals("Should get one message for mock endpoint", resultExchanges.size(), 1);
    String result = resultExchanges.get(0).getIn().getBody(String.class);
    assertEquals("Should get the request", "<response>Hello form Willem</response>", result);
    assertEquals("Should get the responise code", 200, resultExchanges.get(0).getIn().getHeader(Exchange.HTTP_RESPONSE_CODE));
}
Also used : Exchange(org.apache.camel.Exchange) ProducerTemplate(org.apache.camel.ProducerTemplate) Test(org.junit.Test)

Example 84 with ProducerTemplate

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

the class CamelFileGreeterOneWayTest method testFileWithOnewayOperation.

@Test
public void testFileWithOnewayOperation() throws Exception {
    deleteDirectory("target/messages/input/");
    greeterImpl.resetOneWayCounter();
    ProducerTemplate template = context.createProducerTemplate();
    template.sendBodyAndHeader("file://target/messages/input/", "Hello World", Exchange.FILE_NAME, "hello.txt");
    // Sleep a while and wait for the message whole processing
    Thread.sleep(4000);
    template.stop();
    // make sure the greeter is called
    assertEquals("The oneway operation of greeter should be called", 1, greeterImpl.getOneWayCounter());
    File file = new File("target/messages/input/hello.txt");
    assertFalse("File " + file + " should be deleted", file.exists());
}
Also used : ProducerTemplate(org.apache.camel.ProducerTemplate) File(java.io.File) Test(org.junit.Test)

Example 85 with ProducerTemplate

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

the class CamelGreeterConsumerTest method testInvokeServers.

@Test
public void testInvokeServers() throws Exception {
    assertNotNull(camelContext);
    ProducerTemplate template = camelContext.createProducerTemplate();
    List<String> params = new ArrayList<String>();
    params.add("Willem");
    Object result = template.sendBodyAndHeader("cxf://bean:serviceEndpoint", ExchangePattern.InOut, params, CxfConstants.OPERATION_NAME, "greetMe");
    assertTrue("Result is a list instance ", result instanceof List);
    assertEquals("Get the wrong response", ((List<?>) result).get(0), "HelloWillem");
    try {
        template.sendBodyAndHeader("cxf://bean:serviceEndpoint", ExchangePattern.InOut, params, CxfConstants.OPERATION_NAME, "pingMe");
        fail("Expect exception here.");
    } catch (Exception ex) {
        assertTrue("Get a wrong exception.", ex instanceof CamelExecutionException);
        assertTrue("Get a wrong exception cause. ", ex.getCause() instanceof PingMeFault);
    }
    template.stop();
}
Also used : CamelExecutionException(org.apache.camel.CamelExecutionException) ProducerTemplate(org.apache.camel.ProducerTemplate) PingMeFault(org.apache.hello_world_soap_http.PingMeFault) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) CamelExecutionException(org.apache.camel.CamelExecutionException) 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