org.apache.camel.ExchangePattern Java Examples

The following examples show how to use org.apache.camel.ExchangePattern. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SpringWsIntegrationTest.java    From wildfly-camel with Apache License 2.0 6 votes vote down vote up
@Test
public void consumeStockQuoteWebserviceInOnly() throws Exception {
    inOnlyEndpoint.expectedExchangePattern(ExchangePattern.InOnly);
    inOnlyEndpoint.expectedMessageCount(1);

    template.sendBodyAndHeader("direct:stockQuoteWebserviceInOnly", xmlRequestForGoogleStockQuote, "foo", "bar");

    inOnlyEndpoint.assertIsSatisfied();

    Message in = inOnlyEndpoint.getReceivedExchanges().get(0).getIn();

    Object result = in.getBody();
    Assert.assertNotNull(result);
    Assert.assertTrue(result instanceof String);
    String resultMessage = (String) result;
    Assert.assertTrue(resultMessage.contains("Google Inc."));

    Object bar = in.getHeader("foo");
    Assert.assertEquals("The header value should have been preserved", "bar", bar);
}
 
Example #2
Source File: CamelHandler.java    From mdw with Apache License 2.0 6 votes vote down vote up
protected Exchange prepareCamelExchange(Object request, Map<String,Object> headers) {

        // create a Camel exchange
        Exchange exchange = endpoint.createExchange();
        exchange.setPattern(ExchangePattern.InOut);

        Message inMessage = exchange.getIn();

        if (headers != null) {
            // propagate headers
            for (String key : headers.keySet())
                inMessage.setHeader(key, headers.get(key));
        }

        // set the body
        inMessage.setBody(request);

        return exchange;
    }
 
Example #3
Source File: Event2MessageProducer.java    From alfresco-repository with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public void send(String endpointUri, ExchangePattern exchangePattern, Object event, Map<String, Object> headers)
{
    try
    {
        if (!(event instanceof String))
        {
            event = this.objectMapper.writeValueAsString(event);
        }
        if (exchangePattern == null)
        {
            exchangePattern = ExchangePattern.InOnly;
        }

        this.producer.sendBodyAndHeaders(endpointUri, exchangePattern, event, this.addHeaders(headers));
    }
    catch (Exception e)
    {
        throw new AlfrescoRuntimeException(ERROR_SENDING, e);
    }
}
 
Example #4
Source File: TransactionAwareEventProducer.java    From alfresco-repository with GNU Lesser General Public License v3.0 6 votes vote down vote up
public void send(String endpointUri, ExchangePattern exchangePattern, Object event, Map<String, Object> headers)
{
    String currentTxn = AlfrescoTransactionSupport.getTransactionId();
    TransactionListener transactionListener = new TransactionListener("TxEvPr" + currentTxn);

    AlfrescoTransactionSupport.bindListener(transactionListener);
    List<PendingRequest> pendingRequests = AlfrescoTransactionSupport.getResource(POST_TRANSACTION_PENDING_REQUESTS);

    if (pendingRequests == null)
    {
        pendingRequests = new LinkedList<>();
        AlfrescoTransactionSupport.bindResource(POST_TRANSACTION_PENDING_REQUESTS, pendingRequests);
    }

    PendingRequest pendingRequest = new PendingRequest(endpointUri, exchangePattern, event, headers);
    pendingRequests.add(pendingRequest);
}
 
Example #5
Source File: AbstractEventProducer.java    From alfresco-repository with GNU Lesser General Public License v3.0 6 votes vote down vote up
public void send(String endpointUri, ExchangePattern exchangePattern, Object event, Map<String, Object> headers)
{
    try
    {
        if (StringUtils.isEmpty(endpointUri))
        {
            endpointUri = this.endpoint;
        }

        if (this.objectMapper != null && !(event instanceof String))
        {
            event = this.objectMapper.writeValueAsString(event);
        }

        if (exchangePattern == null)
        {
            exchangePattern = ExchangePattern.InOnly;
        }

        this.producer.sendBodyAndHeaders(endpointUri, exchangePattern, event, this.addHeaders(headers));
    }
    catch (Exception e)
    {
        throw new AlfrescoRuntimeException(ERROR_SENDING, e);
    }
}
 
