Search in sources :

Example 6 with ThreadPoolProfile

use of org.apache.camel.spi.ThreadPoolProfile in project camel by apache.

the class ThreadsDefinition method createProcessor.

@Override
public Processor createProcessor(RouteContext routeContext) throws Exception {
    // the threads name
    String name = getThreadName() != null ? getThreadName() : "Threads";
    // prefer any explicit configured executor service
    boolean shutdownThreadPool = ProcessorDefinitionHelper.willCreateNewThreadPool(routeContext, this, true);
    ExecutorService threadPool = ProcessorDefinitionHelper.getConfiguredExecutorService(routeContext, name, this, false);
    // resolve what rejected policy to use
    ThreadPoolRejectedPolicy policy = resolveRejectedPolicy(routeContext);
    if (policy == null) {
        if (callerRunsWhenRejected == null || callerRunsWhenRejected) {
            // should use caller runs by default if not configured
            policy = ThreadPoolRejectedPolicy.CallerRuns;
        } else {
            policy = ThreadPoolRejectedPolicy.Abort;
        }
    }
    log.debug("Using ThreadPoolRejectedPolicy: {}", policy);
    // if no explicit then create from the options
    if (threadPool == null) {
        ExecutorServiceManager manager = routeContext.getCamelContext().getExecutorServiceManager();
        // create the thread pool using a builder
        ThreadPoolProfile profile = new ThreadPoolProfileBuilder(name).poolSize(getPoolSize()).maxPoolSize(getMaxPoolSize()).keepAliveTime(getKeepAliveTime(), getTimeUnit()).maxQueueSize(getMaxQueueSize()).rejectedPolicy(policy).allowCoreThreadTimeOut(getAllowCoreThreadTimeOut()).build();
        threadPool = manager.newThreadPool(this, name, profile);
        shutdownThreadPool = true;
    } else {
        if (getThreadName() != null && !getThreadName().equals("Threads")) {
            throw new IllegalArgumentException("ThreadName and executorServiceRef options cannot be used together.");
        }
        if (getPoolSize() != null) {
            throw new IllegalArgumentException("PoolSize and executorServiceRef options cannot be used together.");
        }
        if (getMaxPoolSize() != null) {
            throw new IllegalArgumentException("MaxPoolSize and executorServiceRef options cannot be used together.");
        }
        if (getKeepAliveTime() != null) {
            throw new IllegalArgumentException("KeepAliveTime and executorServiceRef options cannot be used together.");
        }
        if (getTimeUnit() != null) {
            throw new IllegalArgumentException("TimeUnit and executorServiceRef options cannot be used together.");
        }
        if (getMaxQueueSize() != null) {
            throw new IllegalArgumentException("MaxQueueSize and executorServiceRef options cannot be used together.");
        }
        if (getRejectedPolicy() != null) {
            throw new IllegalArgumentException("RejectedPolicy and executorServiceRef options cannot be used together.");
        }
        if (getAllowCoreThreadTimeOut() != null) {
            throw new IllegalArgumentException("AllowCoreThreadTimeOut and executorServiceRef options cannot be used together.");
        }
    }
    ThreadsProcessor thread = new ThreadsProcessor(routeContext.getCamelContext(), threadPool, shutdownThreadPool, policy);
    List<Processor> pipe = new ArrayList<Processor>(2);
    pipe.add(thread);
    pipe.add(createChildProcessor(routeContext, true));
    // (recipient list definition does this as well)
    return new Pipeline(routeContext.getCamelContext(), pipe) {

        @Override
        public String toString() {
            return "Threads[" + getOutputs() + "]";
        }
    };
}
Also used : ThreadPoolProfile(org.apache.camel.spi.ThreadPoolProfile) ThreadsProcessor(org.apache.camel.processor.ThreadsProcessor) ThreadsProcessor(org.apache.camel.processor.ThreadsProcessor) Processor(org.apache.camel.Processor) ThreadPoolProfileBuilder(org.apache.camel.builder.ThreadPoolProfileBuilder) ExecutorServiceManager(org.apache.camel.spi.ExecutorServiceManager) ArrayList(java.util.ArrayList) Pipeline(org.apache.camel.processor.Pipeline) ThreadPoolRejectedPolicy(org.apache.camel.ThreadPoolRejectedPolicy) ExecutorService(java.util.concurrent.ExecutorService)

Example 7 with ThreadPoolProfile

use of org.apache.camel.spi.ThreadPoolProfile in project camel by apache.

the class DualManagedThreadPoolProfileTest method createRouteBuilder.

@Override
protected RouteBuilder createRouteBuilder() throws Exception {
    return new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            ThreadPoolProfile profile = new ThreadPoolProfile("custom");
            profile.setPoolSize(5);
            profile.setMaxPoolSize(15);
            profile.setKeepAliveTime(25L);
            profile.setMaxQueueSize(250);
            profile.setRejectedPolicy(ThreadPoolRejectedPolicy.Abort);
            context.getExecutorServiceManager().registerThreadPoolProfile(profile);
            from("direct:start").threads().executorServiceRef("custom").to("mock:result");
            from("direct:foo").threads().executorServiceRef("custom").to("mock:foo");
        }
    };
}
Also used : ThreadPoolProfile(org.apache.camel.spi.ThreadPoolProfile) RouteBuilder(org.apache.camel.builder.RouteBuilder)

Example 8 with ThreadPoolProfile

use of org.apache.camel.spi.ThreadPoolProfile in project camel by apache.

the class DefaultExecutorServiceManagerTest method testDefaultNoMaxQueueThreadPool.

public void testDefaultNoMaxQueueThreadPool() throws Exception {
    ThreadPoolProfile custom = new ThreadPoolProfile("custom");
    custom.setPoolSize(10);
    custom.setMaxPoolSize(30);
    custom.setKeepAliveTime(50L);
    custom.setMaxQueueSize(0);
    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(0, 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 9 with ThreadPoolProfile

use of org.apache.camel.spi.ThreadPoolProfile 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 10 with ThreadPoolProfile

use of org.apache.camel.spi.ThreadPoolProfile 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)

Aggregations

ThreadPoolProfile (org.apache.camel.spi.ThreadPoolProfile)41 ExecutorService (java.util.concurrent.ExecutorService)16 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)11 SizedScheduledExecutorService (org.apache.camel.util.concurrent.SizedScheduledExecutorService)11 CamelContext (org.apache.camel.CamelContext)5 RouteBuilder (org.apache.camel.builder.RouteBuilder)5 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)4 ExecutorServiceManager (org.apache.camel.spi.ExecutorServiceManager)4 ThreadPoolProfileBuilder (org.apache.camel.builder.ThreadPoolProfileBuilder)3 HashSet (java.util.HashSet)2 Exchange (org.apache.camel.Exchange)2 Processor (org.apache.camel.Processor)2 ArrayList (java.util.ArrayList)1 LinkedHashSet (java.util.LinkedHashSet)1 Map (java.util.Map)1 ThreadFactory (java.util.concurrent.ThreadFactory)1 ThreadPoolRejectedPolicy (org.apache.camel.ThreadPoolRejectedPolicy)1 ThreadPoolProfileDefinition (org.apache.camel.model.ThreadPoolProfileDefinition)1 BodyInAggregatingStrategy (org.apache.camel.processor.BodyInAggregatingStrategy)1 Pipeline (org.apache.camel.processor.Pipeline)1