KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > mx4j > examples > mbeans > legacy > LegacyService


1 /*
2  * Copyright (C) The MX4J Contributors.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the MX4J License version 1.0.
6  * See the terms of the MX4J License in the documentation provided with this software.
7  */

8
9 package mx4j.examples.mbeans.legacy;
10
11 /**
12  * This service wakes up every once in a while, and does an intensive job
13  * spawning many threads to perform the given operation. <br>
14  * We would like to be informed of this activity, and would like to expose functionality of
15  * this service via JMX. To achieve these goals, we wrap it by means of a DynamicMBean,
16  * {@link DynamicLegacyService}.
17  *
18  * @version $Revision: 1.3 $
19  */

20 public class LegacyService
21 {
22    private boolean running;
23    private ThreadGroup JavaDoc group = new ThreadGroup JavaDoc("Legacy Thread Group");
24
25    /**
26     * This method is called 'execute', but we want to expose it in JMX with the name 'start'.
27     * The magic is done in the DynamicMBean that wraps this service to expose it via JMX.
28     */

29    public void execute()
30    {
31       while (true)
32       {
33          // Wait for a while
34
long wait = Math.round(Math.random() * 10000L) + 1;
35          try
36          {
37             System.out.println("Waiting " + wait + " ms...");
38             Thread.sleep(wait);
39          }
40          catch (InterruptedException JavaDoc ignored)
41          {
42          }
43          // Ok, we've slept enough, time to do some job
44
synchronized (this)
45          {
46             running = true;
47          }
48
49          Thread JavaDoc thread = new Thread JavaDoc(new Runnable JavaDoc()
50          {
51             public void run()
52             {
53                spawnThreads();
54                // We're done now, not running anymore
55
synchronized (this)
56                {
57                   running = false;
58                }
59             }
60          });
61          thread.start();
62          try
63          {
64             thread.join();
65          }
66          catch (InterruptedException JavaDoc ignored)
67          {
68          }
69       }
70    }
71
72    /**
73     * This method is private in the legacy service. However, we want to expose it via JMX
74     * without modifying this service. The magic is done in the DynamicMBean that wraps this
75     * service to expose it via JMX.
76     */

77    private synchronized boolean isRunning()
78    {
79       return running;
80    }
81
82    private void spawnThreads()
83    {
84       Thread JavaDoc[] threads = new Thread JavaDoc[20];
85       for (int i = 0; i < threads.length; ++i)
86       {
87          threads[i] = new Thread JavaDoc(group, new Runnable JavaDoc()
88          {
89             public void run()
90             {
91                // Simulate a job: sleep for a while :D
92
long sleep = Math.round(Math.random() * 5000L) + 1;
93                try
94                {
95                   Thread.sleep(sleep);
96                }
97                catch (InterruptedException JavaDoc ignored)
98                {
99                }
100             }
101          });
102          threads[i].start();
103       }
104
105       // Now wait for everyone to complete:
106
for (int i = 0; i < threads.length; ++i)
107       {
108          try
109          {
110             threads[i].join();
111          }
112          catch (InterruptedException JavaDoc ignored)
113          {
114          }
115       }
116    }
117 }
118
Popular Tags