Search in sources :

Example 21 with AttributeChangeNotification

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

Example 22 with AttributeChangeNotification

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);
}
Also used : AttributeChangeNotification(javax.management.AttributeChangeNotification) ScanState(com.sun.jmx.examples.scandir.ScanManagerMXBean.ScanState)

Example 23 with AttributeChangeNotification

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);
}
Also used : AttributeChangeNotification(javax.management.AttributeChangeNotification)

Example 24 with AttributeChangeNotification

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());
}
Also used : AttributeChangeNotification(javax.management.AttributeChangeNotification) Bundle(org.osgi.framework.Bundle) Logger(org.apache.aries.jmx.Logger) AttributeChangeNotification(javax.management.AttributeChangeNotification) Notification(javax.management.Notification) ServiceReference(org.osgi.framework.ServiceReference) ObjectName(javax.management.ObjectName) AllServiceListener(org.osgi.framework.AllServiceListener) ServiceEvent(org.osgi.framework.ServiceEvent) ExecutorService(java.util.concurrent.ExecutorService) BundleContext(org.osgi.framework.BundleContext) MBeanServer(javax.management.MBeanServer) NotificationListener(javax.management.NotificationListener)

Example 25 with AttributeChangeNotification

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]);
}
Also used : AttributeChangeNotification(javax.management.AttributeChangeNotification) CompositeData(javax.management.openmbean.CompositeData) BundleEventData(org.apache.aries.jmx.codec.BundleEventData) LinkedList(java.util.LinkedList) AttributeChangeNotification(javax.management.AttributeChangeNotification) Notification(javax.management.Notification) Test(org.junit.Test)

Aggregations

AttributeChangeNotification (javax.management.AttributeChangeNotification)28 Notification (javax.management.Notification)14 NotificationListener (javax.management.NotificationListener)8 Test (org.junit.Test)6 ObjectName (javax.management.ObjectName)5 Bundle (org.osgi.framework.Bundle)4 ScanState (com.sun.jmx.examples.scandir.ScanManagerMXBean.ScanState)3 ArrayList (java.util.ArrayList)3 Date (java.util.Date)3 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)3 NotificationFilter (javax.management.NotificationFilter)3 LinkedList (java.util.LinkedList)2 ExecutorService (java.util.concurrent.ExecutorService)2 MBeanServer (javax.management.MBeanServer)2 NotificationEmitter (javax.management.NotificationEmitter)2 RuntimeOperationsException (javax.management.RuntimeOperationsException)2 CompositeData (javax.management.openmbean.CompositeData)2 AbstractIntegrationTest (org.apache.aries.jmx.AbstractIntegrationTest)2 Call (com.sun.jmx.examples.scandir.ScanManagerTest.Call)1 DirectoryScannerConfig (com.sun.jmx.examples.scandir.config.DirectoryScannerConfig)1