Friday, February 13, 2009

Generating alerts from your Java application

You can very easily setup your application to emit MBean notification in case of a severe error. Here is some sample code:

public class AlertNotification
extends NotificationBroadcasterSupport
implements AlertNotificationMBean {

private static final String TYPE_SEVERE = "SEVERE";
private int sequence = 1;
private static AlertNotification instance =
new AlertNotification();

public static AlertNotification getInstance() {
return instance;

private AlertNotification() {

public MBeanNotificationInfo[] getNotificationInfo() {
MBeanNotificationInfo[] notifications =
new MBeanNotificationInfo[1];
notifications[0] =
new MBeanNotificationInfo(new String[] { TYPE_SEVERE },
"Types of notifications emitted by this broadcaster");
return notifications;

public void send(String message) {
this.sendNotification(new Notification(TYPE_SEVERE, this,
sequence++, System.currentTimeMillis(), message));

Also remember to register the MBean above:

MBeanServer mbeanServer =
new ObjectName(ObjectNames.ALERT_NOTIFICATION_BEAN));

The code above will emit a MBean notification when the send() method is called. For example:

AlertNotification.getInstance().send("your alert message here");

You can then configure jManage to listen to this MBean notification and generate an alert that can be delivered to your mail box. To learn more about configuring alerts in jManage see jManage Documentation.

Please let me know what you think of the code above. Also, please share if you have setup something like this with jManage.

No comments: