Search in sources :

Example 1 with CamelThreadFactory

use of org.apache.camel.util.concurrent.CamelThreadFactory in project camel by apache.

the class DefaultJmsMessageListenerContainer method createDefaultTaskExecutor.

/**
     * Create a default TaskExecutor. Called if no explicit TaskExecutor has been specified.
     * <p />
     * The type of {@link TaskExecutor} will depend on the value of
     * {@link JmsConfiguration#getDefaultTaskExecutorType()}. For more details, refer to the Javadoc of
     * {@link DefaultTaskExecutorType}.
     * <p />
     * In all cases, it uses the specified bean name and Camel's {@link org.apache.camel.spi.ExecutorServiceManager}
     * to resolve the thread name.
     * @see JmsConfiguration#setDefaultTaskExecutorType(DefaultTaskExecutorType)
     * @see ThreadPoolTaskExecutor#setBeanName(String)
     */
@Override
protected TaskExecutor createDefaultTaskExecutor() {
    String pattern = endpoint.getCamelContext().getExecutorServiceManager().getThreadNamePattern();
    String beanName = getBeanName() == null ? endpoint.getThreadName() : getBeanName();
    TaskExecutor answer;
    if (endpoint.getDefaultTaskExecutorType() == DefaultTaskExecutorType.ThreadPool) {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setBeanName(beanName);
        executor.setThreadFactory(new CamelThreadFactory(pattern, beanName, true));
        executor.setCorePoolSize(endpoint.getConcurrentConsumers());
        // Direct hand-off mode. Do not queue up tasks: assign it to a thread immediately.
        // We set no upper-bound on the thread pool (no maxPoolSize) as it's already implicitly constrained by
        // maxConcurrentConsumers on the DMLC itself (i.e. DMLC will only grow up to a level of concurrency as
        // defined by maxConcurrentConsumers).
        executor.setQueueCapacity(0);
        executor.initialize();
        answer = executor;
    } else {
        SimpleAsyncTaskExecutor executor = new SimpleAsyncTaskExecutor(beanName);
        executor.setThreadFactory(new CamelThreadFactory(pattern, beanName, true));
        answer = executor;
    }
    taskExecutor = answer;
    return answer;
}
Also used : CamelThreadFactory(org.apache.camel.util.concurrent.CamelThreadFactory) ThreadPoolTaskExecutor(org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor) SimpleAsyncTaskExecutor(org.springframework.core.task.SimpleAsyncTaskExecutor) TaskExecutor(org.springframework.core.task.TaskExecutor) SimpleAsyncTaskExecutor(org.springframework.core.task.SimpleAsyncTaskExecutor) ThreadPoolTaskExecutor(org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor)

Example 2 with CamelThreadFactory

use of org.apache.camel.util.concurrent.CamelThreadFactory in project camel by apache.

the class DefaultNettySharedHttpServer method doStart.

protected void doStart() throws Exception {
    ObjectHelper.notNull(configuration, "setNettyServerBootstrapConfiguration() must be called with a NettyServerBootstrapConfiguration instance", this);
    // port must be set
    if (configuration.getPort() <= 0) {
        throw new IllegalArgumentException("Port must be configured on NettySharedHttpServerBootstrapConfiguration " + configuration);
    }
    // hostname must be set
    if (ObjectHelper.isEmpty(configuration.getHost())) {
        throw new IllegalArgumentException("Host must be configured on NettySharedHttpServerBootstrapConfiguration " + configuration);
    }
    LOG.debug("NettySharedHttpServer using configuration: {}", configuration);
    // force using tcp as the underlying transport
    configuration.setProtocol("tcp");
    channelFactory = new HttpServerMultiplexChannelHandler();
    channelFactory.init(configuration.getPort());
    ChannelPipelineFactory pipelineFactory = new HttpServerSharedPipelineFactory(configuration, channelFactory, classResolver);
    // thread factory and pattern
    String port = Matcher.quoteReplacement("" + configuration.getPort());
    String pattern = threadPattern;
    pattern = pattern.replaceFirst("#port#", port);
    ThreadFactory tf = new CamelThreadFactory(pattern, "NettySharedHttpServer", true);
    // create bootstrap factory and disable compatible check as its shared among the consumers
    bootstrapFactory = new HttpServerBootstrapFactory(channelFactory, false);
    bootstrapFactory.init(tf, configuration, pipelineFactory);
    ServiceHelper.startServices(channelFactory);
    if (startServer) {
        LOG.info("Starting NettySharedHttpServer on {}:{}", configuration.getHost(), configuration.getPort());
        ServiceHelper.startServices(bootstrapFactory);
    }
}
Also used : CamelThreadFactory(org.apache.camel.util.concurrent.CamelThreadFactory) CamelThreadFactory(org.apache.camel.util.concurrent.CamelThreadFactory) ThreadFactory(java.util.concurrent.ThreadFactory) ChannelPipelineFactory(org.jboss.netty.channel.ChannelPipelineFactory) HttpServerMultiplexChannelHandler(org.apache.camel.component.netty.http.handlers.HttpServerMultiplexChannelHandler)

Example 3 with CamelThreadFactory

use of org.apache.camel.util.concurrent.CamelThreadFactory in project camel by apache.

the class DefaultNettySharedHttpServer method doStart.

