Я пытаюсь изменить сообщение OUT
для издевательства продюсера через whenAnyExchangeReceived
.Но мне кажется, что я не получаю доступ к существующему обмену внутри маршрута, а получаю новый обмен вместо этого.Как изменить сообщение OUT и вернуть измененное сообщение исходному потребителю маршрута.
MockEndpoint vmMock = getMockEndpoint("mock:vm:client-search");
vmMock .whenAnyExchangeReceived(exchange -> {
log.info(" *** Exchange arrived to MOCK ");
log.info("Exchange Mock Exchange", exchange);
log.info("Exchange Mock Exchange ID", exchange.getExchangeId());
log.info("Exchange Mock Route ID", exchange.getFromRouteId());
log.info("Exchange Mock Properties", exchange.getProperties());
log.info("Exchange Mock Body", exchange.getIn().getBody(SearchRequest.class));
exchange.getIn().setBody("MOCK-OK-IN", String.class);
exchange.getOut().setBody("MOCK-OK", String.class);
});
То же поведение с перехватчиком
interceptSendToEndpoint("vm:companies-search")
.skipSendToOriginalEndpoint()
.process(exchange -> {
// trying to access exchange and set body in the same way as in example above - but failing as
});
Обновление: тестовый пример от Camel в действии 2 (отлично) книга
public class MirandaTest extends CamelTestSupport {
Logger LOG = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
@Override
protected RouteBuilder createRouteBuilder() throws Exception {
return new RouteBuilder() {
@Override
public void configure() throws Exception {
from("jetty://http://localhost:9080/service/order")
.process(new OrderQueryProcessor())
.to("mock:miranda");
}
};
}
@Test
public void testMiranda() throws Exception {
context.setTracing(true);
MockEndpoint mock = getMockEndpoint("mock:miranda");
mock.expectedBodiesReceived("ID=123");
mock.whenAnyExchangeReceived(exchange -> {
// No previous exchange information here
log.info(" *** Exchange arrived to MOCK ");
log.info("Exchange Mock Exchange", exchange);
log.info("Exchange Mock Exchange ID", exchange.getExchangeId());
log.info("Exchange Mock Route ID", exchange.getFromRouteId());
log.info("Exchange Mock Properties", exchange.getProperties());
log.info("Exchange Body", exchange.getIn(String.class));
exchange.getIn().setBody("MOCK-OK-IN", String.class);
});
String out = template.requestBody("http4://localhost:9080/service/order?id=123", null, String.class);
assertEquals("MOCK-OK-IN", out);
assertMockEndpointsSatisfied();
}
private class OrderQueryProcessor implements Processor {
public void process(Exchange exchange) throws Exception {
String id = exchange.getIn().getHeader("id", String.class);
exchange.getIn().setBody("ID=" + id);
}
}
}