Tuesday, October 2, 2012

HA Singleton, Cluster Wide Singleton as MBean in JBoss 5, part 2/3

Document  Version 1.0

  Copyright © 2012-2013 beijing.beijing.012@gmail.com


Keywords:
JBoss HA  Service, HA Singleton, JBoss Cluster, load migration, load distribution, HA Service deployment



Write and deploy an JBoss Mbean



We will write a JBoss MBean called "JbHaSingletonSvcSample"


Create a simple Java project "TestHASingleton" in Eclipse. 

An MBean needs an interface, an implementation class, and a "jbosss-service.xml" file for MBean description/deployment.



The interface: 

package test.ha;

import org.jboss.system.ServiceMBean;

public interface JbHaSingletonSvcSampleMBean extends ServiceMBean{
}



The implementation class:

package test.ha;
import org.jboss.system.ServiceMBeanSupport;
/**
 * The service itself shoul not be written as singleton.
 * @author ws
 *
 */
public class JbHaSingletonSvcSample extends ServiceMBeanSupport implements
JbHaSingletonSvcSampleMBean {
// The lifecycle
public void startSingletonService() throws Exception {
System.out.println("### Starting JbHaSingletonSvcSample Singleton Service..");
}
public void stopSingletonService() throws Exception {
System.out.println("### Stopping JbHaSingletonSvcSample Singleton Service..");
}
}



Create a "META-INF" folder directly under project root:

TestHASingleton/
                            src/
                            META-INF/



Create a "jboss-service.xml" file in "META-INF" folder with following content:

<?xml version="1.0" encoding="UTF-8"?>

<server>
  <mbean code="test.ha.JbHaSingletonSvcSample" name="myexample:service=testHaSample"/>
    <mbean code="org.jboss.ha.singleton.HASingletonController" name="myexample:service=SingletonServiceControllerA">
        <attribute name="HAPartition"><inject bean="HAPartition" /></attribute>
         <attribute name="Target"><inject bean="myexample:service=testHaSample" /></attribute>
        <attribute name="TargetStartMethod">startSingletonService</attribute>
        <attribute name="TargetStopMethod">stopSingletonService</attribute>
    </mbean>
</server>


MBean will be deployed as ".sar" archive. An ".sar" is nothing else that a ".jar" file. To make an ".sar" file, we just need to export the project binaries as "jar" file with Eclipse, i.e. "TestHASingleton.jar",
and then rename it to "TestHASingleton.sar".


HASingleton, Cluster Wide Singleton as MBean in JBoss 5, part3/3   part1/3



No comments:

Post a Comment