View Javadoc

1   /**
2    * Copyright (C) 2005-2009 Alfresco Software Limited.
3    *
4    * This file is part of the Spring Surf Extension project.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *  http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.springframework.extensions.surf.task;
20  
21  import java.util.Queue;
22  
23  import org.apache.commons.logging.Log;
24  import org.apache.commons.logging.LogFactory;
25  
26  /**
27   * Background thread that wakes up periodically to monitor the task manager's
28   * pool of tasks waiting to be processed. If it finds one, it notifies all
29   * worker threads so that someone will pick up the task and begin to work on it.
30   * 
31   * @author muzquiano
32   */
33  public class TaskStarterThread extends Thread
34  {
35      private static Log logger = LogFactory.getLog(TaskStarterThread.class);
36  
37      protected TaskManager taskManager;
38  
39      /**
40       * Instantiates a new task starter thread.
41       */
42      public TaskStarterThread(TaskManager taskManager)
43      {
44          this.taskManager = taskManager;
45      }
46  
47      /*
48       * (non-Javadoc)
49       * 
50       * @see java.lang.Thread#run()
51       */
52      public void run()
53      {
54          while (true)
55          {
56              try
57              {
58                  // sleep for a bit
59                  Thread.sleep(taskManager.getWakeupPeriod());
60                  
61                  // check whether the task queue has anything in it
62                  Queue<AbstractTask> taskQueue = taskManager.getTaskQueue();
63                  
64                  // if there are tasks to do
65                  if (taskQueue.size() > 0)
66                  {
67                      // peek at the first tasks in the queue
68                      AbstractTask task = (AbstractTask) taskQueue.peek();
69                      if(task != null)
70                      {
71                          // in fact, there is a task...
72                          if (logger.isDebugEnabled())
73                          {
74                              logger.debug("Found a job to start (" + task.getName() + ")");
75                              logger.debug("Attempting to wake up threads");
76                          }
77      
78                          // wake up all of the workers who are waiting
79                          synchronized (taskManager.getClass())
80                          {
81                              taskManager.getClass().notifyAll();
82                          }
83                      }
84                  }
85              }
86              catch (Throwable t)
87              {
88                  t.printStackTrace();
89              }
90          }
91      }
92  
93  }