Example #6
Source File: AuthenticationCustomizerTest.java    From syndesis with Apache License 2.0 6 votes vote down vote up
private static void assertHeaderSetTo(final ComponentProxyComponent component, final String headerName, final String headerValue) throws Exception {
    final Processor processor = component.getBeforeProducer();

    final Exchange exchange = mock(Exchange.class);
    final Message message = mock(Message.class);
    when(exchange.getIn()).thenReturn(message);
    when(exchange.getOut()).thenReturn(message);
    when(exchange.getPattern()).thenReturn(ExchangePattern.InOut);

    final ExtendedCamelContext context = mock(ExtendedCamelContext.class);
    when(exchange.getContext()).thenReturn(context);

    final AsyncProcessorAwaitManager async = mock(AsyncProcessorAwaitManager.class);
    when(context.getAsyncProcessorAwaitManager()).thenReturn(async);

    processor.process(exchange);

    verify(message).setHeader(headerName, headerValue);
}
 
Example #7
Source File: SuspendCommandRoute.java    From hacep with Apache License 2.0 6 votes vote down vote up
@Override
public void configure() throws Exception {
    from("direct:SUSPEND")
            .onException(Exception.class)
            .maximumRedeliveries(0)
            .handled(true)
            .process(exchange -> {
                Exception exception = (Exception) exchange.getProperty(Exchange.EXCEPTION_CAUGHT);
                exchange.getOut().setBody(new ResponseMessage(ResponseCode.ERROR, exception.getMessage()));
            })
            .to("direct:marshal-response")
            .end()
            .setExchangePattern(ExchangePattern.InOut)
            .bean(hacep, "suspend()", false)
            .process(exchange -> {
                ResponseMessage output = new ResponseMessage(ResponseCode.SUCCESS, "OK");
                exchange.getOut().setBody(output);
            })
            .to("direct:marshal-response");
}
 
Example #8
Source File: ResumeCommandRoute.java    From hacep with Apache License 2.0 6 votes vote down vote up
@Override
public void configure() throws Exception {
    from("direct:RESUME")
            .onException(Exception.class)
            .maximumRedeliveries(0)
            .handled(true)
            .process(exchange -> {
                Exception exception = (Exception) exchange.getProperty(Exchange.EXCEPTION_CAUGHT);
                exchange.getOut().setBody(new ResponseMessage(ResponseCode.ERROR, exception.getMessage()));
            })
            .to("direct:marshal-response")
            .end()
            .setExchangePattern(ExchangePattern.InOut)
            .bean(hacep, "resume()", false)
            .process(exchange -> {
                ResponseMessage output = new ResponseMessage(ResponseCode.SUCCESS, "OK");
                exchange.getOut().setBody(output);
            })
            .to("direct:marshal-response");
}
 
Example #9
Source File: CamelMailetProcessor.java    From james-project with Apache License 2.0 6 votes vote down vote up
@Override
public void configure() {
    String state = container.getState();
    CamelProcessor terminatingMailetProcessor = new CamelProcessor(metricFactory, container, new TerminatingMailet());

    RouteDefinition processorDef = from(container.getEndpoint())
        .routeId(state)
        .setExchangePattern(ExchangePattern.InOnly);

    for (MatcherMailetPair pair : pairs) {
        CamelProcessor mailetProccessor = new CamelProcessor(metricFactory, container, pair.getMailet());
        MatcherSplitter matcherSplitter = new MatcherSplitter(metricFactory, container, pair);

        processorDef
                // do splitting of the mail based on the stored matcher
                .split().method(matcherSplitter)
                    .aggregationStrategy(new UseLatestAggregationStrategy())
                .process(exchange -> handleMailet(exchange, container, mailetProccessor));
    }

    processorDef
        .process(exchange -> terminateSmoothly(exchange, container, terminatingMailetProcessor));

}
 
Example #10
Source File: InfoCommandRoute.java    From hacep with Apache License 2.0 6 votes vote down vote up
@Override
public void configure() throws Exception {
    from("direct:INFO")
            .onException(Exception.class)
                .maximumRedeliveries(0)
                .handled(true)
                .process(exchange -> {
                    Exception exception = (Exception) exchange.getProperty(Exchange.EXCEPTION_CAUGHT);
                    exchange.getOut().setBody(new ResponseMessage(ResponseCode.ERROR, exception.getMessage()));
                })
                .to("direct:marshal-response")
            .end()
            .setExchangePattern(ExchangePattern.InOut)
            .bean(hacep, "info()", false)
            .process(exchange -> {
                Object body = exchange.getIn().getBody();
                ResponseMessage output = new ResponseMessage(ResponseCode.SUCCESS, (String) body);
                exchange.getOut().setBody(output);
            })
            .to("direct:marshal-response");
}
 
Example #11
Source File: StatusCommandRoute.java    From hacep with Apache License 2.0 6 votes vote down vote up
@Override
public void configure() throws Exception {
    from("direct:STATUS")
            .onException(Exception.class)
                .maximumRedeliveries(0)
                .handled(true)
                .process(exchange -> {
                    Exception exception = (Exception) exchange.getProperty(Exchange.EXCEPTION_CAUGHT);
                    exchange.getOut().setBody(new ResponseMessage(ResponseCode.ERROR, exception.getMessage()));
                })
                .to("direct:marshal-response")
            .end()
            .setExchangePattern(ExchangePattern.InOut)
            .bean(hacep, "status()", false)
            .process(exchange -> {
                Object body = exchange.getIn().getBody();
                ResponseMessage output = new ResponseMessage(ResponseCode.SUCCESS, (String) body);
                exchange.getOut().setBody(output);
            })
            .to("direct:marshal-response");
}
 
Example #12
Source File: CallingInOnlyTest.java    From camel-cookbook-examples with Apache License 2.0 5 votes vote down vote up
@Test
public void testInOutMEPChangedForOneWay() throws InterruptedException {
    final String messageBody = "Camel Rocks";
    final ExchangePattern callingMEP = ExchangePattern.InOut;

    beforeOneWay.setExpectedMessageCount(1);
    // Should be calling Exchange Pattern
    beforeOneWay.message(0).exchangePattern().isEqualTo(callingMEP);

    oneWay.setExpectedMessageCount(1);
    // Should always be InOnly
    oneWay.message(0).exchangePattern().isEqualTo(ExchangePattern.InOnly);

    afterOneWay.setExpectedMessageCount(1);
    // Should be restored to calling Exchange Pattern
    afterOneWay.message(0).exchangePattern().isEqualTo(callingMEP);

    // requestBody always sets the Exchange Pattern to InOut
    String response = template.requestBody("direct:start", messageBody, String.class);

    assertEquals("Done", response);

    assertMockEndpointsSatisfied();

    Exchange oneWayExchange = oneWay.getReceivedExchanges().get(0);
    Exchange afterOneWayExchange = afterOneWay.getReceivedExchanges().get(0);

    // these are not the same exchange objects
    assertNotEquals(oneWayExchange, afterOneWayExchange);

    // the bodies should be the same - shallow copy
    assertEquals(oneWayExchange.getIn().getBody(), afterOneWayExchange.getIn().getBody());

    // the transactions are the same
    assertEquals(oneWayExchange.getUnitOfWork(), afterOneWayExchange.getUnitOfWork());
}
 
