Search in sources :

Example 81 with Notification

use of javax.management.Notification in project jdk8u_jdk by JetBrains.

the class RMIConnector method close.

// allows to do close after setting the flag "terminated" to true.
// It is necessary to avoid a deadlock, see 6296324
private synchronized void close(boolean intern) throws IOException {
    final boolean tracing = logger.traceOn();
    final boolean debug = logger.debugOn();
    final String idstr = (tracing ? "[" + this.toString() + "]" : null);
    if (!intern) {
        //
        if (terminated) {
            if (closeException == null) {
                if (tracing)
                    logger.trace("close", idstr + " already closed.");
                return;
            }
        } else {
            terminated = true;
        }
    }
    if (closeException != null && tracing) {
        //
        if (tracing) {
            logger.trace("close", idstr + " had failed: " + closeException);
            logger.trace("close", idstr + " attempting to close again.");
        }
    }
    String savedConnectionId = null;
    if (connected) {
        savedConnectionId = connectionId;
    }
    closeException = null;
    if (tracing)
        logger.trace("close", idstr + " closing.");
    if (communicatorAdmin != null) {
        communicatorAdmin.terminate();
    }
    if (rmiNotifClient != null) {
        try {
            rmiNotifClient.terminate();
            if (tracing)
                logger.trace("close", idstr + " RMI Notification client terminated.");
        } catch (RuntimeException x) {
            closeException = x;
            if (tracing)
                logger.trace("close", idstr + " Failed to terminate RMI Notification client: " + x);
            if (debug)
                logger.debug("close", x);
        }
    }
    if (connection != null) {
        try {
            connection.close();
            if (tracing)
                logger.trace("close", idstr + " closed.");
        } catch (NoSuchObjectException nse) {
        // OK, the server maybe closed itself.
        } catch (IOException e) {
            closeException = e;
            if (tracing)
                logger.trace("close", idstr + " Failed to close RMIServer: " + e);
            if (debug)
                logger.debug("close", e);
        }
    }
    // Clean up MBeanServerConnection table
    //
    rmbscMap.clear();
    if (savedConnectionId != null) {
        Notification closedNotif = new JMXConnectionNotification(JMXConnectionNotification.CLOSED, this, savedConnectionId, clientNotifSeqNo++, "Client has been closed", null);
        sendNotification(closedNotif);
    }
    //
    if (closeException != null) {
        if (tracing)
            logger.trace("close", idstr + " failed to close: " + closeException);
        if (closeException instanceof IOException)
            throw (IOException) closeException;
        if (closeException instanceof RuntimeException)
            throw (RuntimeException) closeException;
        final IOException x = new IOException("Failed to close: " + closeException);
        throw EnvHelp.initCause(x, closeException);
    }
}
Also used : NoSuchObjectException(java.rmi.NoSuchObjectException) IOException(java.io.IOException) JMXConnectionNotification(javax.management.remote.JMXConnectionNotification) Notification(javax.management.Notification) MBeanServerNotification(javax.management.MBeanServerNotification) JMXConnectionNotification(javax.management.remote.JMXConnectionNotification)

Example 82 with Notification

use of javax.management.Notification in project jdk8u_jdk by JetBrains.

the class JMXConnectorServer method sendNotification.

private void sendNotification(String type, String connectionId, String message, Object userData) {
    Notification notif = new JMXConnectionNotification(type, getNotificationSource(), connectionId, nextSequenceNumber(), message, userData);
    sendNotification(notif);
}
Also used : Notification(javax.management.Notification)

Example 83 with Notification

use of javax.management.Notification in project jdk8u_jdk by JetBrains.

the class ScanManagerTest method doTestOperation.

/**
     * Test of addNotificationListener method, of class com.sun.jmx.examples.scandir.ScanManager.
     */
