diff --git a/org.springframework.context/.classpath b/org.springframework.context/.classpath index bafbbecdeec..ede89d7aae5 100644 --- a/org.springframework.context/.classpath +++ b/org.springframework.context/.classpath @@ -30,6 +30,7 @@ + diff --git a/org.springframework.context/context.iml b/org.springframework.context/context.iml index aa82b9252ba..7b1d191803d 100644 --- a/org.springframework.context/context.iml +++ b/org.springframework.context/context.iml @@ -102,6 +102,28 @@ + + + + + + + + + + + + + + + + + + + + + + @@ -155,17 +177,6 @@ - - - - - - - - - - - diff --git a/org.springframework.context/ivy.xml b/org.springframework.context/ivy.xml index 768cef067a1..9f84e3bee31 100644 --- a/org.springframework.context/ivy.xml +++ b/org.springframework.context/ivy.xml @@ -50,6 +50,7 @@ + diff --git a/org.springframework.context/src/main/java/org/springframework/scheduling/concurrent/ForkJoinPoolFactoryBean.java b/org.springframework.context/src/main/java/org/springframework/scheduling/concurrent/ForkJoinPoolFactoryBean.java new file mode 100644 index 00000000000..b4bc23b0ea5 --- /dev/null +++ b/org.springframework.context/src/main/java/org/springframework/scheduling/concurrent/ForkJoinPoolFactoryBean.java @@ -0,0 +1,108 @@ +/* + * Copyright 2002-2011 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.scheduling.concurrent; + +import java.util.concurrent.ForkJoinPool; + +import org.springframework.beans.factory.DisposableBean; +import org.springframework.beans.factory.FactoryBean; +import org.springframework.beans.factory.InitializingBean; + +/** + * A Spring {@link FactoryBean} that builds and exposes a preconfigured {@link ForkJoinPool}. + * May be used on Java 7 as well as on Java 6 with jsr166.jar on the classpath + * (ideally on the VM bootstrap classpath). + * + *

For details on the ForkJoinPool API and its its use with RecursiveActions, see the + * JDK 7 javadoc. + * + *

jsr166.jar, containing java.util.concurrent updates for Java 6, can be obtained + * from the concurrency interest website. + * + * @author Juergen Hoeller + * @since 3.1 + */ +public class ForkJoinPoolFactoryBean implements FactoryBean, InitializingBean, DisposableBean { + + private int parallelism = Runtime.getRuntime().availableProcessors(); + + private ForkJoinPool.ForkJoinWorkerThreadFactory threadFactory = ForkJoinPool.defaultForkJoinWorkerThreadFactory; + + private Thread.UncaughtExceptionHandler uncaughtExceptionHandler; + + private boolean asyncMode = false; + + private ForkJoinPool forkJoinPool; + + + /** + * Specify the parallelism level. Default is {@link Runtime#availableProcessors()}. + */ + public void setParallelism(int parallelism) { + this.parallelism = parallelism; + } + + /** + * Set the factory for creating new ForkJoinWorkerThreads. + * Default is {@link ForkJoinPool#defaultForkJoinWorkerThreadFactory}. + */ + public void setThreadFactory(ForkJoinPool.ForkJoinWorkerThreadFactory threadFactory) { + this.threadFactory = threadFactory; + } + + /** + * Set the handler for internal worker threads that terminate due to unrecoverable errors + * encountered while executing tasks. Default is none. + */ + public void setUncaughtExceptionHandler(Thread.UncaughtExceptionHandler uncaughtExceptionHandler) { + this.uncaughtExceptionHandler = uncaughtExceptionHandler; + } + + /** + * Specify whether to establish a local first-in-first-out scheduling mode for forked tasks + * that are never joined. This mode (asyncMode = true) may be more appropriate + * than the default locally stack-based mode in applications in which worker threads only + * process event-style asynchronous tasks. Default is false. + */ + public void setAsyncMode(boolean asyncMode) { + this.asyncMode = asyncMode; + } + + public void afterPropertiesSet() { + this.forkJoinPool = + new ForkJoinPool(this.parallelism, this.threadFactory, this.uncaughtExceptionHandler, this.asyncMode); + } + + + public ForkJoinPool getObject() { + return this.forkJoinPool; + } + + public Class getObjectType() { + return ForkJoinPool.class; + } + + public boolean isSingleton() { + return true; + } + + + public void destroy() { + this.forkJoinPool.shutdown(); + } + +}