Implemented JMX Notifications

 



 

Overview

 

Notifications are generated by an Agent to intimate the change of state of a particular variable in the Agent , to the Managers. In this Reference Implementation, Notification Type construct is defined for a variable and on any change of state of that variable, Notification is generated. Lets discuss in detail the implementation details of sending Notifications to the Managers from our shopping cart Agent.

 

Requirement

 

As per the Reference Implementation, the Agent has to send Notifications to the Managers when :

The maximum inventory level available for all the pet items is 20. Say for example, if the customer places an order for more than 15 for the pet Parrot, then a Notification is generated. This is done through the methods implemented in the java file generated for Traps.

 

Instrumentation

 

In the main file generated the following code snippet is added inside registerJmxServices method.  Once this method is executed (ie) the thread is started, it will execute the run method of inventory monitor class which is defined inside as inner class in ShoppingCartAgent class

 

InventoryMonitor inventoryMonitor = new InventoryMonitor(this, ShoppingCartTable);
Thread invThread = new Thread(inventoryMonitor);
invThread.start();

 

As long as the agent is alive, it will keep calling monitorAndSendNotif()method for every 10000 seconds, as given in the following code-snippet.

 

public void run(){
while(true){
monitorAndSendNotif();
try{
Thread.sleep(10000);
}catch(InterruptedException e){
//e.printStackTrace();
}
}
}

public void {
int parrotInv = Store.getInventoryLevel("Parrot").intValue();
int fishInv = Store.getInventoryLevel("Fish").intValue();
int catInv = Store.getInventoryLevel("Cat").intValue();
int dogInv = Store.getInventoryLevel("Dog").intValue();
try{
if (parrotInv < 5) {
sendNotification("Parrot", new Integer(parrotInv));
// Uncommenting the following code snippet will increase
// the stock Level by 10.
/*if (parrotInv <= 2){
Store.increaseStock("Parrot", new Integer(10));
}*/
}
if (fishInv < 5) {
sendNotification("Fish", new Integer(fishInv));
/*if (fishInv <= 2){
Store.increaseStock("Fish", new Integer(10));
}*/
}
if (catInv < 5) {
sendNotification("Cat", new Integer(catInv));
/*if (catInv <= 2){
Store.increaseStock("Cat", new Integer(10));
}*/
}
if (dogInv < 5) {
sendNotification("Dog", new Integer(dogInv));
/*if (dogInv <= 2){
Store.increaseStock("Dog", new Integer(10));
}*/
}
}catch(Exception e){
e.printStackTrace();
}
}

 

Here the Notification is defined with the following inputs:

NotificationBroadcaster of ShoppingCartTable is obtained and the same is used to send the notification by calling the sendNotification() passing the Notification which is defined above.

 

public void sendNotification(String petName, Integer level)
throws AgentException {
Notification notification = new Notification("com.adventnet.traps.outOfStockNotification", ShoppingCartTable.getClass().getName(), ++sequenceNumber, new Date().getTime(), new String(petName + " Inventory Level Went down to " + level));
try{
ModelMBeanNotificationBroadcaster notifBC= (ModelMBeanNotificationBroadcaster )agent.getNotifHandler(ShoppingCartTable);
setUserDataForNotification(notification);
notifBC.sendNotification(notification);
}
catch(Exception e)
{
e.printStackTrace();
}
}

 

Viewing Notifications

 

The Notification mechanism is designed to forward only notifications from registered MBeans on the agent side to proper listeners on the manager side.



Copyright © 1996-2004, AdventNet Inc. All Rights Reserved.