001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.servicemix.common;
018:
019: import java.util.concurrent.CountDownLatch;
020: import java.util.concurrent.ExecutorService;
021: import java.util.concurrent.Executors;
022:
023: import javax.resource.spi.work.ExecutionContext;
024: import javax.resource.spi.work.Work;
025: import javax.resource.spi.work.WorkException;
026: import javax.resource.spi.work.WorkListener;
027: import javax.resource.spi.work.WorkManager;
028:
029: /**
030: * A simple WorkManager implementation on top of java.util.concurrent thread pool.
031: *
032: * @deprecated Components should use the executor on the ServiceMixComponent
033: * for thread pools
034: */
035: public class BasicWorkManager implements WorkManager {
036:
037: private final ExecutorService executor;
038:
039: public BasicWorkManager() {
040: executor = Executors.newCachedThreadPool();
041: }
042:
043: public void shutDown() {
044: executor.shutdown();
045: }
046:
047: /* (non-Javadoc)
048: * @see javax.resource.spi.work.WorkManager#doWork(javax.resource.spi.work.Work)
049: */
050: public void doWork(Work work) throws WorkException {
051: work.run();
052: }
053:
054: /* (non-Javadoc)
055: * @see javax.resource.spi.work.WorkManager#doWork(javax.resource.spi.work.Work, long, javax.resource.spi.work.ExecutionContext, javax.resource.spi.work.WorkListener)
056: */
057: public void doWork(Work work, long startTimeout,
058: ExecutionContext execContext, WorkListener workListener)
059: throws WorkException {
060: throw new RuntimeException("Not implemented");
061: }
062:
063: /* (non-Javadoc)
064: * @see javax.resource.spi.work.WorkManager#startWork(javax.resource.spi.work.Work)
065: */
066: public long startWork(final Work work) throws WorkException {
067: final CountDownLatch latch = new CountDownLatch(1);
068: executor.execute(new Work() {
069: public void release() {
070: work.release();
071: }
072:
073: public void run() {
074: latch.countDown();
075: work.run();
076: }
077: });
078: long t = System.currentTimeMillis();
079: try {
080: latch.await();
081: } catch (InterruptedException e) {
082: throw new WorkException("Interrupted", e);
083: }
084: return System.currentTimeMillis() - t;
085: }
086:
087: /* (non-Javadoc)
088: * @see javax.resource.spi.work.WorkManager#startWork(javax.resource.spi.work.Work, long, javax.resource.spi.work.ExecutionContext, javax.resource.spi.work.WorkListener)
089: */
090: public long startWork(Work work, long startTimeout,
091: ExecutionContext execContext, WorkListener workListener)
092: throws WorkException {
093: throw new RuntimeException("Not implemented");
094: }
095:
096: /* (non-Javadoc)
097: * @see javax.resource.spi.work.WorkManager#scheduleWork(javax.resource.spi.work.Work)
098: */
099: public void scheduleWork(Work work) throws WorkException {
100: executor.execute(work);
101: }
102:
103: /* (non-Javadoc)
104: * @see javax.resource.spi.work.WorkManager#scheduleWork(javax.resource.spi.work.Work, long, javax.resource.spi.work.ExecutionContext, javax.resource.spi.work.WorkListener)
105: */
106: public void scheduleWork(Work work, long startTimeout,
107: ExecutionContext execContext, WorkListener workListener)
108: throws WorkException {
109: throw new RuntimeException("Not implemented");
110: }
111:
112: }
|