use of org.apache.camel.support.SynchronizationAdapter in project camel by apache.
the class OnCompletionContainsTest method createRouteBuilder.
@Override
protected RouteBuilder createRouteBuilder() throws Exception {
return new RouteBuilder() {
@Override
public void configure() throws Exception {
onCompletion().to("mock:sync");
from("direct:start").process(new Processor() {
public void process(Exchange exchange) throws Exception {
SynchronizationAdapter adapter = new SimpleSynchronizationAdapter("mock:sync", "A");
exchange.addOnCompletion(adapter);
// should not add the adapter again as we already have it
if (!exchange.containsOnCompletion(adapter)) {
exchange.addOnCompletion(adapter);
}
adapter = new SimpleSynchronizationAdapter("mock:sync", "B");
exchange.addOnCompletion(adapter);
// now add the B again as we want to test that this also work
if (exchange.containsOnCompletion(adapter)) {
exchange.addOnCompletion(adapter);
}
// add a C that is no a SimpleSynchronizationAdapter class
exchange.addOnCompletion(new SynchronizationAdapter() {
@Override
public void onDone(Exchange exchange) {
template.sendBody("mock:sync", "C");
}
@Override
public String toString() {
return "C";
}
});
}
}).to("mock:result");
}
};
}
use of org.apache.camel.support.SynchronizationAdapter in project camel by apache.
the class OnCompletionShouldBeLastTest method createRouteBuilder.
@Override
protected RouteBuilder createRouteBuilder() throws Exception {
return new RouteBuilder() {
@Override
public void configure() throws Exception {
onCompletion().to("mock:sync");
from("direct:start").process(new Processor() {
public void process(Exchange exchange) throws Exception {
exchange.addOnCompletion(new SynchronizationAdapter() {
@Override
public void onDone(Exchange exchange) {
template.sendBody("mock:sync", "A");
}
@Override
public String toString() {
return "A";
}
});
exchange.addOnCompletion(new SynchronizationAdapter() {
@Override
public void onDone(Exchange exchange) {
template.sendBody("mock:sync", "B");
}
@Override
public String toString() {
return "B";
}
});
exchange.addOnCompletion(new SynchronizationAdapter() {
@Override
public void onDone(Exchange exchange) {
template.sendBody("mock:sync", "C");
}
@Override
public String toString() {
return "C";
}
});
}
}).to("mock:result");
}
};
}
use of org.apache.camel.support.SynchronizationAdapter in project camel by apache.
the class UnitOfWorkSynchronizationAdapterTest method createRouteBuilder.
protected RouteBuilder createRouteBuilder() {
return new RouteBuilder() {
public void configure() {
from("seda:async").to("direct:foo");
from("direct:foo").process(new Processor() {
public void process(Exchange exchange) throws Exception {
log.info("Received: " + exchange);
exchange.getUnitOfWork().addSynchronization(new SynchronizationAdapter() {
@Override
public void onComplete(Exchange exchange) {
completed = exchange;
foo = exchange.getIn().getHeader("foo");
doneLatch.countDown();
}
});
exchange.getUnitOfWork().addSynchronization(new SynchronizationAdapter() {
@Override
public void onFailure(Exchange exchange) {
failed = exchange;
baz = exchange.getIn().getHeader("baz");
doneLatch.countDown();
}
});
String name = getName();
if (name.equals("testFail")) {
log.info("Failing test!");
exchange.getOut().setFault(true);
exchange.getOut().setBody("testFail() should always fail with a fault!");
} else if (name.equals("testException")) {
log.info("Throwing exception!");
throw new Exception("Failing test!");
}
}
});
}
};
}
use of org.apache.camel.support.SynchronizationAdapter in project camel by apache.
the class DefaultProducerTemplateAsyncTest method testAsyncCallbackBodyInOnlyGetResult.
public void testAsyncCallbackBodyInOnlyGetResult() throws Exception {
ORDER.set(0);
getMockEndpoint("mock:result").expectedBodiesReceived("Hello World");
Future<Object> future = template.asyncCallbackSendBody("direct:start", "Hello", new SynchronizationAdapter() {
@Override
public void onDone(Exchange exchange) {
assertEquals("Hello World", exchange.getIn().getBody());
ORDER.addAndGet(2);
}
});
ORDER.addAndGet(1);
Object reply = future.get(10, TimeUnit.SECONDS);
ORDER.addAndGet(4);
assertMockEndpointsSatisfied();
assertEquals(7, ORDER.get());
// no reply when in only
assertEquals(null, reply);
}
use of org.apache.camel.support.SynchronizationAdapter in project camel by apache.
the class DefaultProducerTemplateAsyncTest method testAsyncCallbackInOnlyProcessor.
public void testAsyncCallbackInOnlyProcessor() throws Exception {
ORDER.set(0);
getMockEndpoint("mock:result").expectedBodiesReceived("Hello World");
final CountDownLatch latch = new CountDownLatch(1);
template.asyncCallback("direct:start", new Processor() {
public void process(Exchange exchange) throws Exception {
exchange.getIn().setBody("Hello");
}
}, new SynchronizationAdapter() {
@Override
public void onDone(Exchange exchange) {
assertEquals("Hello World", exchange.getIn().getBody());
ORDER.addAndGet(2);
latch.countDown();
}
});
ORDER.addAndGet(1);
assertTrue(latch.await(10, TimeUnit.SECONDS));
ORDER.addAndGet(4);
assertMockEndpointsSatisfied();
assertEquals(7, ORDER.get());
}
Aggregations