KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > services > task > impl > TaskServiceImpl


1 /***************************************************************************
2  * Copyright 2001-2003 The eXo Platform SARL All rights reserved. *
3  * Please look at license.txt in info directory for more license detail. *
4  **************************************************************************/

5 package org.exoplatform.services.task.impl;
6
7 import java.util.Collection JavaDoc;
8 import java.util.LinkedList JavaDoc;
9 import java.util.Iterator JavaDoc ;
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 /**
18  * @author Tuan Nguyen (tuan08@users.sourceforge.net)
19  * @since Nov 30, 2004
20  * @version $Id$
21  */

22 public class TaskServiceImpl implements TaskService , Startable {
23   static private Collection JavaDoc EMPTY = new LinkedList JavaDoc() ;
24   
25   private Collection JavaDoc repeatTasks_ ;
26   private Collection JavaDoc tasks_ ;
27   private Log log_;
28   private TaskThread thread_ ;
29   
30   public TaskServiceImpl(LogService lservice) {
31     log_ = lservice.getLog(getClass()) ;
32     repeatTasks_ = new LinkedList JavaDoc() ;
33     tasks_ = new LinkedList JavaDoc() ;
34   }
35   
36   synchronized public void queueTask(Task task) {
37     tasks_.add(task) ;
38   }
39
40   synchronized public Collection JavaDoc getTasks() {
41     if(tasks_.size() == 0) return EMPTY ;
42     return new LinkedList JavaDoc(tasks_) ;
43   }
44   
45   synchronized Collection JavaDoc dequeueTasks() {
46     if(tasks_.size() == 0) return EMPTY ;
47     Collection JavaDoc temp = tasks_ ;
48     tasks_ = new LinkedList JavaDoc() ;
49     return temp ;
50   }
51   
52   synchronized public void queueRepeatTask(Task task) {
53     repeatTasks_.add(task) ;
54   }
55  
56   synchronized public Collection JavaDoc getRepeatTasks() {
57     if(repeatTasks_.size() == 0) return EMPTY ;
58     return new LinkedList JavaDoc(repeatTasks_) ;
59   }
60   
61   synchronized Collection JavaDoc dequeueRepeatTasks() {
62     if(repeatTasks_.size() == 0) return EMPTY ;
63     return new LinkedList JavaDoc(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 JavaDoc tasks) {
80     Iterator JavaDoc 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 JavaDoc t) {
96         String JavaDoc 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 JavaDoc {
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 JavaDoc ex) {
118           return ;
119         } catch (Throwable JavaDoc ex) {
120           ex.printStackTrace() ;
121         }
122       }
123     }
124   }
125 }
126
Popular Tags