2

I am using JMX MBeans in spring 4 framework. I handle notifications from JMX mBean and process them as events.

For testing alternate approach, I just commented out notification handling part. This implies that my JMX Mbean continues to publish notifications and I am just ignoring them.

P.S: I handle the JMX notifications via logstash. For testing I have commented out Logstash configuration

What happens to the Notification? Where it will be stored? Will it impact memory /pile up in application?

DecKno
  • 293
  • 1
  • 5
  • 21

2 Answers2

2

JMX notifications are an implementation of the observer pattern. With this pattern, events are not stored, so there is no memory issue.

NotificationBroadcaster JavaDoc:

When an MBean emits a notification, it considers each listener that has been added with addNotificationListener and not subsequently removed with removeNotificationListener. If a filter was provided with that listener, and if the filter's isNotificationEnabled method returns false, the listener is ignored. Otherwise, the listener's handleNotification method is called with the notification...

See also

Lapsed Listener Problem

The observer pattern link refers to this problem. To be clear, this potential memory link is different than what you worried about originally (storing notifications). This memory leak is caused by listeners not being deregistered, for example the logstash listener. If a listener is not deregistered, then it may not be garbage collected.

If you are concerned about this, you need to confirm that commenting out the logstash configuration prevents the listener from ever registering in the first place (it probably does). Regardless, this is probably not an issue for you, because it is just one listener object. Your concern about notifications was more serious because notifications are continuously created.

DavidS
  • 5,022
  • 2
  • 28
  • 55
  • 1
    Thank you. But with First link on Observer Pattern suggests LapsedListenerProblem which could cause memory leak. I am just confused. – DecKno Mar 17 '16 at 06:26
  • I updated my answer to address this concern, @ArivazhaganJeganathan. – DavidS Mar 18 '16 at 03:07
1

After some debugging of code flow of sendNotification Method in Java: My findings below:

Flow of sendNotification Method: 
org.springframework.jmx.export.notification.ModelMBeanNotificationPublisher
javax.management.modelmbean.RequiredModelMBean (Sendnotification)
javax.management.NotificationBroadcasterSupport

Method in NotificationBroadcasterSupport sends notifications to listeners which are registered only (based on listenersList within the class file). If no listeners are registered, then it skips sending notifications.

Hence from this I assume/conclude that Notifications are not stored anywhere.

As @DavidS suggested, if we fail to removeNotificationListener after registering and do not want to receive notifications, we would end up listeners processing notifications. Possible cause of memory leak.

RubioRic
  • 2,442
  • 4
  • 28
  • 35
DecKno
  • 293
  • 1
  • 5
  • 21