Example #13
Source File: CallingInOnlyTest.java    From camel-cookbook-examples with Apache License 2.0 5 votes vote down vote up
@Test
public void testInOnlyMEPChangedForOneWay() throws InterruptedException {
    final String messageBody = "Camel Rocks";
    final ExchangePattern callingMEP = ExchangePattern.InOnly;

    beforeOneWay.setExpectedMessageCount(1);
    // Should be calling Exchange Pattern
    beforeOneWay.message(0).exchangePattern().isEqualTo(callingMEP);

    oneWay.setExpectedMessageCount(1);
    // Should always be InOnly
    oneWay.message(0).exchangePattern().isEqualTo(ExchangePattern.InOnly);

    afterOneWay.setExpectedMessageCount(1);
    // Should be restored to calling Exchange Pattern
    afterOneWay.message(0).exchangePattern().isEqualTo(callingMEP);

    // Explicitly set Exchange Pattern
    template.sendBody("direct:start", callingMEP, messageBody);

    assertMockEndpointsSatisfied();

    Exchange oneWayExchange = oneWay.getReceivedExchanges().get(0);
    Exchange afterOneWayExchange = afterOneWay.getReceivedExchanges().get(0);

    // these are not the same exchange objects
    assertNotEquals(oneWayExchange, afterOneWayExchange);

    // the bodies should be the same - shallow copy
    assertEquals(oneWayExchange.getIn().getBody(), afterOneWayExchange.getIn().getBody());

    // the transactions are the same
    assertEquals(oneWayExchange.getUnitOfWork(), afterOneWayExchange.getUnitOfWork());
}
 
Example #14
Source File: CallingInOnlySpringTest.java    From camel-cookbook-examples with Apache License 2.0 5 votes vote down vote up
@Test
public void testInOutMEPChangedForOneWay() throws InterruptedException {
    final String messageBody = "Camel Rocks";
    final ExchangePattern callingMEP = ExchangePattern.InOut;

    beforeOneWay.setExpectedMessageCount(1);
    // Should be calling Exchange Pattern
    beforeOneWay.message(0).exchangePattern().isEqualTo(callingMEP);

    oneWay.setExpectedMessageCount(1);
    // Should always be InOnly
    oneWay.message(0).exchangePattern().isEqualTo(ExchangePattern.InOnly);

    afterOneWay.setExpectedMessageCount(1);
    // Should be restored to calling Exchange Pattern
    afterOneWay.message(0).exchangePattern().isEqualTo(callingMEP);

    // Explicitly set Exchange Pattern
    template.sendBody("direct:start", callingMEP, messageBody);

    assertMockEndpointsSatisfied();

    Exchange oneWayExchange = oneWay.getReceivedExchanges().get(0);
    Exchange afterOneWayExchange = afterOneWay.getReceivedExchanges().get(0);

    // these are not the same exchange objects
    assertNotEquals(oneWayExchange, afterOneWayExchange);

    // the bodies should be the same - shallow copy
    assertEquals(oneWayExchange.getIn().getBody(), afterOneWayExchange.getIn().getBody());

    // the transactions are the same
    assertEquals(oneWayExchange.getUnitOfWork(), afterOneWayExchange.getUnitOfWork());
}
 
