Search in sources :

Example 56 with ThreadPoolExecutor

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

the class DefaultExecutorServiceManagerTest method testDefaultUnboundedQueueThreadPool.

public void testDefaultUnboundedQueueThreadPool() throws Exception {
    ThreadPoolProfile custom = new ThreadPoolProfile("custom");
    custom.setPoolSize(10);
    custom.setMaxPoolSize(30);
    custom.setKeepAliveTime(50L);
    custom.setMaxQueueSize(Integer.MAX_VALUE);
    context.getExecutorServiceManager().setDefaultThreadPoolProfile(custom);
    assertEquals(true, custom.isDefaultProfile().booleanValue());
    ExecutorService myPool = context.getExecutorServiceManager().newDefaultThreadPool(this, "myPool");
    assertEquals(false, myPool.isShutdown());
    // should use default settings
    ThreadPoolExecutor executor = (ThreadPoolExecutor) myPool;
    assertEquals(10, executor.getCorePoolSize());
    assertEquals(30, executor.getMaximumPoolSize());
    assertEquals(50, executor.getKeepAliveTime(TimeUnit.SECONDS));
    assertEquals(Integer.MAX_VALUE, executor.getQueue().remainingCapacity());
    context.stop();
    assertEquals(true, myPool.isShutdown());
}
Also used : ThreadPoolProfile(org.apache.camel.spi.ThreadPoolProfile) ExecutorService(java.util.concurrent.ExecutorService) SizedScheduledExecutorService(org.apache.camel.util.concurrent.SizedScheduledExecutorService) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor)

Example 57 with ThreadPoolExecutor

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

the class DefaultExecutorServiceManagerTest method testGetThreadPoolProfileInheritCustomDefaultValues2.

public void testGetThreadPoolProfileInheritCustomDefaultValues2() throws Exception {
    ThreadPoolProfile newDefault = new ThreadPoolProfile("newDefault");
    // just change the max pool as the default profile should then inherit the old default profile
    newDefault.setMaxPoolSize(50);
    context.getExecutorServiceManager().setDefaultThreadPoolProfile(newDefault);
    assertNull(context.getExecutorServiceManager().getThreadPoolProfile("foo"));
    ThreadPoolProfile foo = new ThreadPoolProfile("foo");
    foo.setPoolSize(1);
    context.getExecutorServiceManager().registerThreadPoolProfile(foo);
    assertSame(foo, context.getExecutorServiceManager().getThreadPoolProfile("foo"));
    ExecutorService executor = context.getExecutorServiceManager().newThreadPool(this, "MyPool", "foo");
    ThreadPoolExecutor tp = assertIsInstanceOf(ThreadPoolExecutor.class, executor);
    assertEquals(1, tp.getCorePoolSize());
    // should inherit the default values
    assertEquals(50, tp.getMaximumPoolSize());
    assertEquals(60, tp.getKeepAliveTime(TimeUnit.SECONDS));
    assertEquals("CallerRuns", tp.getRejectedExecutionHandler().toString());
}
Also used : ThreadPoolProfile(org.apache.camel.spi.ThreadPoolProfile) ExecutorService(java.util.concurrent.ExecutorService) SizedScheduledExecutorService(org.apache.camel.util.concurrent.SizedScheduledExecutorService) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor)

Example 58 with ThreadPoolExecutor

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

the class DefaultExecutorServiceManagerTest method testCustomDefaultThreadPool.

public void testCustomDefaultThreadPool() throws Exception {
    ThreadPoolProfile custom = new ThreadPoolProfile("custom");
    custom.setKeepAliveTime(20L);
    custom.setMaxPoolSize(40);
    custom.setPoolSize(5);
    custom.setMaxQueueSize(2000);
    context.getExecutorServiceManager().setDefaultThreadPoolProfile(custom);
    assertEquals(true, custom.isDefaultProfile().booleanValue());
    ExecutorService myPool = context.getExecutorServiceManager().newDefaultThreadPool(this, "myPool");
    assertEquals(false, myPool.isShutdown());
    // should use default settings
    ThreadPoolExecutor executor = (ThreadPoolExecutor) myPool;
    assertEquals(5, executor.getCorePoolSize());
    assertEquals(40, executor.getMaximumPoolSize());
    assertEquals(20, executor.getKeepAliveTime(TimeUnit.SECONDS));
    assertEquals(2000, executor.getQueue().remainingCapacity());
    context.stop();
    assertEquals(true, myPool.isShutdown());
}
Also used : ThreadPoolProfile(org.apache.camel.spi.ThreadPoolProfile) ExecutorService(java.util.concurrent.ExecutorService) SizedScheduledExecutorService(org.apache.camel.util.concurrent.SizedScheduledExecutorService) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor)