protected void doStart() throws Exception {
    ObjectHelper.notNull(configuration, "setNettyServerBootstrapConfiguration() must be called with a NettyServerBootstrapConfiguration instance", this);
    // port must be set
    if (configuration.getPort() <= 0) {
        throw new IllegalArgumentException("Port must be configured on NettySharedHttpServerBootstrapConfiguration " + configuration);
    }
    // hostname must be set
    if (ObjectHelper.isEmpty(configuration.getHost())) {
        throw new IllegalArgumentException("Host must be configured on NettySharedHttpServerBootstrapConfiguration " + configuration);
    }
    LOG.debug("NettySharedHttpServer using configuration: {}", configuration);
    // force using tcp as the underlying transport
    configuration.setProtocol("tcp");
    channelFactory = new HttpServerMultiplexChannelHandler();
    channelFactory.init(configuration.getPort());
    ChannelInitializer<Channel> pipelineFactory = new HttpServerSharedInitializerFactory(configuration, channelFactory, classResolver);
    // thread factory and pattern
    String port = Matcher.quoteReplacement("" + configuration.getPort());
    String pattern = threadPattern;
    pattern = pattern.replaceFirst("#port#", port);
    ThreadFactory tf = new CamelThreadFactory(pattern, "NettySharedHttpServer", true);
    // create bootstrap factory and disable compatible check as its shared among the consumers
    bootstrapFactory = new HttpServerBootstrapFactory(channelFactory, false);
    bootstrapFactory.init(tf, configuration, pipelineFactory);
    ServiceHelper.startServices(channelFactory);
    if (startServer) {
        LOG.info("Starting NettySharedHttpServer on {}:{}", configuration.getHost(), configuration.getPort());
        ServiceHelper.startServices(bootstrapFactory);
    }
}
Also used : CamelThreadFactory(org.apache.camel.util.concurrent.CamelThreadFactory) CamelThreadFactory(org.apache.camel.util.concurrent.CamelThreadFactory) ThreadFactory(java.util.concurrent.ThreadFactory) Channel(io.netty.channel.Channel) HttpServerMultiplexChannelHandler(org.apache.camel.component.netty4.http.handlers.HttpServerMultiplexChannelHandler)

Example 4 with CamelThreadFactory

use of org.apache.camel.util.concurrent.CamelThreadFactory in project camel by apache.

the class NettyComponent method createExecutorService.

protected OrderedMemoryAwareThreadPoolExecutor createExecutorService() {
    // use ordered thread pool, to ensure we process the events in order, and can send back
    // replies in the expected order. eg this is required by TCP.
    // and use a Camel thread factory so we have consistent thread namings
    // we should use a shared thread pool as recommended by Netty
    // NOTE: if we don't specify the MaxChannelMemorySize and MaxTotalMemorySize, the thread pool
    // could eat up all the heap memory when the tasks are added very fast
    String pattern = getCamelContext().getExecutorServiceManager().getThreadNamePattern();
    ThreadFactory factory = new CamelThreadFactory(pattern, "NettyOrderedWorker", true);
    return new OrderedMemoryAwareThreadPoolExecutor(getMaximumPoolSize(), configuration.getMaxChannelMemorySize(), configuration.getMaxTotalMemorySize(), 30, TimeUnit.SECONDS, factory);
}
Also used : CamelThreadFactory(org.apache.camel.util.concurrent.CamelThreadFactory) CamelThreadFactory(org.apache.camel.util.concurrent.CamelThreadFactory) ThreadFactory(java.util.concurrent.ThreadFactory) OrderedMemoryAwareThreadPoolExecutor(org.jboss.netty.handler.execution.OrderedMemoryAwareThreadPoolExecutor)

Example 5 with CamelThreadFactory

use of org.apache.camel.util.concurrent.CamelThreadFactory in project camel by apache.

the class NettyComponent method createExecutorService.

protected EventExecutorGroup createExecutorService() {
    // Provide the executor service for the application 
    // and use a Camel thread factory so we have consistent thread namings
    // we should use a shared thread pool as recommended by Netty
    String pattern = getCamelContext().getExecutorServiceManager().getThreadNamePattern();
    ThreadFactory factory = new CamelThreadFactory(pattern, "NettyEventExecutorGroup", true);
    return new DefaultEventExecutorGroup(getMaximumPoolSize(), factory);
}
Also used : CamelThreadFactory(org.apache.camel.util.concurrent.CamelThreadFactory) CamelThreadFactory(org.apache.camel.util.concurrent.CamelThreadFactory) ThreadFactory(java.util.concurrent.ThreadFactory) DefaultEventExecutorGroup(io.netty.util.concurrent.DefaultEventExecutorGroup)

Aggregations

CamelThreadFactory (org.apache.camel.util.concurrent.CamelThreadFactory)5 ThreadFactory (java.util.concurrent.ThreadFactory)4 Channel (io.netty.channel.Channel)1 DefaultEventExecutorGroup (io.netty.util.concurrent.DefaultEventExecutorGroup)1 HttpServerMultiplexChannelHandler (org.apache.camel.component.netty.http.handlers.HttpServerMultiplexChannelHandler)1 HttpServerMultiplexChannelHandler (org.apache.camel.component.netty4.http.handlers.HttpServerMultiplexChannelHandler)1 ChannelPipelineFactory (org.jboss.netty.channel.ChannelPipelineFactory)1 OrderedMemoryAwareThreadPoolExecutor (org.jboss.netty.handler.execution.OrderedMemoryAwareThreadPoolExecutor)1 SimpleAsyncTaskExecutor (org.springframework.core.task.SimpleAsyncTaskExecutor)1 TaskExecutor (org.springframework.core.task.TaskExecutor)1 ThreadPoolTaskExecutor (org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor)1