Example #15
Source File: CallingInOutSpringTest.java    From camel-cookbook-examples with Apache License 2.0 5 votes vote down vote up
@Test
public void testInOutMEPChangedForModifyMessage() throws InterruptedException {
    final String messageBody = "Camel Rocks";
    final ExchangePattern callingMEP = ExchangePattern.InOut;

    beforeMessageModified.setExpectedMessageCount(1);
    beforeMessageModified.message(0).body().isEqualTo(messageBody);
    // Should always be the calling Exchange Pattern
    beforeMessageModified.message(0).exchangePattern().isEqualTo(callingMEP);

    modifyMessage.setExpectedMessageCount(1);
    modifyMessage.message(0).body().isEqualTo(messageBody);
    // Should always be InOut
    modifyMessage.message(0).exchangePattern().isEqualTo(ExchangePattern.InOut);

    afterMessageModified.setExpectedMessageCount(1);
    afterMessageModified.message(0).body().isEqualTo("[" + messageBody + "] has been modified!");
    // Should always be restored to the calling Exchange Pattern
    afterMessageModified.message(0).exchangePattern().isEqualTo(callingMEP);

    // Explicitly set the Exchange Pattern
    template.sendBody("direct:start", callingMEP, messageBody);

    assertMockEndpointsSatisfied();

    Exchange modifyMessageExchange = modifyMessage.getReceivedExchanges().get(0);
    Exchange afterMessageModifiedExchange = afterMessageModified.getReceivedExchanges().get(0);

    // these are not the same exchange objects
    assertNotEquals(modifyMessageExchange, afterMessageModifiedExchange);

    // the transactions are the same
    assertEquals(modifyMessageExchange.getUnitOfWork(), afterMessageModifiedExchange.getUnitOfWork());
}
 
Example #16
Source File: CallingInOutSpringTest.java    From camel-cookbook-examples with Apache License 2.0 5 votes vote down vote up
@Test
public void testInOnlyMEPChangedForModifyMessage() throws InterruptedException {
    final String messageBody = "Camel Rocks";
    final ExchangePattern callingMEP = ExchangePattern.InOnly;

    beforeMessageModified.setExpectedMessageCount(1);
    beforeMessageModified.message(0).body().isEqualTo(messageBody);
    // Should always be the calling Exchange Pattern
    beforeMessageModified.message(0).exchangePattern().isEqualTo(callingMEP);

    modifyMessage.setExpectedMessageCount(1);
    modifyMessage.message(0).body().isEqualTo(messageBody);
    // Should always be InOut
    modifyMessage.message(0).exchangePattern().isEqualTo(ExchangePattern.InOut);

    afterMessageModified.setExpectedMessageCount(1);
    afterMessageModified.message(0).body().isEqualTo("[" + messageBody + "] has been modified!");
    // Should always be restored to the calling Exchange Pattern
    afterMessageModified.message(0).exchangePattern().isEqualTo(callingMEP);

    // Explicitly set the Exchange Pattern
    template.sendBody("direct:start", callingMEP, messageBody);

    assertMockEndpointsSatisfied();

    Exchange modifyMessageExchange = modifyMessage.getReceivedExchanges().get(0);
    Exchange afterMessageModifiedExchange = afterMessageModified.getReceivedExchanges().get(0);

    // these are not the same exchange objects
    assertNotEquals(modifyMessageExchange, afterMessageModifiedExchange);

    // the transactions are the same
    assertEquals(modifyMessageExchange.getUnitOfWork(), afterMessageModifiedExchange.getUnitOfWork());
}
 
Example #17
Source File: ClientCamelJMSITest.java    From hawkular-apm with Apache License 2.0 5 votes vote down vote up
@Test
public void testTraceIdPropagated() {
    template.sendBody("jms:queue:inboundq", ExchangePattern.InOut, "Test Message");

    Wait.until(() -> getApmMockServer().getTraces().size() == 3);

    // Check stored traces - one btxn represents the test sender
    assertEquals(3, getApmMockServer().getTraces().size());

    // Check only one trace id used for all trace fragments
    assertEquals(1, getApmMockServer().getTraces().stream().map(t -> {
        assertNotNull(t.getTraceId());
        return t.getTraceId();
    }).distinct().count());
}
 
