javax.management.StandardEmitterMBean Java Examples

The following examples show how to use javax.management.StandardEmitterMBean. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JmxService.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
public StandardEmitterMBean registerMBean(List<String> notifications, String name) throws InstanceAlreadyExistsException, MBeanRegistrationException, NotCompliantMBeanException, MalformedObjectNameException, NullPointerException {
    String[] types = notifications.toArray(new String[0]);
    MBeanNotificationInfo info = new MBeanNotificationInfo(types, Notification.class.getName(), "Notification");
    NotificationEmitter emitter = new NotificationBroadcasterSupport(info);
    StandardEmitterMBean mbean = new StandardEmitterMBean(emitter, NotificationEmitter.class, emitter);
    server.registerMBean(mbean, new ObjectName(name));
    return mbean;
}
 
Example #2
Source File: JmxFeedTest.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
@Test
public void testJmxNotificationSubscriptionForSensor() throws Exception {
    final String one = "notification.one", two = "notification.two";
    final StandardEmitterMBean mbean = jmxService.registerMBean(ImmutableList.of(one, two), objectName);
    final AtomicInteger sequence = new AtomicInteger(0);

    feed = JmxFeed.builder()
            .entity(entity)
            .subscribeToNotification(new JmxNotificationSubscriptionConfig<Integer>(intAttribute)
                    .objectName(objectName)
                    .notificationFilter(JmxNotificationFilters.matchesType(one)))
            .build();        

    // Notification updates the sensor
    // Note that subscription is done async, so can't just send notification immediately during test.
    Asserts.succeedsEventually(ImmutableMap.of("timeout", TIMEOUT_MS), new Runnable() {
        @Override
        public void run() {
            sendNotification(mbean, one, sequence.getAndIncrement(), 123);
            assertEquals(entity.getAttribute(intAttribute), (Integer)123);
        }});
    
    // But other notification types are ignored
    sendNotification(mbean, two, sequence.getAndIncrement(), -1);
        
    Asserts.succeedsEventually(ImmutableMap.of("timeout", TIMEOUT_MS), new Runnable() {
        @Override
        public void run() {
            assertEquals(entity.getAttribute(intAttribute), (Integer)123);
        }});
}
 
Example #3
Source File: JmxFeedTest.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
@Test
public void testJmxNotificationSubscriptionForSensorParsingNotification() throws Exception {
    final String one = "notification.one", two = "notification.two";
    final StandardEmitterMBean mbean = jmxService.registerMBean(ImmutableList.of(one, two), objectName);
    final AtomicInteger sequence = new AtomicInteger(0);
    
    feed = JmxFeed.builder()
            .entity(entity)
            .subscribeToNotification(new JmxNotificationSubscriptionConfig<Integer>(intAttribute)
                    .objectName(objectName)
                    .notificationFilter(JmxNotificationFilters.matchesType(one))
                    .onNotification(new Function<Notification, Integer>() {
                        @Override
                        public Integer apply(Notification notif) {
                            return (Integer) notif.getUserData();
                        }
                    }))
            .build();
    
    
    // Notification updates the sensor
    // Note that subscription is done async, so can't just send notification immediately during test.
    Asserts.succeedsEventually(ImmutableMap.of("timeout", TIMEOUT_MS), new Runnable() {
        @Override
        public void run() {
            sendNotification(mbean, one, sequence.getAndIncrement(), 123);
            assertEquals(entity.getAttribute(intAttribute), (Integer)123);
        }});
}
 
Example #4
Source File: JmxFeedTest.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
@Test
public void testJmxNotificationMultipleSubscriptionUsingListener() throws Exception {
    final String one = "notification.one";
    final String two = "notification.two";
    final StandardEmitterMBean mbean = jmxService.registerMBean(ImmutableList.of(one, two), objectName);
    final AtomicInteger sequence = new AtomicInteger(0);

    feed = JmxFeed.builder()
            .entity(entity)
            .subscribeToNotification(new JmxNotificationSubscriptionConfig<Integer>(intAttribute)
                    .objectName(objectName)
                    .notificationFilter(JmxNotificationFilters.matchesTypes(one, two)))
            .build();
    
    // Notification updates the sensor
    // Note that subscription is done async, so can't just send notification immediately during test.
    Asserts.succeedsEventually(ImmutableMap.of("timeout", TIMEOUT_MS), new Runnable() {
        @Override
        public void run() {
            sendNotification(mbean, one, sequence.getAndIncrement(), 123);
            assertEquals(entity.getAttribute(intAttribute), (Integer)123);
        }});

    // And wildcard means other notifications also received
    sendNotification(mbean, two, sequence.getAndIncrement(), 456);
    assertSensorEventually(intAttribute, 456, TIMEOUT_MS);
}
 
Example #5
Source File: JmxFeedTest.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
@Test
public void testSubscribeToJmxNotificationAndEmitCorrespondingNotificationSensor() throws Exception {
    final TestApplication app2 = mgmt.getEntityManager().createEntity(EntitySpec.create(TestApplication.class));
    final EntityWithEmitter entity = app2.addChild(EntitySpec.create(EntityWithEmitter.class));
    
    try {
        app2.start(ImmutableList.of(new SimulatedLocation()));
        
        final List<SensorEvent<String>> received = Lists.newArrayList();
        app2.subscriptions().subscribe(null, EntityWithEmitter.MY_NOTIF, new SensorEventListener<String>() {
            @Override
            public void onEvent(SensorEvent<String> event) {
                received.add(event);
            }});

        final StandardEmitterMBean mbean = jmxService.registerMBean(ImmutableList.of("one"), objectName);
        final AtomicInteger sequence = new AtomicInteger(0);
        
        jmxHelper.connect(TIMEOUT_MS);
        jmxHelper.addNotificationListener(jmxObjectName, new NotificationListener() {
                @Override
                public void handleNotification(Notification notif, Object callback) {
                    if (notif.getType().equals("one")) {
                        entity.sensors().emit(EntityWithEmitter.MY_NOTIF, (String) notif.getUserData());
                    }
                }});
        

        Asserts.succeedsEventually(ImmutableMap.of("timeout", TIMEOUT_MS), new Runnable() {
            @Override
            public void run() {
                sendNotification(mbean, "one", sequence.getAndIncrement(), "abc");
                assertTrue(received.size() > 0, "received size should be bigger than 0");
                assertEquals(received.get(0).getValue(), "abc");
            }});
    } finally {
        Entities.destroyAll(app2.getManagementContext());
    }
}
 
Example #6
Source File: JmxFeedTest.java    From brooklyn-server with Apache License 2.0 4 votes vote down vote up
private Notification sendNotification(StandardEmitterMBean mbean, String type, long seq, Object userData) {
    Notification notif = new Notification(type, mbean, seq);
    notif.setUserData(userData);
    mbean.sendNotification(notif);
    return notif;
}
 
Example #7
Source File: JmxHelperTest.java    From brooklyn-server with Apache License 2.0 4 votes vote down vote up
private Notification sendNotification(StandardEmitterMBean mbean, String type, long seq, Object userData) {
    Notification notif = new Notification(type, mbean, seq);
    notif.setUserData(userData);
    mbean.sendNotification(notif);
    return notif;
}