[hibernate-issues] [Hibernate-JIRA] Commented: (HHH-3593) be able to configure statistics enablement

Steve Ebersole (JIRA) noreply at atlassian.com
Thu Sep 23 00:13:00 EDT 2010


    [ http://opensource.atlassian.com/projects/hibernate/browse/HHH-3593?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=38483#action_38483 ] 

Steve Ebersole commented on HHH-3593:
-------------------------------------

Now that we base on JDK 1.5 (as of the soon final 3.6) I can take a look at this again. 

How about the following?:

{code}
/**
 * Service providing simplified access to JMX related features needed by Hibernate.
 *
 * @author Steve Ebersole
 */
public interface JmxService {
	/**
	 * Registers the given {@code mBean} under the given {@code objectName}
	 *  
	 * @param objectName The name under which to register the MBean
	 * @param mBean The MBean to register
	 */
	public void registerMBean(ObjectName objectName, Object mBean);
}

/**
 * Standard implementation of JMX services
 *
 * @author Steve Ebersole
 */
public class JmxServiceImpl implements JmxService {
	private static final Logger log = LoggerFactory.getLogger( JmxServiceImpl.class );

	public static final String JMX_PLATFORM_SERVER = "hibernate.jmx.usePlatformServer";
	public static final String JMX_AGENT_ID = "hibernate.jmx.agentId";
	public static final String JMX_DOMAIN_NAME = "hibernate.jmx.defaultDomain";

	private final boolean usePlatformServer;
	private final String agentId;
	private final String defaultDomain;

	private boolean startedServer;
	private ArrayList<ObjectName> registeredMBeans;

	public JmxServiceImpl(Properties properties) {
		usePlatformServer = PropertiesHelper.getBoolean( JMX_PLATFORM_SERVER, properties );
		agentId = properties.getProperty( JMX_AGENT_ID );
		defaultDomain = properties.getProperty( JMX_DOMAIN_NAME );
	}

	/**
	 * {@inheritDoc}
	 */
	public void registerMBean(ObjectName objectName, Object mBean) {
		MBeanServer mBeanServer = findServer();
		if ( mBeanServer == null ) {
			if ( startedServer ) {
				throw new HibernateException( "Could not locate previously started MBeanServer" );
			}
			mBeanServer = startMBeanServer();
			startedServer = true;
		}

		try {
			mBeanServer.registerMBean( mBean, objectName );
			if ( registeredMBeans == null ) {
				registeredMBeans = new ArrayList<ObjectName>();
			}
			registeredMBeans.add( objectName );
		}
		catch ( Exception e ) {
			throw new HibernateException( "Unable to register MBean [ON=" + objectName + "]", e );
		}
	}

	public void shutDown() {
		if ( startedServer || registeredMBeans != null ) {
			MBeanServer mBeanServer = findServer();
			if ( mBeanServer == null ) {
				log.warn( "Unable to locate MBeanServer on JMX service shutdown" );
				return;
			}

			if ( registeredMBeans != null ) {
				for ( ObjectName objectName : registeredMBeans ) {
					try {
						log.trace( "Unregistering registered MBean [ON=" + objectName + "]" );
						mBeanServer.unregisterMBean( objectName );
					}
					catch ( Exception e ) {
						log.debug( "Unable to unregsiter registered MBean [ON=" + objectName + "] : " + e.toString() );
					}
				}
				registeredMBeans.clear();
				registeredMBeans = null;
			}

			if ( startedServer ) {
				log.trace( "Attempting to release created MBeanServer" );
				try {
					MBeanServerFactory.releaseMBeanServer( mBeanServer );
				}
				catch ( Exception e ) {
					log.warn( "Unable to release created MBeanServer : " + e.toString() );
				}
			}
		}
	}

	private MBeanServer findServer() {
		if ( usePlatformServer ) {
			return ManagementFactory.getPlatformMBeanServer();
		}

		ArrayList<MBeanServer> mbeanServers = MBeanServerFactory.findMBeanServer( agentId );  // null agentId is ok

		if ( defaultDomain == null ) {
			return mbeanServers.get( 0 );
		}

		for ( MBeanServer mbeanServer : mbeanServers ) {
			if ( defaultDomain.equals( mbeanServer.getDefaultDomain() ) ) {
				return mbeanServer;
			}
		}

		return null;
	}

	private MBeanServer startMBeanServer() {
		try {
			MBeanServer mbeanServer = MBeanServerFactory.createMBeanServer( defaultDomain );
			return mbeanServer;
		}
		catch ( Exception e ) {
			throw new HibernateException( "Unable to start MBeanServer", e );
		}
	}

}
{code}

The idea is that {{JmxService}} would then be used when initializing Statistics to add stats stuff to mbean server as you outlined


> be able to configure statistics enablement
> ------------------------------------------
>
>                 Key: HHH-3593
>                 URL: http://opensource.atlassian.com/projects/hibernate/browse/HHH-3593
>             Project: Hibernate Core
>          Issue Type: Improvement
>          Components: core
>            Reporter: John Mazzitelli
>            Priority: Minor
>         Attachments: HHH-3593.patch
>
>
> Right now, in order to enable Hibernate to collect and emit statistics to monitoring tools, your application has to manually execute code similar to the following:
>    StatisticsService mBean = new StatisticsService();
>    SessionFactory sessionFactory = ...get hibernate session factory...
>    mBean.setSessionFactory(sessionFactory);
>    ObjectName objectName = new ObjectName("Hibernate:application=MY_APP_NAME,type=statistics");
>    MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
>    mbs.registerMBean(mBean, objectName);
>    sessionFactory.getStatistics().setStatisticsEnabled(true);
> It would be nice if this was configurable, so a user of Hibernate doesn't need to write this code.
> For example:
> hibernate.statistics.enabled=true
> hibernate.statistics.objectname=Hibernate:application=MY_APP_NAME,type=statistics
> hibernate.statistics.mbeanserver=*platform*
> This would turn on Hibernate statistics and tell it to register the Statistics MBean in the JVM's platform MBean Server (ManagementFactory.getPlatformMBeanServer). Of course, *platform* would only be valid on Java5 or higher VMs. Note that hibernate will need to allow the object name to be configurable as well.
> Or...
> hibernate.statistics.enabled=true
> hibernate.statistics.objectname=Hibernate:application=MY_APP_NAME,type=statistics
> hibernate.statistics.mbeanserver=my_mbs_name
> This tells Hibernate to register the MBean in the named MBeanServer where the "my_mbs_name" is the default domain name of the MBeanServer you want (if it doesn't exist, Hibernate should create the MBeanServer with the named default domain).
> In fact, I had the Remoting project do something similar, so you can see code that gets the MBeanServer using these two ways by looking at the .patch attached to: https://jira.jboss.org/jira/browse/JBREM-746 and its related fix.

-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators: http://opensource.atlassian.com/projects/hibernate/secure/Administrators.jspa
-
For more information on JIRA, see: http://www.atlassian.com/software/jira

        


More information about the hibernate-issues mailing list