2014년 2월 25일 화요일

JMX-Communication with Notifications

Components of the JMX notification model
MBean events are wrapped as notification objects and broadcast from MBeans. Notification listeners, like event listeners, register with MBeans in order to receive notifications, including user-deined notifications.

Component
Descripton

Notification broadcaster
An object that implements the javax.management.NotificationBroadcaster interface, allowing it to send notifications.

Notification
An object emitted from broadcaters that contain information for a listener.

Notificatioin listener
An object that implements the javax.management.NotificationListener interface, allowing it to receive notifiacaitons.

Notification filter
An obejct associated with a listener that can filter notifications, allowing only the desired notifications to be delivered to a listener.


The Notification class
The Notification class extends java.util.EventObject and is used as a super class for other notification classes.
The Notification class contains six member variables that are all accessible through getter methods.
The Notification class has several different constructors, each providing a different set of initializaation arguments for these class members.

Class member
Purpose
Message
A String object representing a message. This could be the reason for the notification.
SequenceNumber
A number indicating the order in relation of events from the source. The source populates this field if it intends to give listeners the ability to sort incoming notifications. The notification model makes no guaranties that notifications will be received in the order they were sent.
TimeStamp
The timestamp of the notification, represented as a long value.
Type
The dot-separated String value indicating the type of the notification. Not a class type. Fore example:acme.mbeanA.event1.
UserData
An object used to contain any data that a source wants to send to a notification listener.
Source
The source of the notification. This object contains an ObjectName or a reference to the object that generated the notification.



package com.daddycat.blog.jmx;

public interface HelloMBean {
public void setMessage(String message); → message 설정

public String getMessage();

public void sayingHello(); → 설정된 message로 Notification을 1초마다 발생한다.
public void shutup(); → Notification 중지
}



package com.daddycat.blog.jmx;

import javax.management.AttributeChangeNotification;
import javax.management.MBeanNotificationInfo;
import javax.management.Notification;
import javax.management.NotificationBroadcasterSupport;

public class Hello extends NotificationBroadcasterSupport implements HelloMBean, Runnable {
private String message = null;
private boolean stop = true;
private int sayCount = 0;
public Hello() {
message = "Hello, world";
}

public Hello(String message) {
this.message = message;
}


public void setMessage(String message) {
String oldValue = this.message;
this.message = message;
//source, sequenceNumber, timeStamp, msg, attributeName, attributeType, oldValue, newValue
AttributeChangeNotification atrChangeNotification =
new AttributeChangeNotification(this, 0, System.currentTimeMillis(), "Hello Message Change", "message", "String", oldValue, message);
sendNotification(atrChangeNotification);

}


public String getMessage() {
return message;
}


public void sayingHello() {
stop = false;
Thread t = new Thread(this);
t.start();
}

public void shutup() {
stop = true;
}


public void run() {
while(!stop) {
try {
System.out.println("waiting...");
Thread.sleep(1000);
System.out.println("Saying...");
} catch (InterruptedException e) {
e.printStackTrace();
}
// Type, Source, sequenceNumber, TimeStamp, Message
Notification notification = new Notification("DaddyCat.JMX.Say.Counter", this, sayCount++, System.currentTimeMillis(), message+"("+sayCount+")");
sendNotification(notification);
}
}

@Override
public MBeanNotificationInfo[] getNotificationInfo() {
String[] type = {"DaddyCat.JMX.Say.Counter" };
MBeanNotificationInfo[] info = new MBeanNotificationInfo[ 2 ];
info[ 0 ] = new MBeanNotificationInfo( type, "javax.management.Notification", "The HelloMBean counter" );
String[] attChanges = {
AttributeChangeNotification.ATTRIBUTE_CHANGE };
info[ 1 ] = new MBeanNotificationInfo( attChanges, "javax.management.AttributeChangeNotification", "The HelloMBean counter" );
return info;
}
}
AttributeChangeNotification은 MBean의 Attribute가 변경되었을 때 사용된다.  해당 attributeName, attributeType, oldValue, newValue가 포함된다.



package com.daddycat.blog.jmx;

import java.lang.management.ManagementFactory;

import javax.management.MBeanServer;
import javax.management.Notification;
import javax.management.NotificationListener;
import javax.management.ObjectName;


public class NotificationAgent implements NotificationListener {
private MBeanServer mbs = null;

public NotificationAgent() {

// Get the platform MBeanServer
mbs = ManagementFactory.getPlatformMBeanServer();
// Unique identification of MBeans
Hello helloBean = new Hello();
ObjectName helloName = null;

try {
// Uniquely identify the MBeans and register them with the platform
// MBeanServer
helloName = new ObjectName("DADDYCAT:name=Communication_Notification");
mbs.registerMBean(helloBean, helloName);
helloBean.addNotificationListener(this, null, null); → 리스너 등록
} catch (Exception e) {
e.printStackTrace();
}
}

// Utility method: so that the application continues to run
private static void waitForEnterPressed() {
try {
System.out.println("Press  to continue...");
System.in.read();
} catch (Exception e) {
e.printStackTrace();
}
}

public static void main(String argv[]) {
NotificationAgent agent = new NotificationAgent();
System.out.println("Communication_Notification is running...");
NotificationAgent.waitForEnterPressed();
}

@Override
public void handleNotification(Notification notification, Object handback) {
System.out.println("Receiving notification....");
System.out.println(notification.getType());
System.out.println(notification.getMessage());
}
}


NtoficationAgent의 Operation의 sayingHello을 실행하면 Notification이 전달 된다.
물론 shutup을 실행하면 중지.



Attribute의 Message을 수정하면 AttributeChangeNotification이 발생한다.


댓글 없음:

댓글 쓰기

블록체인 개요 및 오픈소스 동향

블록체인(block chain) 블록체인은 공공 거래장부이며 가상 화폐로 거래할때 발생할때 발생할 수 있는 해킹을 막는 기술. 분산 데이터베이스의 한 형태로, 지속적으로 성장하는 데이터 기록 리스트로서 분산 노드의 운영자에 의한 임의 조작이 불가...