001: /*
002: * Copyright (C) The MX4J Contributors.
003: * All rights reserved.
004: *
005: * This software is distributed under the terms of the MX4J License version 1.0.
006: * See the terms of the MX4J License in the documentation provided with this software.
007: */
008:
009: package mx4j.examples.mbeans.dynamic;
010:
011: import javax.management.MBeanAttributeInfo;
012: import javax.management.MBeanOperationInfo;
013: import javax.management.MBeanParameterInfo;
014:
015: import mx4j.AbstractDynamicMBean;
016:
017: /**
018: * This is a DynamicMBean. Note how the usage of the {@link AbstractDynamicMBean}
019: * class simplifies a lot the coding of DynamicMBeans.
020: * The code itself is divided in two parts: the implementation part and the JMX part.
021: *
022: * @version $Revision: 1.1 $
023: */
024: public class DynamicService extends AbstractDynamicMBean {
025: //
026: // Implementation part.
027: // This part gives the MBean the service functionality.
028: //
029:
030: private boolean running;
031: private int concurrent;
032:
033: public void start() {
034: // Simulate the accept on incoming client requests
035: // We will track how many requests we have, and if we pass a certain threshold,
036: // we issue a notification.
037:
038: synchronized (this ) {
039: running = true;
040: }
041:
042: Thread thread = new Thread(new Runnable() {
043: public void run() {
044: simulateClientRequests();
045: }
046: });
047: thread.start();
048: }
049:
050: public void stop() {
051: synchronized (this ) {
052: running = false;
053: }
054: }
055:
056: private void simulateClientRequests() {
057: while (isRunning()) {
058: // Pick a time in ms to simulate the interval between incoming client requests
059: long interval = Math.round(Math.random() * 1000L) + 1;
060: try {
061: Thread.sleep(interval);
062: } catch (InterruptedException ignored) {
063: }
064:
065: // Spawn a new Thread to accept the client request
066: Thread thread = new Thread(new Runnable() {
067: public void run() {
068: // Increase the number of concurrent clients
069: synchronized (DynamicService.this ) {
070: ++concurrent;
071: System.out
072: .println("--DynamicService--"
073: + Thread.currentThread()
074: + "-- Incoming client request -- concurrent clients: "
075: + concurrent);
076: }
077:
078: // Pick a time in ms to simulate the processing of the client request
079: long processing = Math.round(Math.random() * 5000L) + 1;
080: try {
081: Thread.sleep(processing);
082: } catch (InterruptedException ignored) {
083: }
084:
085: // We're done with this client, decrease the number of concurrent clients
086: synchronized (DynamicService.this ) {
087: --concurrent;
088: }
089: }
090: });
091: thread.start();
092: }
093: }
094:
095: public synchronized boolean isRunning() {
096: return running;
097: }
098:
099: public synchronized int getConcurrentClients() {
100: return concurrent;
101: }
102:
103: //
104: // JMX part.
105: // Note how short is :)
106: //
107:
108: protected MBeanAttributeInfo[] createMBeanAttributeInfo() {
109: return new MBeanAttributeInfo[] {
110: new MBeanAttributeInfo("Running", "boolean",
111: "The running status of the DynamicService",
112: true, false, true),
113: new MBeanAttributeInfo("ConcurrentClients", "int",
114: "The number of concurrent clients", true,
115: false, false) };
116: }
117:
118: protected MBeanOperationInfo[] createMBeanOperationInfo() {
119: return new MBeanOperationInfo[] {
120: new MBeanOperationInfo("start",
121: "Starts the DynamicService",
122: new MBeanParameterInfo[0], "void",
123: MBeanOperationInfo.ACTION),
124: new MBeanOperationInfo("stop",
125: "Stops the DynamicService",
126: new MBeanParameterInfo[0], "void",
127: MBeanOperationInfo.ACTION) };
128: }
129: }
|