1 | // Copyright 2004-2007 Jean-Francois Poilpret |
2 | // |
3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | // you may not use this file except in compliance with the License. |
5 | // You may obtain a copy of the License at |
6 | // |
7 | // http://www.apache.org/licenses/LICENSE-2.0 |
8 | // |
9 | // Unless required by applicable law or agreed to in writing, software |
10 | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | // See the License for the specific language governing permissions and |
13 | // limitations under the License. |
14 | |
15 | package net.sourceforge.hiveutils.service.impl; |
16 | |
17 | import java.util.List; |
18 | |
19 | import org.apache.commons.logging.Log; |
20 | import org.apache.hivemind.events.RegistryShutdownListener; |
21 | |
22 | import net.sourceforge.hiveutils.collections.Queue; |
23 | import net.sourceforge.hiveutils.collections.impl.QueueImpl; |
24 | import net.sourceforge.hiveutils.service.AsynchronousTaskPerformer; |
25 | |
26 | /** |
27 | * Implementation of the Asynchroneous task performer service. |
28 | * |
29 | * @author Jean-Francois Poilpret |
30 | */ |
31 | public class AsynchronousTaskPerformerImpl |
32 | implements AsynchronousTaskPerformer, |
33 | RegistryShutdownListener, |
34 | Runnable |
35 | { |
36 | public AsynchronousTaskPerformerImpl(Log logger, long waitTime, int minTasks) |
37 | { |
38 | _logger = logger; |
39 | _waitTime = waitTime; |
40 | _minTasks = minTasks; |
41 | _thread = new Thread(this); |
42 | _thread.start(); |
43 | } |
44 | |
45 | public void registryDidShutdown() |
46 | { |
47 | _logger.info("registryDidShutdown()"); |
48 | executeNowAndWait(false); |
49 | _logger.debug("LEAVE registryDidShutdown()"); |
50 | } |
51 | |
52 | public void addTask(Runnable task) |
53 | { |
54 | if (task != null) |
55 | { |
56 | _tasks.add(task); |
57 | } |
58 | } |
59 | |
60 | public void executeNowAndWait() |
61 | { |
62 | executeNowAndWait(true); |
63 | } |
64 | |
65 | synchronized private void executeNowAndWait(boolean restart) |
66 | { |
67 | _logger.debug("executeNowAndWait() #1"); |
68 | _suspend = true; |
69 | _tasks.unblock(); |
70 | // Have to interrupt the thread just in case _tasks.take() has just been called |
71 | _thread.interrupt(); |
72 | _logger.debug("executeNowAndWait() #2"); |
73 | try |
74 | { |
75 | _thread.join(); |
76 | } |
77 | catch (InterruptedException e) |
78 | { |
79 | _logger.warn("executeNowAndWait()", e); |
80 | } |
81 | _logger.debug("executeNowAndWait() #3"); |
82 | if (restart) |
83 | { |
84 | _suspend = false; |
85 | _thread = new Thread(this); |
86 | _thread.start(); |
87 | } |
88 | else |
89 | { |
90 | _thread = null; |
91 | } |
92 | _logger.debug("executeNowAndWait() #4"); |
93 | } |
94 | |
95 | public void executeNow() |
96 | { |
97 | _tasks.unblock(); |
98 | } |
99 | |
100 | //CSOFF: IllegalCatchCheck |
101 | public void run() |
102 | { |
103 | while (!_suspend) |
104 | { |
105 | _logger.debug("run() #1"); |
106 | List<Runnable> tasks = _tasks.take(_minTasks, _waitTime); |
107 | _logger.debug("run() #2 Num tasks to perform: " + tasks.size()); |
108 | for (Runnable task: tasks) |
109 | { |
110 | try |
111 | { |
112 | task.run(); |
113 | } |
114 | catch (Exception e) |
115 | { |
116 | _logger.warn("Task " + task, e); |
117 | } |
118 | } |
119 | } |
120 | _logger.debug("run() exiting thread"); |
121 | } |
122 | //CSON: IllegalCatchCheck |
123 | |
124 | final private Log _logger; |
125 | final private Queue<Runnable> _tasks = new QueueImpl<Runnable>(); |
126 | final private long _waitTime; |
127 | final private int _minTasks; |
128 | private Thread _thread; |
129 | private boolean _suspend = false; |
130 | } |