private void doTestOperation(ScanManagerMXBean proxy, Call op, EnumSet<ScanState> after, String testName) throws Exception {
    System.out.println("doTestOperation: " + testName);
    final LinkedBlockingQueue<Notification> queue = new LinkedBlockingQueue<Notification>();
    NotificationListener listener = new NotificationListener() {

        public void handleNotification(Notification notification, Object handback) {
            try {
                queue.put(notification);
            } catch (Exception x) {
                System.err.println("Failed to queue notif: " + x);
            }
        }
    };
    NotificationFilter filter = null;
    Object handback = null;
    final ScanState before;
    final NotificationEmitter emitter = (NotificationEmitter) proxy;
    emitter.addNotificationListener(listener, filter, handback);
    before = proxy.getState();
    op.call();
    try {
        final Notification notification = queue.poll(3000, TimeUnit.MILLISECONDS);
        assertEquals(AttributeChangeNotification.ATTRIBUTE_CHANGE, notification.getType());
        assertEquals(AttributeChangeNotification.class, notification.getClass());
        assertEquals(ScanManager.SCAN_MANAGER_NAME, notification.getSource());
        AttributeChangeNotification acn = (AttributeChangeNotification) notification;
        assertEquals("State", acn.getAttributeName());
        assertEquals(ScanState.class.getName(), acn.getAttributeType());
        assertEquals(before, ScanState.valueOf((String) acn.getOldValue()));
        assertContained(after, ScanState.valueOf((String) acn.getNewValue()));
        emitter.removeNotificationListener(listener, filter, handback);
    } finally {
        try {
            op.cancel();
        } catch (Exception x) {
            System.err.println("Failed to cleanup: " + x);
        }
    }
}
Also used : NotificationEmitter(javax.management.NotificationEmitter) AttributeChangeNotification(javax.management.AttributeChangeNotification) ScanState(com.sun.jmx.examples.scandir.ScanManagerMXBean.ScanState) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) NotificationFilter(javax.management.NotificationFilter) AttributeChangeNotification(javax.management.AttributeChangeNotification) Notification(javax.management.Notification) InstanceNotFoundException(javax.management.InstanceNotFoundException) IOException(java.io.IOException) JMException(javax.management.JMException) ListenerNotFoundException(javax.management.ListenerNotFoundException) NotificationListener(javax.management.NotificationListener)

Example 84 with Notification

use of javax.management.Notification in project jdk8u_jdk by JetBrains.

the class DirectoryScannerTest method doTestOperation.

private void doTestOperation(DirectoryScannerMXBean proxy, Call op, EnumSet<ScanState> after, String testName) throws Exception {
    System.out.println("doTestOperation: " + testName);
    final LinkedBlockingQueue<Notification> queue = new LinkedBlockingQueue<Notification>();
    NotificationListener listener = new NotificationListener() {

        public void handleNotification(Notification notification, Object handback) {
            try {
                queue.put(notification);
            } catch (Exception x) {
                System.err.println("Failed to queue notif: " + x);
            }
        }
    };
    NotificationFilter filter = null;
    Object handback = null;
    final ScanState before;
    final NotificationEmitter emitter = (NotificationEmitter) makeNotificationEmitter(proxy, DirectoryScannerMXBean.class);
    emitter.addNotificationListener(listener, filter, handback);
    before = proxy.getState();
    op.call();
    try {
        final Notification notification = queue.poll(3000, TimeUnit.MILLISECONDS);
        assertEquals(AttributeChangeNotification.ATTRIBUTE_CHANGE, notification.getType());
        assertEquals(AttributeChangeNotification.class, notification.getClass());
        assertEquals(getObjectName(proxy), notification.getSource());
        AttributeChangeNotification acn = (AttributeChangeNotification) notification;
        assertEquals("State", acn.getAttributeName());
        assertEquals(ScanState.class.getName(), acn.getAttributeType());
        assertEquals(before, ScanState.valueOf((String) acn.getOldValue()));
        assertContained(after, ScanState.valueOf((String) acn.getNewValue()));
        emitter.removeNotificationListener(listener, filter, handback);
    } finally {
        try {
            op.cancel();
        } catch (Exception x) {
            System.err.println("Failed to cleanup: " + x);
        }
    }
}
Also used : NotificationEmitter(javax.management.NotificationEmitter) AttributeChangeNotification(javax.management.AttributeChangeNotification) ScanState(com.sun.jmx.examples.scandir.ScanManagerMXBean.ScanState) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) NotificationFilter(javax.management.NotificationFilter) Notification(javax.management.Notification) AttributeChangeNotification(javax.management.AttributeChangeNotification) NotificationListener(javax.management.NotificationListener)

