use of org.apache.camel.FluentProducerTemplate in project camel by apache.
the class CdiCamelFactory method fluentProducerTemplateFromUri.
private static FluentProducerTemplate fluentProducerTemplateFromUri(InjectionPoint ip, @Any Instance<CamelContext> instance, CdiCamelExtension extension, Uri uri) {
try {
CamelContext context = uri.context().isEmpty() ? selectContext(ip, instance, extension) : selectContext(uri.context(), instance);
FluentProducerTemplate producerTemplate = context.createFluentProducerTemplate();
Endpoint endpoint = context.getEndpoint(uri.value(), Endpoint.class);
producerTemplate.setDefaultEndpoint(endpoint);
return producerTemplate;
} catch (Exception cause) {
throw new InjectionException("Error injecting fluent producer template annotated with " + uri + " into " + ip, cause);
}
}
use of org.apache.camel.FluentProducerTemplate in project camel by apache.
the class FluentProducerTemplateTest method testAsyncSend.
public void testAsyncSend() throws Exception {
MockEndpoint mock = getMockEndpoint("mock:async");
mock.expectedMessageCount(2);
FluentProducerTemplate fluent = context.createFluentProducerTemplate();
Future<Exchange> future1 = fluent.to("direct:async").withHeader("action", "action-1").withBody("body-1").asyncSend();
Future<Exchange> future2 = fluent.to("direct:async").withHeader("action", "action-2").withBody("body-2").asyncSend();
Exchange exchange1 = future1.get();
Exchange exchange2 = future2.get();
assertEquals("action-1", exchange1.getIn().getHeader("action", String.class));
assertEquals("body-1", exchange1.getIn().getBody(String.class));
assertEquals("action-2", exchange2.getIn().getHeader("action", String.class));
assertEquals("body-2", exchange2.getIn().getBody(String.class));
}
use of org.apache.camel.FluentProducerTemplate in project camel by apache.
the class FluentProducerTemplateTest method testRequestBody.
public void testRequestBody() throws Exception {
// with endpoint as string uri
FluentProducerTemplate template = DefaultFluentProducerTemplate.on(context);
final Integer expectedResult = new Integer(123);
assertEquals(expectedResult, template.clearBody().clearHeaders().withBody("Hello").to("direct:inout").request(Integer.class));
assertEquals(expectedResult, template.clearBody().clearHeaders().withHeader("foo", "bar").withBody("Hello").to("direct:inout").request(Integer.class));
assertEquals(expectedResult, template.clearBody().clearHeaders().withBody("Hello").to("direct:inout").request(Integer.class));
assertEquals(expectedResult, template.clearBody().clearHeaders().withBody("Hello").to(context.getEndpoint("direct:inout")).request(Integer.class));
assertEquals(expectedResult, template.clearBody().clearHeaders().withHeader("foo", "bar").withBody("Hello").to(context.getEndpoint("direct:inout")).request(Integer.class));
assertEquals(expectedResult, template.clearBody().clearHeaders().withBody("Hello").to(context.getEndpoint("direct:inout")).request(Integer.class));
}
use of org.apache.camel.FluentProducerTemplate in project camel by apache.
the class FluentProducerTemplateTest method testAsyncRequest.
public void testAsyncRequest() throws Exception {
MockEndpoint mock = getMockEndpoint("mock:async");
mock.expectedMessageCount(2);
mock.expectedHeaderValuesReceivedInAnyOrder("action", "action-1", "action-2");
mock.expectedBodiesReceivedInAnyOrder("body-1", "body-2");
FluentProducerTemplate fluent = context.createFluentProducerTemplate();
Future<String> future1 = fluent.to("direct:async").withHeader("action", "action-1").withBody("body-1").asyncRequest(String.class);
Future<String> future2 = fluent.to("direct:async").withHeader("action", "action-2").withBody("body-2").asyncRequest(String.class);
String result1 = future1.get();
String result2 = future2.get();
mock.assertIsSatisfied();
assertEquals("body-1", result1);
assertEquals("body-2", result2);
String action = mock.getExchanges().get(0).getIn().getHeader("action", String.class);
if (action.equals("action-1")) {
assertEquals("body-1", mock.getExchanges().get(0).getIn().getBody(String.class));
}
if (action.equals("action-2")) {
assertEquals("body-2", mock.getExchanges().get(0).getIn().getBody(String.class));
}
}
use of org.apache.camel.FluentProducerTemplate in project camel by apache.
the class FluentProducerTemplateTest method testDefaultEndpoint.
public void testDefaultEndpoint() throws Exception {
MockEndpoint mock = getMockEndpoint("mock:result");
mock.expectedBodiesReceived("Bye World");
FluentProducerTemplate fluent = context.createFluentProducerTemplate();
fluent.setDefaultEndpointUri("direct:in");
Object result = fluent.withBody("Hello World").request();
assertMockEndpointsSatisfied();
assertEquals("Bye World", result);
assertSame(context, fluent.getCamelContext());
}
Aggregations