KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > connector > work > GeronimoWorkManager


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.geronimo.connector.work;
19
20 import javax.resource.spi.work.ExecutionContext JavaDoc;
21 import javax.resource.spi.work.Work JavaDoc;
22 import javax.resource.spi.work.WorkCompletedException JavaDoc;
23 import javax.resource.spi.work.WorkException JavaDoc;
24 import javax.resource.spi.work.WorkListener JavaDoc;
25 import javax.resource.spi.work.WorkManager JavaDoc;
26
27 import edu.emory.mathcs.backport.java.util.concurrent.Executor;
28
29 import org.apache.geronimo.connector.work.pool.ScheduleWorkExecutor;
30 import org.apache.geronimo.connector.work.pool.StartWorkExecutor;
31 import org.apache.geronimo.connector.work.pool.SyncWorkExecutor;
32 import org.apache.geronimo.connector.work.pool.WorkExecutor;
33 import org.apache.geronimo.transaction.manager.XAWork;
34
35 /**
36  * WorkManager implementation which uses under the cover three WorkExecutorPool
37  * - one for each synchronization policy - in order to dispatch the submitted
38  * Work instances.
39  * <P>
40  * A WorkManager is a component of the JCA specifications, which allows a
41  * Resource Adapter to submit tasks to an Application Server for execution.
42  *
43  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
44  */

