Test Notification Service In Java
TestNotificationService.java
import java.io.IOException;
import org.glassfish.openesb.extended.management.client.ExtendedManagementClient;
import org.glassfish.openesb.extended.management.client.ExtendedManagementClientFactory;
import org.glassfish.openesb.extended.management.common.jbi.ManagementRemoteExceptionProcessor;
import com.sun.esb.management.api.notification.NotificationService;
import com.sun.esb.management.common.ManagementRemoteException;
import org.glassfish.openesb.management.ServerConnectionFactory;
/**
* @author gopalan
*
*/
public class TestNotificationService {
/**
*
*/
public TestNotificationService() {
// TODO Auto-generated constructor stub
}
private static void sleep(int millis) {
try {
Thread.sleep(millis);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private static void waitForEnterPressed() {
try {
System.out.println("\nPress <Break> to exit ...");
System.in.read();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* @param args
*/
public static void main(String[] args) {
String hostName = ServerConnectionFactory.LOCALHOST,
userName = ServerConnectionFactory.ADMIN_USERNAME,
password = ServerConnectionFactory.ADMINADMIN_PASSWORD;
int portNumber = ServerConnectionFactory.RMI_ADMIN_PORT;
String targetName = ServerConnectionFactory.SERVER_TARGET;
try {
ExtendedManagementClient client = null;
client = ExtendedManagementClientFactory.getInstance(hostName, portNumber, userName, password);
// Get the Notification service
NotificationService notificationService = client.getNotificationService();
// Create Client Notification Processors/Event Listeners
TheClientNotificationProcessor client1 = new TheClientNotificationProcessor(1);
TheClientNotificationProcessor client2 = new TheClientNotificationProcessor(2);
// Register notification listeners
notificationService.addNotificationEventListener(client1);
notificationService.addNotificationEventListener(client2);
// Wait for completion
waitForEnterPressed();
// unregister notification processors/notification listeners
notificationService.removeNotificationEventListener(client1);
notificationService.removeNotificationEventListener(client2);
} catch (ManagementRemoteException e) {
String result = ManagementRemoteExceptionProcessor.processTaskException(e);
System.out.println(result);
}
}
}
TheClientNotificationProcessor.java
package org.glassfish.extended.management.sample.impl.notification;
import java.util.Random;
import com.sun.esb.management.api.notification.EventNotification;
import com.sun.esb.management.api.notification.EventNotificationListener;
import javax.management.Notification;
import javax.management.openmbean.CompositeDataSupport;
/**
* Listener class implemented by the client which processes the notification.
* This implements the callback/handback object for the Notification Service
* to callback to the client when new notifications are received.
*
* @author gopalan
*/
public class TheClientNotificationProcessor implements EventNotificationListener, Runnable {
private String clientId;
// Ensure prompt communication of the suspend-request in the absence of
// explicit synchronization using a volatile attribute
private volatile boolean isThreadSuspended;
private Thread theThread;
private Random randomNumber;
/**
* Constructor
*/
public TheClientNotificationProcessor(int id) {
this.clientId = id + "";
this.randomNumber = new Random(id);
this.theThread = new Thread(this);
this.theThread.start();
}
/**
* Callback operation that the client implements which will be
* invoked by the service when new event notifications are received.
*
* @param anEvent
* @see com.sun.esb.management.notification.impl.client.NotificationEventListener#processNotification(com.sun.esb.management.notification.impl.client.NotificationEvent)
*/
public synchronized void processNotification(EventNotification anEvent) {
if (anEvent != null) {
Notification aNotification = anEvent.getNotification();
CompositeDataSupport userData = (CompositeDataSupport) aNotification.getUserData();
System.out.println("\tFor client Id:" + this.clientId + ", Notification is:" + aNotification.toString());
System.out.println("\tFor client Id:" + this.clientId + ", User Data is:" + userData.toString());
extractUserData(userData);
}
isThreadSuspended = !isThreadSuspended;
if (!isThreadSuspended) {
notify();
}
}
/**
* From http://wiki.open-esb.java.net/Wiki.jsp?page=SierraMBeanNotifications
*
* The CompositeData that is passed in the UserData field of the notifications can have either of two formats.
* The first format is for all notifications except for those for Service Unit events;
* The second format is for Service Unit events only.
*
* The following fields are present in both formats:
* SourceType the type of source of the event: JBIRuntime, BindingComponent, ServiceEngine, SharedLibrary, ServiceAssembly, or ServiceUnit
* EventType the type of event: "Installed", "Uninstalled", Deployed, Undeployed, Started, Ready, Stopped, or ShutDown
* TargetName the name of the target where the event originated: either "server" or "clustername-instancename"
* SourceName the name of the source of the event: "JBIFramework", component name, shared library name, service assembly name, or service unit name
*
* The following fields are present only in the UserData for a Service Unit event:
* ServiceAssemblyName the name of the service assembly to which the service unit belongs
* ComponentName the name of the component to which the service unit is deployed
*
* @param userData
*/
private void extractUserData(CompositeDataSupport userData) {
String notificationSourceType = (String) userData.get("SourceType"); // NOI18N
String notificationEventType = (String) userData.get("EventType");
String notificationTargetName = (String) userData.get("TargetName");
String notificationSourceName = (String) userData.get("SourceName");
String notificationServiceAssemblyName = "INVALID_SERVICE_ASSEMBLY_NAME";
String notificationComponentName = "INVALID_COMPONENT_NAME";
System.out.println("***********************************************");
System.out.println("*************** User Data *********************");
System.out.println("***********************************************");
System.out.println("** Source: "+notificationSourceType);
System.out.println("** Type: "+notificationEventType);
System.out.println("** Target Name: "+notificationTargetName);
System.out.println("** Source Name: "+notificationSourceName);
if (notificationSourceType != null) {
if ("ServiceUnit".equals(notificationSourceType.trim())) {
notificationServiceAssemblyName = (String) userData.get("ServiceAssemblyName");
notificationComponentName = (String) userData.get("ComponentName");
System.out.println("** Service Assembly Name: "+notificationServiceAssemblyName);
System.out.println("** Component Name: "+notificationComponentName);
}
}
System.out.println("***********************************************");
}
/* (non-Javadoc)
* @see java.lang.Runnable#run()
*/
public void run() {
while (true) {
try {
Thread.sleep(randomNumber.nextInt(3000));
// Enter a synchronized block only if the thread has
// actually been suspended
if (isThreadSuspended) {
// eliminate race conditions that could cause the
// "suspended" thread to miss a notify and remain
// suspended indefinitely by synchronizing access
synchronized (this) {
while (isThreadSuspended) {
wait();
}
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
Quick Navigation Links
Number of visits: 6
This page (revision-4) was last changed on
24-Feb-10 01:35 AM, -0800
by NewAcct.
This page was created on
21-Sep-09 10:23 AM, -0700 by Gopalan Suresh Raj.
More info...
| Version |
Date |
Author |
Size |
Changes ... |
|
4
|
24-Feb-10 01:35 AM, -0800
|
NewAcct |
10172 |
to previous
|
|
3
|
04-Feb-10 19:36 PM, -0800
|
newacct |
10193 |
to previous
|
to last
|
|
2
|
21-Sep-09 10:23 AM, -0700
|
Gopalan Suresh Raj |
10201 |
to previous
|
to last
|
|
1
|
21-Sep-09 10:23 AM, -0700
|
Gopalan Suresh Raj |
10203 |
to last
|