Java Reference
In-Depth Information
An important item to notice is the
package
statement. All examples in this chap-
ter are in the package
jmxbook.ch2
, and each chapter will package its examples
accordingly (for example,
jmxbook.ch3
for chapter 3).
The
HelloWorldMBean
interface declares a getter
(
getGreeting()
) and setter
(
setGreeting()
), as well as a
printGreeting()
method. You'll use the
printGreet-
ing()
method later to display the MBean's greeting value.
Listing 2.1 shows the implementation of the interface.
Listing 2.1
HelloWorld.java
package jmxbook.ch2;
public class HelloWorld implements HelloWorldMBean
{
Implements
HelloWorldMBean
interface
private String greeting = null;
public HelloWorld()
{
this.greeting = "Hello World! I am a Standard MBean";
}
public HelloWorld( String greeting )
{
this.greeting = greeting;
}
public void setGreeting( String greeting )
{
this.greeting = greeting;
}
public String getGreeting()
{
return greeting;
}
public void printGreeting()
{
System.out.println( greeting );
}
}
And with that, you have created your first MBean. Now, in order to test the
MBean, you need to create a
JMX
agent to contain it. The next section discusses
the creation of the
HelloAgent
class. After creating your agent, you can begin
using the MBean.