Search in sources :

Example 6 with Future

use of java.util.concurrent.Future in project camel by apache.

the class SplitterParallelAggregateTest method timeSplitRoutes.

protected void timeSplitRoutes(int numberOfRequests) throws Exception {
    String[] endpoints = new String[] { "direct:splitSynchronizedAggregation", "direct:splitUnsynchronizedAggregation" };
    List<Future<File>> futures = new ArrayList<Future<File>>();
    StopWatch stopWatch = new StopWatch(false);
    for (String endpoint : endpoints) {
        stopWatch.restart();
        for (int requestIndex = 0; requestIndex < numberOfRequests; requestIndex++) {
            futures.add(template.asyncRequestBody(endpoint, null, File.class));
        }
        for (int i = 0; i < futures.size(); i++) {
            Future<File> future = futures.get(i);
            future.get();
        }
        stopWatch.stop();
        log.info(String.format("test%d.%s=%d\n", numberOfRequests, endpoint, stopWatch.taken()));
    }
}
Also used : ArrayList(java.util.ArrayList) Future(java.util.concurrent.Future) File(java.io.File) StopWatch(org.apache.camel.util.StopWatch)

Example 7 with Future

use of java.util.concurrent.Future in project camel by apache.

the class JdbcProducerConcurrenctTest method doSendMessages.

@SuppressWarnings("rawtypes")
private void doSendMessages(int files, int poolSize) throws Exception {
    mock.expectedMessageCount(files);
    ExecutorService executor = Executors.newFixedThreadPool(poolSize);
    // we access the responses Map below only inside the main thread,
    // so no need for a thread-safe Map implementation
    Map<Integer, Future<List<?>>> responses = new HashMap<Integer, Future<List<?>>>();
    for (int i = 0; i < files; i++) {
        final int index = i;
        Future<List<?>> out = executor.submit(new Callable<List<?>>() {

            public List<?> call() throws Exception {
                int id = (index % 2) + 1;
                return template.requestBody("direct:start", "select * from customer where id = 'cust" + id + "'", List.class);
            }
        });
        responses.put(index, out);
    }
    assertMockEndpointsSatisfied();
    assertEquals(files, responses.size());
    for (int i = 0; i < files; i++) {
        List<?> rows = responses.get(i).get();
        Map columns = (Map) rows.get(0);
        if (i % 2 == 0) {
            assertEquals("jstrachan", columns.get("NAME"));
        } else {
            assertEquals("nsandhu", columns.get("NAME"));
        }
    }
    executor.shutdownNow();
}
Also used : HashMap(java.util.HashMap) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) List(java.util.List) Map(java.util.Map) HashMap(java.util.HashMap) MockEndpoint(org.apache.camel.component.mock.MockEndpoint)

Example 8 with Future

use of java.util.concurrent.Future in project camel by apache.

the class HttpProducerConcurrentTest method doSendMessages.

private void doSendMessages(int files, int poolSize) throws Exception {
    getMockEndpoint("mock:result").expectedMessageCount(files);
    getMockEndpoint("mock:result").assertNoDuplicates(body());
    ExecutorService executor = Executors.newFixedThreadPool(poolSize);
    // we access the responses Map below only inside the main thread,
    // so no need for a thread-safe Map implementation
    Map<Integer, Future<String>> responses = new HashMap<Integer, Future<String>>();
    for (int i = 0; i < files; i++) {
        final int index = i;
        Future<String> out = executor.submit(new Callable<String>() {

            public String call() throws Exception {
                return template.requestBody("http://localhost:{{port}}/echo", "" + index, String.class);
            }
        });
        responses.put(index, out);
    }
    assertMockEndpointsSatisfied();
    assertEquals(files, responses.size());
    // get all responses
    Set<String> unique = new HashSet<String>();
    for (Future<String> future : responses.values()) {
        unique.add(future.get());
    }
    // should be 'files' unique responses
    assertEquals("Should be " + files + " unique responses", files, unique.size());
    executor.shutdownNow();
}
Also used : HashMap(java.util.HashMap) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) HashSet(java.util.HashSet)

Example 9 with Future

use of java.util.concurrent.Future in project camel by apache.

the class JpaProducerConcurrentTest method doSendMessages.

private void doSendMessages(int files, int poolSize) throws Exception {
    getMockEndpoint("mock:result").expectedMessageCount(files);
    getMockEndpoint("mock:result").assertNoDuplicates(body());
    ExecutorService executor = Executors.newFixedThreadPool(poolSize);
    // we access the responses Map below only inside the main thread,
    // so no need for a thread-safe Map implementation
    Map<Integer, Future<SendEmail>> responses = new HashMap<Integer, Future<SendEmail>>();
    for (int i = 0; i < files; i++) {
        final int index = i;
        Future<SendEmail> out = executor.submit(new Callable<SendEmail>() {

            public SendEmail call() throws Exception {
                return template.requestBody("direct:start", new SendEmail("user" + index + "@somewhere.org"), SendEmail.class);
            }
        });
        responses.put(index, out);
    }
    assertMockEndpointsSatisfied(30, TimeUnit.SECONDS);
    assertEquals(files, responses.size());
    // get them so they are complete
    for (Future<SendEmail> future : responses.values()) {
        SendEmail sendEmail = future.get();
        assertNotNull(sendEmail);
        log.info("Persisted the SendEmail entity with the id {} and the address {}", sendEmail.getId(), sendEmail.getAddress());
    }
    // assert in the database
    assertEntityInDB(files);
    executor.shutdownNow();
}
Also used : HashMap(java.util.HashMap) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) SendEmail(org.apache.camel.examples.SendEmail)

Example 10 with Future

use of java.util.concurrent.Future in project camel by apache.

the class MinaProducerConcurrentTest method doSendMessages.

private void doSendMessages(int files, int poolSize) throws Exception {
    getMockEndpoint("mock:result").expectedMessageCount(files);
    ExecutorService executor = Executors.newFixedThreadPool(poolSize);
    // we access the responses Map below only inside the main thread,
    // so no need for a thread-safe Map implementation
    Map<Integer, Future<String>> responses = new HashMap<Integer, Future<String>>();
    for (int i = 0; i < files; i++) {
        final int index = i;
        Future<String> out = executor.submit(new Callable<String>() {

            public String call() throws Exception {
                return template.requestBody("mina:tcp://localhost:{{port}}?sync=true", index, String.class);
            }
        });
        responses.put(index, out);
    }
    assertMockEndpointsSatisfied();
    assertEquals(files, responses.size());
    // get all responses
    Set<String> unique = new HashSet<String>();
    for (Future<String> future : responses.values()) {
        unique.add(future.get());
    }
    // should be 'files' unique responses
    assertEquals("Should be " + files + " unique responses", files, unique.size());
    executor.shutdownNow();
}
Also used : HashMap(java.util.HashMap) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) HashSet(java.util.HashSet)

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