public class StatisticsMBean extends Object implements DynamicMBean, MBeanRegistration
ManagementFactory.getPlatformMBeanServer().createMBean(
HierarchyDynamicMBean.class.getName(),
new ObjectName("log4j:name=Config"));
ManagementFactory.getPlatformMBeanServer().createMBean(
StatisticsMBean.class.getName(),
new ObjectName("log4j:name=StatisticsEnabled"));
Note that the object names for both beans can be whatever you want. The
names that I show here are consistent with the hardcoded names produced
by both the Log4J appender/layout beans and the appender statistics beans
from this package, so if you use them you'll see everything Log4J in one
place.
Support for multiple Log4J hierarchies
For a stand-alone application, you would typically register both the Log4J
HierarchyDynamicMBean and this bean with the platform MBean
server. For a multi-container deployment such as an app-server, however,
you may have a container-specific MBean server. However, there's no simple
way to identify which of possibly many MBean servers may be the one we use
to register, or if indeed we should register.
The solution I've chosen is to maintain two static collections: one holds registrations for instances of this class, the other holds references to each of the configured appenders. When one or the other of these collections changes then the appropriate JMX registrations/deregistrations are performed.
If you never register an instance of this class, then JMX is effectively disabled.
| Constructor and Description |
|---|
StatisticsMBean() |
| Modifier and Type | Method and Description |
|---|---|
Object |
getAttribute(String attribute) |
AttributeList |
getAttributes(String[] attributes) |
MBeanInfo |
getMBeanInfo() |
Object |
invoke(String actionName,
Object[] params,
String[] signature) |
void |
postDeregister() |
void |
postRegister(Boolean registrationDone) |
void |
preDeregister() |
ObjectName |
preRegister(MBeanServer server,
ObjectName name) |
void |
setAttribute(Attribute attribute) |
AttributeList |
setAttributes(AttributeList attributes) |
public Object getAttribute(String attribute) throws AttributeNotFoundException, MBeanException, ReflectionException
getAttribute in interface DynamicMBeanAttributeNotFoundExceptionMBeanExceptionReflectionExceptionpublic void setAttribute(Attribute attribute) throws AttributeNotFoundException, InvalidAttributeValueException, MBeanException, ReflectionException
setAttribute in interface DynamicMBeanAttributeNotFoundExceptionInvalidAttributeValueExceptionMBeanExceptionReflectionExceptionpublic AttributeList getAttributes(String[] attributes)
getAttributes in interface DynamicMBeanpublic AttributeList setAttributes(AttributeList attributes)
setAttributes in interface DynamicMBeanpublic Object invoke(String actionName, Object[] params, String[] signature) throws MBeanException, ReflectionException
invoke in interface DynamicMBeanMBeanExceptionReflectionExceptionpublic MBeanInfo getMBeanInfo()
getMBeanInfo in interface DynamicMBeanpublic ObjectName preRegister(MBeanServer server, ObjectName name) throws Exception
preRegister in interface MBeanRegistrationExceptionpublic void postRegister(Boolean registrationDone)
postRegister in interface MBeanRegistrationpublic void preDeregister()
throws Exception
preDeregister in interface MBeanRegistrationExceptionpublic void postDeregister()
postDeregister in interface MBeanRegistrationCopyright © 2018. All rights reserved.