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"));
}
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();
}
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));
}
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());
}
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();
}
Aggregations