KickJava   Java API By Example, From Geeks To Geeks.

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

50 public class ConcurrentTaskExecutor implements SchedulingTaskExecutor, Executor JavaDoc {
51
52     private Executor JavaDoc concurrentExecutor;
53
54
55     /**
56      * Create a new ConcurrentTaskExecutor,
57      * using a single thread executor as default.
58      * @see java.util.concurrent.Executors#newSingleThreadExecutor()
59      */

60     public ConcurrentTaskExecutor() {
61         setConcurrentExecutor(null);
62     }
63
64     /**
65      * Create a new ConcurrentTaskExecutor,
66      * using the given JDK 1.5 concurrent executor.
67      * @param concurrentExecutor the JDK 1.5 concurrent executor to delegate to
68      */

69     public ConcurrentTaskExecutor(Executor JavaDoc concurrentExecutor) {
70         setConcurrentExecutor(concurrentExecutor);
71     }
72
73
74     /**
75      * Specify the JDK 1.5 concurrent executor to delegate to.
76      */

77     public void setConcurrentExecutor(Executor JavaDoc concurrentExecutor) {
78         this.concurrentExecutor =
79                 (concurrentExecutor != null ? concurrentExecutor : Executors.newSingleThreadExecutor());
80     }
81
82     /**
83      * Return the JDK 1.5 concurrent executor that this adapter
84      * delegates to.
85      */

86     public Executor JavaDoc getConcurrentExecutor() {
87         return this.concurrentExecutor;
88     }
89
90
91     /**
92      * Delegates to the specified JDK 1.5 concurrent executor.
93      * @see java.util.concurrent.Executor#execute(Runnable)
94      */

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

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