use of java.util.concurrent.CountDownLatch in project camel by apache.
the class XPathTest method testXPathSplitConcurrent.
public void testXPathSplitConcurrent() throws Exception {
int size = 100;
final Object node = XPathBuilder.xpath("foo/bar").nodeResult().evaluate(createExchange("<foo><bar>cheese</bar><bar>cake</bar><bar>beer</bar></foo>"));
assertNotNull(node);
// convert the node concurrently to test that XML Parser is not thread safe when
// importing nodes to a new Document, so try a test for that
final List<Document> result = new ArrayList<Document>();
ExecutorService executor = Executors.newFixedThreadPool(size);
final CountDownLatch latch = new CountDownLatch(size);
for (int i = 0; i < size; i++) {
executor.submit(new Callable<Document>() {
public Document call() throws Exception {
try {
Document doc = context.getTypeConverter().convertTo(Document.class, node);
result.add(doc);
return doc;
} finally {
latch.countDown();
}
}
});
}
// give time to convert concurrently
assertTrue(latch.await(20, TimeUnit.SECONDS));
Iterator<Document> it = result.iterator();
int count = 0;
while (it.hasNext()) {
count++;
Document doc = it.next();
assertNotNull(doc);
}
assertEquals(size, count);
executor.shutdownNow();
}
use of java.util.concurrent.CountDownLatch in project camel by apache.
the class TestClient method reset.
public void reset(int count) {
latch = new CountDownLatch(count);
received.clear();
}
use of java.util.concurrent.CountDownLatch in project camel by apache.
the class AsyncProcessorHelper method process.
/**
* Calls the async version of the processor's process method and waits
* for it to complete before returning. This can be used by {@link AsyncProcessor}
* objects to implement their sync version of the process method.
* <p/>
* <b>Important:</b> This method is discouraged to be used, as its better to invoke the asynchronous
* {@link AsyncProcessor#process(org.apache.camel.Exchange, org.apache.camel.AsyncCallback)} method, whenever possible.
*
* @param processor the processor
* @param exchange the exchange
* @throws Exception can be thrown if waiting is interrupted
*/
public static void process(final AsyncProcessor processor, final Exchange exchange) throws Exception {
final AsyncProcessorAwaitManager awaitManager = exchange.getContext().getAsyncProcessorAwaitManager();
final CountDownLatch latch = new CountDownLatch(1);
boolean sync = processor.process(exchange, new AsyncCallback() {
public void done(boolean doneSync) {
if (!doneSync) {
awaitManager.countDown(exchange, latch);
}
}
@Override
public String toString() {
return "Done " + processor;
}
});
if (!sync) {
awaitManager.await(exchange, latch);
}
}
use of java.util.concurrent.CountDownLatch in project camel by apache.
the class ValidatorRouteTest method testConcurrentUseNotASharedSchema.
public void testConcurrentUseNotASharedSchema() throws Exception {
validEndpoint.expectedMessageCount(10);
// latch for the 10 exchanges we expect
final CountDownLatch latch = new CountDownLatch(10);
// setup a task executor to be able send the messages in parallel
ExecutorService executor = Executors.newCachedThreadPool();
for (int i = 0; i < 10; i++) {
executor.execute(new Runnable() {
public void run() {
template.requestBody("direct:useNotASharedSchema", "<mail xmlns='http://foo.com/bar'><subject>Hey</subject><body>Hello world!</body></mail>");
latch.countDown();
}
});
}
try {
// wait for test completion, timeout after 30 sec to let other unit test run to not wait forever
assertTrue(latch.await(30000L, TimeUnit.MILLISECONDS));
assertEquals("Latch should be zero", 0, latch.getCount());
} finally {
executor.shutdown();
}
MockEndpoint.assertIsSatisfied(validEndpoint, invalidEndpoint, finallyEndpoint);
}
use of java.util.concurrent.CountDownLatch in project camel by apache.
the class SedaRouteTest method testThatShowsEndpointResolutionIsNotConsistent.
public void testThatShowsEndpointResolutionIsNotConsistent() throws Exception {
final CountDownLatch latch = new CountDownLatch(1);
CamelContext context = new DefaultCamelContext();
// lets add some routes
context.addRoutes(new RouteBuilder() {
public void configure() {
from("seda:test.a").to("seda:test.b");
from("seda:test.b").process(new Processor() {
public void process(Exchange e) {
log.debug("Received exchange: " + e.getIn());
latch.countDown();
}
});
}
});
context.start();
// now lets fire in a message
Endpoint endpoint = context.getEndpoint("seda:test.a");
Exchange exchange = endpoint.createExchange();
exchange.getIn().setHeader("cheese", 123);
Producer producer = endpoint.createProducer();
producer.process(exchange);
// now lets sleep for a while
assertTrue(latch.await(5, TimeUnit.SECONDS));
context.stop();
}
Aggregations