Search in sources :

Example 86 with Notification

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

the class DirectoryScanner method notifyMatch.

// Notifies of a matching file.
private boolean notifyMatch(File file) {
    try {
        final Notification n = new Notification(FILE_MATCHES_NOTIFICATION, this, getNextSeqNumber(), file.getAbsolutePath());
        // This method *is not* called from any synchronized block, so
        // we can happily call broadcaster.sendNotification() here.
        // Note that verifying whether a method is called from within
        // a synchronized block demends a thoroughful code reading,
        // examining each of the 'parent' methods in turn.
        //
        broadcaster.sendNotification(n);
        return true;
    } catch (Exception x) {
        LOG.fine("Failed to notify: " + file.getAbsolutePath());
    }
    return false;
}
Also used : AttributeChangeNotification(javax.management.AttributeChangeNotification) Notification(javax.management.Notification) InstanceNotFoundException(javax.management.InstanceNotFoundException) IOException(java.io.IOException) ListenerNotFoundException(javax.management.ListenerNotFoundException)

Example 87 with Notification

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

the class ResultLogManager method checkLogFile.

/**
     * Check whether a new log file should be created.
     * If a new file needs to be created, creates it, renaming
     * previously existing file by appending '~' to its name.
     * Also reset the log count and file capacity.
     * Sends a notification indicating that the log file was changed.
     * Returns the new log stream;
     * Creation of a new file can be forced by passing force=true.
     **/
private OutputStream checkLogFile(String basename, long maxRecords, boolean force) throws IOException {
    final OutputStream newStream;
    synchronized (this) {
        if ((force == false) && (logCount < maxRecords))
            return logStream;
        final OutputStream oldStream = logStream;
        //
        if (oldStream != null) {
            oldStream.flush();
            oldStream.close();
        }
        final File newFile = (basename == null) ? null : createNewLogFile(basename);
        newStream = (newFile == null) ? null : new FileOutputStream(newFile, true);
        logStream = newStream;
        logFile = newFile;
        fileCapacity = maxRecords;
        logCount = 0;
    }
    sendNotification(new Notification(LOG_FILE_CHANGED, objectName, getNextSeqNumber(), basename));
    return newStream;
}
Also used : OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) File(java.io.File) Notification(javax.management.Notification)

Example 88 with Notification

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

the class Client method run.

public static void run(String url) throws Exception {
    final int notifEmittedCnt = 10;
    final CountDownLatch counter = new CountDownLatch(notifEmittedCnt);
    final Set<Long> seqSet = Collections.synchronizedSet(new HashSet<Long>());
    final AtomicBoolean duplNotification = new AtomicBoolean();
    JMXServiceURL serverUrl = new JMXServiceURL(url);
    ObjectName name = new ObjectName("test", "foo", "bar");
    JMXConnector jmxConnector = JMXConnectorFactory.connect(serverUrl);
    System.out.println("client connected");
    jmxConnector.addConnectionNotificationListener(new NotificationListener() {

        @Override
        public void handleNotification(Notification notification, Object handback) {
            System.out.println("connection notification: " + notification);
            if (!seqSet.add(notification.getSequenceNumber())) {
                duplNotification.set(true);
            }
            if (notification.getType().equals(JMXConnectionNotification.NOTIFS_LOST)) {
                long lostNotifs = ((Long) ((JMXConnectionNotification) notification).getUserData()).longValue();
                for (int i = 0; i < lostNotifs; i++) {
                    counter.countDown();
                }
            }
        }
    }, null, null);
    MBeanServerConnection jmxServer = jmxConnector.getMBeanServerConnection();
    jmxServer.addNotificationListener(name, new NotificationListener() {

        @Override
        public void handleNotification(Notification notification, Object handback) {
            System.out.println("client got: " + notification);
            if (!seqSet.add(notification.getSequenceNumber())) {
                duplNotification.set(true);
            }
            counter.countDown();
        }
    }, null, null);
    System.out.println("client invoking foo (" + notifEmittedCnt + " times)");
    for (int i = 0; i < notifEmittedCnt; i++) {
        System.out.print(".");
        jmxServer.invoke(name, "foo", new Object[] {}, new String[] {});
    }
    System.out.println();
    try {
        System.out.println("waiting for " + notifEmittedCnt + " notifications to arrive");
        if (!counter.await(30, TimeUnit.SECONDS)) {
            throw new InterruptedException();
        }
        if (duplNotification.get()) {
            System.out.println("ERROR: received duplicated notifications");
            throw new Error("received duplicated notifications");
        }
        System.out.println("\nshutting down client");
    } catch (InterruptedException e) {
        System.out.println("ERROR: notification processing thread interrupted");
        throw new Error("notification thread interrupted unexpectedly");
    }
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) CountDownLatch(java.util.concurrent.CountDownLatch) Notification(javax.management.Notification) JMXConnectionNotification(javax.management.remote.JMXConnectionNotification) ObjectName(javax.management.ObjectName) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) JMXConnector(javax.management.remote.JMXConnector) JMXConnectionNotification(javax.management.remote.JMXConnectionNotification) MBeanServerConnection(javax.management.MBeanServerConnection) NotificationListener(javax.management.NotificationListener)

