Java Code Examples for org.apache.camel.ExchangePattern#InOut

The following examples show how to use org.apache.camel.ExchangePattern#InOut . 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: 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 2
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 3
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 4
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 5
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());
}