Search in sources :

Example 1 with ThreadPoolProfile

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

the class AbstractCamelThreadPoolFactoryBean method getObject.

public ExecutorService getObject() throws Exception {
    int size = CamelContextHelper.parseInteger(getCamelContext(), poolSize);
    if (size <= 0) {
        throw new IllegalArgumentException("PoolSize must be a positive number");
    }
    int max = size;
    if (maxPoolSize != null) {
        max = CamelContextHelper.parseInteger(getCamelContext(), maxPoolSize);
    }
    long keepAlive = 60;
    if (keepAliveTime != null) {
        keepAlive = CamelContextHelper.parseLong(getCamelContext(), keepAliveTime);
    }
    int queueSize = -1;
    if (maxQueueSize != null) {
        queueSize = CamelContextHelper.parseInteger(getCamelContext(), maxQueueSize);
    }
    boolean allow = false;
    if (allowCoreThreadTimeOut != null) {
        allow = CamelContextHelper.parseBoolean(getCamelContext(), allowCoreThreadTimeOut);
    }
    ThreadPoolProfile profile = new ThreadPoolProfileBuilder(getId()).poolSize(size).maxPoolSize(max).keepAliveTime(keepAlive, timeUnit).maxQueueSize(queueSize).allowCoreThreadTimeOut(allow).rejectedPolicy(rejectedPolicy).build();
    ExecutorService answer;
    if (scheduled != null && scheduled) {
        answer = getCamelContext().getExecutorServiceManager().newScheduledThreadPool(getId(), getThreadName(), profile);
    } else {
        answer = getCamelContext().getExecutorServiceManager().newThreadPool(getId(), getThreadName(), profile);
    }
    return answer;
}
Also used : ThreadPoolProfile(org.apache.camel.spi.ThreadPoolProfile) ThreadPoolProfileBuilder(org.apache.camel.builder.ThreadPoolProfileBuilder) ExecutorService(java.util.concurrent.ExecutorService)

Example 2 with ThreadPoolProfile

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

the class DefaultErrorHandlerBuilder method getExecutorService.

protected synchronized ScheduledExecutorService getExecutorService(CamelContext camelContext) {
    if (executorService == null || executorService.isShutdown()) {
        // camel context will shutdown the executor when it shutdown so no need to shut it down when stopping
        if (executorServiceRef != null) {
            executorService = camelContext.getRegistry().lookupByNameAndType(executorServiceRef, ScheduledExecutorService.class);
            if (executorService == null) {
                ExecutorServiceManager manager = camelContext.getExecutorServiceManager();
                ThreadPoolProfile profile = manager.getThreadPoolProfile(executorServiceRef);
                executorService = manager.newScheduledThreadPool(this, executorServiceRef, profile);
            }
            if (executorService == null) {
                throw new IllegalArgumentException("ExecutorServiceRef " + executorServiceRef + " not found in registry.");
            }
        } else {
            // no explicit configured thread pool, so leave it up to the error handler to decide if it need
            // a default thread pool from CamelContext#getErrorHandlerExecutorService
            executorService = null;
        }
    }
    return executorService;
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ThreadPoolProfile(org.apache.camel.spi.ThreadPoolProfile) ExecutorServiceManager(org.apache.camel.spi.ExecutorServiceManager)

Example 3 with ThreadPoolProfile

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

the class CamelCustomDefaultThreadPoolProfileTest method testCamelCustomDefaultThreadPoolProfile.

public void testCamelCustomDefaultThreadPoolProfile() throws Exception {
    DefaultExecutorServiceManager manager = (DefaultExecutorServiceManager) context.getExecutorServiceManager();
    ThreadPoolProfile profile = manager.getDefaultThreadPoolProfile();
    assertEquals(5, profile.getPoolSize().intValue());
    assertEquals(15, profile.getMaxPoolSize().intValue());
    assertEquals(25, profile.getKeepAliveTime().longValue());
    assertEquals(250, profile.getMaxQueueSize().intValue());
    assertEquals(true, profile.getAllowCoreThreadTimeOut().booleanValue());
    assertEquals(ThreadPoolRejectedPolicy.Abort, profile.getRejectedPolicy());
}
Also used : ThreadPoolProfile(org.apache.camel.spi.ThreadPoolProfile)

Example 4 with ThreadPoolProfile

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

the class AggregateThreadPoolProfileTest method createRouteBuilder.

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

        @Override
        public void configure() throws Exception {
            // create and register thread pool profile
            ThreadPoolProfile profile = new ThreadPoolProfile("myProfile");
            profile.setPoolSize(2);
            profile.setMaxPoolSize(8);
            profile.setRejectedPolicy(ThreadPoolRejectedPolicy.Abort);
            context.getExecutorServiceManager().registerThreadPoolProfile(profile);
            from("direct:start").aggregate(header("id"), new BodyInAggregatingStrategy()).completionSize(3).executorServiceRef("myProfile").to("log:foo").to("mock:aggregated");
        }
    };
}
Also used : ThreadPoolProfile(org.apache.camel.spi.ThreadPoolProfile) RouteBuilder(org.apache.camel.builder.RouteBuilder) BodyInAggregatingStrategy(org.apache.camel.processor.BodyInAggregatingStrategy)

Example 5 with ThreadPoolProfile

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

the class DefaultExecutorServiceManager method newThreadPool.

@Override
public ExecutorService newThreadPool(Object source, String name, ThreadPoolProfile profile) {
    String sanitizedName = URISupport.sanitizeUri(name);
    ObjectHelper.notNull(profile, "ThreadPoolProfile");
    ThreadPoolProfile defaultProfile = getDefaultThreadPoolProfile();
    profile.addDefaults(defaultProfile);
    ThreadFactory threadFactory = createThreadFactory(sanitizedName, true);
    ExecutorService executorService = threadPoolFactory.newThreadPool(profile, threadFactory);
    onThreadPoolCreated(executorService, source, profile.getId());
    if (LOG.isDebugEnabled()) {
        LOG.debug("Created new ThreadPool for source: {} with name: {}. -> {}", source, sanitizedName, executorService);
    }
    return executorService;
}
Also used : ThreadFactory(java.util.concurrent.ThreadFactory) CamelThreadFactory(org.apache.camel.util.concurrent.CamelThreadFactory) ThreadPoolProfile(org.apache.camel.spi.ThreadPoolProfile) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ExecutorService(java.util.concurrent.ExecutorService) SizedScheduledExecutorService(org.apache.camel.util.concurrent.SizedScheduledExecutorService)

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