FileDocCategorySizeDatePackage
NotificationConnection.javaAPI DocGlassfish v2 API8344Fri May 04 22:36:30 BST 2007com.sun.enterprise.admin.jmx.remote.server.notification

NotificationConnection

public class NotificationConnection extends Object implements Runnable
A Connection class that represents a single client connection. This class maintains a notification buffer that is used to buffer notifications that needs to be sent to the client that is being represented by an object of this class. A dispatch thread is started, to dispatch the buffered notifications to the client.

Fields Summary
private com.sun.enterprise.admin.jmx.remote.notification.SimpleQueue
que
private int
bufsiz
private Thread
dispatchThr
private long
lastNotifTime
private OutputStream
out
private boolean
exiting
private boolean
dispatching
private boolean
isIOException
private static final Logger
logger
Constructors Summary
public NotificationConnection(OutputStream out, int bufsiz)

/*, 
        DefaultConfiguration.LOGGER_RESOURCE_BUNDLE_NAME );*/

         
        this.out = out;
        if (bufsiz <= DefaultConfiguration.NOTIF_MIN_BUFSIZ)
            this.bufsiz = DefaultConfiguration.NOTIF_MAX_BUFSIZ;
        else
            this.bufsiz = bufsiz;
        dispatchThr = new Thread(this);
        dispatchThr.start();
    
Methods Summary
public voidclose()
When the server-side connector webapp is shutdown by the servlet container, the ServerNotificationManager calls this method. All pending notifications are dropped.

        exiting = true;
        synchronized (que) {
            que.notify();
        }
        try {
            dispatchThr.join();
        } catch (InterruptedException intre) {
        }
        try {
            out.close();
        } catch (IOException ioe) {
            // XXX: Log it
        }
    
public voidfireNotification(com.sun.enterprise.admin.jmx.remote.notification.NotificationWrapper wrapr)
Called by the ServerNotificationManager whenever a notification needs to be sent to the client.

        if (que.size() < bufsiz) {
            synchronized (que) {
                que.add(wrapr);
                que.notify();
            }
        }
    
public voidfireWaitNotif()
Sends an empty notification to the client. An empty notification is sent every 10 seconds.

        if (!hasIOExceptionOccurred() && (que.size() < bufsiz) && !dispatching && isIdle()) {
            synchronized (que) {
                que.add(new NotificationWrapper(NotificationWrapper.WAIT, null, null));
                que.notify();
            }
        }
    
public booleanhasIOExceptionOccurred()
Returns true if an IOException had occurred while dispatching a notification to the client. If true, then the connection to the client may have dropped. If true, the ServerNotificationManager suspends this connection, waiting for the client to reconnect.

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

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

        return exiting;
    
private booleanisIdle()

        boolean ret = ((System.currentTimeMillis() - lastNotifTime) >= DefaultConfiguration.NOTIF_WAIT_INTERVAL);
        return ret;
    
public voidreinit(java.io.OutputStream out)
Reinitialized this connection and restarts the dispatch thread. This method is called everytime the client reconnects to the server because the connection had dropped.

        this.out = out;
        isIOException = false;
        dispatchThr = new Thread(this);
        dispatchThr.start();
    
public voidrun()
The notifications dispatch thread. The dispatch thread sends all pending notifications in the buffer to the client. The dispatch thread exits, whenever an IOException occurs during actual dispatch or whenever this connection is being closed (after a call to close())

        /* XXX: Even when we are exiting should we send the remaining notifications?
         *      OR just drop the remaining notifications ?
         *
         *     Currently we drop all the remaining notifications!!
         */
        while (!isExiting() && !hasIOExceptionOccurred()) {
            synchronized (que) {
                while (que.isEmpty() && !isExiting() && !hasIOExceptionOccurred()) {
                    try {
                        que.wait();
                    } catch (InterruptedException intre) {
                    }
                }
            }
            if (isExiting() || hasIOExceptionOccurred())
                break;
            dispatching = true;
            while (!que.isEmpty() && !isExiting() && !hasIOExceptionOccurred()) {
                NotificationWrapper wrapr = (NotificationWrapper) que.remove();
                try {
                    sendNotificationMsg(wrapr);
                } catch (IOException ioe) {
                    if (isExiting())
                        break;
                    // XXX: Log it; drop the notification
                    if (!isDisconnected(ioe))
                        break;
                    isIOException = true;
                    synchronized (this) {
                        this.notify();
                    }
                    break;
                }
            }
            lastNotifTime = System.currentTimeMillis();
            dispatching = false;
        }
    
private voidsendNotificationMsg(com.sun.enterprise.admin.jmx.remote.notification.NotificationWrapper wrapr)

        ObjectOutputStream objout = new ObjectOutputStream(out);
        objout.writeObject(wrapr);
        objout.flush();