How to change a jboss-service.xml/sar MBean Singleton to a Java EE Singleton in JBoss EAP

Solution Unverified - Updated -

Environment

  • Red Hat JBoss Enterprise Application Platform (EAP)
    • 6.x
    • 7.x

Issue

  • How to change a jboss-service.xml / sar MBean Singleton to a Java EE 6 Singleton in JBoss EAP 6
  • How to change a jboss-service.xml / sar MBean Singleton to a Java EE 7 Singleton in JBoss EAP 7

Resolution

Note: The description below show how the implementation would look like for Java EE6. This is valid as is for Java EE7 as well.

jboss-service.xml / sar / old JBoss specific MBeans are not recommended as they are JBoss specific deployment types and may eventually be removed. They were originally used in the old JBoss EAP 4 implementation, but the kernel level has been rewritten and is no longer based on JMX, because of this these MBeans do not have all of the functionality they used to have.

The recommendation is to use Java EE 6 specification EJB Singletons.

For example if you currently have a jboss-service.xml MBean such as:

<server xmlns="urn:jboss:service:7.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="urn:jboss:service:7.0 jboss-service_7_0.xsd">
  <mbean code="com.jboss.examples.mbean.JBossServiceExample" name="com.jboss.examples.mbean:service=JBossServiceExample"/>
</server>

MBean interface

public interface JBossServiceExampleMBean { 
  public void start() throws Exception;
  public void stop() throws Exception;
}

MBean implementation

public class JBossServiceExample implements JBossServiceExampleMBean {
  private Logger log = Logger.getLogger(this.getClass().getName());
  public void start() throws Exception {
    log.info("Starting");
  }
  public void stop() throws Exception {
    log.info("Stopping");
  }
}

The example jboss-service.xml MBean above can be changed into a Java EE 6 Singleton in one of two ways. The first step for both methods is to remove the jboss-service.xml, it will be replaced with either annotations or a ejb-jar.xml.

Using annotations

This can be done using annotations such as @Singleton, @Startup, @PostConstruct and @PreDestroy.

import javax.ejb.*;
import javax.annotation.*;

@Singleton
@Startup
public class JBossServiceExample implements JBossServiceExampleMBean {
  private Logger log = Logger.getLogger(this.getClass().getName());

  @PostConstruct
  public void start() throws Exception {
    log.info("Starting");
  }

  @PreDestroy
  public void stop() throws Exception {
    log.info("Stopping");
  }
}

Using ejb-jar.xml

The example JBossServiceExample source code does not have to change, it can be turned into a Singleton just by using the ejb-jar.xml as shown below:

<?xml version="1.0" encoding="UTF-8"?>
<ejb-jar xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_1.xsd"
    version="3.1">
    <enterprise-beans>
        <session>
            <ejb-name>JBossServiceExample</ejb-name>
            <ejb-class>com.jboss.examples.mbean.JBossServiceExample</ejb-class>
            <session-type>Singleton</session-type>
            <init-on-startup>true</init-on-startup>
            <post-construct>
              <lifecycle-callback-method>start</lifecycle-callback-method>
            </post-construct>
            <pre-destroy>
              <lifecycle-callback-method>stop</lifecycle-callback-method>
            </pre-destroy>
        </session>
   </enterprise-beans>
</ejb-jar>

Notes
The Singleton will be bound into JNDI where it can be looked up and invoked, however by default it will not be bound into JMX. If you want to have it bound into JMX also see How to expose a Java EE Singleton as a MBean in JMX in JBoss EAP

Attachments

This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.

Comments