Example 89 with Notification

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

the class RelationNotificationSeqNoTest method main.

public static void main(String[] args) throws Exception {
    MBeanServer mbs = MBeanServerFactory.newMBeanServer();
    ObjectName relSvcName = new ObjectName("a:type=relationService");
    RelationServiceMBean relSvc = JMX.newMBeanProxy(mbs, relSvcName, RelationServiceMBean.class);
    mbs.createMBean("javax.management.relation.RelationService", relSvcName, new Object[] { Boolean.TRUE }, new String[] { "boolean" });
    final BlockingQueue<Notification> q = new ArrayBlockingQueue<Notification>(100);
    NotificationListener qListener = new NotificationListener() {

        public void handleNotification(Notification notification, Object handback) {
            q.add(notification);
        }
    };
    mbs.addNotificationListener(relSvcName, qListener, null, null);
    RoleInfo leftInfo = new RoleInfo("left", "javax.management.timer.TimerMBean");
    RoleInfo rightInfo = new RoleInfo("right", "javax.management.timer.Timer");
    relSvc.createRelationType("typeName", new RoleInfo[] { leftInfo, rightInfo });
    ObjectName timer1 = new ObjectName("a:type=timer,number=1");
    ObjectName timer2 = new ObjectName("a:type=timer,number=2");
    mbs.createMBean("javax.management.timer.Timer", timer1);
    mbs.createMBean("javax.management.timer.Timer", timer2);
    Role leftRole = new Role("left", Arrays.asList(new ObjectName[] { timer1 }));
    Role rightRole = new Role("right", Arrays.asList(new ObjectName[] { timer2 }));
    RoleList roles = new RoleList(Arrays.asList(new Role[] { leftRole, rightRole }));
    final int NREPEAT = 10;
    for (int i = 0; i < NREPEAT; i++) {
        relSvc.createRelation("relationName", "typeName", roles);
        relSvc.removeRelation("relationName");
    }
    Notification firstNotif = q.remove();
    long seqNo = firstNotif.getSequenceNumber();
    for (int i = 0; i < NREPEAT * 2 - 1; i++) {
        Notification n = q.remove();
        long nSeqNo = n.getSequenceNumber();
        if (nSeqNo != seqNo + 1) {
            throw new Exception("TEST FAILED: expected seqNo " + (seqNo + 1) + "; got " + nSeqNo);
        }
        seqNo++;
    }
    System.out.println("TEST PASSED: got " + (NREPEAT * 2) + " notifications " + "with contiguous sequence numbers");
}
Also used : RoleList(javax.management.relation.RoleList) Notification(javax.management.Notification) ObjectName(javax.management.ObjectName) Role(javax.management.relation.Role) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) RoleInfo(javax.management.relation.RoleInfo) RelationServiceMBean(javax.management.relation.RelationServiceMBean) MBeanServer(javax.management.MBeanServer) NotificationListener(javax.management.NotificationListener)

Example 90 with Notification

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

the class RMIPasswdAuthTest method main.

