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(
058 Work work,
059 long startTimeout,
060 ExecutionContext execContext,
061 WorkListener workListener)
062 throws WorkException {
063 throw new RuntimeException("Not implemented");
064 }
065
066 /* (non-Javadoc)
067 * @see javax.resource.spi.work.WorkManager#startWork(javax.resource.spi.work.Work)
068 */
069 public long startWork(final Work work) throws WorkException {
070 final CountDownLatch latch = new CountDownLatch(1);
071 executor.execute(new Work() {
072 public void release() {
073 work.release();
074 }
075 public void run() {
076 latch.countDown();
077 work.run();
078 }
079 });
080 long t = System.currentTimeMillis();
081 try {
082 latch.await();
083 } catch (InterruptedException e) {
084 throw new WorkException("Interrupted", e);
085 }
086 return System.currentTimeMillis() - t;
087 }
088
089 /* (non-Javadoc)
090 * @see javax.resource.spi.work.WorkManager#startWork(javax.resource.spi.work.Work, long, javax.resource.spi.work.ExecutionContext, javax.resource.spi.work.WorkListener)
091 */
092 public long startWork(
093 Work work,
094 long startTimeout,
095 ExecutionContext execContext,
096 WorkListener workListener)
097 throws WorkException {
098 throw new RuntimeException("Not implemented");
099 }
100
101 /* (non-Javadoc)
102 * @see javax.resource.spi.work.WorkManager#scheduleWork(javax.resource.spi.work.Work)
103 */
104 public void scheduleWork(Work work) throws WorkException {
105 executor.execute(work);
106 }
107
108 /* (non-Javadoc)
109 * @see javax.resource.spi.work.WorkManager#scheduleWork(javax.resource.spi.work.Work, long, javax.resource.spi.work.ExecutionContext, javax.resource.spi.work.WorkListener)
110 */
111 public void scheduleWork(
112 Work work,
113 long startTimeout,
114 ExecutionContext execContext,
115 WorkListener workListener)
116 throws WorkException {
117 throw new RuntimeException("Not implemented");
118 }
119
120 }