Search in sources :

Example 6 with CountDownLatch

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();
}
Also used : ArrayList(java.util.ArrayList) ExecutorService(java.util.concurrent.ExecutorService) Document(org.w3c.dom.Document) CountDownLatch(java.util.concurrent.CountDownLatch) XPathExpressionException(javax.xml.xpath.XPathExpressionException)

Example 7 with CountDownLatch

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

Example 8 with CountDownLatch

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);
    }
}
Also used : AsyncCallback(org.apache.camel.AsyncCallback) AsyncProcessorAwaitManager(org.apache.camel.spi.AsyncProcessorAwaitManager) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 9 with CountDownLatch

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);
}
Also used : ExecutorService(java.util.concurrent.ExecutorService) CountDownLatch(java.util.concurrent.CountDownLatch) MockEndpoint(org.apache.camel.component.mock.MockEndpoint)

Example 10 with CountDownLatch

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();
}
Also used : CamelContext(org.apache.camel.CamelContext) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) RouteBuilder(org.apache.camel.builder.RouteBuilder) Endpoint(org.apache.camel.Endpoint) Producer(org.apache.camel.Producer) CountDownLatch(java.util.concurrent.CountDownLatch) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext)

Aggregations

CountDownLatch (java.util.concurrent.CountDownLatch)5355 Test (org.junit.Test)2594 IOException (java.io.IOException)631 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)550 AtomicReference (java.util.concurrent.atomic.AtomicReference)501 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)475 ArrayList (java.util.ArrayList)471 QuickTest (com.hazelcast.test.annotation.QuickTest)375 ParallelTest (com.hazelcast.test.annotation.ParallelTest)355 ExecutorService (java.util.concurrent.ExecutorService)322 Test (org.testng.annotations.Test)310 HazelcastInstance (com.hazelcast.core.HazelcastInstance)251 List (java.util.List)212 HashMap (java.util.HashMap)207 HttpServletResponse (javax.servlet.http.HttpServletResponse)207 ExecutionException (java.util.concurrent.ExecutionException)203 HttpServletRequest (javax.servlet.http.HttpServletRequest)189 Ignite (org.apache.ignite.Ignite)188 ServletException (javax.servlet.ServletException)183 TimeoutException (java.util.concurrent.TimeoutException)168