1 17 18 package org.apache.geronimo.connector.work; 19 20 import javax.resource.spi.work.ExecutionContext ; 21 import javax.resource.spi.work.Work ; 22 import javax.resource.spi.work.WorkCompletedException ; 23 import javax.resource.spi.work.WorkException ; 24 import javax.resource.spi.work.WorkListener ; 25 import javax.resource.spi.work.WorkManager ; 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 45 public class GeronimoWorkManager implements WorkManager { 46 47 49 53 private Executor syncWorkExecutorPool; 54 55 59 private Executor startWorkExecutorPool; 60 61 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 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 { 88 } 89 90 public void doStop() throws Exception { 91 } 92 93 public void doFail() { 94 try { 95 doStop(); 96 } catch (Exception e) { 97 } 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 116 public void doWork(Work work) throws WorkException { 117 executeWork(new WorkerContext(work, transactionManager), syncWorkExecutor, syncWorkExecutorPool); 118 } 119 120 123 public void doWork( 124 Work work, 125 long startTimeout, 126 ExecutionContext execContext, 127 WorkListener workListener) 128 throws WorkException { 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 138 public long startWork(Work work) throws WorkException { 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 148 public long startWork( 149 Work work, 150 long startTimeout, 151 ExecutionContext execContext, 152 WorkListener workListener) 153 throws WorkException { 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 164 public void scheduleWork(Work work) throws WorkException { 165 WorkerContext workWrapper = new WorkerContext(work, transactionManager); 166 workWrapper.setThreadPriority(Thread.currentThread().getPriority()); 167 executeWork(workWrapper, scheduleWorkExecutor, scheduledWorkExecutorPool); 168 } 169 170 173 public void scheduleWork( 174 Work work, 175 long startTimeout, 176 ExecutionContext execContext, 177 WorkListener workListener) 178 throws WorkException { 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 193 private void executeWork(WorkerContext work, WorkExecutor workExecutor, Executor pooledExecutor) throws WorkException { 194 work.workAccepted(this); 195 try { 196 workExecutor.doExecute(work, pooledExecutor); 197 WorkException exception = work.getWorkException(); 198 if (null != exception) { 199 throw exception; 200 } 201 } catch (InterruptedException e) { 202 WorkCompletedException wcj = new WorkCompletedException ( 203 "The execution has been interrupted.", e); 204 wcj.setErrorCode(WorkException.INTERNAL); 205 throw wcj; 206 } 207 } 208 209 } 210 | Popular Tags |