About Jakub Kubrynski

I'm software developer by vocation. Java team leader at work and open source enthusiast at home. Strongly interested in new technologies. Fan of java.util.concurrent and sun.misc.Unsafe. My motto is "minimum code, maximum functionality.

Using HA Singleton in JBoss 7

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).

<dependency>
  <groupid>org.jboss.as</groupid>
  <artifactid>jboss-as-clustering-singleton</artifactid>
  <version>7.1.1.Final</version>
  <scope>provided</scope>
</dependency>

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.io.Serializable;
import java.util.concurrent.atomic.AtomicBoolean;

public class MasterStatusHaSingleton 
    extends AbstractService<Serializable> {

  private static final Logger LOGGER = 
      LoggerFactory.getLogger(MasterStatusHaSingleton.class);

  private static AtomicBoolean masterStatus = 
      new AtomicBoolean(false);

  @Override
  public void start(StartContext startContext) {
    LOGGER.info("MasterStatusHaSingleton started");
    masterStatus.set(true);
  }

  @Override
  public void stop(StopContext stopContext) {
    LOGGER.info("MasterStatusHaSingleton stopped");
    masterStatus.set(false);
  }

  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.as.clustering.singleton.SingletonService;
import org.jboss.msc.service.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class HaSingletonActivator implements ServiceActivator {

  private final static Logger LOGGER =
      LoggerFactory.getLogger(HaSingletonActivator.class);

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

  @Override
  public void activate(ServiceActivatorContext context) 
      throws ServiceRegistryException {
    LOGGER.info("HaSingletonActivator will be installed");

    MasterStatusHaSingleton srv = new MasterStatusHaSingleton();
    SingletonService singleton = 
        new SingletonService(srv, SINGLETON_SERVICE_NAME);

    singleton.build(new DelegatingServiceContainer(
        context.getServiceTarget(),context.getServiceRegistry()))
        .setInitialMode(ServiceController.Mode.ACTIVE).install();
    LOGGER.info("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:

<configuration>
  <archive>
    <manifestentries>
      <dependencies>
        org.jboss.msc,org.jboss.as.server,
        org.jboss.as.clustering.singleton
      </dependencies>
    </manifestentries>
  </archive>
</configuration>

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">
  <global-modules>
    <module name="org.jboss.msc" slot="main">
    <module name="org.jboss.as.clustering.singleton" slot="main">
  </global-modules>
</subsystem>

And finally you can execute your server and enjoy new, cool functionality.
 

Reference: Using HA Singleton in JBoss 7 from our JCG partner Jakub Kubrynski at the Java(B)Log blog.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.

2 Responses to "Using HA Singleton in JBoss 7"

  1. twain says:

    Nice tutorial, thanks. I have been looking for something like this for a long time now.
    How did you manage to start your Spring based @Scheduled annotation only on the Master Node?

  2. Hi,

    I decided to choose simpler way (following KISS rule) and at the begging of the @Scheduled method I’m checking if it’s started on master and only then continue execution.

    Kind regards,
    Kuba

Leave a Reply


three − 1 =



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy | Contact
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close