KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > mx4j > examples > mbeans > dynamic > DynamicService


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.dynamic;
10
11 import javax.management.MBeanAttributeInfo JavaDoc;
12 import javax.management.MBeanOperationInfo JavaDoc;
13 import javax.management.MBeanParameterInfo JavaDoc;
14
15 import mx4j.AbstractDynamicMBean;
16
17 /**
18  * This is a DynamicMBean. Note how the usage of the {@link AbstractDynamicMBean}
19  * class simplifies a lot the coding of DynamicMBeans.
20  * The code itself is divided in two parts: the implementation part and the JMX part.
21  *
22  * @version $Revision: 1.1 $
23  */

24 public class DynamicService extends AbstractDynamicMBean
25 {
26    //
27
// Implementation part.
28
// This part gives the MBean the service functionality.
29
//
30

31    private boolean running;
32    private int concurrent;
33
34    public void start()
35    {
36       // Simulate the accept on incoming client requests
37
// We will track how many requests we have, and if we pass a certain threshold,
38
// we issue a notification.
39

40       synchronized (this)
41       {
42          running = true;
43       }
44
45       Thread JavaDoc thread = new Thread JavaDoc(new Runnable JavaDoc()
46       {
47          public void run()
48          {
49             simulateClientRequests();
50          }
51       });
52       thread.start();
53    }
54
55    public void stop()
56    {
57       synchronized (this)
58       {
59          running = false;
60       }
61    }
62
63    private void simulateClientRequests()
64    {
65       while (isRunning())
66       {
67          // Pick a time in ms to simulate the interval between incoming client requests
68
long interval = Math.round(Math.random() * 1000L) + 1;
69          try
70          {
71             Thread.sleep(interval);
72          }
73          catch (InterruptedException JavaDoc ignored)
74          {
75          }
76
77          // Spawn a new Thread to accept the client request
78
Thread JavaDoc thread = new Thread JavaDoc(new Runnable JavaDoc()
79          {
80             public void run()
81             {
82                // Increase the number of concurrent clients
83
synchronized (DynamicService.this)
84                {
85                   ++concurrent;
86                   System.out.println("--DynamicService--" + Thread.currentThread() + "-- Incoming client request -- concurrent clients: " + concurrent);
87                }
88
89                // Pick a time in ms to simulate the processing of the client request
90
long processing = Math.round(Math.random() * 5000L) + 1;
91                try
92                {
93                   Thread.sleep(processing);
94                }
95                catch (InterruptedException JavaDoc ignored)
96                {
97                }
98
99                // We're done with this client, decrease the number of concurrent clients
100
synchronized (DynamicService.this)
101                {
102                   --concurrent;
103                }
104             }
105          });
106          thread.start();
107       }
108    }
109
110    public synchronized boolean isRunning()
111    {
112       return running;
113    }
114
115    public synchronized int getConcurrentClients()
116    {
117       return concurrent;
118    }
119
120
121    //
122
// JMX part.
123
// Note how short is :)
124
//
125

126    protected MBeanAttributeInfo JavaDoc[] createMBeanAttributeInfo()
127    {
128       return new MBeanAttributeInfo JavaDoc[]
129       {
130          new MBeanAttributeInfo JavaDoc("Running", "boolean", "The running status of the DynamicService", true, false, true),
131          new MBeanAttributeInfo JavaDoc("ConcurrentClients", "int", "The number of concurrent clients", true, false, false)
132       };
133    }
134
135    protected MBeanOperationInfo JavaDoc[] createMBeanOperationInfo()
136    {
137       return new MBeanOperationInfo JavaDoc[]
138       {
139          new MBeanOperationInfo JavaDoc("start", "Starts the DynamicService", new MBeanParameterInfo JavaDoc[0], "void", MBeanOperationInfo.ACTION),
140          new MBeanOperationInfo JavaDoc("stop", "Stops the DynamicService", new MBeanParameterInfo JavaDoc[0], "void", MBeanOperationInfo.ACTION)
141       };
142    }
143 }
144
Popular Tags