1 16 17 package org.springframework.jca.work; 18 19 import javax.resource.spi.BootstrapContext ; 20 import javax.resource.spi.work.ExecutionContext ; 21 import javax.resource.spi.work.Work ; 22 import javax.resource.spi.work.WorkException ; 23 import javax.resource.spi.work.WorkListener ; 24 import javax.resource.spi.work.WorkManager ; 25 import javax.resource.spi.work.WorkRejectedException ; 26 27 import org.springframework.core.task.AsyncTaskExecutor; 28 import org.springframework.core.task.TaskRejectedException; 29 import org.springframework.core.task.TaskTimeoutException; 30 import org.springframework.scheduling.SchedulingException; 31 import org.springframework.scheduling.SchedulingTaskExecutor; 32 import org.springframework.util.Assert; 33 34 53 public class WorkManagerTaskExecutor implements SchedulingTaskExecutor, AsyncTaskExecutor, WorkManager { 54 55 private WorkManager workManager = new SimpleTaskWorkManager(); 56 57 private boolean blockUntilStarted = false; 58 59 private boolean blockUntilCompleted = false; 60 61 private WorkListener workListener; 62 63 64 68 public WorkManagerTaskExecutor() { 69 } 70 71 75 public WorkManagerTaskExecutor(WorkManager workManager) { 76 setWorkManager(workManager); 77 } 78 79 80 83 public void setWorkManager(WorkManager workManager) { 84 Assert.notNull(workManager, "WorkManager must not be null"); 85 this.workManager = workManager; 86 } 87 88 92 public void setBootstrapContext(BootstrapContext bootstrapContext) { 93 Assert.notNull(bootstrapContext, "BootstrapContext must not be null"); 94 this.workManager = bootstrapContext.getWorkManager(); 95 } 96 97 105 public void setBlockUntilStarted(boolean blockUntilStarted) { 106 this.blockUntilStarted = blockUntilStarted; 107 } 108 109 117 public void setBlockUntilCompleted(boolean blockUntilCompleted) { 118 this.blockUntilCompleted = blockUntilCompleted; 119 } 120 121 126 public void setWorkListener(WorkListener workListener) { 127 this.workListener = workListener; 128 } 129 130 131 135 public void execute(Runnable task) { 136 execute(task, TIMEOUT_INDEFINITE); 137 } 138 139 public void execute(Runnable task, long startTimeout) { 140 Assert.state(this.workManager != null, "No WorkManager specified"); 141 Work work = new DelegatingWork(task); 142 try { 143 if (this.blockUntilCompleted) { 144 if (startTimeout != TIMEOUT_INDEFINITE || this.workListener != null) { 145 this.workManager.doWork(work, startTimeout, null, this.workListener); 146 } 147 else { 148 this.workManager.doWork(work); 149 } 150 } 151 else if (this.blockUntilStarted) { 152 if (startTimeout != TIMEOUT_INDEFINITE || this.workListener != null) { 153 this.workManager.startWork(work, startTimeout, null, this.workListener); 154 } 155 else { 156 this.workManager.startWork(work); 157 } 158 } 159 else { 160 if (startTimeout != TIMEOUT_INDEFINITE || this.workListener != null) { 161 this.workManager.scheduleWork(work, startTimeout, null, this.workListener); 162 } 163 else { 164 this.workManager.scheduleWork(work); 165 } 166 } 167 } 168 catch (WorkRejectedException ex) { 169 if (WorkException.START_TIMED_OUT.equals(ex.getErrorCode())) { 170 throw new TaskTimeoutException("JCA WorkManager rejected task because of timeout: " + task, ex); 171 } 172 else { 173 throw new TaskRejectedException("JCA WorkManager rejected task: " + task, ex); 174 } 175 } 176 catch (WorkException ex) { 177 throw new SchedulingException("Could not schedule task on JCA WorkManager", ex); 178 } 179 } 180 181 184 public boolean prefersShortLivedTasks() { 185 return true; 186 } 187 188 189 193 public void doWork(Work work) throws WorkException { 194 this.workManager.doWork(work); 195 } 196 197 public void doWork(Work work, long delay, ExecutionContext executionContext, WorkListener workListener) 198 throws WorkException { 199 200 this.workManager.doWork(work, delay, executionContext, workListener); 201 } 202 203 public long startWork(Work work) throws WorkException { 204 return this.workManager.startWork(work); 205 } 206 207 public long startWork(Work work, long delay, ExecutionContext executionContext, WorkListener workListener) 208 throws WorkException { 209 210 return this.workManager.startWork(work, delay, executionContext, workListener); 211 } 212 213 public void scheduleWork(Work work) throws WorkException { 214 this.workManager.scheduleWork(work); 215 } 216 217 public void scheduleWork(Work work, long delay, ExecutionContext executionContext, WorkListener workListener) 218 throws WorkException { 219 220 this.workManager.scheduleWork(work, delay, executionContext, workListener); 221 } 222 223 } 224 | Popular Tags |