use of javax.management.AttributeChangeNotification 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);
}
}
}
use of javax.management.AttributeChangeNotification in project jdk8u_jdk by JetBrains.
the class DirectoryScanner method setStateAndNotify.
// Switch this object state to the desired value an send
// a notification. Don't call this method from within a
// synchronized block!
//
private final void setStateAndNotify(ScanState desired) {
final ScanState old = state;
if (old == desired)
return;
state = desired;
final AttributeChangeNotification n = new AttributeChangeNotification(this, getNextSeqNumber(), System.currentTimeMillis(), "state change", "State", ScanState.class.getName(), String.valueOf(old), String.valueOf(desired));
broadcaster.sendNotification(n);
}
use of javax.management.AttributeChangeNotification in project jdk8u_jdk by JetBrains.
the class SimpleStandard method reset.
/**
* Operation: reset to their initial values the "State" and "NbChanges"
* attributes of the "SimpleStandard" standard MBean.
*/
public void reset() {
checkSubject("reset");
AttributeChangeNotification acn = new AttributeChangeNotification(this, 0, 0, "NbChanges reset", "NbChanges", "Integer", new Integer(nbChanges), new Integer(0));
state = "initial state";
nbChanges = 0;
nbResets++;
sendNotification(acn);
}
use of javax.management.AttributeChangeNotification in project aries by apache.
the class ServiceStateTest method createService.
private void createService(StateConfig stateConfig, final List<Notification> received, final List<AttributeChangeNotification> attributeChanges) throws Exception {
BundleContext context = mock(BundleContext.class);
Logger logger = mock(Logger.class);
ServiceState serviceState = new ServiceState(context, stateConfig, logger);
ServiceReference reference = mock(ServiceReference.class);
Bundle b1 = mock(Bundle.class);
when(b1.getBundleId()).thenReturn(new Long(9));
when(b1.getSymbolicName()).thenReturn("bundle");
when(b1.getLocation()).thenReturn("file:/location");
when(reference.getBundle()).thenReturn(b1);
when(reference.getProperty(Constants.SERVICE_ID)).thenReturn(new Long(44));
when(reference.getProperty(Constants.OBJECTCLASS)).thenReturn(new String[] { "org.apache.aries.jmx.Mock" });
when(context.getAllServiceReferences(null, null)).thenReturn(new ServiceReference[] { reference });
ServiceEvent registeredEvent = mock(ServiceEvent.class);
when(registeredEvent.getServiceReference()).thenReturn(reference);
when(registeredEvent.getType()).thenReturn(ServiceEvent.REGISTERED);
ServiceEvent modifiedEvent = mock(ServiceEvent.class);
when(modifiedEvent.getServiceReference()).thenReturn(reference);
when(modifiedEvent.getType()).thenReturn(ServiceEvent.MODIFIED);
MBeanServer server = mock(MBeanServer.class);
//setup for notification
ObjectName objectName = new ObjectName(OBJECTNAME);
serviceState.preRegister(server, objectName);
serviceState.postRegister(true);
//add NotificationListener to receive the events
serviceState.addNotificationListener(new NotificationListener() {
public void handleNotification(Notification notification, Object handback) {
if (notification instanceof AttributeChangeNotification) {
attributeChanges.add((AttributeChangeNotification) notification);
} else {
received.add(notification);
}
}
}, null, null);
// capture the ServiceListener registered with BundleContext to issue ServiceEvents
ArgumentCaptor<AllServiceListener> argument = ArgumentCaptor.forClass(AllServiceListener.class);
verify(context).addServiceListener(argument.capture());
//send events
AllServiceListener serviceListener = argument.getValue();
serviceListener.serviceChanged(registeredEvent);
serviceListener.serviceChanged(modifiedEvent);
//shutdown dispatcher via unregister callback
serviceState.postDeregister();
//check the ServiceListener is cleaned up
verify(context).removeServiceListener(serviceListener);
ExecutorService dispatcher = serviceState.getEventDispatcher();
assertTrue(dispatcher.isShutdown());
dispatcher.awaitTermination(2, TimeUnit.SECONDS);
assertTrue(dispatcher.isTerminated());
}
use of javax.management.AttributeChangeNotification in project aries by apache.
the class BundleStateTest method testNotificationsForBundleEvents.
@Test
public void testNotificationsForBundleEvents() throws Exception {
StateConfig stateConfig = new StateConfig();
//holders for Notifications captured
List<Notification> received = new LinkedList<Notification>();
List<AttributeChangeNotification> attributeChanges = new LinkedList<AttributeChangeNotification>();
createBundle(stateConfig, received, attributeChanges);
assertEquals(2, received.size());
Notification installed = received.get(0);
assertEquals(1, installed.getSequenceNumber());
CompositeData installedCompositeData = (CompositeData) installed.getUserData();
BundleEventData installedData = BundleEventData.from(installedCompositeData);
assertEquals("bundle", installedData.getBundleSymbolicName());
assertEquals(9, installedData.getBundleId());
assertEquals("file:/location", installedData.getLocation());
assertEquals(BundleEvent.INSTALLED, installedData.getEventType());
Notification resolved = received.get(1);
assertEquals(2, resolved.getSequenceNumber());
CompositeData resolvedCompositeData = (CompositeData) resolved.getUserData();
BundleEventData resolvedData = BundleEventData.from(resolvedCompositeData);
assertEquals("bundle", resolvedData.getBundleSymbolicName());
assertEquals(9, resolvedData.getBundleId());
assertEquals("file:/location", resolvedData.getLocation());
assertEquals(BundleEvent.RESOLVED, resolvedData.getEventType());
assertEquals(1, attributeChanges.size());
AttributeChangeNotification ac = attributeChanges.get(0);
assertEquals("BundleIds", ac.getAttributeName());
assertEquals(0, ((long[]) ac.getOldValue()).length);
assertEquals(1, ((long[]) ac.getNewValue()).length);
assertEquals(9L, ((long[]) ac.getNewValue())[0]);
}
Aggregations