Search in sources :

Example 1 with FluentProducerTemplate

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);
    }
}
Also used : CamelContext(org.apache.camel.CamelContext) InjectionException(javax.enterprise.inject.InjectionException) Endpoint(org.apache.camel.Endpoint) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) FluentProducerTemplate(org.apache.camel.FluentProducerTemplate) UnsatisfiedResolutionException(javax.enterprise.inject.UnsatisfiedResolutionException) InjectionException(javax.enterprise.inject.InjectionException)

Example 2 with FluentProducerTemplate

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));
}
Also used : Exchange(org.apache.camel.Exchange) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) FluentProducerTemplate(org.apache.camel.FluentProducerTemplate)

Example 3 with FluentProducerTemplate

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));
}
Also used : FluentProducerTemplate(org.apache.camel.FluentProducerTemplate)

Example 4 with FluentProducerTemplate

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));
    }
}
Also used : MockEndpoint(org.apache.camel.component.mock.MockEndpoint) FluentProducerTemplate(org.apache.camel.FluentProducerTemplate)

Example 5 with FluentProducerTemplate

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());
}
Also used : MockEndpoint(org.apache.camel.component.mock.MockEndpoint) FluentProducerTemplate(org.apache.camel.FluentProducerTemplate)

Aggregations

FluentProducerTemplate (org.apache.camel.FluentProducerTemplate)9 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)5 CamelContext (org.apache.camel.CamelContext)2 Endpoint (org.apache.camel.Endpoint)2 InjectionException (javax.enterprise.inject.InjectionException)1 UnsatisfiedResolutionException (javax.enterprise.inject.UnsatisfiedResolutionException)1 Exchange (org.apache.camel.Exchange)1 NoSuchBeanException (org.apache.camel.NoSuchBeanException)1 ProxyInstantiationException (org.apache.camel.ProxyInstantiationException)1 DefaultFluentProducerTemplate (org.apache.camel.builder.DefaultFluentProducerTemplate)1 Test (org.junit.Test)1