1 24 package org.riotfamily.riot.job.support; 25 26 import java.util.HashSet ; 27 import java.util.Iterator ; 28 29 import org.apache.commons.logging.Log; 30 import org.apache.commons.logging.LogFactory; 31 import org.riotfamily.riot.job.persistence.JobDetail; 32 33 public class TaskList { 34 35 private static Log log = LogFactory.getLog(TaskList.class); 36 37 private HashSet activeTasks = new HashSet (); 38 39 43 public synchronized JobTask getJobTask(JobDetail detail) { 44 Iterator it = activeTasks.iterator(); 45 while (it.hasNext()) { 46 JobTask task = (JobTask) it.next(); 47 if (task.getDetail().getId().equals(detail.getId())) { 48 return task; 49 } 50 } 51 return null; 52 } 53 54 58 public synchronized void addTask(JobTask task) { 59 activeTasks.add(task); 60 notifyAll(); 61 } 62 63 66 public synchronized void removeTask(JobTask task) { 67 activeTasks.remove(task); 68 } 69 70 public void interrupt(JobDetail detail) { 71 JobTask task = getJobTask(detail); 72 if (task != null) { 73 task.interrupt(); 74 removeTask(task); 75 } 76 } 77 78 public void interruptAll() { 79 Iterator it = activeTasks.iterator(); 81 while (it.hasNext()) { 82 JobTask task = (JobTask) it.next(); 83 log.info("Interrupting task " + task.getDetail().getId()); 84 task.interrupt(); 85 } 86 activeTasks.clear(); 87 } 88 89 93 public synchronized void updateExecutionTimes() { 94 Iterator it = activeTasks.iterator(); 95 while (it.hasNext()) { 96 JobTask task = (JobTask) it.next(); 97 task.updateExecutionTime(); 98 } 99 } 100 101 104 public synchronized void waitForTasks() { 105 if (activeTasks.isEmpty()) { 106 try { 107 wait(); 108 } 109 catch (InterruptedException e) { 110 } 111 } 112 } 113 114 } 115 | Popular Tags |