Performance Zone is brought to you in partnership with:

I'm a software developer for whom coding is a way of life as well as a hobby. Focused on slimming enterprise class systems, strongly interested in new technologies and continuously improving the software delivery process. My great passion are lightweight applications, in which the ratio of provided functionality to the code is as smallest as possible. Co-founder of the agile software house Codearte. Confitura conference organizer. Jakub is a DZone MVB and is not an employee of DZone and has posted 16 posts at DZone. You can read more from them at their website. View Full User Profile

Using HA Singleton in JBoss 7

  • submit to reddit
Some time ago I had to change standard clustering behavior of Quartz Scheduler, and let it work without synchronizing over database. There are of course a lot of options to do that, but because I'm big fan of simplicity I've decided to use standard Spring @Scheduled configuration and totally skip thinking about cluster at this level. The idea was to just invoke "check that I'm on master node" method at the beginning of the scheduled method. The only problem was how to write such method :) The choice was to use JBoss HA Singleton functionality. It's available in JBoss 7.x but the big lack of documentation forces some experiments... nice!

First thing which we need to do is to provide proper dependency, containing few important classes. Of course remember about right version (here I'm using 7.1.1.Final because it's available in public repositories and all next versions of 7 needs to be build manually).
Now it's time to do some coding! Let's start from class containing service that will be installed into application server. Its role is to set master status flag and expose it by simple static method.
package com.stackholder.jboss.ha;

import org.jboss.msc.service.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.concurrent.atomic.AtomicBoolean;

public class MasterStatusHaSingleton 
    extends AbstractService<Serializable> {

  private static final Logger LOGGER = 

  private static AtomicBoolean masterStatus = 
      new AtomicBoolean(false);

  public void start(StartContext startContext) {"MasterStatusHaSingleton started");

  public void stop(StopContext stopContext) {"MasterStatusHaSingleton stopped");

  public static boolean isMaster() {
    return masterStatus.get();
Now we need to write class that will install prepared service into container.
package com.stackholder.jboss.ha;

import org.jboss.msc.service.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class HaSingletonActivator implements ServiceActivator {

  private final static Logger LOGGER =

  public static final ServiceName SINGLETON_SERVICE_NAME =
      ServiceName.JBOSS.append("ha", "singleton");

  public void activate(ServiceActivatorContext context) 
      throws ServiceRegistryException {"HaSingletonActivator will be installed");

    MasterStatusHaSingleton srv = new MasterStatusHaSingleton();
    SingletonService singleton = 
        new SingletonService(srv, SINGLETON_SERVICE_NAME); DelegatingServiceContainer(
        .setInitialMode(ServiceController.Mode.ACTIVE).install();"HaSingletonActivator installation SUCCESSFUL");
Remember that JBoss 7 is using OSGi, so we've to tell application server what modules we're going to use in our application. The easiest way to do that is to add configuration into war or jar plugin:
Great - almost finished! Almost :) Last thing we have to do is to activate proper modules in JBoss configuration. Just edit standalone-full-ha.xml file (or other configuration that you use of course) and add following modules into ee subsystem:
<subsystem xmlns="urn:jboss:domain:ee:1.0">
    <module name="org.jboss.msc" slot="main">
    <module name="" slot="main">
And finally you can execute your server and enjoy new, cool functionality ;)
Published at DZone with permission of Jakub Kubrynski, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)