The use of plugin approach in Active MQ which allows the clients to levarage the broker services and make use of different methods with in the Broker object and many others.
We will see how it really helps in getting some custom metrics even those metrics are not exposed as an Mbean. We will write the custom plugin and will expose this custom plugin as Mbean .
We all require is the current version Active MQ distribution , Please find it here.
http://activemq.apache.org/download.html
Once you are done with the downloading and start running the activemq. To start activemq , please go to the bin folder and run the command ./activemq start on MAC but on windows you can just use activemq start. Once you started the activemq you should be able to see the admin console using this URL :
http://localhost:8161/admin . Please use admin / admin as user name and password to get on to the console. You will see the below console ...if everything runs as expected.
Active MQ Console
So once the Active MQ is running on your machine , We need to identify what MBeans are exposed in the Active MQ broker.You can see the below screen shot where it shows all the active mq mbeans got exposed through jconsole.
Jconsole with the exposed Active MQ Mbeans.
So now we are going to write a custom plugin where this plugin is not a plain old activemq plugin but it also serves as an mbean where we can get the required data.
To develop a custom active mq plugin we have to start with the class implementing the broker plugin as a factory class in which where we can provide the body for the install plugin method . The install plugin method you need to override where you can invoke the Mbean Implenatation which extends the broker filter.
Here is what sample Broker plugin looks like :
public class IPAuthenticationPlugin implements BrokerPlugin{
public Broker installPlugin(Broker broker) throws Exception {
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
// Construct the ObjectName for the Hello MBean we will register
ObjectName mbeanName = new ObjectName("com.test:type=Hello");
// Create the Hello World MBean
IPAuth ipAuth = new IPAuthenticationBroker(broker);
StandardMBean mbean = new StandardMBean(ipAuth, IPAuth.class);
// Register the Hello World MBean
mbs.registerMBean(mbean, mbeanName);
return (Broker)ipAuth;
}
IPAuth is an mbean interface where it contains the only one method called getSessionCount(). It looks like this :
public interface IPAuth {
public int getSessionCount();
}
and coming to the actual IPAuthenticationBroker where it extends the broker filter and implements the Mbean. My Mbean interface is IpAuth and impl class is IPAuthenticationBroker.
It looks like :
public class IPAuthenticationBroker extends BrokerFilter implements IPAuth {
public IPAuthenticationBroker(Broker next) {
super(next);
}
public int getSessionCount() {
RegionBroker rb = null;
try {
rb = (RegionBroker) getBrokerService().getBroker().getAdaptor(RegionBroker.class);
} catch (Exception e) {
e.printStackTrace();
}
Map<ConnectionId, ConnectionState> connStates = rb.getConnectionStates();
Iterator it = connStates.entrySet().iterator();
int totalCount = 0;
while (it.hasNext()) {
Map.Entry pair = (Map.Entry) it.next();
ConnectionState connectionState = (ConnectionState) pair.getValue();
totalCount += connectionState.getSessionStates().size();
System.out.println(pair.getKey() + " = " + pair.getValue());
}
return totalCount;
}
Once you create all the above class , please make sure that you expose this project as a jar and place it in the active mq lib folder. Once you placed that you can include this plugin in the active mq xml file to make sure it loads up this plugin while it starts up.
<plugins>
<bean id="ipAuthenticationPlugin" class="com.test.plugin.broker.IPAuthenticationPlugin" xmlns="http://www.springframework.org/schema/beans">
</bean>
</plugins>
So now when you start the activemq broker , then you can go to jconsole and look for the session count :
Please start the producer and consumer in your local machine to test this count . Create a sample queue , write a junit or simple java class where you can publish and consume at a time and then you can find the session count as 2.
Will place the code in github.
No comments:
Post a Comment