aspose file tools*
The moose likes Distributed Java and the fly likes JMX sendNotification functionality is broken Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Distributed Java
Bookmark "JMX sendNotification functionality is broken" Watch "JMX sendNotification functionality is broken" New topic
Author

JMX sendNotification functionality is broken

Catalin Merfu
Ranch Hand

Joined: May 26, 2004
Posts: 42
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?


Catalin Merfu<br /><a href="http://www.accendia.com" target="_blank" rel="nofollow">High Performance Java Networking</a>
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

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?


-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18135
    
  39

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Catalin Merfu
Ranch Hand

Joined: May 26, 2004
Posts: 42
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
Sheriff

Joined: Sep 28, 2004
Posts: 18135
    
  39

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

Joined: May 26, 2004
Posts: 42
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
Sheriff

Joined: Sep 28, 2004
Posts: 18135
    
  39

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JMX sendNotification functionality is broken
 
Similar Threads
MBean InitialContext() Tomcat query- what is the relation
MBean InitialContext() Tomcat query- what is the relation
Difference between sleep() and wait()