Example #18
Source File: CallingInOutTest.java    From camel-cookbook-examples with Apache License 2.0 5 votes vote down vote up
@Test
public void testInOnlyMEPChangedForModifyMessage() throws InterruptedException {
    final String messageBody = "Camel Rocks";
    final ExchangePattern callingMEP = ExchangePattern.InOnly;

    beforeMessageModified.setExpectedMessageCount(1);
    beforeMessageModified.message(0).body().isEqualTo(messageBody);
    // Should match calling MEP
    beforeMessageModified.message(0).exchangePattern().isEqualTo(callingMEP);

    modifyMessage.setExpectedMessageCount(1);
    modifyMessage.message(0).body().isEqualTo(messageBody);
    // Should always be InOut
    modifyMessage.message(0).exchangePattern().isEqualTo(ExchangePattern.InOut);

    afterMessageModified.setExpectedMessageCount(1);
    afterMessageModified.message(0).body().isEqualTo("[" + messageBody + "] has been modified!");
    // the exchange pattern is restored after the inOut call to the calling MEP
    afterMessageModified.message(0).exchangePattern().isEqualTo(callingMEP);

    template.sendBody("direct:start", callingMEP, messageBody);

    assertMockEndpointsSatisfied();

    Exchange modifyMessageExchange = modifyMessage.getReceivedExchanges().get(0);
    Exchange afterMessageModifiedExchange = afterMessageModified.getReceivedExchanges().get(0);

    // these are the same exchange objects, but we can't just do an object comparison as mock: copies the Exchange
    assertEquals(modifyMessageExchange.getExchangeId(), afterMessageModifiedExchange.getExchangeId());

    // the transactions are the same
    assertEquals(modifyMessageExchange.getUnitOfWork(), afterMessageModifiedExchange.getUnitOfWork());
}
 
Example #19
Source File: CallingInOutTest.java    From camel-cookbook-examples with Apache License 2.0 5 votes vote down vote up
@Test
public void testInOutMEPChangedForModifyMessage() throws InterruptedException {
    final String messageBody = "Camel Rocks";
    final ExchangePattern callingMEP = ExchangePattern.InOut;

    beforeMessageModified.setExpectedMessageCount(1);
    beforeMessageModified.message(0).body().isEqualTo(messageBody);
    // Should match calling MEP
    beforeMessageModified.message(0).exchangePattern().isEqualTo(callingMEP);

    modifyMessage.setExpectedMessageCount(1);
    modifyMessage.message(0).body().isEqualTo(messageBody);
    // Should always be InOut
    modifyMessage.message(0).exchangePattern().isEqualTo(ExchangePattern.InOut);

    afterMessageModified.setExpectedMessageCount(1);
    afterMessageModified.message(0).body().isEqualTo("[" + messageBody + "] has been modified!");
    // the exchange pattern is restored after the inOut call to the calling MEP
    afterMessageModified.message(0).exchangePattern().isEqualTo(callingMEP);

    template.sendBody("direct:start", callingMEP, messageBody);

    assertMockEndpointsSatisfied();

    Exchange modifyMessageExchange = modifyMessage.getReceivedExchanges().get(0);
    Exchange afterMessageModifiedExchange = afterMessageModified.getReceivedExchanges().get(0);

    // these are the same exchange objects, but we can't just do an object comparison as mock: copies the Exchange
    assertEquals(modifyMessageExchange.getExchangeId(), afterMessageModifiedExchange.getExchangeId());

    // the transactions are the same
    assertEquals(modifyMessageExchange.getUnitOfWork(), afterMessageModifiedExchange.getUnitOfWork());
}
 
Example #20
Source File: PropertyPlaceholderWithRouteBuilderSpringTest.java    From camel-cookbook-examples with Apache License 2.0 5 votes vote down vote up
@Test
public void testPropertiesLoaded() throws InterruptedException {
    final String messageBody = "Camel Rocks";
    final ExchangePattern callingMEP = ExchangePattern.InOnly;

    out.setExpectedMessageCount(1);
    out.message(0).body().isEqualTo("I hear you: Camel Rocks");

    // Explicitly set the Exchange Pattern
    template.sendBody("direct:in", messageBody);

    assertMockEndpointsSatisfied();
}
 
