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;
}
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;
}
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");
}
}
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");
}
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);
}
}
Aggregations