1 5 package org.exoplatform.services.task.impl; 6 7 import java.util.Collection ; 8 import java.util.LinkedList ; 9 import java.util.Iterator ; 10 import org.exoplatform.services.task.Task; 11 import org.exoplatform.services.task.TaskService; 12 import org.exoplatform.services.database.HibernateService; 13 import org.exoplatform.services.log.LogService; 14 import org.apache.commons.logging.Log; 15 import org.exoplatform.container.PortalContainer ; 16 import org.picocontainer.Startable; 17 22 public class TaskServiceImpl implements TaskService , Startable { 23 static private Collection EMPTY = new LinkedList () ; 24 25 private Collection repeatTasks_ ; 26 private Collection tasks_ ; 27 private Log log_; 28 private TaskThread thread_ ; 29 30 public TaskServiceImpl(LogService lservice) { 31 log_ = lservice.getLog(getClass()) ; 32 repeatTasks_ = new LinkedList () ; 33 tasks_ = new LinkedList () ; 34 } 35 36 synchronized public void queueTask(Task task) { 37 tasks_.add(task) ; 38 } 39 40 synchronized public Collection getTasks() { 41 if(tasks_.size() == 0) return EMPTY ; 42 return new LinkedList (tasks_) ; 43 } 44 45 synchronized Collection dequeueTasks() { 46 if(tasks_.size() == 0) return EMPTY ; 47 Collection temp = tasks_ ; 48 tasks_ = new LinkedList () ; 49 return temp ; 50 } 51 52 synchronized public void queueRepeatTask(Task task) { 53 repeatTasks_.add(task) ; 54 } 55 56 synchronized public Collection getRepeatTasks() { 57 if(repeatTasks_.size() == 0) return EMPTY ; 58 return new LinkedList (repeatTasks_) ; 59 } 60 61 synchronized Collection dequeueRepeatTasks() { 62 if(repeatTasks_.size() == 0) return EMPTY ; 63 return new LinkedList (repeatTasks_) ; 64 } 65 66 synchronized public void removeRepeatTask(Task task) { 67 repeatTasks_.remove(task) ; 68 } 69 70 public void start() { 71 thread_ = new TaskThread(1000) ; 72 thread_.start() ; 73 } 74 75 public void stop() { 76 if(thread_ != null) thread_.interrupt() ; 77 } 78 79 void runTasks(Collection tasks) { 80 Iterator i = tasks.iterator() ; 81 while(i.hasNext()) { 82 Task task = (Task) i.next() ; 83 try { 84 PortalContainer pcontainer = task.getPortalContainer() ; 85 if(pcontainer != null) { 86 PortalContainer.setInstance(pcontainer) ; 87 } 88 task.execute() ; 89 if(pcontainer != null) { 90 HibernateService hservice = 91 (HibernateService) pcontainer.getComponentInstanceOfType(HibernateService.class) ; 92 if(hservice != null) hservice.closeSession() ; 93 PortalContainer.setInstance(null) ; 94 } 95 } catch (Throwable t) { 96 String error = "task: " + task.getName() + 97 "\n task description: " + task.getDescription(); 98 log_.error(error, t) ; 99 } 100 } 101 } 102 103 public class TaskThread extends Thread { 104 private long period_ ; 105 106 public TaskThread(long period) { 107 setPriority(NORM_PRIORITY) ; 108 period_ = period ; 109 } 110 111 public void run() { 112 while (true) { 113 try { 114 sleep(period_) ; 115 runTasks(dequeueTasks()) ; 116 runTasks(dequeueRepeatTasks()) ; 117 } catch (InterruptedException ex) { 118 return ; 119 } catch (Throwable ex) { 120 ex.printStackTrace() ; 121 } 122 } 123 } 124 } 125 } 126 | Popular Tags |