Example #21
Source File: AsyncProcessorTest.java    From camel-cookbook-examples with Apache License 2.0 5 votes vote down vote up
@Test
public void testAsyncProcessing() throws InterruptedException {
    MockEndpoint mockOut = getMockEndpoint("mock:out");
    mockOut.setExpectedMessageCount(messageCount);
    mockOut.setResultWaitTime(5000);

    for (int i = 0; i < messageCount; i++) {
        template.sendBody("seda:in", ExchangePattern.InOnly, "Message[" + i + "]");
    }

    assertMockEndpointsSatisfied();
}
 
Example #22
Source File: AsyncProcessorTest.java    From camel-cookbook-examples with Apache License 2.0 5 votes vote down vote up
@Test
public void testSyncProcessing() throws InterruptedException {
    MockEndpoint mockOut = getMockEndpoint("mock:out");
    mockOut.setExpectedMessageCount(messageCount);
    mockOut.setResultWaitTime(5000);

    for (int i = 0; i < messageCount; i++) {
        template.sendBody("direct:sync", ExchangePattern.InOnly, "Message[" + i + "]");
    }

    assertMockEndpointsSatisfied();
}
 
Example #23
Source File: AsyncProcessorSpringTest.java    From camel-cookbook-examples with Apache License 2.0 5 votes vote down vote up
@Test
public void testAsyncProcessing() throws InterruptedException {
    MockEndpoint mockOut = getMockEndpoint("mock:out");
    mockOut.setExpectedMessageCount(messageCount);
    mockOut.setResultWaitTime(5000);

    for (int i = 0; i < messageCount; i++) {
        template.sendBody("seda:in", ExchangePattern.InOnly, "Message[" + i + "]");
    }

    assertMockEndpointsSatisfied();
}
 
Example #24
Source File: CallingInOnlySpringTest.java    From camel-cookbook-examples with Apache License 2.0 5 votes vote down vote up
@Test
public void testInOnlyMEPChangedForOneWay() throws InterruptedException {
    final String messageBody = "Camel Rocks";
    final ExchangePattern callingMEP = ExchangePattern.InOnly;

    beforeOneWay.setExpectedMessageCount(1);
    // Should be calling Exchange Pattern
    beforeOneWay.message(0).exchangePattern().isEqualTo(callingMEP);

    oneWay.setExpectedMessageCount(1);
    // Should always be InOnly
    oneWay.message(0).exchangePattern().isEqualTo(ExchangePattern.InOnly);

    afterOneWay.setExpectedMessageCount(1);
    // Should be restored to calling Exchange Pattern
    afterOneWay.message(0).exchangePattern().isEqualTo(callingMEP);

    // Explicitly set Exchange Pattern
    template.sendBody("direct:start", callingMEP, messageBody);

    assertMockEndpointsSatisfied();

    Exchange oneWayExchange = oneWay.getReceivedExchanges().get(0);
    Exchange afterOneWayExchange = afterOneWay.getReceivedExchanges().get(0);

    // these are not the same exchange objects
    assertNotEquals(oneWayExchange, afterOneWayExchange);

    // the bodies should be the same - shallow copy
    assertEquals(oneWayExchange.getIn().getBody(), afterOneWayExchange.getIn().getBody());

    // the transactions are the same
    assertEquals(oneWayExchange.getUnitOfWork(), afterOneWayExchange.getUnitOfWork());
}
 
Example #25
Source File: CallingInOutViaToRoute.java    From camel-cookbook-examples with Apache License 2.0 5 votes vote down vote up
@Override
public void configure() throws Exception {
    from("direct:start")
        .to("mock:beforeMessageModified")
        .to(ExchangePattern.InOut, "direct:modifyMessage")
        .to("mock:afterMessageModified");

    from("direct:modifyMessage")
        .to("mock:modifyMessage")
        .transform(simple("[${body}] has been modified!"));
}
 
