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


Discover cluster nodes using Probe shell script

Another discovery tool which shipped with JBoss AS 4-5-6 is the probe.sh shell which invokes a test class contained in JGroups API named org.jgroups.tests.Probe.

This class sends a multicasts a packet to 224.0.75.75:7500 (default multicast address and port) and waits for responses until the timeout has elapsed. Here's how you can adapt it to JBoss AS 7 (warning not tested!)

 

 

set CLASSPATH=%JBOSS_HOME%\modules\org\jgroups\main\jgroups-3.0.6.Final.jar

set CP=%CLASSPATH%

java -cp %CP% org.jgroups.tests.Probe %*

Here's the expected output:

-- send probe on /224.0.75.75:7500

#1 (263 bytes): 192.168.0.1:2222 (ClusterGroup)
local_addr=192.168.0.1:2222
group_name=ClusterGroup
Version=2.2.9 beta, cvs="$Id: Version.java,v 1.23 2006/04/04 17:21:24 
p $"
view: [192.168.0.1:2222|1] [192.168.0.2:2222:2226|1]
group_addr=226.6.6.6:12345
. . . . . . .
Finally, if you want a Quick Servlet for dumping the various programming options displayed in this tutorial:


package com.sample;

import java.io.IOException;
import java.io.PrintWriter;
import java.lang.management.ManagementFactory;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import org.infinispan.manager.CacheContainer;
import org.infinispan.remoting.transport.Address;
import org.infinispan.remoting.transport.jgroups.JGroupsAddress;
import org.jboss.as.server.CurrentServiceContainer;
import org.jboss.msc.service.ServiceName;

import org.jgroups.Channel;
import org.jgroups.Event;
import org.jgroups.PhysicalAddress;
import org.jgroups.stack.IpAddress;

import java.util.Hashtable;
import javax.management.*;
import javax.management.remote.*;


/**
 * Servlet implementation class TestCluster
 */
@WebServlet("/TestCluster")
public class TestCluster extends HttpServlet {
    private static final long serialVersionUID = 1L;
    @Resource(lookup="java:jboss/infinispan/container/cluster")
    private CacheContainer container;


    public TestCluster() {
        super();
        // TODO Auto-generated constructor stub
    }


    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //getClusterMembersInfinispan();
        try {
            getClusterMembersJGroups(response);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    public Collection<IpAddress> getClusterMembersJGroups(HttpServletResponse response) {
        PrintWriter out = response.getWriter();
        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;
            out.println("Found IP: "+ipAddr.getIpAddress().getHostAddress());
            addresses.add(ipAddr);
        }
        out.close();
        return addresses;
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
    }
    public Collection<org.infinispan.remoting.transport.Address> getClusterMembersInfinispan(HttpServletResponse response) {
        PrintWriter out = response.getWriter();
        List<org.infinispan.remoting.transport.Address> members = container.getCache().getCacheManager().getMembers();
        for (org.infinispan.remoting.transport.Address member: members) {
            out.println("Found Member:" +member.toString());
        }
        out.close();
        return members;
    }

    public void getClusterMembersJMX(HttpServletResponse response) throws Exception {
        PrintWriter out = response.getWriter();
        Object obj = ManagementFactory.getPlatformMBeanServer().getAttribute(ObjectName.getInstance("jgroups:type=channel,cluster=\"web\""), "View");
        out.println(obj);
        out.close();
    }
}  

Just keep in mind that when deploying your Servlet, you need to trigger some dependencies like the JBoss AS core modules, the Infinispan API and JGroups API.

Dependencies: org.jboss.as.server,org.jgroups,org.infinispan export                         
0
0
0
s2sdefault

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