FileDocCategorySizeDatePackage
NotificationReceiver.javaAPI DocGlassfish v2 API9357Fri May 04 22:36:24 BST 2007com.sun.enterprise.admin.jmx.remote.notification

NotificationReceiver

public class NotificationReceiver extends Object implements Runnable
This is the notification receiver thread. This thread makes the connection to the server-side and starts to receive the notifications. During a JMXConnector.close call, this thread would send a close message to the server-side and would exit.

Fields Summary
private ClientNotificationManager
mgr
private String
notifMgrUri
private com.sun.enterprise.admin.jmx.remote.comm.HttpConnectorAddress
ad
private URLConnection
mConnection
private ObjectInputStream
objIn
private InputStream
in
private boolean
exit
private boolean
connected
private boolean
timedout
private int
nReconnected
private Thread
receiveThr
private static final Logger
logger
Constructors Summary
public NotificationReceiver(com.sun.enterprise.admin.jmx.remote.comm.HttpConnectorAddress ad, ClientNotificationManager mgr)

/*, 
        DefaultConfiguration.LOGGER_RESOURCE_BUNDLE_NAME );*/

        
              
        this.mgr = mgr;
        this.ad = ad;
        this.notifMgrUri = getNotifMgrURI();
        connect();
        receiveThr = new Thread(this);
        receiveThr.start();
    
Methods Summary
private voidconnect()

        if (connected)
            return;
        connect(null, false);
    
private voidconnect(java.lang.String cmd, boolean disconnect)

		try{
            String uri = notifMgrUri;
            if (cmd != null)
                uri = uri + "&" +
                      DefaultConfiguration.NOTIF_CMD_PARAM +
                      "=" + DefaultConfiguration.NOTIF_CMD_CLOSE;
            System.setProperty("sun.net.client.defaultConnectTimeout",
                               Integer.toString(DefaultConfiguration.NOTIF_CONNECT_TIMEOUT)); // XXX: For Sun JDK only
            URLConnection conn = ad.openConnection(uri);
//            conn.setConnectTimeout(2000); // XXX: For jdk 1.5
            InputStream inStream = conn.getInputStream();
            if (!disconnect) {
                mConnection = conn;
                in = inStream;
                connected = true;
                nReconnected = 0;
            } else {
                disconnect(conn, inStream);
            }
		} catch (IOException ioe){
		ioe.printStackTrace();
            throw (ioe);
		}
    
private voiddisconnect(java.net.URLConnection conn, java.io.InputStream in)

        if (conn instanceof HttpURLConnection) {
            ((HttpURLConnection)conn).disconnect();
        } else
            in.close();
    
private voiddisconnect()

        disconnect(mConnection, in);
    
public voidexit()

        exit = true;
        sendCloseMessage();
        disconnect();
        receiveThr.join();
    
private java.lang.StringgetNotifMgrURI()

        String uri = ad.getPath();
        if (uri == null || uri.trim().length() == 0)
            uri = DefaultConfiguration.DEFAULT_SERVLET_CONTEXT_ROOT;
        uri = uri +
              DefaultConfiguration.NOTIF_MGR_PATHINFO +
              "?" + DefaultConfiguration.NOTIF_ID_PARAM +
              "=" + mgr.getId();
        return uri;
   
public booleanhasTimedout()
Returns if the receiver has timedout while trying to receive notitifications from the server. Timedout -- If a socket timeout happens while trying to connect If the server is not reachable 3 times consequtively during thread loop. If not able to connect when reinit is called.

        return timedout;
    
private booleanisDisconnected(java.io.IOException ioe)

        if (ioe instanceof ClosedChannelException ||
            ioe instanceof SocketException ||
            ioe instanceof ConnectException ||
            ioe instanceof ConnectIOException ||
            ioe instanceof EOFException)
            return true;
        return false;
    
private booleanisExiting()

        return exit;
    
private voidreadNotification()

        Object obj = null;
        try {
            objIn = new ObjectInputStream(in);
            obj = objIn.readObject();
        } catch (IOException ioe) {
            String msg = ioe.getMessage();
            if (msg != null && msg.indexOf("EOF") != -1)
                throw (new EOFException(msg));
            throw ioe;
        } catch (ClassNotFoundException notfound) {
            // Ignore; Unknown Object ???
            return;
        }

        NotificationWrapper wrapr = (NotificationWrapper) obj;
        if (wrapr.getType() == NotificationWrapper.WAIT) {
            return;
        }

        mgr.raiseEvent(wrapr);
    
public booleanreinit()
Reinitialized the connection, if the connection is dropped. This method is invoked from ClientNotificationManager every time the client invokes a method on MBeanServerConnection.

        if (connected)
            return true;

        timedout = false;
        try {
            connect();
        } catch (IOException ioe) {
            timedout = true;
            throw ioe;
        }
        nReconnected = 0;
        receiveThr = new Thread(this);
        receiveThr.start();
        return true;
    
public voidrun()
The notification receiver thread loop. This loop, if client has not called JMXConnector.close, will try to read a notification message from the connection and notify the ClientNotificationManager to dispatch the notification.

        while (!isExiting()) {
            try {
                connect();
                readNotification();
            } catch (IOException ioe) {
                if (isExiting())
                    break;
                ioe.printStackTrace();
                if (ioe instanceof SocketTimeoutException) {
                    timedout = true;
                    break;
                }
                if (isDisconnected(ioe)) {
                    connected = false;
                    nReconnected++;
                    if (nReconnected > 3) {
                        timedout = true;
                        break;
                    }
                    continue;
                } else if (isExiting()) {
                    break;
                }
            }
        }
    
private voidsendCloseMessage()

        connect("close", true);