use of org.apache.camel.ProducerTemplate in project camel by apache.
the class DisruptorConcurrentTest method testDisruptorConcurrentInOutWithAsync.
@Test
public void testDisruptorConcurrentInOutWithAsync() throws Exception {
final MockEndpoint mock = getMockEndpoint("mock:result");
mock.expectedMessageCount(20);
mock.allMessages().body().startsWith("Bye");
// should at least take 3 sec
mock.setMinimumResultWaitTime(3000);
// use our own template that has a higher thread pool than default camel that uses 5
final ExecutorService executor = Executors.newFixedThreadPool(10);
final ProducerTemplate pt = new DefaultProducerTemplate(context, executor);
// must start the template
pt.start();
final List<Future<Object>> replies = new ArrayList<Future<Object>>(20);
for (int i = 0; i < 20; i++) {
final Future<Object> out = pt.asyncRequestBody("disruptor:bar", "Message " + i);
replies.add(out);
}
assertMockEndpointsSatisfied();
assertEquals(20, replies.size());
for (int i = 0; i < 20; i++) {
final String out = (String) replies.get(i).get();
assertTrue(out.startsWith("Bye"));
}
pt.stop();
executor.shutdownNow();
}
use of org.apache.camel.ProducerTemplate in project camel by apache.
the class DozerBeanMappingTest method testBeanMapping.
@Test
public void testBeanMapping() throws Exception {
CamelContext context = new DefaultCamelContext();
context.addRoutes(new RouteBuilder() {
@Override
public void configure() throws Exception {
from("direct:start").convertBodyTo(CustomerB.class);
}
});
DozerBeanMapperConfiguration mconfig = new DozerBeanMapperConfiguration();
mconfig.setMappingFiles(Arrays.asList("bean-to-bean-dozer-mappings.xml"));
new DozerTypeConverterLoader(context, mconfig);
CustomerA customerA = new CustomerA("Peter", "Post", "SomeStreet", "12345");
context.start();
try {
ProducerTemplate producer = context.createProducerTemplate();
CustomerB result = producer.requestBody("direct:start", customerA, CustomerB.class);
Assert.assertEquals(customerA.getFirstName(), result.getFirstName());
Assert.assertEquals(customerA.getLastName(), result.getLastName());
Assert.assertEquals(customerA.getStreet(), result.getAddress().getStreet());
Assert.assertEquals(customerA.getZip(), result.getAddress().getZip());
} finally {
context.stop();
}
}
use of org.apache.camel.ProducerTemplate in project camel by apache.
the class DozerBeanMappingTest method testMarshalViaDozer.
@Test
public void testMarshalViaDozer() throws Exception {
CamelContext context = new DefaultCamelContext();
context.addRoutes(new RouteBuilder() {
@Override
public void configure() throws Exception {
from("direct:start").convertBodyTo(HashMap.class);
}
});
DozerBeanMapperConfiguration mconfig = new DozerBeanMapperConfiguration();
mconfig.setMappingFiles(Arrays.asList("bean-to-map-dozer-mappings.xml"));
new DozerTypeConverterLoader(context, mconfig);
context.start();
try {
ProducerTemplate producer = context.createProducerTemplate();
Map<?, ?> result = producer.requestBody("direct:start", new Customer("John", "Doe", null), Map.class);
Assert.assertEquals("John", result.get("firstName"));
Assert.assertEquals("Doe", result.get("lastName"));
} finally {
context.stop();
}
}
use of org.apache.camel.ProducerTemplate in project camel by apache.
the class ExecJava8IssueTest method test.
@Test
public void test() throws Exception {
if (!OS.isFamilyUnix()) {
System.err.println("The test 'CamelExecTest' does not support the following OS : " + System.getProperty("os.name"));
return;
}
String tempFilePath = tempDir.getAbsolutePath() + "/" + tempFileName;
final File script = File.createTempFile("script", ".sh", tempDir);
writeScript(script);
final String exec = "bash?args=" + script.getAbsolutePath() + " " + tempFilePath + "&outFile=" + tempFilePath;
DefaultCamelContext context = new DefaultCamelContext();
context.addRoutes(new RouteBuilder() {
@Override
public void configure() throws Exception {
from("direct:source").to("file:" + tempDir.getAbsolutePath() + "?fileName=" + tempFileName).to("exec:" + exec).process(new Processor() {
@Override
public void process(Exchange exchange) throws Exception {
String output = exchange.getIn().getBody(String.class);
assertEquals("hello world\n", output);
}
});
}
});
context.start();
ProducerTemplate pt = context.createProducerTemplate();
String payload = "hello";
pt.sendBody("direct:source", payload);
}
use of org.apache.camel.ProducerTemplate in project camel by apache.
the class StopRouteFromRouteTest method testStopRouteFromRoute.
// START SNIPPET: e1
public void testStopRouteFromRoute() throws Exception {
// create camel, add routes, and start camel
CamelContext context = new DefaultCamelContext();
context.addRoutes(createMyRoutes());
context.start();
assertTrue("Route myRoute should be started", context.getRouteStatus("myRoute").isStarted());
assertTrue("Route bar should be started", context.getRouteStatus("bar").isStarted());
// setup mock expectations for unit test
MockEndpoint start = context.getEndpoint("mock:start", MockEndpoint.class);
start.expectedMessageCount(1);
MockEndpoint done = context.getEndpoint("mock:done", MockEndpoint.class);
done.expectedMessageCount(1);
// send a message to the route
ProducerTemplate template = context.createProducerTemplate();
template.sendBody("direct:start", "Hello Camel");
// just wait a bit for the thread to stop the route
Thread.sleep(1500);
// the route should now be stopped
assertTrue("Route myRoute should be stopped", context.getRouteStatus("myRoute").isStopped());
assertTrue("Route bar should be started", context.getRouteStatus("bar").isStarted());
// stop camel
context.stop();
// unit test assertions
start.assertIsSatisfied();
done.assertIsSatisfied();
}
Aggregations