1 16 17 package org.springframework.scheduling.commonj; 18 19 import java.util.Collection ; 20 21 import javax.naming.NamingException ; 22 23 import commonj.work.Work; 24 import commonj.work.WorkException; 25 import commonj.work.WorkItem; 26 import commonj.work.WorkListener; 27 import commonj.work.WorkManager; 28 import commonj.work.WorkRejectedException; 29 30 import org.springframework.beans.factory.InitializingBean; 31 import org.springframework.core.task.TaskRejectedException; 32 import org.springframework.jndi.JndiLocatorSupport; 33 import org.springframework.scheduling.SchedulingException; 34 import org.springframework.scheduling.SchedulingTaskExecutor; 35 import org.springframework.util.Assert; 36 37 82 public class WorkManagerTaskExecutor extends JndiLocatorSupport 83 implements SchedulingTaskExecutor, WorkManager, InitializingBean { 84 85 private WorkManager workManager; 86 87 private String workManagerName; 88 89 private WorkListener workListener; 90 91 92 98 public void setWorkManager(WorkManager workManager) { 99 this.workManager = workManager; 100 } 101 102 110 public void setWorkManagerName(String workManagerName) { 111 this.workManagerName = workManagerName; 112 } 113 114 119 public void setWorkListener(WorkListener workListener) { 120 this.workListener = workListener; 121 } 122 123 public void afterPropertiesSet() throws NamingException { 124 if (this.workManager == null) { 125 if (this.workManagerName == null) { 126 throw new IllegalArgumentException ("Either 'workManager' or 'workManagerName' must be specified"); 127 } 128 this.workManager = (WorkManager) lookup(this.workManagerName, WorkManager.class); 129 } 130 } 131 132 133 137 public void execute(Runnable task) { 138 Assert.state(this.workManager != null, "No WorkManager specified"); 139 Work work = new DelegatingWork(task); 140 try { 141 if (this.workListener != null) { 142 this.workManager.schedule(work, this.workListener); 143 } 144 else { 145 this.workManager.schedule(work); 146 } 147 } 148 catch (WorkRejectedException ex) { 149 throw new TaskRejectedException("CommonJ WorkManager did not accept task: " + task, ex); 150 } 151 catch (WorkException ex) { 152 throw new SchedulingException("Could not schedule task on CommonJ WorkManager", ex); 153 } 154 } 155 156 159 public boolean prefersShortLivedTasks() { 160 return true; 161 } 162 163 164 168 public WorkItem schedule(Work work) 169 throws WorkException, IllegalArgumentException { 170 171 return this.workManager.schedule(work); 172 } 173 174 public WorkItem schedule(Work work, WorkListener workListener) 175 throws WorkException, IllegalArgumentException { 176 177 return this.workManager.schedule(work, workListener); 178 } 179 180 public boolean waitForAll(Collection workItems, long timeout) 181 throws InterruptedException , IllegalArgumentException { 182 183 return this.workManager.waitForAll(workItems, timeout); 184 } 185 186 public Collection waitForAny(Collection workItems, long timeout) 187 throws InterruptedException , IllegalArgumentException { 188 189 return this.workManager.waitForAny(workItems, timeout); 190 } 191 192 } 193 | Popular Tags |