Test Alert Reception API with Alert FilteringIn Java
Note
The sample
filters on component name and only receives alerts from a component with name
sun-http-binding
package org.glassfish.extended.management.sample.impl.alerts;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import org.glassfish.openesb.extended.management.api.alerts.Alert;
import org.glassfish.openesb.extended.management.api.alerts.AlertNotificationFilterElementType;
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 TestAlertFiltering {
String dasPort;
String dasHostName;
String dasUser;
String dasPassword;
AlertsManagementService alertManagementService;
AlertNotificationService alertNotificationService;
ExtendedManagementClient client;
String subscriptionID;
Map<String/*AlertNotificationFilterElementType*/, String /*value*/> filterMap = new HashMap<String/*AlertNotificationFilterElementType*/, String /*value*/>();
/**
* No argumant Constructor
*/
public TestAlertFiltering() {
}
/**
*
* @return
* @throws ExtendedManagementRemoteException
*/
boolean initialize() throws ExtendedManagementRemoteException {
boolean valid = false;
dasHostName = getUserInput("Enter DAS host name <localhost> :", "localhost");
dasPort = getUserInput("Enter DAS http administration 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;
alertManagementService = client.getService(org.glassfish.openesb.extended.management.api.alerts.AlertsManagementService.class);
alertNotificationService = alertManagementService.getAlertNotificationService();
if (alertNotificationService == 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;
}
// String alertCodeToFilter = "";
// String alertSeveretyToFilter = "";
// String deploymentNameToFilter = "";
// String projectNameToFilter = "";
// String serverNameToFilter = "";
String componentNameToFilter = "sun-http-binding";
filterMap.put(AlertNotificationFilterElementType.COMPONENTNAME.getAlertNotificationFilterElement(), componentNameToFilter);
// filterMap.put(AlertNotificationFilterElementType.ALERTCODE.getAlertNotificationFilterElement(), alertCodeToFilter);
// filterMap.put(AlertNotificationFilterElementType.ALERTSEVERITY.getAlertNotificationFilterElement(), alertSeveretyToFilter);
// filterMap.put(AlertNotificationFilterElementType.DEPLOYMENTNAME.getAlertNotificationFilterElement(), deploymentNameToFilter);
// filterMap.put(AlertNotificationFilterElementType.PROJECTNAME.getAlertNotificationFilterElement(), projectNameToFilter);
// filterMap.put(AlertNotificationFilterElementType.SERVERNAME.getAlertNotificationFilterElement(), serverNameToFilter);
System.out.println("////////////////////////////////////////////");
System.out.println("// Filter Map is set to the following ...");
System.out.println("////////////////////////////////////////////");
if (filterMap != null) {
Set<Entry<String, String>> entries = filterMap.entrySet();
for (Map.Entry<String, String> entry : entries) {
String key = entry.getKey();
String value = entry.getValue();
System.out.println(key + " = " + value);
}
}
System.out.println("////////////////////////////////////////////");
return valid;
}
/**
*
* @param message
* @param defaultValue
* @return
*/
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) {
e.printStackTrace();
}
return returnvalue;
}
/**
*
* @param targets
* @throws Exception
*/
public void subscribe(String[] targets) throws Exception {
subscriptionID = alertNotificationService.subscribe(filterMap,
targets,
this,
"eventProcessCallBack",
new Boolean(true),
this,
"exceptionProcessCallBack");
System.err.println(" subcription id is " + subscriptionID);
}
/**
*
* @param anAlert
*/
public void eventProcessCallBack(Alert anAlert) {
System.out.println(" ---> alert received <-----");
System.out.println("alert date :" + anAlert.getAlertDateString());
System.out.println("alert severity :" + anAlert.getAlertSeverity());
System.out.println("alert message code :" + anAlert.getAlertMessageCode());
System.out.println("alert server name :" + anAlert.getAlertServer());
System.out.println("alert message details :" + anAlert.getAlertMessageDetails());
System.out.println("alert component name :" + anAlert.getAlertComponentName());
System.out.println("alert component project path :" + anAlert.getAlertComponentProjectPath());
System.out.println("alert component type :" + anAlert.getAlertComponentType());
System.out.println("alert deployment :" + anAlert.getAlertDeployment());
System.out.println("alert environment :" + anAlert.getAlertEnvironment());
System.out.println("alert logical host :" + anAlert.getAlertLogicalHost());
System.out.println("alert physical host :" + anAlert.getAlertPhysicalHost());
System.out.println("alert server type :" + anAlert.getAlertServerType());
System.out.println("alert state :" + anAlert.getAlertState());
System.out.println("alert status :" + anAlert.getAlertStatus());
System.out.println("alert type :" + anAlert.getAlertType());
}
/**
*
* @param processCallbackException
*/
public void exceptionProcessCallBack(Exception processCallbackException) {
System.out.println("Process Callback Exception Details :\n");
processCallbackException.printStackTrace();
}
/**
*
*/
public void unsubscribe() {
if (subscriptionID == null) {
return;
}
String[] subscriptionIDs = {subscriptionID};
try {
alertNotificationService.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);
}
}
}
/**
*
* @param aCommand
* @throws Exception
*/
private void executeCommand(String aCommand) throws Exception {
try {
if (aCommand.toLowerCase().equals("reg")) {
subscribe(null);
} else if (aCommand.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);
}
}
}
}
/**
*
* @return
* @throws Exception
*/
public String displayOptions() throws Exception {
String theCommand = "";
System.out.println("");
initialize();
while (true) {
System.out.println("Valid Operations:\n" +
" REG - subscribe client for receiving event notifications with no filters and optional targets (comma delimited string) (can be called multiple times - returns the subscription ID for each) \n" +
" UNREG - unsubscribe client from receiving notifications\n" +
" EX - to exit the program ");
System.out.println("Please Select opertion:");
theCommand = new BufferedReader(new InputStreamReader(System.in)).readLine();
String[] validOptions = {"reg", "unreg", "ex"};
boolean valid = false;
for (String option : validOptions) {
String testCommand = theCommand.toLowerCase();
if (testCommand.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 = " + theCommand);
System.out.println();
System.out.println();
if (theCommand.equalsIgnoreCase("ex")) {
unsubscribe();
System.exit(0);
} else {
executeCommand(theCommand);
}
}
}
}
/**
* @param args
*/
public static void main(String[] args) {
try {
TestAlertFiltering test = new TestAlertFiltering();
test.displayOptions();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Quick Navigation Links
Number of visits: 4