45 public class GeronimoWorkManager implements WorkManager JavaDoc {
46
47 // private final static int DEFAULT_POOL_SIZE = 10;
48

49     /**
50      * Pool of threads used by this WorkManager in order to process
51      * the Work instances submitted via the doWork methods.
52      */

53     private Executor syncWorkExecutorPool;
54
55     /**
56      * Pool of threads used by this WorkManager in order to process
57      * the Work instances submitted via the startWork methods.
58      */

59     private Executor startWorkExecutorPool;
60
61     /**
62      * Pool of threads used by this WorkManager in order to process
63      * the Work instances submitted via the scheduleWork methods.
64      */

65     private Executor scheduledWorkExecutorPool;
66
67     private final XAWork transactionManager;
68
69     private final WorkExecutor scheduleWorkExecutor = new ScheduleWorkExecutor();
70     private final WorkExecutor startWorkExecutor = new StartWorkExecutor();
71     private final WorkExecutor syncWorkExecutor = new SyncWorkExecutor();
72
73     /**
74      * Create a WorkManager.
75      */

76     public GeronimoWorkManager() {
77         this(null, null, null, null);
78     }
79
80     public GeronimoWorkManager(Executor sync, Executor start, Executor sched, XAWork xaWork) {
81         syncWorkExecutorPool = sync;
82         startWorkExecutorPool = start;
83         scheduledWorkExecutorPool = sched;
84         this.transactionManager = xaWork;
85     }
86
87     public void doStart() throws Exception JavaDoc {
88     }
89
90     public void doStop() throws Exception JavaDoc {
91     }
92
93     public void doFail() {
94         try {
95             doStop();
96         } catch (Exception JavaDoc e) {
97             //TODO what to do?
98
}
99     }
100
101     public Executor getSyncWorkExecutorPool() {
102         return syncWorkExecutorPool;
103     }
104
105     public Executor getStartWorkExecutorPool() {
106         return startWorkExecutorPool;
107     }
108
109     public Executor getScheduledWorkExecutorPool() {
110         return scheduledWorkExecutorPool;
111     }
112
113     /* (non-Javadoc)
114     * @see javax.resource.spi.work.WorkManager#doWork(javax.resource.spi.work.Work)
115     */

116     public void doWork(Work JavaDoc work) throws WorkException JavaDoc {
117         executeWork(new WorkerContext(work, transactionManager), syncWorkExecutor, syncWorkExecutorPool);
118     }
119
120     /* (non-Javadoc)
121      * @see javax.resource.spi.work.WorkManager#doWork(javax.resource.spi.work.Work, long, javax.resource.spi.work.ExecutionContext, javax.resource.spi.work.WorkListener)
122      */

123     public void doWork(
124             Work JavaDoc work,
125             long startTimeout,
126             ExecutionContext JavaDoc execContext,
127             WorkListener JavaDoc workListener)
128             throws WorkException JavaDoc {
129         WorkerContext workWrapper =
130                 new WorkerContext(work, startTimeout, execContext, transactionManager, workListener);
131         workWrapper.setThreadPriority(Thread.currentThread().getPriority());
132         executeWork(workWrapper, syncWorkExecutor, syncWorkExecutorPool);
133     }
134
135     /* (non-Javadoc)
136      * @see javax.resource.spi.work.WorkManager#startWork(javax.resource.spi.work.Work)
137      */

138     public long startWork(Work JavaDoc work) throws WorkException JavaDoc {
139         WorkerContext workWrapper = new WorkerContext(work, transactionManager);
140         workWrapper.setThreadPriority(Thread.currentThread().getPriority());
141         executeWork(workWrapper, startWorkExecutor, startWorkExecutorPool);
142         return System.currentTimeMillis() - workWrapper.getAcceptedTime();
143     }
144
145     /* (non-Javadoc)
146      * @see javax.resource.spi.work.WorkManager#startWork(javax.resource.spi.work.Work, long, javax.resource.spi.work.ExecutionContext, javax.resource.spi.work.WorkListener)
147      */

148     public long startWork(
149             Work JavaDoc work,
150             long startTimeout,
151             ExecutionContext JavaDoc execContext,
152             WorkListener JavaDoc workListener)
153             throws WorkException JavaDoc {
154         WorkerContext workWrapper =
155                 new WorkerContext(work, startTimeout, execContext, transactionManager, workListener);
156         workWrapper.setThreadPriority(Thread.currentThread().getPriority());
157         executeWork(workWrapper, startWorkExecutor, startWorkExecutorPool);
158         return System.currentTimeMillis() - workWrapper.getAcceptedTime();
159     }
160
161     /* (non-Javadoc)
162      * @see javax.resource.spi.work.WorkManager#scheduleWork(javax.resource.spi.work.Work)
163      */

164     public void scheduleWork(Work JavaDoc work) throws WorkException JavaDoc {
165         WorkerContext workWrapper = new WorkerContext(work, transactionManager);
166         workWrapper.setThreadPriority(Thread.currentThread().getPriority());
167         executeWork(workWrapper, scheduleWorkExecutor, scheduledWorkExecutorPool);
168     }
169
170     /* (non-Javadoc)
171      * @see javax.resource.spi.work.WorkManager#scheduleWork(javax.resource.spi.work.Work, long, javax.resource.spi.work.ExecutionContext, javax.resource.spi.work.WorkListener)
172      */

173     public void scheduleWork(
174             Work JavaDoc work,
175             long startTimeout,
176             ExecutionContext JavaDoc execContext,
177             WorkListener JavaDoc workListener)
178             throws WorkException JavaDoc {
179         WorkerContext workWrapper =
180                 new WorkerContext(work, startTimeout, execContext, transactionManager, workListener);
181         workWrapper.setThreadPriority(Thread.currentThread().getPriority());
182         executeWork(workWrapper, scheduleWorkExecutor, scheduledWorkExecutorPool);
183     }
184
185     /**
186      * Execute the specified Work.
187      *
188      * @param work Work to be executed.
189      *
190      * @exception WorkException Indicates that the Work execution has been
191      * unsuccessful.
192      */

193     private void executeWork(WorkerContext work, WorkExecutor workExecutor, Executor pooledExecutor) throws WorkException JavaDoc {
194         work.workAccepted(this);
195         try {
196             workExecutor.doExecute(work, pooledExecutor);
197             WorkException JavaDoc exception = work.getWorkException();
198             if (null != exception) {
199                 throw exception;
200             }
201         } catch (InterruptedException JavaDoc e) {
202             WorkCompletedException JavaDoc wcj = new WorkCompletedException JavaDoc(
203                     "The execution has been interrupted.", e);
204             wcj.setErrorCode(WorkException.INTERNAL);
205             throw wcj;
206         }
207     }
208
209 }
210
Popular Tags