Search in sources :

Example 11 with ThreadPoolTaskExecutor

use of org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor in project camel by apache.

the class XQueryConcurrencyTest method testConcurrency.

@Test
public void testConcurrency() throws Exception {
    int total = 1000;
    MockEndpoint mock = getMockEndpoint("mock:result");
    mock.expectedMessageCount(total);
    // setup a task executor to be able send the messages in parallel
    ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
    executor.setCorePoolSize(5);
    executor.afterPropertiesSet();
    for (int i = 0; i < 5; i++) {
        final int threadCount = i;
        executor.execute(new Runnable() {

            public void run() {
                int start = threadCount * 200;
                for (int i = 0; i < 200; i++) {
                    try {
                        // do some random sleep to simulate spread in user activity
                        Thread.sleep(new Random().nextInt(10));
                    } catch (InterruptedException e) {
                    // ignore
                    }
                    template.sendBody(uri, "<person><id>" + (start + i + 1) + "</id><name>James</name></person>");
                }
            }
        });
    }
    mock.assertNoDuplicates(body());
    assertMockEndpointsSatisfied();
    executor.shutdown();
}
Also used : Random(java.util.Random) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) ThreadPoolTaskExecutor(org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) Test(org.junit.Test)

Example 12 with ThreadPoolTaskExecutor

use of org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor in project opennms by OpenNMS.

the class CollectionCommanderStarter method start.

public void start() {
    ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
    taskExecutor.setCorePoolSize(4);
    taskExecutor.initialize();
    taskExecutor.setMaxPoolSize(4);
    taskExecutor.setQueueCapacity(8);
    taskExecutor.initialize();
    Long startTime = System.currentTimeMillis();
    Integer i = 0;
    while (i < 100) {
        CollectionTask ct = new CollectionTask(1200, "SNMP_All_Metrics");
        PooledJobPublisher jobPublisher = new PooledJobPublisher(ct);
        taskExecutor.execute(jobPublisher);
        i++;
    }
    logger.info("All started '{}'ms", System.currentTimeMillis() - startTime);
    Boolean done = false;
    while (!done) {
        if (taskExecutor.getActiveCount() == 0) {
            logger.info("Tasks active '{}'", taskExecutor.getActiveCount());
            logger.info("All done '{}'ms", System.currentTimeMillis() - startTime);
            taskExecutor.shutdown();
            done = true;
        } else {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                logger.error("'{}'", e.getMessage());
            }
        }
    }
}
Also used : ThreadPoolTaskExecutor(org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor) CollectionTask(org.opennms.nrtg.api.model.CollectionTask)

Example 13 with ThreadPoolTaskExecutor

use of org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor in project zipkin by openzipkin.

the class ZipkinMySQLStorageAutoConfiguration method executor.

@Bean
@ConditionalOnMissingBean(Executor.class)
Executor executor() {
    ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
    executor.setThreadNamePrefix("ZipkinMySQLStorage-");
    executor.initialize();
    return executor;
}
Also used : ThreadPoolTaskExecutor(org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) Bean(org.springframework.context.annotation.Bean)

Example 14 with ThreadPoolTaskExecutor

use of org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor in project camel by apache.

the class JmsConcurrentConsumersTest method testConcurrentConsumersWithReply.

@Test
public void testConcurrentConsumersWithReply() throws Exception {
    // latch for the 5 exchanges we expect
    final CountDownLatch latch = new CountDownLatch(5);
    // setup a task executor to be able send the messages in parallel
    ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
    executor.setCorePoolSize(5);
    executor.afterPropertiesSet();
    for (int i = 0; i < 5; i++) {
        final int count = i;
        executor.execute(new Runnable() {

            public void run() {
                // request body is InOut pattern and thus we expect a reply (JMSReply)
                Object response = template.requestBody("activemq:a", "World #" + count);
                assertEquals("Bye World #" + count, response);
                latch.countDown();
            }
        });
    }
    long start = System.currentTimeMillis();
    // 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());
    long delta = System.currentTimeMillis() - start;
    assertTrue("Should be faster than 20000 millis, took " + delta + " millis", delta < 20000L);
    executor.shutdown();
}
Also used : ThreadPoolTaskExecutor(org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 15 with ThreadPoolTaskExecutor

use of org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor in project camel by apache.

the class DefaultJmsMessageListenerContainer method stop.

@Override
public void stop() throws JmsException {
    if (logger.isDebugEnabled()) {
        logger.debug("Stopping listenerContainer: " + this + " with cacheLevel: " + getCacheLevel() + " and sharedConnectionEnabled: " + sharedConnectionEnabled());
    }
    super.stop();
    if (taskExecutor instanceof ThreadPoolTaskExecutor) {
        ThreadPoolTaskExecutor executor = (ThreadPoolTaskExecutor) taskExecutor;
        executor.destroy();
    }
}
Also used : ThreadPoolTaskExecutor(org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor)

Aggregations

ThreadPoolTaskExecutor (org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor)24 Test (org.junit.Test)8 Bean (org.springframework.context.annotation.Bean)8 Random (java.util.Random)2 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)2 ConditionalOnMissingBean (org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean)2 AbstractSubscribableChannel (org.springframework.messaging.support.AbstractSubscribableChannel)2 Brave (com.github.kristofa.brave.Brave)1 ServerSpan (com.github.kristofa.brave.ServerSpan)1 ServerSpanState (com.github.kristofa.brave.ServerSpanState)1 Endpoint (com.twitter.zipkin.gen.Endpoint)1 InetAddress (java.net.InetAddress)1 UnknownHostException (java.net.UnknownHostException)1 ByteBuffer (java.nio.ByteBuffer)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Executor (java.util.concurrent.Executor)1 FutureTask (java.util.concurrent.FutureTask)1 CamelThreadFactory (org.apache.camel.util.concurrent.CamelThreadFactory)1 ExecuteContext (org.jooq.ExecuteContext)1 ExecuteListenerProvider (org.jooq.ExecuteListenerProvider)1