Search in sources :

Example 96 with StopWatch

use of org.apache.camel.util.StopWatch in project camel by apache.

the class NettyConcurrentTest method doSendMessages.

private void doSendMessages(int files, int poolSize) throws Exception {
    StopWatch watch = new StopWatch();
    NotifyBuilder notify = new NotifyBuilder(context).whenDone(files).create();
    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 {
                String reply = template.requestBody("netty:tcp://localhost:{{port}}", index, String.class);
                log.debug("Sent {} received {}", index, reply);
                assertEquals("Bye " + index, reply);
                return reply;
            }
        });
        responses.put(index, out);
    }
    notify.matches(2, TimeUnit.MINUTES);
    log.info("Took " + watch.taken() + " millis to process " + files + " messages using " + poolSize + " client threads.");
    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) StopWatch(org.apache.camel.util.StopWatch) NotifyBuilder(org.apache.camel.builder.NotifyBuilder) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) HashSet(java.util.HashSet)

Example 97 with StopWatch

use of org.apache.camel.util.StopWatch in project camel by apache.

the class DisruptorVmInOutChainedTimeoutTest method testDisruptorVmInOutChainedTimeout.

public void testDisruptorVmInOutChainedTimeout() throws Exception {
    StopWatch watch = new StopWatch();
    try {
        template2.requestBody("disruptor-vm:a?timeout=1000", "Hello World");
        fail("Should have thrown an exception");
    } catch (CamelExecutionException e) {
        // the chained vm caused the timeout
        ExchangeTimedOutException cause = assertIsInstanceOf(ExchangeTimedOutException.class, e.getCause());
        assertEquals(200, cause.getTimeout());
    }
    long delta = watch.stop();
    assertTrue("Should be faster than 1 sec, was: " + delta, delta < 1100);
}
Also used : CamelExecutionException(org.apache.camel.CamelExecutionException) ExchangeTimedOutException(org.apache.camel.ExchangeTimedOutException) StopWatch(org.apache.camel.util.StopWatch)

Example 98 with StopWatch

use of org.apache.camel.util.StopWatch in project camel by apache.

the class JettyAsyncContinuationTimeoutTest method testJettyAsyncTimeout.

@Test
public void testJettyAsyncTimeout() throws Exception {
    getMockEndpoint("mock:result").expectedBodiesReceived("Bye World");
    StopWatch watch = new StopWatch();
    try {
        template.requestBody("http://localhost:{{port}}/myservice", null, String.class);
        fail("Should have thrown an exception");
    } catch (CamelExecutionException e) {
        log.info("Timeout hit and client got reply with failure status code");
        long taken = watch.stop();
        HttpOperationFailedException cause = assertIsInstanceOf(HttpOperationFailedException.class, e.getCause());
        assertEquals(504, cause.getStatusCode());
        // should be approx 3-4 sec.
        assertTrue("Timeout should occur faster than " + taken, taken < 4500);
    }
    assertMockEndpointsSatisfied();
}
Also used : CamelExecutionException(org.apache.camel.CamelExecutionException) HttpOperationFailedException(org.apache.camel.http.common.HttpOperationFailedException) StopWatch(org.apache.camel.util.StopWatch) BaseJettyTest(org.apache.camel.component.jetty.BaseJettyTest) Test(org.junit.Test)

Example 99 with StopWatch

use of org.apache.camel.util.StopWatch in project camel by apache.

the class JmsRequestReplyExclusiveReplyToComponentTest method testJmsRequestReplyExclusiveFixedReplyTo.

@Test
public void testJmsRequestReplyExclusiveFixedReplyTo() throws Exception {
    StopWatch watch = new StopWatch();
    assertEquals("Hello A", template.requestBody("activemq:queue:foo?replyTo=bar", "A"));
    assertEquals("Hello B", template.requestBody("activemq:queue:foo?replyTo=bar", "B"));
    assertEquals("Hello C", template.requestBody("activemq:queue:foo?replyTo=bar", "C"));
    assertEquals("Hello D", template.requestBody("activemq:queue:foo?replyTo=bar", "D"));
    assertEquals("Hello E", template.requestBody("activemq:queue:foo?replyTo=bar", "E"));
    long delta = watch.stop();
    assertTrue("Should be faster than about 4 seconds, was: " + delta, delta < 4200);
}
Also used : StopWatch(org.apache.camel.util.StopWatch) Test(org.junit.Test)

Example 100 with StopWatch

use of org.apache.camel.util.StopWatch in project camel by apache.

the class JmsRequestReplyExclusiveReplyToConcurrentTest method testJmsRequestReplyExclusiveFixedReplyTo.

@Test
public void testJmsRequestReplyExclusiveFixedReplyTo() throws Exception {
    StopWatch watch = new StopWatch();
    ExecutorService executor = Executors.newFixedThreadPool(10);
    for (int i = 0; i < size; i++) {
        final Integer num = i;
        executor.submit(new Runnable() {

            @Override
            public void run() {
                String reply = template.requestBody("direct:start", "" + num, String.class);
                log.info("Sent {} expecting reply 'Hello {}' got --> {}", new Object[] { num, num, reply });
                assertNotNull(reply);
                assertEquals("Hello " + num, reply);
                latch.countDown();
            }
        });
    }
    log.info("Waiting to process {} messages...", size);
    // if any of the assertions above fails then the latch will not get decremented 
    assertTrue("All assertions outside the main thread above should have passed", latch.await(3, TimeUnit.SECONDS));
    long delta = watch.stop();
    log.info("Took {} millis", delta);
    // just sleep a bit before shutting down
    Thread.sleep(1000);
    executor.shutdownNow();
}
Also used : ExecutorService(java.util.concurrent.ExecutorService) StopWatch(org.apache.camel.util.StopWatch) Test(org.junit.Test)

Aggregations

StopWatch (org.apache.camel.util.StopWatch)101 Test (org.junit.Test)40 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)14 CamelExecutionException (org.apache.camel.CamelExecutionException)10 Exchange (org.apache.camel.Exchange)8 CamelExchangeException (org.apache.camel.CamelExchangeException)6 File (java.io.File)5 ExecutorService (java.util.concurrent.ExecutorService)5 AsyncProcessor (org.apache.camel.AsyncProcessor)5 Producer (org.apache.camel.Producer)5 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 Future (java.util.concurrent.Future)4 AsyncCallback (org.apache.camel.AsyncCallback)4 Endpoint (org.apache.camel.Endpoint)4 ExchangeTimedOutException (org.apache.camel.ExchangeTimedOutException)4 NotifyBuilder (org.apache.camel.builder.NotifyBuilder)4 Date (java.util.Date)3 GenericFile (org.apache.camel.component.file.GenericFile)3