• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

JMX sendNotification functionality is broken  RSS feed

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
NotificationBroadcasterSupport.sendNotification( notif ) works only when called from inside the MBean method invocation (JMX thread) and does nothing when called from an user thread.

I want to spawn a thread inside an MBean method call, perform a lengthy op and send a notif when it's done.

Any thoughts?
 
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know that much about JMX really, but this is more of a threading problem, isn't it? Why not spawn a new user thread and then do a join() inside the JMX bean method?

Another question is, why do you need a new user thread? Does each request to a JMX bean create a new thread, or does JMX do some synchronization to make it single threaded?
 
author
Posts: 23832
140
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Catalin Merfu:
NotificationBroadcasterSupport.sendNotification( notif ) works only when called from inside the MBean method invocation (JMX thread) and does nothing when called from an user thread.

I want to spawn a thread inside an MBean method call, perform a lengthy op and send a notif when it's done.



You might want to take a look at it again, because it should work. I do this all the time. In fact, I have mbeans who methods may never ever get called, but they report their status every few minutes. A thread is created upon instantiation, which sends the repeated notifications.

Henry
 
Catalin Merfu
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks,

I'm building an appender for log4j that send the log events to JConsole.
Do you know what's wrong here, I'm not receiving any notifications:


import javax.management.MBeanNotificationInfo;
import javax.management.Notification;
import org.apache.log4j.spi.LoggingEvent;
import javax.management.NotificationBroadcasterSupport;
import org.apache.log4j.Appender;
import org.apache.log4j.spi.ErrorHandler;
import org.apache.log4j.Layout;
import org.apache.log4j.spi.Filter;

public class JMXLogAppender extends NotificationBroadcasterSupport implements JMXLogAppenderMBean, Appender
{
static private final String LOG_EVENT_TYPE = "LOG_EVENT";
static private final String LOG_MBEAN_DESCR = "LogAppender MBean";

private String appenderName_;
private Layout layout_;

private long sequence_ = 0;

public JMXLogAppender()
{
}

public MBeanNotificationInfo[] getNotificationInfo()
{
String[] types = new String[] { LOG_EVENT_TYPE };
String name = Notification.class.getName();

MBeanNotificationInfo info = new MBeanNotificationInfo( types, name, LOG_MBEAN_DESCR );

return new MBeanNotificationInfo[] { info };
}

public int getValue()
{
return 0;
}

public void setName( String name )
{
appenderName_ = name;
}

public String getName()
{
return appenderName_;
}

public void addFilter(Filter filter)
{
return;
}

public Filter getFilter()
{
return null;
}

public void clearFilters()
{
}

public void setErrorHandler( ErrorHandler errorHandler )
{
}

public ErrorHandler getErrorHandler()
{
return null;
}

public void setLayout(Layout layout)
{
layout_ = layout;
}

public Layout getLayout()
{
return layout_;
}

public void doAppend( LoggingEvent event )
{
synchronized(this)
{
sequence_++ ;
}

String message = event.getRenderedMessage();

Notification notif = new Notification( LOG_EVENT_TYPE, this, sequence_ );// , event.timeStamp, message );
sendNotification( notif );
}

public boolean requiresLayout()
{
return true;
}

public void close()
{
}
}
 
Henry Wong
author
Posts: 23832
140
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Never used the JMX functionality of log4j, so take answer with a grain of salt.

There is no black magic here. First, I would make sure that the mbean is actually registered. Especially, if you are using mlet to load it, as it is notoriously bad at reporting errors. Second, I would make sure that the listeners were actually added.

Henry
 
Catalin Merfu
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Henry,

The listener should be automatically added when JConsole connects to the mbean server?

Would you be able to post a bean that doesn't export methods but is just used for posting notification?
 
Henry Wong
author
Posts: 23832
140
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Catalin Merfu:
Thanks Henry,

The listener should be automatically added when JConsole connects to the mbean server?

Would you be able to post a bean that doesn't export methods but is just used for posting notification?



Don't think I can actually give out customer source code, but here is a snippet from one of them. This mbean does publish an interface, as you need setters and getters for attributes. But this mbean also have a separate thread that periodically sends out attribute change notifications, for an attribute that changes frequently. The method that sends the notification is...



I still don't know why you are so focused on the API itself. If you are losing notifications, I would still look at the mbean server registrations, whether the listeners are added, and whether you are listening for the correct notification.

Henry
 
crispy bacon. crispy tiny ad:
ScroogeXHTML - small and flexible RTF to HTML converter library
https://coderanch.com/t/710903/ScroogeXHTML-RTF-HTML-XHTML-converter
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!