Allow configuring custom ThreadPoolTaskExecutor

Issue: SPR-12272
This commit is contained in:
Rossen Stoyanchev
2014-09-29 23:02:40 -04:00
parent 179b236608
commit 521bbfcf56
4 changed files with 29 additions and 35 deletions

View File

@@ -21,6 +21,7 @@ import java.util.Arrays;
import java.util.List;
import org.springframework.messaging.support.ChannelInterceptor;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
/**
* A registration class for customizing the configuration for a
@@ -46,6 +47,17 @@ public class ChannelRegistration {
return this.registration;
}
/**
* Configure the thread pool backing this message channel using a custom
* ThreadPoolTaskExecutor.
*/
public TaskExecutorRegistration taskExecutor(ThreadPoolTaskExecutor taskExecutor) {
if (this.registration == null) {
this.registration = new TaskExecutorRegistration(taskExecutor);
}
return this.registration;
}
/**
* Configure interceptors for the message channel.
*/

View File

@@ -26,6 +26,8 @@ import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
*/
public class TaskExecutorRegistration {
private ThreadPoolTaskExecutor taskExecutor;
private int corePoolSize = Runtime.getRuntime().availableProcessors() * 2;
private int maxPoolSize = Integer.MAX_VALUE;
@@ -35,6 +37,13 @@ public class TaskExecutorRegistration {
private int keepAliveSeconds = 60;
public TaskExecutorRegistration() {
}
public TaskExecutorRegistration(ThreadPoolTaskExecutor taskExecutor) {
this.taskExecutor = taskExecutor;
}
/**
* Set the core pool size of the ThreadPoolExecutor.
* <p><strong>NOTE:</strong> The core pool size is effectively the max pool size
@@ -93,7 +102,7 @@ public class TaskExecutorRegistration {
}
protected ThreadPoolTaskExecutor getTaskExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
ThreadPoolTaskExecutor executor = (this.taskExecutor != null ? this.taskExecutor : new ThreadPoolTaskExecutor());
executor.setCorePoolSize(this.corePoolSize);
executor.setMaxPoolSize(this.maxPoolSize);
executor.setKeepAliveSeconds(this.keepAliveSeconds);

View File

@@ -1,31 +0,0 @@
/*
* Copyright 2002-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.messaging.support;
/**
* @author Rossen Stoyanchev
* @since 4.1
*/
public class Test {
public static void main(String[] args) {
ExecutorSubscribableChannel.ExecutorSubscribableChannelTask task = null;
}
}