Test Alert Reception API In Java
package org.glassfish.extended.management.sample.impl.alerts;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.glassfish.openesb.extended.management.api.alerts.Alert;
import org.glassfish.openesb.extended.management.api.alerts.AlertNotificationService;
import org.glassfish.openesb.extended.management.api.alerts.AlertsManagementService;
import org.glassfish.openesb.extended.management.client.ExtendedManagementClient;
import org.glassfish.openesb.extended.management.client.ExtendedManagementClientFactory;
import org.glassfish.openesb.extended.management.common.ExtendedManagementRemoteException;
/**
*
* @author gopalan
*/
public class TestAlertNotificationListener {
String dasPort;
String dashostname;
String dasUser;
String dasPassword;
AlertsManagementService managementService;
AlertNotificationService notifService;
ExtendedManagementClient client;
String subscriptionID;
List idList = new ArrayList();
Map filterMap = new HashMap();
/**
* @throws ExtendedManagementRemoteException
*
*/
public TestAlertNotificationListener() throws ExtendedManagementRemoteException {
init();
}
public boolean init() throws ExtendedManagementRemoteException {
boolean valid = false;
dashostname = getUserInput(" enter DAS host name <localhost> :",
"localhost");
dasPort = getUserInput(" enter DAS http port <4848> :", "4848");
dasUser = getUserInput(" enter user name <admin> :", "admin");
dasPassword = getUserInput(" enter user password <adminadmin> :",
"adminadmin");
int port = new Integer(dasPort);
client = ExtendedManagementClientFactory.getInstance(dashostname, port,
dasUser, dasPassword);
if (client != null) {
System.out.println("Management Client is valid.");
valid = true;
managementService = client.getService(org.glassfish.openesb.extended.management.api.alerts.AlertsManagementService.class);
notifService = managementService.getAlertNotificationService();
if (notifService == null) {
valid = false;
System.out.println("Could not obtain Alert Management Notification service");
}
} else {
System.out.println("Could not obtain the Management Client.");
valid = false;
}
return valid;
}
public String getUserInput(String message, String defaultValue) {
String returnvalue = "";
System.out.println(message);
try {
returnvalue = new BufferedReader(new InputStreamReader(System.in)).readLine();
if (returnvalue.length() == 0) {
returnvalue = defaultValue;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return returnvalue;
}
public void subscribe(String[] targets) throws Exception {
subscriptionID = notifService.subscribe(new HashMap<String, String>(),
targets, this, "eventProcessCallBack", new Boolean(true), this,
"exceptionProcessCallBack");
System.err.println(" subcription id is " + subscriptionID);
}
public void eventProcessCallBack(Alert alert) {
System.out.println(" ---> alert received <-----");
System.out.println("alert date :" + alert.getAlertDateString());
System.out.println("alert severity :" + alert.getAlertSeverity());
System.out.println("alert message code :" + alert.getAlertMessageCode());
System.out.println("alert server name :" + alert.getAlertServer());
System.out.println("alert message details :" + alert.getAlertMessageDetails());
System.out.println("alert component name :" + alert.getAlertComponentName());
System.out.println("alert component project path :" + alert.getAlertComponentProjectPath());
System.out.println("alert component type :" + alert.getAlertComponentType());
System.out.println("alert deployment :" + alert.getAlertDeployment());
System.out.println("alert environment :" + alert.getAlertEnvironment());
System.out.println("alert logical host :" + alert.getAlertLogicalHost());
System.out.println("alert physical host :" + alert.getAlertPhysicalHost());
System.out.println("alert server type :" + alert.getAlertServerType());
System.out.println("alert state :" + alert.getAlertState());
System.out.println("alert status :" + alert.getAlertStatus());
System.out.println("alert type :" + alert.getAlertType());
}
public void exceptionProcessCallBack(Exception e) {
System.out.println("exception details :\n");
e.printStackTrace();
}
public void unsubscribe() {
if (subscriptionID == null) {
return;
}
String[] SubscriptionIDs = {subscriptionID};
try {
notifService.unsubscribe((String[]) SubscriptionIDs);
subscriptionID = null;
} catch (ExtendedManagementRemoteException exception) {
int index = 0;
String[] causes = exception.getCauseMessageTrace();
for (String cause : causes) {
System.out.println("Cause #" + (++index) + ": " + cause);
}
}
}
private void execCommand(String cmd) throws Exception {
try {
if (cmd.toLowerCase().equals("reg")) {
subscribe(null);
} else if (cmd.toLowerCase().equals("unreg")) {
unsubscribe();
}
} catch (ExtendedManagementRemoteException exception) {
int index = 0;
String[] causes = exception.getCauseMessageTrace();
if (causes != null) {
for (String cause : causes) {
System.out.println("Cause #" + (++index) + ": " + cause);
}
}
}
}
public String displayOptions() throws Exception {
String cmd = "";
init();
while (true) {
System.out.println("valid opeartions:\n" + " REG - subscribe client for notification no filter and targets optional (comma delimited string) (can be called multiple time -return ID for each) \n" + " UNREG - unsubscribe client from receiving notification\n" + " EX - to exit the program ");
System.out.println("Please Select opertion:");
cmd = new BufferedReader(new InputStreamReader(System.in)).readLine();
String[] validOption = {"reg", "unreg", "ex"};
boolean valid = false;
for (String option : validOption) {
String testCmd = cmd.toLowerCase();
if (testCmd.startsWith(option)) {
valid = true;
break;
}
}
if (valid == false) {
System.out.println();
System.out.println();
System.out.println();
displayOptions();
} else {
System.out.println();
System.out.println();
System.out.println("the command is = " + cmd);
System.out.println();
System.out.println();
if (cmd.equalsIgnoreCase("ex")) {
unsubscribe();
System.exit(0);
} else {
execCommand(cmd);
}
}
}
}
/**
* @param args
*/
public static void main(String[] args) {
try {
TestAlertNotificationListener test = new TestAlertNotificationListener();
test.displayOptions();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Quick Navigation Links
Number of visits: 18