use of java.util.concurrent.Future in project camel by apache.
the class DisruptorConcurrentTest method testDisruptorConcurrentInOutWithAsync.
@Test
public void testDisruptorConcurrentInOutWithAsync() throws Exception {
final MockEndpoint mock = getMockEndpoint("mock:result");
mock.expectedMessageCount(20);
mock.allMessages().body().startsWith("Bye");
// should at least take 3 sec
mock.setMinimumResultWaitTime(3000);
// use our own template that has a higher thread pool than default camel that uses 5
final ExecutorService executor = Executors.newFixedThreadPool(10);
final ProducerTemplate pt = new DefaultProducerTemplate(context, executor);
// must start the template
pt.start();
final List<Future<Object>> replies = new ArrayList<Future<Object>>(20);
for (int i = 0; i < 20; i++) {
final Future<Object> out = pt.asyncRequestBody("disruptor:bar", "Message " + i);
replies.add(out);
}
assertMockEndpointsSatisfied();
assertEquals(20, replies.size());
for (int i = 0; i < 20; i++) {
final String out = (String) replies.get(i).get();
assertTrue(out.startsWith("Bye"));
}
pt.stop();
executor.shutdownNow();
}
use of java.util.concurrent.Future in project camel by apache.
the class PredicateBuilderConcurrentTest method testPredicateBuilderConcurrent.
public void testPredicateBuilderConcurrent() throws Exception {
context.start();
List<Future<Boolean>> futures = new ArrayList<Future<Boolean>>();
ExecutorService pool = Executors.newFixedThreadPool(10);
for (int i = 0; i < 1000; i++) {
final Integer num = i;
Future<Boolean> future = pool.submit(new Callable<Boolean>() {
public Boolean call() throws Exception {
Expression left = ExpressionBuilder.headerExpression("foo");
Expression right;
if (num % 2 == 0) {
right = ExpressionBuilder.constantExpression("ABC");
} else {
right = ExpressionBuilder.constantExpression("DEF");
}
Predicate predicate = PredicateBuilder.isEqualTo(left, right);
Exchange exchange = new DefaultExchange(context);
exchange.getIn().setBody("Hello World");
exchange.getIn().setHeader("foo", "ABC");
return predicate.matches(exchange);
}
});
futures.add(future);
}
for (int i = 0; i < 1000; i++) {
Boolean result = futures.get(i).get(10, TimeUnit.SECONDS);
if (i % 2 == 0) {
assertEquals("Should be true for #" + i, true, result.booleanValue());
} else {
assertEquals("Should be false for #" + i, false, result.booleanValue());
}
}
pool.shutdownNow();
}
use of java.util.concurrent.Future in project camel by apache.
the class ServicePoolTest method testConcurrent.
public void testConcurrent() throws Exception {
final Endpoint endpoint = context.getEndpoint("mock:foo");
ExecutorService executor = Executors.newFixedThreadPool(5);
List<Future<Integer>> response = new ArrayList<Future<Integer>>();
for (int i = 0; i < 5; i++) {
final int index = i;
Future<Integer> out = executor.submit(new Callable<Integer>() {
public Integer call() throws Exception {
Producer producer = pool.acquire(endpoint);
if (producer == null) {
producer = pool.addAndAcquire(endpoint, new MyProducer(endpoint));
}
assertNotNull(producer);
pool.release(endpoint, producer);
return index;
}
});
response.add(out);
}
for (int i = 0; i < 5; i++) {
assertEquals(i, response.get(i).get().intValue());
}
executor.shutdownNow();
}
use of java.util.concurrent.Future in project camel by apache.
the class ServicePoolTest method testConcurrentStress.
public void testConcurrentStress() throws Exception {
final Endpoint endpoint = context.getEndpoint("mock:foo");
ExecutorService executor = Executors.newFixedThreadPool(5);
List<Future<Integer>> response = new ArrayList<Future<Integer>>();
for (int i = 0; i < 5; i++) {
final int index = i;
Future<Integer> out = executor.submit(new Callable<Integer>() {
public Integer call() throws Exception {
for (int j = 0; j < 100; j++) {
Producer producer = pool.acquire(endpoint);
if (producer == null) {
producer = pool.addAndAcquire(endpoint, new MyProducer(endpoint));
}
assertNotNull(producer);
pool.release(endpoint, producer);
}
return index;
}
});
response.add(out);
}
for (int i = 0; i < 5; i++) {
assertEquals(i, response.get(i).get().intValue());
}
executor.shutdownNow();
}
use of java.util.concurrent.Future in project camel by apache.
the class BeanstalkProducer method process.
@Override
public void process(final Exchange exchange) throws Exception {
Future f = executor.submit(new RunCommand(exchange));
f.get();
}
Aggregations