An overview of the AMX API and code samples that demonstrate various uses of the AMX API can be found at http://glassfish.dev.java.net/nonav/javaee5/amx/samples/javadoc/index.html and http://glassfish.dev.java.net/nonav/javaee5/amx/samples/javadoc/amxsamples/Samples.html.
The sample implementation is based around the SampleMain class. The principal uses of AMX demonstrated by SampleMain are the following:
All of these actions are performed by commands that you give to SampleMain. Although these commands are executed by SampleMain, they are defined as methods of the class Samples, which is also found in the com.sun.appserv.management.sample package.
The SampleMain class creates a connection to a DAS, and creates an interactive loop in which you can run the various commands defined in Samples that demonstrate different uses of AMX.
The connection to the DAS is shown in the following code.
[...] public static AppserverConnectionSource connect( final String host, final int port, final String user, final String password, final TLSParams tlsParams ) throws IOException { final String info = "host=" + host + ", port=" + port + ", user=" + user + ", password=" + password + ", tls=" + (tlsParams != null); SampleUtil.println( "Connecting...:" + info ); final AppserverConnectionSource conn = new AppserverConnectionSource( AppserverConnectionSource.PROTOCOL_RMI, host, port, user, password, tlsParams, null); conn.getJMXConnector( false ); SampleUtil.println( "Connected: " + info ); return( conn ); } [...]
A connection to the DAS is obtained using an instance of the com.sun.appserv.management.client.AppserverConnectionSource class. For the connection to be established, you must know the name of the host and port number on which the DAS is running, and have the correct user name, password and TLS parameters.
After the connection to the DAS is established, DomainRoot is obtained as follows:
DomainRoot domainRoot = appserverConnectionSource.getDomainRoot();
This DomainRoot instance is a client-side dynamic proxy to the MBean amx:j2eeType=X-DomainRoot,name=amx.
See the API documentation for com.sun.appserv.management.client.AppserverConnectionSource for further details about connecting to the DAS using the AppserverConnectionSource class.
However, if you prefer to work with standard JMX, instead of getting DomainRoot, you can get the MBeanServerConnection or JMXConnector, as shown:
MBeanServerConnection conn = appserverConnectionSource.getMBeanServerConnection( false ); JMXConnector jmxConn = appserverConnectionSource.getJMXConnector( false );
The Samples.startServer method demonstrates how to start an Enterprise Server.
In this sample AMX implementation, all the tasks are performed by the command start-server when you run SampleMain. See the startServer method to see how this command is implemented. Click the method name to see the source code.
The Samples.uploadArchive() and deploy methods demonstrate how to upload and deploy a Java EE archive file.
The Samples.displayHierarchy method demonstrates how to display the AMX MBean hierarchy.
The Samples.setMonitoring method demonstrates how to set monitoring states.
The Samples.handleList method demonstrates how to access many (but not all) configuration elements.
The Samples.displayAllAttributes method demonstrates how to access and display the attributes of an AMX MBean.
The Samples.displayAllProperties method demonstrates how to list AMX MBean properties.
The Samples.demoQuery method demonstrates how to perform queries.
The demoQuery() method uses other methods that are defined by Samples, namely displayWild(), and displayJ2EEType().
The Samples.demoJMXMonitor method demonstrates how to monitor attribute changes.
The Samples.undeploy method demonstrates how to undeploy a module.
The Samples.stopServer method demonstrates how to stop an Enterprise Server. The stopServer method simply calls the Samples.getJ2EEServer method on a given server instance, and then calls J2EEServer.stop.