Example 85 with Notification

use of javax.management.Notification in project jdk8u_jdk by JetBrains.

the class DirectoryScannerTest method testScan.

/**
     * Test of scan method, of class com.sun.jmx.examples.scandir.DirectoryScanner.
     */
public void testScan() throws Exception {
    System.out.println("scan");
    final ScanManagerMXBean manager = ScanManager.register();
    try {
        final String tmpdir = System.getProperty("java.io.tmpdir");
        final ScanDirConfigMXBean config = manager.getConfigurationMBean();
        final DirectoryScannerConfig bean = config.addDirectoryScanner("test1", tmpdir, ".*", 0, 0);
        config.addDirectoryScanner("test2", tmpdir, ".*", 0, 0);
        config.addDirectoryScanner("test3", tmpdir, ".*", 0, 0);
        manager.applyConfiguration(true);
        final DirectoryScannerMXBean proxy = manager.getDirectoryScanners().get("test1");
        final Call op = new Call() {

            public void call() throws Exception {
                final BlockingQueue<Notification> queue = new LinkedBlockingQueue<Notification>();
                final NotificationListener listener = new NotificationListener() {

                    public void handleNotification(Notification notification, Object handback) {
                        try {
                            queue.put(notification);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                };
                manager.start();
                while (true) {
                    final Notification n = queue.poll(10, TimeUnit.SECONDS);
                    if (n == null)
                        break;
                    final AttributeChangeNotification at = (AttributeChangeNotification) n;
                    if (RUNNING == ScanState.valueOf((String) at.getNewValue()))
                        break;
                    else {
                        System.err.println("New state: " + (String) at.getNewValue() + " isn't " + RUNNING);
                    }
                }
                assertContained(EnumSet.of(SCHEDULED, RUNNING, COMPLETED), proxy.getState());
            }

            public void cancel() throws Exception {
                manager.stop();
            }
        };
        doTestOperation(proxy, op, EnumSet.of(RUNNING, SCHEDULED, COMPLETED), "scan");
    } catch (Exception x) {
        x.printStackTrace();
        throw x;
    } finally {
        try {
            manager.stop();
        } catch (Exception x) {
            System.err.println("Failed to stop: " + x);
        }
        try {
            ManagementFactory.getPlatformMBeanServer().unregisterMBean(ScanManager.SCAN_MANAGER_NAME);
        } catch (Exception x) {
            System.err.println("Failed to cleanup: " + x);
        }
    }
}
Also used : Call(com.sun.jmx.examples.scandir.ScanManagerTest.Call) AttributeChangeNotification(javax.management.AttributeChangeNotification) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Notification(javax.management.Notification) AttributeChangeNotification(javax.management.AttributeChangeNotification) DirectoryScannerConfig(com.sun.jmx.examples.scandir.config.DirectoryScannerConfig) NotificationListener(javax.management.NotificationListener)

Aggregations

Notification (javax.management.Notification)134 ObjectName (javax.management.ObjectName)47 NotificationListener (javax.management.NotificationListener)30 AttributeChangeNotification (javax.management.AttributeChangeNotification)22 HashMap (java.util.HashMap)16 MBeanServer (javax.management.MBeanServer)15 JMXConnector (javax.management.remote.JMXConnector)14 JMXServiceURL (javax.management.remote.JMXServiceURL)14 NotificationFilter (javax.management.NotificationFilter)13 MBeanServerConnection (javax.management.MBeanServerConnection)12 JMXConnectorServer (javax.management.remote.JMXConnectorServer)11 Test (org.junit.Test)11 IOException (java.io.IOException)10 MBeanServerNotification (javax.management.MBeanServerNotification)9 ArrayList (java.util.ArrayList)8 Attribute (javax.management.Attribute)8 ListenerNotFoundException (javax.management.ListenerNotFoundException)7 CompositeData (javax.management.openmbean.CompositeData)7 JMXConnectionNotification (javax.management.remote.JMXConnectionNotification)7 MalformedURLException (java.net.MalformedURLException)6