JBoss cluster: discover cluster members

In this tutorial we will learn some techniquesto discover your JBoss AS cluster members using all the available instruments in JBoss AS 7: the mod_cluster API, the underlying JGroups transport, the embedded Infinispan cache manager or plain JMX API.

Discover cluster nodes with mod_cluster

The first (and most obvious) source of information about your cluster, when using AS 7, is mod_cluster. If you have installed the mod_cluster Manager:


<Location /mod_cluster-manager>
    SetHandler mod_cluster-manager

    Order deny,allow
    Deny from all
    Allow from 127.0.0.1
 </Location>

Then you can have a quick look at cluster members by requesting the mod_cluster-manager Web context:

mod cluster jboss as 7 discover cluster members

Note: You can use as well the CLI to achieve this information, using the list-proxies command on the modcluster's subsystem as shown here:

[standalone@localhost:9999 subsystem=modcluster] :list-proxies
{
    "outcome" => "success",
    "result" => [
        "machine1:6666",
        "machine2:6666"
    ]
}

Discover cluster nodes using JGroups

Another needful source of information is the underlying JGroups transport protocol. Since all members which join the cluster need to use a Channel object, then a simple and elegant solution to provide cluster members is reading the Channel's view which contains a getMembers method. Here's a sample code snippet:

        Channel channel = (Channel) CurrentServiceContainer.getServiceContainer().getService(ServiceName.JBOSS.append("jgroups", "channel", "web")).getValue();

        List<org.jgroups.Address> members = channel.getView().getMembers();
        List< IpAddress> addresses = new ArrayList< IpAddress>();


        for (org.jgroups.Address member: members) {
            PhysicalAddress physicalAddr = (PhysicalAddress)channel.down(new Event(Event.GET_PHYSICAL_ADDRESS, member));
            IpAddress ipAddr = ( IpAddress)physicalAddr;
            System.out.println("Found IP: "+ipAddr.getIpAddress().getHostAddress());
            addresses.add(ipAddr);
        }

Discover cluster nodes using Infinispan

Infinispan is the default Cache Manager for JBoss AS 6 and JBoss AS 7. There's a simple shortcut into the EmbeddedCacheManager object which allows reading the cluster members:


       @Resource(lookup="java:jboss/infinispan/container/cluster")
       private CacheContainer container;

       . . . . . . 
        List<org.infinispan.remoting.transport.Address> members = container.getCache().getCacheManager().getMembers();


        for (org.infinispan.remoting.transport.Address member: members) {
            System.out.println("Found Member:" +member.toString());
        }

Be Careful! Both Infinispan API and JGroups API use the Address class. We have fully specified the Class name in order to avoid conflicts between the imports!

Discover cluster nodes using JMX

If you are not satisfied with all the methods just shown, you can use plain old JMX to gather information about cluster nodes:

      Object obj = ManagementFactory.

                                   getPlatformMBeanServer().

                                        getAttribute(ObjectName.getInstance("jgroups:type=channel,cluster=\"web\""), "View");
      System.out.println(obj);

Related articles available on mastertheboss.com

JBoss Clustering a Web Application

Please Note: This article cover JBoss AS 4/5/6 releases. If you w

Clustering EJB 3 with JBoss AS

To cluster a stateless session bean in EJB 3 all you need to do i

JBoss monitoring HTTP Session replication

In this article we'll show how to monitor HTTPSession replication

How do I change multicast address of JBoss cluster ?

Since JBoss AS 4.0.3, the jboss.partition.udpGroup property can b

JBoss farming service

What is the farming service ? this article explains about it, als

JBoss HTTP Session replication

HTTP session replication is used to replicate the state associate

Follow us on Twitter