Example 59 with ThreadPoolExecutor

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

the class ThreadsRejectedExecutionTest method testThreadsRejectedExecution.

public void testThreadsRejectedExecution() throws Exception {
    context.addRoutes(new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            // use a custom pool which rejects any new tasks while currently in progress
            // this should force the ThreadsProcessor to run the tasks itself
            ExecutorService pool = new ThreadPoolExecutor(1, 1, 0, TimeUnit.SECONDS, new SynchronousQueue<Runnable>());
            from("seda:start").to("log:before").threads().executorService(pool).delay(1000).to("log:after").to("mock:result");
        }
    });
    context.start();
    getMockEndpoint("mock:result").expectedMessageCount(3);
    template.sendBody("seda:start", "Hello World");
    template.sendBody("seda:start", "Hi World");
    template.sendBody("seda:start", "Bye World");
    assertMockEndpointsSatisfied();
}
Also used : RouteBuilder(org.apache.camel.builder.RouteBuilder) SynchronousQueue(java.util.concurrent.SynchronousQueue) ExecutorService(java.util.concurrent.ExecutorService) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor)

Example 60 with ThreadPoolExecutor

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

the class ThreadsRejectedExecutionTest method testThreadsRejectedExecutionCallerNotRuns.

public void testThreadsRejectedExecutionCallerNotRuns() throws Exception {
    context.addRoutes(new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            // use a custom pool which rejects any new tasks while currently in progress
            // this should force the ThreadsProcessor to run the tasks itself
            ExecutorService pool = new ThreadPoolExecutor(1, 1, 0, TimeUnit.SECONDS, new SynchronousQueue<Runnable>());
            from("seda:start").to("log:before").threads().executorService(pool).callerRunsWhenRejected(false).delay(1000).to("log:after").to("mock:result");
        }
    });
    context.start();
    MockEndpoint mock = getMockEndpoint("mock:result");
    mock.expectedMessageCount(3);
    // wait at most 5 seconds
    mock.setResultWaitTime(5000);
    template.sendBody("seda:start", "Hello World");
    template.sendBody("seda:start", "Hi World");
    template.sendBody("seda:start", "Bye World");
    // should not be possible to route all 3
    mock.assertIsNotSatisfied();
    // only 1 should arrive
    assertEquals(1, mock.getReceivedCounter());
}
Also used : RouteBuilder(org.apache.camel.builder.RouteBuilder) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) SynchronousQueue(java.util.concurrent.SynchronousQueue) ExecutorService(java.util.concurrent.ExecutorService) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor)

Aggregations

ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)441 Test (org.junit.Test)87 ExecutorService (java.util.concurrent.ExecutorService)79 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)66 ThreadFactory (java.util.concurrent.ThreadFactory)45 SynchronousQueue (java.util.concurrent.SynchronousQueue)38 IOException (java.io.IOException)37 ArrayList (java.util.ArrayList)36 ScheduledThreadPoolExecutor (java.util.concurrent.ScheduledThreadPoolExecutor)34 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)27 RejectedExecutionHandler (java.util.concurrent.RejectedExecutionHandler)26 ArrayBlockingQueue (java.util.concurrent.ArrayBlockingQueue)25 CountDownLatch (java.util.concurrent.CountDownLatch)25 ExecutionException (java.util.concurrent.ExecutionException)25 Future (java.util.concurrent.Future)23 ThreadFactoryBuilder (com.google.common.util.concurrent.ThreadFactoryBuilder)19 Test (org.testng.annotations.Test)18 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)17 HashMap (java.util.HashMap)16 SizedScheduledExecutorService (org.apache.camel.util.concurrent.SizedScheduledExecutorService)16