KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > scheduling > backportconcurrent > ConcurrentTaskExecutor


1 /*
2  * Copyright 2002-2007 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.scheduling.backportconcurrent;
18
19 import edu.emory.mathcs.backport.java.util.concurrent.Executor;
20 import edu.emory.mathcs.backport.java.util.concurrent.Executors;
21 import edu.emory.mathcs.backport.java.util.concurrent.RejectedExecutionException;
22
23 import org.springframework.core.task.TaskRejectedException;
24 import org.springframework.scheduling.SchedulingTaskExecutor;
25
26 /**
27  * Adapter that takes a JSR-166 backport
28  * <code>edu.emory.mathcs.backport.java.util.concurrent.Executor</code> and
29  * exposes a Spring {@link org.springframework.core.task.TaskExecutor} for it.
30  *
31  * <p><b>NOTE:</b> This class implements Spring's
32  * {@link org.springframework.core.task.TaskExecutor} interface as well as
33  * the JSR-166 {@link edu.emory.mathcs.backport.java.util.concurrent.Executor}
34  * interface, with the former being the primary interface, the other just
35  * serving as secondary convenience. For this reason, the exception handling
36  * follows the TaskExecutor contract rather than the Executor contract, in
37  * particular regarding the {@link org.springframework.core.task.TaskRejectedException}.
38  *
39  * <p>Note that there is a pre-built {@link ThreadPoolTaskExecutor} that allows for
40  * defining a JSR-166 backport
41  * {@link edu.emory.mathcs.backport.java.util.concurrent.ThreadPoolExecutor} in bean
42  * style, exposing it as a Spring {@link org.springframework.core.task.TaskExecutor}
43  * directly. This is a convenient alternative to a raw ThreadPoolExecutor
44  * definition with a separate definition of the present adapter class.
45  *
46  * @author Juergen Hoeller
47  * @since 2.0.3
48  * @see edu.emory.mathcs.backport.java.util.concurrent.Executor
49  * @see edu.emory.mathcs.backport.java.util.concurrent.ThreadPoolExecutor
50  * @see edu.emory.mathcs.backport.java.util.concurrent.Executors
51  * @see ThreadPoolTaskExecutor
52  */

53 public class ConcurrentTaskExecutor implements SchedulingTaskExecutor, Executor {
54
55     private Executor concurrentExecutor;
56
57
58     /**
59      * Create a new ConcurrentTaskExecutor,
60      * using a single thread executor as default.
61      * @see edu.emory.mathcs.backport.java.util.concurrent.Executors#newSingleThreadExecutor()
62      */

63     public ConcurrentTaskExecutor() {
64         setConcurrentExecutor(null);
65     }
66
67     /**
68      * Create a new ConcurrentTaskExecutor,
69      * using the given JSR-166 backport concurrent executor.
70      * @param concurrentExecutor the JSR-166 backport concurrent executor to delegate to
71      */

72     public ConcurrentTaskExecutor(Executor concurrentExecutor) {
73         setConcurrentExecutor(concurrentExecutor);
74     }
75
76     /**
77      * Specify the JSR-166 backport concurrent executor to delegate to.
78      */

79     public void setConcurrentExecutor(Executor concurrentExecutor) {
80         this.concurrentExecutor =
81                 (concurrentExecutor != null ? concurrentExecutor : Executors.newSingleThreadExecutor());
82     }
83
84     /**
85      * Return the JSR-166 backport concurrent executor that this adapter
86      * delegates to.
87      */

88     public Executor getConcurrentExecutor() {
89         return this.concurrentExecutor;
90     }
91
92
93     /**
94      * Delegates to the specified JSR-166 backport concurrent executor.
95      * @see edu.emory.mathcs.backport.java.util.concurrent.Executor#execute(Runnable)
96      */

97     public void execute(Runnable JavaDoc task) {
98         try {
99             this.concurrentExecutor.execute(task);
100         }
101         catch (RejectedExecutionException ex) {
102             throw new TaskRejectedException(
103                     "Executor [" + this.concurrentExecutor + "] did not accept task: " + task, ex);
104         }
105     }
106
107     /**
108      * This task executor prefers short-lived work units.
109      */

110     public boolean prefersShortLivedTasks() {
111         return true;
112     }
113
114 }
115
Popular Tags