//enterprise java /
@Path("configuration")
@Produces(TEXT_PLAIN)
@LocalBean
@Singleton
public class Configurator implements ConfiguratorMXBean {
LISTING 19 LISTING 20
COMMUNITY
Figure 1
private ObjectName objectName;
private MBeanServer platformMBeanServer;
the MXBean ending and register the
Singleton instance at the JMX runtime:
public interface
ConfiguratorMXBean {
String getConfiguration();
}
bute or the name of the key in the configuration, which could break the system
at runtime.
@PostConstruct
public void fetchConfiguration() {
//...
mergeWithCustomConfiguration();
registerInJMX();
}
JAVA IN ACTION
The Configurator first has to
be registered at the JMX runtime. The @PostConstruct method
Configurator#fetchConfiguration invokes
the Configurator#registerInJMX, which
performs the JMX registration (see
Listing 19).
Equally important is the unregistra-tion of the Configurator at shutdown time,
destroy time, or undeploy time. The
method Configurator#unregisterFromJMX
unregisters the Configurator from the
MBeanServer. Without this important
task, you can’t redeploy your application.
After the deployment, the Configurator
with its configuration becomes visible in
the Java Monitoring and Management
Console (JConsole), as shown in Figure 1.
void registerInJMX(){
try {
objectName = new ObjectName("Configurator:type=" + this.getClass().getName());
platformMBeanServer = ManagementFactory.getPlatformMBeanServer();
platformMBeanServer.registerMBean(this, objectName);
} catch (Exception e) {
throw new IllegalStateException("..." + e);
}
}
ABOUT US
Think About the Stakeholders
With Java EE 6, it is trivial to configure all
components of an application. It is even
possible to change the configuration on
every request/transaction and change it
via REST, JMX, SOAP, IIOP, or any other
available protocol.
Before you eagerly make everything
in your system configurable, first think
about the stakeholders and the real
requirements. Usually, no one wants
(or actually dares) to change configuration entries in the running system; if
you change the configuration entries,
@PreDestroy
public void unregisterFromJMX() {
try {
platformMBeanServer.unregisterMBean(this.objectName);
} catch (Exception e) {
throw new IllegalStateException("...:" + e);
}
//...other methods omitted
}
Download all listings in this issue as text
blog
Keeping Things Consistent
The coupling between the attributes and
the configuration store is loose. You can
accidentally change a name of the attri-you will have to retest and redeploy your
application anyway.
63
ORACLE.COM/JAVAMAGAZINE /////////////////////////////////////////////// MAY/JUNE 2012