users@glassfish.java.net

Accesing remote Management Objects from glassfish AppServer through AMX

From: <glassfish_at_javadesktop.org>
Date: Tue, 12 May 2009 02:48:24 PDT

Hello!

I have been researching on making some configuration tools for configuring new resources programatically in a Glassfish and it was difficult to find good examples over the internet. For this reason, I think this code could be helpful. These examples were created to configure datasource resources in the AppServer. For creating them we need three objects, connection-pool, jdbc-resource and resource-ref.

[u][b]Getting all the objectNames from the AppServer[/b].[/u]

Management remoteHome;
Set<ObjectName> objectNames = remoteHome.queryNames(new ObjectName(""), null);

[u][b]Taking attributes from the managementObjects, once you have the objectName. [/b].[/u]
This example is for accesing the ConnectionPool attributes for other remoteObjects it will be the same structure:

                this.objectName = objectName;
                this.remoteHome = remoteHome;
                // Takes the attributes from the datasource-connection-pool
        try {
                String[] attributes = {"allow-non-components-callers", "associate-with-thread", "connection-creation-retry-attemps","connection-creation-retry-interval-in-seconds","connection-leak-reclaim","connection-leak-timeout-in-seconds", "connection-validation-method","datasource-classname","description","fail-all-connections","idle-timeout-in-seconds","is-connection-validation-required","is-isolation-level-guaranteed","lazy-connection-association","lazy-connection-enlistment","match-connections","max-connection-usage-count","max-pool-size","max-wait-time-in-millis","name","non-transactional-connections","pool-resize-quantity","res-type","statement-timeout-in-seconds","steady-pool-size","transaction-isolation-level","validate-atmost-once-period-in-seconds","validation-table-name","wrap-jdbc-objects"};
                
                        connectionPoolProperties = remoteHome.getAttributes(objectName, attributes);
                
                        datasourceClassname = (String) remoteHome.getAttribute(objectName, "datasource-classname");
                        connectionPoolName = (String) remoteHome.getAttribute(objectName, "name");


[u][b]Creating Datasource objects (other Management Objects should have similar "create" methods) [/b].[/u]

                DomainRoot domainRoot;

                AppserverConnectionSource appserver = new AppserverConnectionSource(
                                AppserverConnectionSource.PROTOCOL_RMI, hostName, JMX_PORT,
                                user, password, null);

                domainRoot = appserver.getDomainRoot();

                // check whether the con.pool already exists, else, create one
                Map<String, JDBCConnectionPoolConfig> conPoolMap = domainRoot
                                .getDomainConfig().getJDBCConnectionPoolConfigMap();
                if (conPoolMap.containsKey(connPoolName)) {
                        logger.fine("JDBC Connection pool : " + connPoolName
                                        + " already exists");
                        report.append("JDBC Connection pool : " + connPoolName
                                        + " already exists \n");
                        // domainRoot.getDomainConfig().createJDBCConnectionPoolConfig(connPoolName,
                        // datasourceClassname, connectionPropertiesMap);

                } else {
                        try {
                                domainRoot.getDomainConfig().createJDBCConnectionPoolConfig(
                                                connPoolName, datasourceClassname, connectionPropertiesMap);
                        } catch (Exception e) {
                                report.append("Incorrect format of database properties, the DataSource has not been created");
                                return report.toString();
                        }
                        logger.fine("JDBC Connection pool : " + connPoolName
                                        + " created ! ");
                        report.append("JDBC Connection pool : " + connPoolName
                                        + " created ! \n");
                        
                        
                }
                // check whether the jdbc resource already exists, else, create one
                Map<String, JDBCResourceConfig> resMap = domainRoot.getDomainConfig()
                                .getJDBCResourceConfigMap();
                if (resMap.containsKey(resourceName)) {
                        logger.fine("JDBC Resource : " + resourceName + " already exists");
                        report.append("JDBC Resource : " + resourceName
                                        + " already exists \n");
                } else {
                        domainRoot.getDomainConfig().createJDBCResourceConfig(resourceName,
                                        connPoolName, null);
                        logger.fine("JDBC Resource : " + resourceName + " created !");
                        report.append("JDBC Resource : " + resourceName + " created ! \n");
                }
                // create a resource-ref for the instance "server"
                Map<String, StandaloneServerConfig> map1 = domainRoot.getDomainConfig()
                                .getStandaloneServerConfigMap();
                {
                        StandaloneServerConfig sc = map1.get("server");
                        Map<String, ResourceRefConfig> rm = sc.getResourceRefConfigMap();

                        if (rm.containsKey(resourceName)) {
                                logger.fine(resourceName
                                                + " already referenced for the server instance");
                                report.append(resourceName
                                                + " already referenced for the server instance \n");
                        } else {
                                sc.createResourceRefConfig(resourceName, true);
                                logger.fine(resourceName
                                                + " reference created for the server instance");
                                report.append(resourceName
                                                + " reference created for the server instance \n");
                        }

                        return report.toString();
                }

[b][u]Accesing remote methods in a management object.[/b][/u]

These methods could be found through the Jconsole. You have to connect to the jmxservice (service:jmx:rmi:///jndi/rmi://localhost:8686/jmxrmi). In the lines below there is an example of using the remote method setProperties from the amx object: X-JDBCConnectionPoolConfig.

                DomainRoot domainRoot;

                AppserverConnectionSource appserver = new AppserverConnectionSource(
                                AppserverConnectionSource.PROTOCOL_RMI, hostName, JMX_PORT,
                                user, password, null);
 
                
                domainRoot = appserver.getDomainRoot();
                MBeanServerConnection mBeanConnection = appserver
                                .getExistingMBeanServerConnection();
                Set<ObjectInstance> objectNames = mBeanConnection.queryMBeans(
                                new ObjectName(""), null);
                // domainRoot.getDomainConfig().
                Iterator objectNamesIterator = objectNames.iterator();
                while (objectNamesIterator.hasNext()) {
                        ObjectInstance objectInstance = (ObjectInstance) objectNamesIterator
                                        .next();
                        // System.out.println("Object Names de MBeanConnection"+
                        // objectNamesIterator.next().toString());
                        ObjectName objectName = objectInstance.getObjectName();

                        if (objectName.getKeyProperty("j2eeType") != null) {
                                if (objectName.getKeyProperty("j2eeType").equals(
                                                "X-JDBCConnectionPoolConfig")) {
                                        if(objectName.getKeyProperty("name") != null){
                                        if(objectName.getKeyProperty("name").equals(resourceName)){
                                        //System.out.println(objectName.toString());

                                        String[] params = { key,value };

                                        String[] signature = { "java.lang.String",
                                                        "java.lang.String" };
                                        
                                                MBeanInfo info = mBeanConnection.getMBeanInfo(objectName);
                                                //This is the method for invoking remote methods y dynamic Mbeans.
                                                [b]mBeanConnection.invoke(objectName, "setPropertyValue",params, signature); [/b]

                                                techReportBuilder.append("New Property setted in " + resourceName + "Key= "+key +"value= "+value);

I hope these examples I have written above are helpful for anyone trying to develop programmatic tools to manage a Glassfish AppServer.
[Message sent by forum member 'pakito' (pakito)]

http://forums.java.net/jive/thread.jspa?messageID=345898