Index Changes
ESB Console Home

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.HashMap;
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 alertsManagementService;
    AlertNotificationService alertNotificationService;
    ExtendedManagementClient client;
    String subscriptionID;
    Map<String/*AlertNotificationFilterElementType*/, String /*value*/> filterMap = new HashMap<String/*AlertNotificationFilterElementType*/, String /*value*/>();

    /**
     * No argumant Constructor
     */
    public TestAlertNotificationListener() {
    }

    /**
     *
     * @return
     * @throws ExtendedManagementRemoteException
     */
    public 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;
            alertsManagementService = client.getService(org.glassfish.openesb.extended.management.api.alerts.AlertsManagementService.class);
            alertNotificationService = alertsManagementService.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;
        }
        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) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return returnValue;

    }

    /**
     * 
     * @param targets
     * @throws Exception
     */
    public void subscribe(String[] targets) throws ExtendedManagementRemoteException {
        subscriptionID = alertNotificationService.subscribe(filterMap,
                targets, 
                this,
                "onEventCallback",
                new Boolean(true),
                this,
                "onExceptionCallback");
        System.err.println(" subcription id is " + subscriptionID);
    }

    /**
     * 
     * @param anAlert
     */
    public void onEventCallback(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 onExceptionCallback(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.equalsIgnoreCase("reg")) {
                subscribe(null);
            } else if (aCommand.equalsIgnoreCase("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) {
                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 {
            TestAlertNotificationListener test = new TestAlertNotificationListener();
            test.displayOptions();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}





Number of visits: 52

JSPWiki v2.4.100
[RSS]
« Home Index Changes Prefs
This page (revision-4) was last changed on 04-Feb-10 23:40 PM, -0800 by newacct