|
|
|||||||||||||||||
Eamonn McManus's BlogCommunity: JDK ArchivesA query language for the JMX APIPosted by emcmanus on April 25, 2008 at 08:21 AM | Permalink | Comments (0)The JMX API is being updated by JSR 255. That JSR is currently planned to be part of Java SE 7, and some of the API changes it defines have started to appear in JDK 7. So far, the main one is a Query Language. Here's what that is and what it's for. The JMX API has always included the idea of queries.
The idea is that you can tell the
The recommended way to obtain Up until now, the way to code the query I described above was this:
QueryExp query =
Query.and(Query.eq(Query.attr("Enabled"), Query.value(true)),
Query.eq(Query.attr("Owner"), Query.value("Duke")));
While it's possible to decipher that and determine that it does
indeed mean what I described, it isn't very easy. The idea of the
query language is that you can get the same
QueryExp query = Query.fromString("Enabled = true and Owner = 'Duke'");
Much easier to understand! (Let me stress that this is just an alternative way of writing existing queries. It doesn't introduce any new types of query.) The query language is closely based on the
If you're familiar with SQL, all of these should be familiar, except the last two, which have no SQL equivalent. The full specification also includes a formal grammar, which I won't reproduce here. I'll just say that I got to liberate my repressed inner compiler geek when writing the parser. Other uses for the Query LanguageApart from making it easier to write code that does queries, a standard query language is very practical for tools like JConsole or VisualVM that might want to allow the user to select a subset of MBeans using a query. A simple text field can now be used to do this. The query language also provides one solution to a problem with
the existing Query API. The methods of the (You might be wondering why we bothered defining
Custom queriesNothing prevents you from writing your own class that
implements This is why Why SQL?You might be wondering why the query language is based on SQL, which is a database query language. Is that really appropriate to query management objects? Although it's far from obvious, the original JMX query API was
closely based on SQL too. In fact, the places where the query
language described here differs from SQL are essentially the
places where the JMX query API has changed since its original
version. One strong historical hint here is that the Reference
Implementation has always used SQL syntax in the
Quite apart from this, SQL is familiar to very many programmers, and is also the inspiration for the query languages used by the Java Persistence API (JPA) and the Java Message Service (JMS). Evolution of the standard queriesThe set of available standard queries has expanded slightly over time. The original set was defined in version 1.0 of the JMX API, way back in 2000. In version 1.2 of the API, we made In Java SE 6 we added In Java SE 7 (assuming plausibly that that includes JSR 255,
the new JMX API), in addition to the query language, we're
including the ability to use dotted attribute expression like
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
ObjectName poolPattern = new ObjectName("java.lang:type=MemoryPool,*");
QueryExp q = Query.fromString("Usage.init = Usage.committed");
// or: Query.eq(Query.attr("Usage.init"), Query.attr("Usage.committed"));
Set<ObjectName> names = mbs.queryNames(poolPattern, q);
If you're sending either (We could add a method, say A pattern matching problemOne area where I'm not sure the query language does the right
thing is with patterns. In the existing API, if you want to query
for all MBeans that have a
QueryExp objectNameQuery = new ObjectName("mydomain:*");
System.out.println(Query.toString(objectNameQuery));
// prints: LIKE 'mydomain:*'
// this is not a standard SQL query so we don't have to respect precedent
QueryExp objectNameStringQuery =
Query.match(Query.attr("Name.canonicalName"), Query.value("mydomain:*"));
System.out.println(Query.toString(objectNameStringQuery));
// prints: Name.canonicalName like 'mydomain:%'
Never mind the inconsistent case of This is just the beginningYou can expect other new features from 2.0 to show up in the JDK 7 snapshots in the coming months. Namespaces, Event Service, localization support, you name it! Public Review of Web Services Connector for JMX AgentsPosted by emcmanus on February 18, 2008 at 08:52 AM | Permalink | Comments (2)The Public Review of JSR 262, "Web Services Connector for JMX Agents", is underway, and there's a new snapshot of the Reference Implementation that corresponds to the Public Review specification. Jean-François's blog has the full details. [Tags: jmx jsr262 ws-management.]
JMX API 2.0 Early Draft ReviewPosted by emcmanus on December 28, 2007 at 02:14 AM | Permalink | Comments (5)The first draft of JSR 255 is out! This defines version 2.0 of the JMX API. We're planning to integrate it into the Java SE 7 platform, subject to the approval of the Expert Group for that platform. Here's a summary of the important changes. If you're interested, I'd encourage you to download the draft and look at the summary in the Overview Description, which has links into the relevant parts of the API. This draft contains all the major features that we are planning to add in this version of the API. If there's anything you'd like to see changed, this would be a very good time to let us know! Namespaces and CascadingThe concept of namespaces is new. All MBeans whose domain
begins with Cascading or Federation means that it is straightforward to import MBeans from a remote MBean Server as if they were local. This blog entry gives an overview of what Cascading is about. Event Service and NotificationsThe Event Service provides greater control over notification
handling than the default technique using
A new class Resource injection provides an alternative to implementing the
Annotations and Resource InjectionMBeans can now be defined using annotations. Also, the
@Resource annotation allows an MBean to get a reference
to its MBeanServer and ObjectName references, as an alternative
to implementing This blog entry provides details and rationale. Client Contexts and LocalizationMBeans now have access to a context that can contain information such as locale or transaction ids. (Note though that there is no explicit support for transactions in the API.) The descriptions in an QueriesA new Query Language provides an alternative way to
specify queries that is often simpler than constructing
Attributes appearing in a query can now use a dot (.) to
specify a value contained in an attribute of complex
type, similar to the existing support in the
MXBeansThe type mappings can now be customized for any MXBean using annotations or options. Previously the mapping rules were fixed. Options for StandardMBean and ProxiesA new DynamicWrapperMBeanA new interface NotificationManager interfaceThree methods of the Model MBeansIt is no longer required that the Send feedback!Feel free to comment here, or send mail to jmx-spec-comments@sun.com. [Tags: jmx jsr jsr255 jcp edr] Defining MBeans with annotationsPosted by emcmanus on August 31, 2007 at 08:11 AM | Permalink | Comments (9)The number one question I get about the JMX API at conferences and other public events is whether there will be support for defining MBeans using annotations. People see that they can make EJBs or Web Services just by adding annotations to a POJO, and they ask why they can't make MBeans the same way. In version 2.0 of the JMX API, being defined by JSR 255, this will be possible. The exact details are still subject to change as a result of discussions within the JSR 255 Expert Group, but here's a snapshot of where we are now. I think the final version will be fairly close to this. In addition to defining MBeans with annotations, there are some new proposed annotations that will also apply to MBeans defined in the existing ways. Prior artSeveral projects already exist that provide this functionality, but the most developed is probably Spring. So our starting point was Spring's MBean annotations (see also API documentation). Defining an MBeanIn the proposed design, an MBean can be defined through annotations to achieve the same effect as a Standard MBean. In this and other examples, I'll show what you write with today's API, and what you'll be able to write with the new annotations.
This defines an MBean with read-only attribute I'll call an MBean defined this way an @MBean. One way to look at this is that with the existing Standard MBeans,
public methods from the class are picked out as being management
methods by virtue of being in the Standard MBean interface that
the class implements. So in this example the
Pros and cons of @MBeansThe new style appears considerably more convenient than Standard MBeans. You only have to maintain one source file, rather than managing a class and an interface. There is a downside, however, which may show up in bigger projects. The advantage of the Standard MBean approach is that the MBean interface tells you exactly what the attributes and operations of the MBean are. There is no extraneous information in the MBean interface: every method defines an attribute or an operation. On the other hand, with @MBeans the management attributes and operations are potentially mixed in with many other methods, public or private. So it is not immediately obvious what the management interface of the MBean is. This disadvantage applies both when reading the source code and when looking at the Javadoc output. A second disadvantage is that it is no longer possible to construct a proxy. Proxies simplify client code by allowing it to access attributes and operations as compiler-checked method calls. They don't matter if you are only going to interact with your MBeans through a graphical interface like JConsole, but they are a big help if you are writing an application that will interact with your MBeans specifically. For smallish projects, these disadvantages are likely to be
minor. Furthermore, it should be possible to define an annotation
processor that extracts a Standard MBean interface from an
@MBean, so it can be used for documentation and proxying. In
the example above, the annotation processor could create the
Defining an MXBeanThe existing @MXBean
public class Cache {
...remainder as above...
}
DescriptionsAlthough the JMX API allows for textual descriptions to be associated with attributes, operations, and parameters, when you use a Standard MBean today these descriptions have meaningless default values. I've written before about how you can add meaningful descriptions, but it isn't easy. This is a really obvious use for annotations. The proposed new
We international types will of course be thinking about internationalization, and I'll have more to say about that below. Finding the MBeanServer and/or ObjectNameOften an MBean needs to know what MBean Server it is registered
in, or what its name is in that MBean Server. To do this it
currently needs to implement the In the new proposal, the
When the MBean is registered, the MBean Server will inject the appropriate values into these fields. This possibility is open to all types of MBeans, not just
@MBeans. You could continue to have a Standard MBean as today,
but stop implementing Simplified notification handlingToday, if an MBean emits notifications then it must implement
the In practice, everybody uses the New annotations allow you to define the list of notification types more simply, and to emit notifications without having to keep track of listeners.
The The new Resource injection of More detail than you want to know about
You can stop reading nowIf your eyes are already glazing over with all this code, you can safely stop here, and you'll have seen the main ideas. The remainder of this entry is about secondary items, and further details about the main ones. Descriptor contentsIn the JMX API included in the Java SE 6 platform, we introduced a way to define your own annotations to specify Descriptor contents. So you might define @Units like this: @Documented @Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Units {
@DescriptorKey("units")
String value();
}
The new API accepts such annotations on classes or methods that also have the @ManagedX annotations. For example: @MBean
public class Cache {
...
@Units("bytes")
@ManagedAttribute
public int getUsed() {...}
...
}
We've also added a new @MBean
public class Cache {
...
@DescriptorFields("units=bytes")
@ManagedAttribute
public int getUsed() {...}
...
}
This annotation can be used in Standard MBeans and MXBeans as well as @MBeans and @MXBeans. Operation impactThe @MBean
public class Cache {
...
@ManagedOperation(impact = Impact.ACTION)
public int dropOldest(int n) {...}
}
You can also apply MBean constructorsEach public constructor in an @MBean or @MXBean is converted into an
StandardMBean classThe class Details on Resource injectionThe @Resource // field injection
private volatile ObjectName name;
private MBeanServer mbs;
@Resource // method injection
private synchronized void setMBeanServer(MBeanServer mbs) {
this.mbs = mbs;
}
The MBean Server determines what to inject based on the
type. The type is either the declared type of the field or
parameter, or it is specified explicitly in the
@Resource
private volatile ObjectName name;
@Resource(type = ObjectName.class)
private volatile Object name;
I don't think the second form will be used very often, but it might
be used to inject the The ObjectName (etc) will be injected as many times as there are
appropriate
I've used Resource injection happens after the MBean's More on descriptionsIn addition to the description text, the @Description(value="some sort of cache",
key="cache.mbean.description",
bundleBaseName="MyResources")
To complete the story here, we need to have something that is
able to apply these Descriptor fields to localize the
More on @NotificationInfoAs I threatened, here is more information than you wanted to
know about If an MBean has a @NotificationInfo(types = {AttributeChangeNotification.ATTRIBUTE_CHANGE},
notificationClass = AttributeChangeNotification.class)
If the MBean can emit more than one class of MBean, then it can
use @NotificationInfos(
@NotificationInfo(types = {"my.first.notif", "my.second.notif"})
@NotificationInfo(types = {AttributeChangeNotification.ATTRIBUTE_CHANGE},
notificationClass = AttributeChangeNotification.class)
)
The This is why there is an optional element of type
@NotificationInfo(types={"my.notif.type"},
description=@Description(value="my notification", key="my.notif.descr"))
You cannot use @NotificationInfo(types={"my.notif.type"},
descriptorFields={"foo=bar"})
Ideas still in progressWe're studying the possibility of providing a way to cause a notification that is sent every time a given operation is completed. We're looking at ways in which an MBean could say what its What next?This is still work in progress, as you'll have gathered. I'm
very much interested in comments and suggestions, either here or
at [Tags: jmx jsr jsr255 annotations] When can JMX notifications be lost?Posted by emcmanus on August 23, 2007 at 12:32 AM | Permalink | Comments (6)The JMX Best Practices guide says notifications can sometimes be lost. Why is that? When might it happen? Read on. Here's the relevant text from the Best Practices guide:
This text might seem somewhat alarming. First of all, notice that it only applies to remote clients. A local client (within the same Java VM) will reliably get all notifications it asks for. Secondly, the text is describing something that will only happen in unusual circumstances. Notifications will only be lost when they arrive so fast that they cannot be delivered to the remote client quickly enough, or if there is a long network outage during which enough notifications arrive to overflow the notification buffer on the server. If you're sure that the rate of notifications is always low then you probably don't need to worry. Long network outages will probably trigger other problems in your client, so you'll need to deal with them more generally than just worrying about lost notifications. Careful clientsBut if you have many notifications, you probably want to follow the advice in the subsequent paragraphs of the Best Practices guide:
Stateless serversThe design of the existing standard connectors is such that notification loss can happen when there are many notifications coming from the MBeans in the MBean Server. This is true even for clients that are only listening for a small subset of those notifications. In the extreme case, a client that is listening for a very rare notification might not see it, because other MBeans are generating frequent notifications that nobody is listening to. Once again, the client can tell that this has happened (via JMXConnector.addConnectionNotificationListener). The existing connectors behave like this because they have been designed to have no non-transient state on the server. A consequence is that the server has no non-transient record of which clients are interested in which notifications. Therefore it has to store all notifications in its buffer, in case some client it doesn't remember is interested in them. The servers were designed to have no non-transient state for better scalability. In retrospect, this was probably a design mistake. In many client/server systems, you have one server, or just a few servers, and a large number of clients. So limiting state in the server is an excellent idea, because it allows the server to handle many more clients. But in management systems, the situation is usually the opposite: you typically have one client (a management program such as JConsole) that may connect to and manage many servers. There are no common use cases where a server might have a large number of JMX clients. In version 2.0 of the JMX API, being defined by JSR 255, we are adding an Event Service. Among other things, this will fix the problem where a client might lose notifications that it is interested in because there are many other notifications that it is not interested in. Notification loss is inevitableEven with the new Event Service, notification loss will still be possible, however. Can't we get rid of it? To answer this question, consider what happens when notifications are produced faster than they can be handled. This might be because of network delays, or because the client needs to do some work for each notification. Suppose this situation persists. What should the system do? There are basically three possibilities:
When we were designing the JMX Remote API, we assumed that most MBeans that send notifications were not expecting sending to be slow. In the local case, sending is just invoking a method, and that method is usually punctual. If we had wanted to apply solution 2, slowing down senders, that could have broken the assumptions of existing MBeans. Coding MBeans so that they can cope with a blocked send would also be considerably more difficult. So, even though this solution (flow control) is arguably better, we were reluctant to impose it. The future: JMX Event ServiceAs I mentioned, in version 2.0 of the JMX API we are designing a new Event Service. This will be part of the JDK 7 platform. Though it will not eliminate notification loss, it will significantly reduce the likelihood of such loss. And it will also allow you to plug in your own transport for notifications. In particular you could plug in the Java Message Service to use an existing message bus. Combining Cascading with the Attach APIPosted by emcmanus on August 01, 2007 at 03:09 AM | Permalink | Comments (7)The Attach API lets you discover and attach to the Java VMs running on your local machine. JMX Cascading lets you federate several JMX agents together. Can we combine the two? This is a question we've had fairly often, and I was prompted to write about it after Nilesh Bansal posted a question to the JMX forum in the Sun Developer Network to which one possible answer is to combine Cascading with the Attach API. The solution I propose here assumes that you are running on JDK 6. If you are stuck on JDK 5, I have a few suggestions below, but there is inevitably a loss of functionality. The basic ideaThe Attach API is what JConsole uses to list the local Java processes in its Connect dialog. It is a nonstandard but supported API, which means it should be present on any JDK-derived VM that is at least version 6. Once you select a Java process to connect to, JConsole again uses the Attach API to find the address of the JMX agent within that process, so it can connect. And, thanks to some deep magic, JConsole can use the Attach API to create a JMX agent within the process if it doesn't already have one. So if we grab the code out of JConsole that does all this, we can combine it with Cascading. The picture below is my standard Cascading picture. Here, we're going to create a Master Agent that you can use to see the MBeans in all of your local Java processes. The Subagents in the picture are these local processes, and all of their MBeans are imported into the Master Agent. So if you connect JConsole to the Master Agent, then you can also see what's going on in each of the Subagents. In other words, you can use one JConsole connection to see all of your Java processes on that machine.
If you set up the Master Agent to be remotely connectable, then
you can also see all of your processes from a JConsole running
on another machine. The easiest way to do this is with
Here's what we'd like JConsole to look like when it connects to the Master Agent:
In the picture, you can see cascaded MBeans from processes
16872, 18213, and 29375. Through the "Cascader" MBean, which
I'll define below, you can see what command each process id
corresponds to. 29375 is the command
The processes you see are your own processes. In other words they are the ones that you are able to manipulate if you are logged in to the machine. On Unix machines, this means they are processes that are running with the same user id as the Master Agent. On Windows machines, it means processes that you have the necessary privileges to open and interact with. The detailsI'll use the Cascading implementation from Open DMK, the Open Source version of the Java Dynamic Management Kit (Java DMK) product. (When I wrote my earlier blog entry about cascading, Open DMK hadn't yet been released.) So to try this out, you'll need to download the
Open DMK binaries. You'll need to compile and run with
You'll also need to compile and run with The program creates a "Cascader" MBean, which has one operation
and one attribute. The The (A bug in JConsole makes it hard to see the command-line when it is long. If you let the mouse hover over the value, you can see a longer string.) Since the full code is fairly involved, here's a sketch of how I discover the Java processes and set up cascading for them. It omits exception handling and the like.
List<VirtualMachineDescriptor> vmds = VirtualMachine.list();
for (VirtualMachineDescriptor vmd : vmds) {
String id = vmd.id();
// This is the id that we will prefix to MBean names. In practice
// it is a numeric process id, as we have seen.
VirtualMachine vm = VirtualMachine.attach(vmd);
Properties agentProps = vm.getAgentProperties();
String connectorAddressProperty =
"com.sun.management.jmxremote.localConnectorAddress";
if (!agentProps.containsKey(connectorAddressProperty)) {
// The above property will be present if the process was launched
// with -Dcom.sun.management.jmxremote or if this program or
// JConsole previously attached to it. Otherwise we need to start
// the management agent within the process:
String javaHome = vm.getSystemProperties().getProperty("java.home");
String agent = javaHome + "/lib/management-agent.jar";
vm.loadAgent(agent, "com.sun.management.jmxremote");
// This has the same effect as running with -Dcom.sun.management.jmxremote
agentProps = vm.getAgentProperties();
}
String connectorAddress = agentProps.getProperty(connectorAddressProperty);
JMXServiceURL url = new JMXServiceURL(connectorAddress);
CascadingService cascade = ...;
cascade.mount(url, connectOptions, objectNamePattern, id);
vm.detach();
}
The complete code is below. You'll probably want to customize it, for example so it only cascades from certain of your processes rather than all of them. Cascade loopsOne detail I omitted from the sketch is that we have to be
careful that the Master Agent doesn't try to import its own
MBeans. Say the Master Agent is process 12345. It will show up
in the list of Java processes from the Attach API, along with
all the others. If we handled it the same way as the others,
then the MBean The way I prevent this is by checking the
A more efficient technique would be to compare the VM id
against the RuntimeMXBean's Stuck on JDK 5?If you're currently on JDK 5, the Attach API is one of the many goodies that may encourage you to migrate to JDK 6. But what if you can't? In that case, I think your best bet is to look at the JConsole code from the JDK 5 sources, and try to do the same thing it does. The relevant code is in the method getManagedVirtualMachines() within the class sun.tools.jconsole.ConnectDialog. This solution is fragile, because nothing prevents an update of JDK 5 from changing how this works and breaking your code. But if you're prepared to live with that, this is a possible approach. Since JDK 5 doesn't have Attach On Demand, you will only be
able to cascade processes that were explicitly run with the
The code
package net.mcmanus.eamonn.cascadelocalvms;
import com.sun.jdmk.remote.cascading.CascadingService;
import com.sun.tools.attach.AgentInitializationException;
import com.sun.tools.attach.AgentLoadException;
import com.sun.tools.attach.VirtualMachine;
import com.sun.tools.attach.VirtualMachineDescriptor;
import java.io.File;
import java.io.IOException;
import java.lang.management.ManagementFactory;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.management.JMX;
import javax.management.MBeanServer;
import javax.management.MBeanServerConnection;
import javax.management.MBeanServerDelegate;
import javax.management.MBeanServerDelegateMBean;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
public class Cascade {
private static final String localConnectorAddressProperty =
"com.sun.management.jmxremote.localConnectorAddress";
private static final Logger logger = Logger.getLogger(Cascade.class.getName());
public static void main(String[] args) throws Exception {
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
CascadingService cascade = new CascadingService(mbs);
CascaderImpl cascader = new CascaderImpl(cascade);
cascader.refresh();
mbs.registerMBean(cascader, new ObjectName(":type=Cascader"));
System.out.println("Ready");
Thread.sleep(Long.MAX_VALUE);
// If this thread exits then the app will exit, so don't let it.
}
public static interface CascaderMXBean {
public void refresh();
public SortedMap<String, String> getCascadeResults();
// key in map is VM id (typically a pid)
// value is result of trying to cascade from that VM
}
public static class CascaderImpl implements CascaderMXBean {
private final CascadingService cascade;
private SortedMap<String, String> cascadeResults;
CascaderImpl(CascadingService cascade) {
this.cascade = cascade;
}
public void refresh() {
uncascadeJVMs(cascade);
cascadeResults = cascadeJVMs(cascade);
}
public SortedMap<String, String> getCascadeResults() {
return cascadeResults;
}
}
private static SortedMap<String, String> cascadeJVMs(CascadingService cascade) {
logger.fine("Cascading JVMs...");
SortedMap<String, String> results = new TreeMap<String, String>();
List<VirtualMachineDescriptor> vmds = VirtualMachine.list();
for (VirtualMachineDescriptor vmd : vmds) {
logger.fine("Attaching to " + vmd);
String id = vmd.id();
VirtualMachine vm;
try {
vm = VirtualMachine.attach(vmd);
} catch (Exception e) {
logger.log(Level.FINE, "...exception attaching", e);
results.put(id, "Exception attaching: " + e);
continue;
}
// From this point on we must detach, so we need a 'finally' block
try {
Properties agentProps = vm.getAgentProperties();
if (!agentProps.containsKey(localConnectorAddressProperty)) {
logger.fine("...loading management agent into JVM");
try {
loadManagementAgent(vm);
} catch (Exception e) {
logger.log(Level.FINE, "...exception loading agent", e);
results.put(id, "Exception loading agent: " + e);
continue;
}
agentProps = vm.getAgentProperties();
}
String addr = agentProps.getProperty(localConnectorAddressProperty);
if (addr == null) {
logger.fine("...still don't have connector address??");
results.put(id, "No connector address even after loading agent");
continue;
}
JMXServiceURL url = new JMXServiceURL(addr);
logger.finer("...connector address is " + url);
if (isThisJVM(url, cascade.getTargetMBeanServer())) {
logger.fine("...is local JVM");
results.put(id, "Local JVM");
continue;
}
Map<String, ?> connectOptions = null;
ObjectName pattern = new ObjectName("*:*");
String mountTo = id;
try {
String mountId =
cascade.mount(url, connectOptions, pattern, mountTo);
logger.log(Level.FINE, "...mounted with id " + mountId);
results.put(id, "Success: " + vmd.displayName());
} catch (Exception e) {
logger.log(Level.FINE, "...exception mounting JVM", e);
results.put(id, "Exception mounting JVM: " + e);
}
} catch (Exception e) {
logger.log(Level.FINE, "...unexpected exception", e);
results.put(id, "Unexpected exception: " + e);
} finally {
try {
vm.detach();
} catch (IOException e) {
logger.log(Level.INFO, "Could not detach vm " + id, e);
}
}
}
return results;
}
private static void uncascadeJVMs(CascadingService cascade) {
String[] ids = cascade.getMountPointIDs();
for (String id : ids) {
try {
boolean unmounted = cascade.unmount(id);
if (!unmounted)
logger.info("Was not mounted: " + id);
} catch (IOException e) {
logger.log(Level.FINE, "Exception unmounting " + id, e);
}
}
}
private static void loadManagementAgent(VirtualMachine vm)
throws AgentLoadException, AgentInitializationException, IOException {
String javaHome = vm.getSystemProperties().getProperty("java.home");
String agent = javaHome + File.separator +
"lib" + File.separator + "management-agent.jar";
File f = new File(agent);
if (!f.exists())
throw new IOException("Management agent not found: " + agent);
agent = f.getCanonicalPath();
logger.fine("...load management agent from " + agent);
vm.loadAgent(agent, "com.sun.management.jmxremote");
}
private static boolean isThisJVM(JMXServiceURL url, MBeanServer mbs) {
try {
JMXConnector jmxc = JMXConnectorFactory.connect(url);
MBeanServerConnection mbsc = jmxc.getMBeanServerConnection();
MBeanServerDelegateMBean myDelegate =
JMX.newMBeanProxy(mbs, MBeanServerDelegate.DELEGATE_NAME,
MBeanServerDelegateMBean.class);
MBeanServerDelegateMBean remoteDelegate =
JMX.newMBeanProxy(mbsc, MBeanServerDelegate.DELEGATE_NAME,
MBeanServerDelegateMBean.class);
boolean same = myDelegate.getMBeanServerId().equals(
remoteDelegate.getMBeanServerId());
jmxc.close();
return same;
} catch (Exception e) {
logger.log(Level.FINE, "Cannot determine if same JVM", e);
return true; // if it really IS the same JVM, then we might
// get an infinite cascading loop
}
}
}
// END OF CODE
"Top threads" plugin for JConsolePosted by emcmanus on June 21, 2007 at 08:14 AM | Permalink | Comments (1)Peter Doornbosch has created a much improved version of the JTop sample plugin for JConsole. JTop only shows thread name, cumulative CPU time, and thread state. The "top threads" plugin also shows, per thread, current CPU percentage, average CPU percentage, and the evolution of CPU percentage over time. Very nice! There does seem to be a bug whereby the TextArea that shows the stack trace of the selected thread is editable. Furthermore this TextArea is "always on top" so it can obscure part of the "new connection" dialogue. I found that I could shrink the TextArea down to nothing by grabbing the control at its top, so this problem isn't very severe. Thanks to Daniel Fuchs for the pointer. Disassembling serialized Java objectsPosted by emcmanus on June 12, 2007 at 06:51 AM | Permalink | Comments (5)Presenting Serialysis, a library that allows you to disassemble the serial form of Java objects. This can allow you to retrieve information about an object that is not available through its public API. It is also a useful tool when testing the serialization of your classes. When the public API is not enoughMy reason for writing this library is that I encountered a couple of problems where I found that I needed information from an object that was not available through its public API, but that was available through its serial form. One example is if you have a stub for a remote RMI object, and you want to know what address it will connect to, or what port, or using what socket factory. The standard RMI API doesn't give you any way to extract this information from the stub. But the information is there, and it must be included when the stub is serialized so that the stub is usable when it is later deserialized. So if we could somehow parse the serialized stub we could get the information we want. A second example comes from the JMX API. Queries to the MBean Server are represented by the interface QueryExp. QueryExp instances are constructed using the methods of the Query class. If you have an object implementing QueryExp, how can you know what query it executes? The JMX API doesn't include any method to find out. The information must be present in the serial form, so that when a client sends a query to a remote server it can be reconstituted on the server. If we could look at the serial form, we could find out what the query was. This second example is what prompted me to write this library. The existing standard JMX connectors are based on Java serialization, so they don't need to do anything special for QueryExps. But the new Web Services Connector being defined by JSR 262 uses XML for serialization. How can it analyze a QueryExp in order to convert it into XML? The answer is that the WS Connector uses a version of this library to look at the Java-serialized QueryExp. What these examples have in common is that they illustrate gaps in the relevant APIs. There ought to be methods that allow you to extract the information contained in an RMI stub. There ought to be methods that convert back from a QueryExp object to the original Query methods that constructed it. (Even a standardized parseable toString() would be enough.) But those methods aren't there today, and if we want code that works with those APIs as they are now, we need another approach. Grabbing the private fields of objectsIf you have the source code of the classes you're interested
in, it's tempting just to barrel in and grab the information you
need. In the RMI stub example, we can find out by experiment that
the stub's getRef()
method returns a
// This is NOT a good idea!!!
import sun.rmi.server.*;
import sun.rmi.transport.*;
import java.rmi.*;
import java.rmi.server.*;
public class StubDigger {
public static getPort(RemoteStub stub) throws Exception {
RemoteRef ref = stub.getRef();
UnicastRef uref = (UnicastRef) ref;
Field refField = UnicastRef.class.getDeclaredField("ref");
refField.setAccessible(true);
LiveRef lref = (LiveRef) refField.get(uref);
return lref.getPort();
}
}
You might be satisfied with this, but you shouldn't be. The
code in bold is full of horrors. First of all, you should
never depend on (The above code was written for JDK 5. It turns out that in
JDK 6, LiveRef acquires a public getPort() method, so you no
longer need Field.setAccessible. But you still need to depend
on Well, sometimes you can't do any better than this. But if the class you're interested in is serializable, often you can. The reason is that the serial form of a class is part of its public interface. If the API is any good at all then its public interfaces will evolve compatibly in every update. This is a very strong requirement on the JDK platform in particular. So if the information you need isn't available through a class's public methods, but is part of the documented serial form, then you can rely on it remaining in the serial form in the future. The serial form is included in the Javadoc output as part of the See Also for each serializable class. You can see the serial form of all public JDK classes in a single giant page. Enter SerialysisMy library to parse serialized objects is called Serialysis, the result of cramming the words "serial analysis" too close together. Here's a simple example of what it looks like in action. This code...
SEntity sint = SerialScan.examine(new Integer(5));
System.out.println(sint);
...produces this output...
SObject(java.lang.Integer){
value = Prim(int){5}
}
This tells us that the If you check out the source code of
/**
* The value of the <code>Integer</code>.
*
* @serial
*/
private final int value;
But private fields are an implementation detail. An update
could rename this field, or replace it with a new field
inherited from the parent class Here's a more complicated example. Suppose that, for some reason, we want to know how big the array in an ArrayList is. The API doesn't allow us to find out, though it does allow us to force the array to be at least a certain size. If we check the serial
form of ArrayList, we see that it does contain the information
we're looking for. There's a serialized field
If we execute this code... List<Integer> list = new ArrayList<Integer>(); list.add(5); SObject slist = (SObject) SerialScan.examine(list); System.out.println(slist); ...we get this output...
SObject(java.util.ArrayList){
size = SPrim(int){1}
-- data written by class's writeObject:
SBlockData(blockdata){4 bytes of binary data}
SObject(java.lang.Integer){
value = SPrim(int){5}
}
}
This is where we get into the gory details of serialization.
In addition to, or instead of, serializing an object's fields, its
class can declare a method
The Based on the ArrayList documentation, we can find the size of the array like this:
SObject slist = (SObject) SerialScan.examine(list);
List<SEntity> writeObjectData = slist.getAnnotations();
SBlockData data = (SBlockData) writeObjectData.get(0);
DataInputStream din = data.getDataInputStream();
int alen = din.readInt();
System.out.println("Array length: " + alen);
How Serialysis solves my example problemsWithout showing all the details of the code, here's the outline of the solution to the QueryExp problem I mentioned. Suppose I have a QueryExp constructed like this:
QueryExp query =
Query.or(Query.gt(Query.attr("Version"), Query.value(5)),
Query.eq(Query.attr("SupportsSpume"), Query.value(true)));
This means, "MBeans where the ((Version) > (5)) or ((SupportsSpume) = (true)) The result of SerialScan.examine looks like this:
SObject(javax.management.OrQueryExp){
exp1 = SObject(javax.management.BinaryRelQueryExp){
relOp = SPrim(int){0}
exp1 = SObject(javax.management.AttributeValueExp){
attr = SString(String){"version"}
}
exp2 = SObject(javax.management.NumericValueExp){
val = SObject(java.lang.Long){
value = SPrim(long){5}
}
}
}
exp2 = SObject(javax.management.BinaryRelQueryExp){
relOp = SPrim(int){4}
exp1 = SObject(javax.management.AttributeValueExp){
attr = SString(String){"supportsSpume"}
}
exp2 = SObject(javax.management.BooleanValueExp){
val = SPrim(boolean){true}
}
}
}
You can imagine code that descends into this structure producing an XML equivalent. Every conformant implementation of the JMX API is required to produce this same serial form, so the code that parses it is guaranteed to work everywhere. Now here's the code that solves the RMI stub port number problem:
public static int getPort(RemoteStub stub) throws IOException {
SObject sstub = (SObject) SerialScan.examine(stub);
List<SEntity> writeObjectData = sstub.getAnnotations();
SBlockData sdata = (SBlockData) writeObjectData.get(0);
DataInputStream din = sdata.getDataInputStream();
String type = din.readUTF();
if (type.equals("UnicastRef"))
return getPortUnicastRef(din);
else if (type.equals("UnicastRef2"))
return getPortUnicastRef2(din);
else
throw new IOException("Can't handle ref type " + type);
}
private static int getPortUnicastRef(DataInputStream din) throws IOException {
String host = din.readUTF();
return din.readInt();
}
private static int getPortUnicastRef2(DataInputStream din) throws IOException {
byte hasCSF = din.readByte();
String host = din.readUTF();
return din.readInt();
}
To understand this, you need to see the serial
form for ConclusionsYou really don't want to get into disassembling serial forms unless you have to. But if you do have to, then Serialysis should make your task a little less painful. It's also a good way to check that your own classes serialize the way you expect them to. DownloadYou can download the Serialysis library at http://weblogs.java.net/blog/emcmanus/serialysis.zip. [Tags: jmx, rmi, serialization.] Custom types for MXBeansPosted by emcmanus on May 30, 2007 at 07:00 AM | Permalink | Comments (0)MXBeans map between arbitrary Java types and a fixed set of types in javax.management.openmbean called the Open Types. This allows clients to interact with MXBeans, without needing to know the original Java types (which might require putting extra jars in their classpath and so on). Up until now the mapping rules were fixed. Certain types can not be mapped by these rules, for example self-referential types or types such as Object or Number. In the Java 7 platform, we're planning to allow customization of the rules, as part of JSR 255 which is defining version 2.0 of the JMX API. This is a summary of the proposed changes. What problem are we solving?There are three sorts of use cases here:
The proposal is to add one class, one interface, and two annotations to address these cases. The existing class javax.management.StandardMBean acquires two new constructors and the existing class javax.management.JMX acquires two new overloads of existing methods. MXBeanMapping and @MXBeanMappingClassThe most important change is the new class javax.management.openmbean.MXBeanMapping: public abstract class MXBeanMapping { Suppose we want to define a mapping for the class MyLinkedList, which looks like this: public class MyLinkedList { This is not a valid type for MXBeans, because it contains a self-referential property "next" defined by the getNext() method. (This example comes from Simone Bordet.) So we would like to specify a mapping for it explicitly. When an MXBean interface contains MyLinkedList, that will be mapped into a String[], which is a valid Open Type. To define this mapping, we first subclass MXBeanMapping: public class MyLinkedListMapping extends MXBeanMapping { @Override @Override The call to the superclass constructor specifies what the original Java type is (MyLinkedList.class) and what Open Type it is mapped to (ArrayType.getArrayType(SimpleType.STRING)). The fromOpenValue method says how we go from the Java type to the Open Type, and the toOpenValue method says how we go from the Open Type to the Java type. With this mapping defined, we can annotate MyLinkedList with the new annotation @javax.management.openmbean.MXBeanMappingClass: @MXBeanMappingClass(MyLinkedListMapping.class) Now we can use MyLinkedList in an MXBean interface and it will work. This satisfies use case 1 above. MXBeanMappingFactory and @MXBeanMappingFactoryClassIf we are unable to annotate individual classes, then we can define a mapping factory that is consulted every time a type needs to be mapped. This is also useful if we would like to apply the same set of rules across a whole set of classes (for example, any class that implements List<E> is mapped in the same way as List<E>). A mapping factory is a subclass of javax.management.openmbean.MXBeanMappingFactory: public abstract class MXBeanMappingFactory { For example, suppose we can't change MyLinkedList, so we can't add the @MXBeanMappingClass annotation to it. We can achieve the same effect by defining a mapping factory as follows: public class MyLinkedListMappingFactory extends
MXBeanMappingFactory { Now we can add the new annotation javax.management.openmbean.MXBeanMappingFactoryClass to any MXBean interface that references MyLinkedList: @MXBeanMappingFactoryClass(MyLinkedListMappingFactory.class) This satisfies use case 2 above. New StandardMBean constructors and JMX.newMXBeanProxy methodThe existing class StandardMBean is used to make instances of Standard MBeans and MXBeans when you need to control some aspects of their behaviour, such as the descriptions in the MBeanInfo. We can extend the set of constructors as follows: public class StandardMBean implements DynamicMBean { The options parameter allows us to specify a number of potentially interesting things:
The question of what the MBeanOptions type is is still open. It could be a Map<String, ?> where each option is represented by a string constant. This is the approach taken by the JMX Remote API, for example. Or, it could be a special-purpose class called MBeanOptions with methods to set each of the options. I plan to write more on this later; for now I'll assume it's the MBeanOptions option. If you need to create an MXBean that implements the SomethingMXBean interface above and uses the MyLinkedListMappingFactory, but you can't add an annotation to SomethingMXBean, then you can do so using either of the two existing ways to use StandardMBean, subclassing or delegation, but supplying an option: MBeanOptions options = MBeanOptions.DEFAULT.withMXBeanMappingFactory( If you have a custom mapping in your MBean server, then you need the same custom mapping in a client if the client is making a proxy. So we add another overloading of JMX.newMXBeanProxy: public class JMX { Using this, you can create a proxy like this: SomethingMXBean proxy = JMX.newMXBeanProxy( Here the options object can be the same as before. This satisfies use case 3 above. Interoperation when there are custom typesMXBeans map arbitrary Java types to Open Types. The addition of custom mappings doesn't change this - the result still has to be Open Types. So for generic clients like JConsole, nothing changes when custom types are added into the mix. A client that is aware of the MXBean interfaces in use (like SomethingMXBean) can construct a proxy. To do that, it must have the interface available. If the interface has a @MXBeanMappingFactoryClass annotation, or if it contains a type that has a @MXBeanMappingClass annotation, then the classes referenced by those annotations must be present in the client too. It usually isn't any more difficult to arrange for the mapping classes to be present than to arrange for the original MXBean interface to be present. If the mapping has been specified on the server using an explicit MXBeanMappingFactory, then the same or an equivalent factory must be used on the client. This is the case where there is the most risk of inconsistency. To help check that client and server are using the same MXBeanMappingFactory, the Descriptor of an MXBean using an MXBeanMappingFactory will contain a field naming the factory class, MyLinkedListMappingFactory in the examples above. Some detailsHere are some details I omitted above for clarity. The methods toOpenValue and fromOpenValue have inconsistent throws clauses. This reflects a similar inconsistency in the MXBean spec, itself due to historical reasons. The Java type and Open Type in MXBeanMapping must be supplied to the constructor by the subclass, and cannot be changed thereafter. This could be a limitation but I cannot currently see any cases where you would not be able to supply values to the super-constructor call. MXBeanMapping is a class and not an interface for a number of reasons, notably that it allows us to have final methods (getOpenType, getOpenClass, getJavaType) and methods with default implementations (checkReconstructible). MXBeanMappingFactory is a class not an interface so that we can add methods to it in later versions of the API if necessary. The method MXBeanMapping.checkReconstructible() is used to determine if it is possible to map back from a value of the OpenType to a value of the original Java type. The default implementation does nothing. A subclass can override this method to throw OpenDataException if this mapping is not possible. See the MXBean specification for a discussion of reconstructible types. MXBeanMappingClass can be defined like this:
The signature of MXBeanMappingFactory.mappingForType is this:
Open questionsThis is still a draft proposal, and some questions remain. As I mentioned above, the exact type of the MBeanOptions parameter to various methods is yet to be determined. Should the @MXBeanMappingFactory option be inherited? For example, if you define public interface SubSomethingMXBean extends SomethingMXBean {...} should you inherit the @MXBeanMappingFactory annotation from SomethingMXBean? I think the answer must be yes, but what if SubSomethingMXBean inherits from more than one MXBean interface, and they have different @MXBeanMappingFactory annotations? Should the @MXBeanMappingFactory option be applicable to packages? A reminder that you can annotate packages by creating a file called package-info.java in the package with contents like this: @javax.management.openmbean.MXBeanMappingFactory(MyLinkedListMappingFactory.class) If @MXBeanMappingFactory can apply to packages and is also inherited from superinterfaces then we may have some complicated rules for precedence between the two. ConclusionIn conclusion, we're specifying something quite simple: how to extend the standard MXBean mappings with custom mappings. But the details turn out not to be so simple! AcknowledgementsThis specification has been discussed in the JSR 255 Expert Group. Mandy Chung also had some very helpful comments. Making a JMX connection with a timeoutPosted by emcmanus on May 23, 2007 at 01:23 PM | Permalink | Comments (9)One question I encounter frequently about the JMX Remote API is how to reduce the time taken to notice that a remote machine is dead when making a connection to it. The default timeout is typically a couple of minutes! Here's one way to do it. Probably the cleanest technique for connection timeouts in general is to set a connection timeout on the socket. The idea is that instead of using... Socket s = new Socket(host, port); ...you use... SocketAddress addr = new InetSocketAddress(host, port); Socket s = new Socket(); s.connect(addr, timeoutInMilliSeconds); The problem is that this is at a rather low level. If you're making connections with the JMX Remote API you usually don't see Socket objects at all. It's still possible to use this technique, but it requires a certain amount of fiddling, and the particular fiddling you need depends on which connector protocol you are using. A lot of the time, a much simpler and more general technique is applicable. You simply create the connection in another thread, and you wait for that thread to complete. If it doesn't complete before your timeout, you just abandon it. It might still take two minutes to notice that the remote machine is dead, but in the meantime you can continue doing other things. If you're making a lot of connections to a lot of machines, you might want to think twice about abandoning threads, because you might end up with a lot of them. But in the more typical case where you're just making one connection, this technique may well be for you. Assuming you're using at least Java SE 5, you'll certainly want
to use java.util.concurrent
to manage the thread creation and communication. There are a few
ways of doing it, but the easiest is probably a
The method below allows you to connect to a given JMXServiceURL with a timeout of five seconds like this: JMXConnector jmxc = connectWithTimeout(jmxServiceURL, 5, TimeUnit.SECONDS); My first cut at the problemIn my first version of this entry, I proposed a solution with the following outline.
JMXConnector connectWithTimeout(JMXServiceURL url, long timeout, TimeUnit unit) {
ExecutorService executor = Executors.newSingleThreadExecutor();
Future<JMXConnector> future = executor.submit(new Callable<JMXConnector>() {
public JMXConnector call() {
return JMXConnectorFactory.connect(url);
}
});
return future.get(timeout, unit);
}
Half an hour after posting, I suddenly realised that this version is incorrect. It reminds of the saying that for every complex problem there is a solution that is simple, obvious, and wrong. This solution does the r | |||||||||||||||||