KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > common > BasicWorkManager


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 package org.apache.servicemix.common;
18
19 import edu.emory.mathcs.backport.java.util.concurrent.CountDownLatch;
20 import edu.emory.mathcs.backport.java.util.concurrent.ExecutorService;
21 import edu.emory.mathcs.backport.java.util.concurrent.Executors;
22
23 import javax.resource.spi.work.ExecutionContext JavaDoc;
24 import javax.resource.spi.work.Work JavaDoc;
25 import javax.resource.spi.work.WorkException JavaDoc;
26 import javax.resource.spi.work.WorkListener JavaDoc;
27 import javax.resource.spi.work.WorkManager JavaDoc;
28
29 public class BasicWorkManager implements WorkManager JavaDoc {
30
31     private final ExecutorService executor;
32     
33     public BasicWorkManager() {
34         executor = Executors.newCachedThreadPool();
35     }
36     
37     public void shutDown() {
38         executor.shutdown();
39     }
40     
41     /* (non-Javadoc)
42      * @see javax.resource.spi.work.WorkManager#doWork(javax.resource.spi.work.Work)
43      */

44     public void doWork(Work JavaDoc work) throws WorkException JavaDoc {
45         work.run();
46     }
47
48     /* (non-Javadoc)
49      * @see javax.resource.spi.work.WorkManager#doWork(javax.resource.spi.work.Work, long, javax.resource.spi.work.ExecutionContext, javax.resource.spi.work.WorkListener)
50      */

51     public void doWork(
52             Work JavaDoc work,
53             long startTimeout,
54             ExecutionContext JavaDoc execContext,
55             WorkListener JavaDoc workListener)
56             throws WorkException JavaDoc {
57         throw new RuntimeException JavaDoc("Not implemented");
58     }
59
60     /* (non-Javadoc)
61      * @see javax.resource.spi.work.WorkManager#startWork(javax.resource.spi.work.Work)
62      */

63     public long startWork(final Work JavaDoc work) throws WorkException JavaDoc {
64         final CountDownLatch latch = new CountDownLatch(1);
65         executor.execute(new Work JavaDoc() {
66             public void release() {
67                 work.release();
68             }
69             public void run() {
70                 latch.countDown();
71                 work.run();
72             }
73         });
74         long t = System.currentTimeMillis();
75         try {
76             latch.await();
77         } catch (InterruptedException JavaDoc e) {
78             throw new WorkException JavaDoc("Interrupted", e);
79         }
80         return System.currentTimeMillis() - t;
81     }
82
83     /* (non-Javadoc)
84      * @see javax.resource.spi.work.WorkManager#startWork(javax.resource.spi.work.Work, long, javax.resource.spi.work.ExecutionContext, javax.resource.spi.work.WorkListener)
85      */

86     public long startWork(
87             Work JavaDoc work,
88             long startTimeout,
89             ExecutionContext JavaDoc execContext,
90             WorkListener JavaDoc workListener)
91             throws WorkException JavaDoc {
92         throw new RuntimeException JavaDoc("Not implemented");
93     }
94
95     /* (non-Javadoc)
96      * @see javax.resource.spi.work.WorkManager#scheduleWork(javax.resource.spi.work.Work)
97      */

98     public void scheduleWork(Work JavaDoc work) throws WorkException JavaDoc {
99         executor.execute(work);
100     }
101
102     /* (non-Javadoc)
103      * @see javax.resource.spi.work.WorkManager#scheduleWork(javax.resource.spi.work.Work, long, javax.resource.spi.work.ExecutionContext, javax.resource.spi.work.WorkListener)
104      */

105     public void scheduleWork(
106             Work JavaDoc work,
107             long startTimeout,
108             ExecutionContext JavaDoc execContext,
109             WorkListener JavaDoc workListener)
110             throws WorkException JavaDoc {
111         throw new RuntimeException JavaDoc("Not implemented");
112     }
113
114 }
115
Popular Tags