Search in sources :

Example 1 with Future

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();
}
Also used : ProducerTemplate(org.apache.camel.ProducerTemplate) DefaultProducerTemplate(org.apache.camel.impl.DefaultProducerTemplate) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) ExecutorService(java.util.concurrent.ExecutorService) ArrayList(java.util.ArrayList) Future(java.util.concurrent.Future) DefaultProducerTemplate(org.apache.camel.impl.DefaultProducerTemplate) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) Test(org.junit.Test)

Example 2 with Future

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();
}
Also used : DefaultExchange(org.apache.camel.impl.DefaultExchange) ArrayList(java.util.ArrayList) Predicate(org.apache.camel.Predicate) DefaultExchange(org.apache.camel.impl.DefaultExchange) Exchange(org.apache.camel.Exchange) Expression(org.apache.camel.Expression) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future)

Example 3 with Future

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();
}
Also used : Endpoint(org.apache.camel.Endpoint) Producer(org.apache.camel.Producer) ExecutorService(java.util.concurrent.ExecutorService) ArrayList(java.util.ArrayList) Future(java.util.concurrent.Future) Endpoint(org.apache.camel.Endpoint)

Example 4 with Future

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();
}
Also used : Endpoint(org.apache.camel.Endpoint) Producer(org.apache.camel.Producer) ExecutorService(java.util.concurrent.ExecutorService) ArrayList(java.util.ArrayList) Future(java.util.concurrent.Future) Endpoint(org.apache.camel.Endpoint)

Example 5 with Future

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();
}
Also used : Future(java.util.concurrent.Future)

Aggregations

Future (java.util.concurrent.Future)1138 ArrayList (java.util.ArrayList)479 ExecutorService (java.util.concurrent.ExecutorService)445 Test (org.junit.Test)413 ExecutionException (java.util.concurrent.ExecutionException)264 Callable (java.util.concurrent.Callable)206 IOException (java.io.IOException)177 ParallelTest (com.hazelcast.test.annotation.ParallelTest)148 QuickTest (com.hazelcast.test.annotation.QuickTest)148 HashMap (java.util.HashMap)92 List (java.util.List)84 CountDownLatch (java.util.concurrent.CountDownLatch)71 LinkedList (java.util.LinkedList)67 TimeoutException (java.util.concurrent.TimeoutException)63 HashSet (java.util.HashSet)62 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)59 Map (java.util.Map)58 ICompletableFuture (com.hazelcast.core.ICompletableFuture)57 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)53 File (java.io.File)46