|
| 1 | +package JVM.jmx.MBean; |
| 2 | + |
| 3 | +/* Hello.java - MBean implementation for the Hello World MBean. |
| 4 | +This class must implement all the Java methods declared in the |
| 5 | +HelloMBean interface, with the appropriate behavior for each one. */ |
| 6 | + |
| 7 | +public class Hello implements HelloMBean { |
| 8 | + public void sayHello() { |
| 9 | + System.out.println("hello, world"); |
| 10 | + } |
| 11 | + |
| 12 | + public int add(int x, int y) { |
| 13 | + return x + y; |
| 14 | + } |
| 15 | + |
| 16 | + /* |
| 17 | + * Getter for the Name attribute. The pattern shown here is frequent: the getter |
| 18 | + * returns a private field representing the attribute value. In our case, the |
| 19 | + * attribute value never changes, but for other attributes it might change as |
| 20 | + * the application runs. Consider an attribute representing statistics such as |
| 21 | + * uptime or memory usage, for example. Being read-only just means that it can't |
| 22 | + * be changed through the management interface. |
| 23 | + */ |
| 24 | + public String getName() { |
| 25 | + return this.name; |
| 26 | + } |
| 27 | + |
| 28 | + /* |
| 29 | + * Getter for the CacheSize attribute. The pattern shown here is frequent: the |
| 30 | + * getter returns a private field representing the attribute value, and the |
| 31 | + * setter changes that field. |
| 32 | + */ |
| 33 | + public int getCacheSize() { |
| 34 | + return this.cacheSize; |
| 35 | + } |
| 36 | + |
| 37 | + /* |
| 38 | + * Setter for the CacheSize attribute. To avoid problems with stale values in |
| 39 | + * multithreaded situations, it is a good idea for setters to be synchronized. |
| 40 | + */ |
| 41 | + public synchronized void setCacheSize(int size) { |
| 42 | + this.cacheSize = size; |
| 43 | + |
| 44 | + /* |
| 45 | + * In a real application, changing the attribute would typically have effects |
| 46 | + * beyond just modifying the cacheSize field. For example, resizing the cache |
| 47 | + * might mean discarding entries or allocating new ones. The logic for these |
| 48 | + * effects would be here. |
| 49 | + */ |
| 50 | + System.out.println("Cache size now " + this.cacheSize); |
| 51 | + } |
| 52 | + |
| 53 | + private final String name = "Reginald"; |
| 54 | + private int cacheSize = DEFAULT_CACHE_SIZE; |
| 55 | + private static final int DEFAULT_CACHE_SIZE = 200; |
| 56 | +} |
0 commit comments