Example #26
Source File: CallingInOnlyViaToRoute.java    From camel-cookbook-examples with Apache License 2.0 5 votes vote down vote up
@Override
public void configure() throws Exception {
    from("direct:start")
        .to("mock:beforeOneWay")
        .to(ExchangePattern.InOnly, "direct:oneWay")
        .to("mock:afterOneWay")
        .transform().constant("Done");

    from("direct:oneWay")
        .to("mock:oneWay");
}
 
Example #27
Source File: AsyncProcessorSpringTest.java    From camel-cookbook-examples with Apache License 2.0 5 votes vote down vote up
@Test
public void testSyncProcessing() throws InterruptedException {
    MockEndpoint mockOut = getMockEndpoint("mock:out");
    mockOut.setExpectedMessageCount(messageCount);
    mockOut.setResultWaitTime(5000);

    for (int i = 0; i < messageCount; i++) {
        template.sendBody("direct:sync", ExchangePattern.InOnly, "Message[" + i + "]");
    }

    assertMockEndpointsSatisfied();
}
 
Example #28
Source File: ApacheCamelExample.java    From yuzhouwan with Apache License 2.0 5 votes vote down vote up
@Override
public void process(Exchange exchange) throws Exception {
    // 因为很明确消息格式是http的,所以才使用这个类
    // 否则还是建议使用org.apache.camel.Message这个抽象接口
    HttpMessage message = (HttpMessage) exchange.getIn();
    InputStream bodyStream = (InputStream) message.getBody();
    String inputContext = this.analysisMessage(bodyStream);
    bodyStream.close();

    // 存入到 exchange的 out区域
    if (exchange.getPattern() == ExchangePattern.InOut) {
        Message outMessage = exchange.getOut();
        outMessage.setBody(inputContext + " || out");
    }
}
 
Example #29
Source File: UpgradeCommandRoute.java    From hacep with Apache License 2.0 5 votes vote down vote up
@Override
public void configure() throws Exception {
    from("direct:UPGRADE")
            .onException(Exception.class)
                .maximumRedeliveries(0)
                .handled(true)
                .process(exchange -> {
                    Exception exception = (Exception) exchange.getProperty(Exchange.EXCEPTION_CAUGHT);
                    exchange.getOut().setBody(new ResponseMessage(ResponseCode.ERROR, exception.getMessage()));
                })
                .to("direct:marshal-response")
            .end()
            .setExchangePattern(ExchangePattern.InOut)
            .log(LoggingLevel.INFO, "Upgrade to version ${body.toString()}")
            .process(exchange -> {
                Command dto = exchange.getIn().getBody(Command.class);
                String value = dto.getParams().stream()
                        .filter(kv -> (kv.getKey() != null && kv.getKey().equals("RELEASE_ID")))
                        .map(KeyValueParam::getValue)
                        .findFirst().orElseThrow(() -> new IllegalArgumentException("RELEASE_ID cannot be null"));
                exchange.getOut().setBody(value);
            })
            .bean(hacep, "update(${body})")
            .process(exchange -> {
                Object body = exchange.getIn().getBody();
                ResponseMessage output = new ResponseMessage(ResponseCode.SUCCESS, "Upgraded completed to version [" + body + "]");
                exchange.getOut().setBody(output);
            })
            .log(LoggingLevel.INFO, "Upgrade complete to version ${body.toString()}")
            .to("direct:marshal-response");
}
 
Example #30
Source File: ExecuteCommandsFromJmsRoute.java    From hacep with Apache License 2.0 5 votes vote down vote up
@Override
public void configure() throws Exception {
    from("jms:" + queueName)
            .setExchangePattern(ExchangePattern.InOut)
            .to("log:it.redhat.hacep.camel.Router?level=INFO&showAll=true&multiline=true")
            .unmarshal().json(JsonLibrary.Jackson, Command.class)
            .to("direct:execute-command");

    from("direct:execute-command")
            .setExchangePattern(ExchangePattern.InOut)
            .recipientList()
            .simple("direct:${body.command}");
}