public static void main(String[] args) {
    try {
        // Set the default password file
        //
        final String passwordFile = System.getProperty("test.src") + File.separator + "jmxremote.password";
        System.out.println("Password file = " + passwordFile);
        // Create an RMI registry
        //
        System.out.println("Start RMI registry...");
        Registry reg = null;
        int port = 5800;
        while (port++ < 6000) {
            try {
                reg = LocateRegistry.createRegistry(port);
                System.out.println("RMI registry running on port " + port);
                break;
            } catch (RemoteException e) {
                // Failed to create RMI registry...
                System.out.println("Failed to create RMI registry " + "on port " + port);
            }
        }
        if (reg == null) {
            System.exit(1);
        }
        // Instantiate the MBean server
        //
        System.out.println("Create the MBean server");
        MBeanServer mbs = MBeanServerFactory.createMBeanServer();
        // Register the ClassPathClassLoaderMBean
        //
        System.out.println("Create ClassPathClassLoader MBean");
        ObjectName cpcl = new ObjectName("ClassLoader:name=ClassPathClassLoader");
        mbs.createMBean("javax.management.loading.MLet", cpcl);
        // Register the SimpleStandardMBean
        //
        System.out.println("Create SimpleStandard MBean");
        mbs.createMBean("SimpleStandard", new ObjectName("MBeans:name=SimpleStandard"));
        // Create Properties containing the location of the password file
        //
        Properties props = new Properties();
        props.setProperty("jmx.remote.x.password.file", passwordFile);
        // Initialize environment map to be passed to the connector server
        //
        System.out.println("Initialize environment map");
        HashMap env = new HashMap();
        env.put("jmx.remote.authenticator", new JMXPluggableAuthenticator(props));
        // Create an RMI connector server
        //
        System.out.println("Create an RMI connector server");
        JMXServiceURL url = new JMXServiceURL("rmi", null, 0, "/jndi/rmi://:" + port + "/server" + port);
        JMXConnectorServer rcs = JMXConnectorServerFactory.newJMXConnectorServer(url, env, mbs);
        rcs.start();
        // Create an RMI connector client
        //
        System.out.println("Create an RMI connector client");
        HashMap cli_env = new HashMap();
        // These credentials must match those in the supplied password file
        //
        String[] credentials = new String[] { "monitorRole", "QED" };
        cli_env.put("jmx.remote.credentials", credentials);
        JMXConnector jmxc = JMXConnectorFactory.connect(url, cli_env);
        MBeanServerConnection mbsc = jmxc.getMBeanServerConnection();
        // Get domains from MBeanServer
        //
        System.out.println("Domains:");
        String[] domains = mbsc.getDomains();
        for (int i = 0; i < domains.length; i++) {
            System.out.println("\tDomain[" + i + "] = " + domains[i]);
        }
        // Get MBean count
        //
        System.out.println("MBean count = " + mbsc.getMBeanCount());
        // Get State attribute
        //
        String oldState = (String) mbsc.getAttribute(new ObjectName("MBeans:name=SimpleStandard"), "State");
        System.out.println("Old State = \"" + oldState + "\"");
        // Set State attribute
        //
        System.out.println("Set State to \"changed state\"");
        mbsc.setAttribute(new ObjectName("MBeans:name=SimpleStandard"), new Attribute("State", "changed state"));
        // Get State attribute
        //
        String newState = (String) mbsc.getAttribute(new ObjectName("MBeans:name=SimpleStandard"), "State");
        System.out.println("New State = \"" + newState + "\"");
        if (!newState.equals("changed state")) {
            System.out.println("Invalid State = \"" + newState + "\"");
            System.exit(1);
        }
        // Add notification listener on SimpleStandard MBean
        //
        System.out.println("Add notification listener...");
        mbsc.addNotificationListener(new ObjectName("MBeans:name=SimpleStandard"), new NotificationListener() {

            public void handleNotification(Notification notification, Object handback) {
                System.out.println("Received notification: " + notification);
            }
        }, null, null);
        // Unregister SimpleStandard MBean
        //
        System.out.println("Unregister SimpleStandard MBean...");
        mbsc.unregisterMBean(new ObjectName("MBeans:name=SimpleStandard"));
        // Close MBeanServer connection
        //
        jmxc.close();
        System.out.println("Bye! Bye!");
    } catch (Exception e) {
        System.out.println("Unexpected exception caught = " + e);
        e.printStackTrace();
        System.exit(1);
    }
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) JMXPluggableAuthenticator(com.sun.jmx.remote.security.JMXPluggableAuthenticator) HashMap(java.util.HashMap) Attribute(javax.management.Attribute) Registry(java.rmi.registry.Registry) LocateRegistry(java.rmi.registry.LocateRegistry) Properties(java.util.Properties) Notification(javax.management.Notification) RemoteException(java.rmi.RemoteException) ObjectName(javax.management.ObjectName) JMXConnectorServer(javax.management.remote.JMXConnectorServer) JMXConnector(javax.management.remote.JMXConnector) RemoteException(java.rmi.RemoteException) MBeanServerConnection(javax.management.MBeanServerConnection) MBeanServer(javax.management.MBeanServer) 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