Version 19

    A Hello World Service

     

    This is a simple example of a JBoss Service. It lets you configure a message, which it prints out at start/stop. There is also a printMessage() operation and the message is configurable as an attribute.

     

    The Management Interface

     

    The key things are to extend

    ServiceMBean

    and define our management interface.

     

    package com.acme;
    
    import org.jboss.system.ServiceMBean;
    
    public interface HelloWorldServiceMBean extends ServiceMBean
    {
       // Configure getters and setters for the message attribute
       String getMessage();
       void setMessage(String message);
       
       // The print message operation
       void printMessage();
    }
    

     

    The Service Implementation

     

    Now we have to implement our management interface and the start/stop example lifecyle.

    We must implement our management according to the jmx spec and we extend

    ServiceMBeanSupport

    to do the heavy lifting.

     

    package com.acme;
    
    import org.jboss.system.ServiceMBeanSupport;
    
    public class HelloWorldService extends ServiceMBeanSupport implements HelloWorldServiceMBean
    {
       // Our message attribute
       private String message = "Sorry no message today";
    
       // Getters and Setters
       public String getMessage()
       {
          return message;
       }
       public void setMessage(String message)
       {
          this.message = message;
       }
    
       // The printMessage operation
       public void printMessage()
       {
          log.info(message);
       }
    
       // The lifecycle
       protected void startService() throws Exception
       {
          log.info("Starting with message=" + message);
       }
       protected void stopService() throws Exception
       {
          log.info("Stopping with message=" + message);
       }
    }
    

     

    The deployment descriptor (jboss-service.xml)

     

    <?xml version="1.0" encoding="UTF-8"?>
    
    <server>
      <mbean code="com.acme.HelloWorldService" name="acme.com:service=HelloWorld">
        <attribute name="Message">Hello World</attribute>
      </mbean>
    </server>
    

     

    Now create a sar for your service

     

    This is a jar file or directory (called hello-world.sar in this example) with the following

    structure:

    hello-world.sar
    hello-world.sar/META-INF/jboss-service.xml
    hello-world.sar/com/acme/HelloWorldService.class
    hello-world.sar/com/acme/HelloWorldServiceMBean.class
    

     

    The attachment ant project

     

    Attached you will find an Ant project to run the example.

     

    You need to change

    build.properties

    to point at your JBoss distribution.

     

    Type "

    ant deploy

    " to deploy the mbean, you should see the following on the JBoss console:

    02:07:39,938 INFO  [HelloWorldService] Starting with message=Hello World

     

    Type "

    ant undeploy

    " to undeploy the mbean.

    02:08:05,170 INFO  [HelloWorldService] Stopping with message=Hello World

     

    Redeploy the mbean then go to http://localhost:8080/jmx-console. Find

    acme.com:service=HelloWorld

    and click on it. Change the message attribute to say Goodbye and click Apply Changes. Now click the invoke for the printMessage operation you will see the following output on the console:

    02:08:47,256 INFO  [HelloWorldService] Goodbye

     

    How about not extending a JBoss baseclass/interface?

     

    It is not a requirement that your service extends JBoss specific baseclasses/interfaces, rather a convenience. JBoss treats all service deployments the same as long as they follow the JMX specification.

     

    In the example below we have modified the example to remove those dependencies so the management interface looks like:

    package com.acme;
    
    public interface HelloWorldServiceMBean
    {
       // Configure getters and setters for the message attribute
       String getMessage();
       void setMessage(String message);
       
       // The print message operation
       void printMessage();
       
       // Lifecycle callbacks
       void start() throws Exception;
       void stop();
    }
    

    We removed the extension of the ServiceMBean interface. Note that we need to add the start()/stop() methods if we want to receive lifecycle callbacks, but this is optional. If JBoss detects those methods on the MBean interface it will call them at the appropriate time. We could also have create()/destroy() methods if we need to fully participage in the JBoss lifecycle model.

     

    The implementation class now becomes:

    package com.acme;
    
    public class HelloWorldService implements HelloWorldServiceMBean
    {
       // Our message attribute
       private String message = "Sorry no message today";
    
       // Getters and Setters
       public String getMessage()
       {
          return message;
       }
       
       public void setMessage(String message)
       {
          this.message = message;
       }
    
       // The printMessage operation
       public void printMessage()
       {
          System.out.println(message);
       }
    
       // The lifecycle
       public void start() throws Exception
       {
          System.out.println("Starting with message=" + message);
       }
       
       public void stop()
       {
          System.out.println("Stopping with message=" + message);
       }
    }
    

    We have removed the ServiceMBeanSupport extension and we log directly to the standard output since the baseclass logger is not available. startService() and stopService() became start() and stop() correspondingly since we now deal directly with the lifecycle callbacks, rather than letting the ServiceMBeanSupport baseclass implement them and delegate to our implementation.

     

    The attached hellombean2.zip file contains the sources for the modified example that removes the JBoss class/interface dependency.

     

    Related

     

     

    Referenced by: