1 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 ; 24 import javax.resource.spi.work.Work ; 25 import javax.resource.spi.work.WorkException ; 26 import javax.resource.spi.work.WorkListener ; 27 import javax.resource.spi.work.WorkManager ; 28 29 public class BasicWorkManager implements WorkManager { 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 44 public void doWork(Work work) throws WorkException { 45 work.run(); 46 } 47 48 51 public void doWork( 52 Work work, 53 long startTimeout, 54 ExecutionContext execContext, 55 WorkListener workListener) 56 throws WorkException { 57 throw new RuntimeException ("Not implemented"); 58 } 59 60 63 public long startWork(final Work work) throws WorkException { 64 final CountDownLatch latch = new CountDownLatch(1); 65 executor.execute(new Work () { 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 e) { 78 throw new WorkException ("Interrupted", e); 79 } 80 return System.currentTimeMillis() - t; 81 } 82 83 86 public long startWork( 87 Work work, 88 long startTimeout, 89 ExecutionContext execContext, 90 WorkListener workListener) 91 throws WorkException { 92 throw new RuntimeException ("Not implemented"); 93 } 94 95 98 public void scheduleWork(Work work) throws WorkException { 99 executor.execute(work); 100 } 101 102 105 public void scheduleWork( 106 Work work, 107 long startTimeout, 108 ExecutionContext execContext, 109 WorkListener workListener) 110 throws WorkException { 111 throw new RuntimeException ("Not implemented"); 112 } 113 114 } 115 | Popular Tags |