View Javadoc

1   package org.exolab.jms.service;
2   
3   import java.util.List;
4   import java.util.Collections;
5   import java.util.ArrayList;
6   
7   import org.exolab.jms.common.threads.ThreadListener;
8   
9   
10  /***
11   * A <code>ThreadListener</code> that notifies child listeners of thread events..
12   *
13   * @author <a href="mailto:tma@netspace.net.au">Tim Anderson</a>
14   * @version $LastChangedDate$
15   */
16  public class ServiceThreadListener implements ThreadListener {
17  
18      /***
19       * The thread listeners.
20       */
21      private List _listeners = Collections.synchronizedList(new ArrayList());
22  
23  
24      /***
25       * Add a listener.
26       *
27       * @param listener the listener to add
28       */
29      public void addThreadListener(ThreadListener listener) {
30          _listeners.add(listener);
31      }
32  
33      /***
34       * Remove a listener.
35       *
36       * @param listener the listener tp remove
37       */
38      public void removeThreadListener(ThreadListener listener) {
39          _listeners.remove(listener);
40      }
41  
42      /***
43       * Invoked when a command is to be executed.
44       *
45       * @param command the command
46       */
47      public void begin(Runnable command) {
48          ThreadListener[] listeners
49                  = (ThreadListener[]) _listeners.toArray(new ThreadListener[0]);
50          for (int i = 0; i < listeners.length; ++i) {
51              listeners[i].begin(command);
52          }
53      }
54  
55      /***
56       * Invoked when a command completes execution.
57       *
58       * @param command the command.
59       */
60      public void end(Runnable command) {
61          ThreadListener[] listeners
62                  = (ThreadListener[]) _listeners.toArray(new ThreadListener[0]);
63          for (int i = 0; i < listeners.length; ++i) {
64              listeners[i].end(command);
65          }
66      }
67  }