WebLogic contains hundreds of MBeans. The following sections cover examples of some of the Runtime, Configuration, and Security MBeans and examines common ways of manipulating these MBeans. Other MBeans provided by WebLogic may be manipulated similarly.
20.4.1 Runtime MBeans
A prime example of a Runtime MBean is the ServerRuntimeMBean, which provides information on the operational status of a server and other details such as its listen address and port. The following code uses WebLogic's type-safe approach to connect to a Managed Server, print out its listen address and port, and then shut it down:
serverRuntime = (ServerRuntimeMBean) localHomeB.getRuntimeMBean("ServerB", "ServerRuntime"); System.out.println("Listens on " + serverRuntime.getListenAddress( )+":"+serverRuntime.getListenPort( )); serverRuntime.shutdown( );
You can do the same thing using the weblogic.Admin tool:
java weblogic.Admin -url http://serverb.x:7001 -username system -password pssst INVOKE -mbean "myClusterDomain:Location=ServerB,Name=ServerB,Type=ServerRuntime" -method shutdown
The ServerRuntimeMBean will exist on the Local Home of each server instance. To find all of the server runtimes, we will have to contact the Administration Server:
Set mbeanSet = adminHome.getMBeansByType("ServerRuntime"); Iterator mbeanIterator = mbeanSet.iterator( ); while (mbeanIterator.hasNext( )) { ServerRuntimeMBean serverRuntime = (ServerRuntimeMBean)mbeanIterator.next( ); System.err.println("Found server: " + serverRuntime.getName( )); }
Runtime MBeans often hold a lot of useful information when it comes to monitoring a resource and its usage. Here, for example, we list the maximum capacity and current connection count for a JDBC pool targeted to a Managed Server, using the standard JMX interface to the JDBCConnectionPoolRuntimeMBean:
MBeanServer mbs = localHomeB.getMBeanServer( ); WebLogicObjectName parent = new WebLogicObjectName("ServerB", "ServerRuntime", "myClusterDomain"); WebLogicObjectName oname = new WebLogicObjectName("NoTxPool", "JDBCConnectionPoolRuntime", "myClusterDomain", "ServerB", parent); Set s = mbs.queryMBeans(oname,null); for (Iterator i = s.iterator( ); i.hasNext( );) { Object o = i.next( ); ObjectInstance m = (ObjectInstance) o; System.err.println(mbs.getAttribute(m.getObjectName( ), "MaxCapacity")); System.err.println(mbs.getAttribute(m.getObjectName( ), "ActiveConnectionsCurrentCount")); }
In this case, we found the name of the desired MBean by issuing the following command:
java weblogic.Admin -url http://10.0.10.10:8001 -username system -password psst GET -pretty -type JDBCConnectionPoolRuntime
Similar statistics are available for all kinds of managed resources, including EJBs, JMS servers, web applications, and more.
20.4.2 Administration MBeans
The Administration MBeans hosted by the Administration Server completely define the configuration of the domain. You can manipulate the Administration MBeans to alter the configuration of the domain. The following example locates a JDBC connection pool and changes its maximum capacity:
WebLogicObjectName n = new WebLogicObjectName("MyJDBC Connection Pool", "JDBCConnectionPool", "myClusterDomain"); JDBCConnectionPoolMBean cp = (JDBCConnectionPoolMBean) adminHome.getMBean(n); cp.setMaxCapacity(20);
If you take a closer look at the JavaDoc documentation, you will find that the setMaxCapacity( ) method on the JDBCConnectionPoolMBean is dynamic. This means that the effect of changing the maximum capacity will be immediate, something you can verify for yourself using the Administration Console. In addition, the change is persisted so that the next time the server is started, the new configuration will be in place.
You also can create new Administration MBeans dynamically by using the home methods createAdminMBean( ) or findOrCreateAdminMBean( ). The latter variant returns an existing MBean if it finds one. There are two main variations of this method call:
findOrCreateAdminMBean(String name, String type, String domain)
This creates an Administration MBean in the given domain with the appropriate name and type.
findOrCreateAdminMBean(String name, String type, String domain, ConfigurationMBean parent)
This also creates an Administration MBean with the given name and type, but in addition it makes the newly created MBean a child of the parent.
In the following code sample, we use both variants to create a JMS Connection Factory, Server, and Queue:
String domain="myClusterDomain"; JMSConnectionFactoryMBean cf = (JMSConnectionFactoryMBean) ahome.findOrCreateAdminMBean("oreillyConnectionFactory", "JMSConnectionFactory", domain); cf.setJNDIName("oreilly.CF"); JMSServerMBean server = (JMSServerMBean) ahome.findOrCreateAdminMBean("oreillyJMSServer", "JMSServer", domain); JMSDestinationMBean queue = (JMSDestinationMBean) home.findOrCreateAdminMBean("oreillyQ", "JMSQueue", domain, server); queue.setJNDIName("oreilly.Q");
In this case, both the JMSConnectionFactory and the JMSServer MBeans are implicit children of the DomainMBean, while the JMSDestination MBean associated with the JMS queue is a child of the JMSServer MBean.
20.4.3 Security MBeans
In order to manipulate the Security MBeans, you need to be familiar with WebLogic's SSPI architecture as outlined in Chapter 17. For instance, suppose you need to provide a facility to programmatically add a new WebLogic user to the default security realm. Then, you need to know how to find an Authentication Provider that also implements the optional UserEditorMBean interface. Using WebLogic's type-safe interface, the following method shows how to programmatically add new users to WebLogic's default security realm:
public void createUsers(MBeanHome ahome, String un, String pw) { // First we locate the default security realm for the domain RealmMBean securityRealm = ahome.getActiveDomain( ) .getSecurityConfiguration( ) .findDefaultRealm( ); // We then find all the authentication providers AuthenticationProviderMBean[] providers = securityRealm.getAuthenticationProviders( ); for (int i = 0; i < providers.length; i++) { // We look for a provider that implements UserEditorMBean if (providers[i] instanceof UserEditorMBean) { UserEditorMBean editor = (UserEditorMBean) providers[i]; try { editor.createUser(un, pw, pw); System.out.println("Created User " + un); } catch (Exception e) { System.err.println("Exception " + e.toString( )); } } } }
It is important to remember that the Security MBeans live in their own Object Name domain called Security. For instance, the name of the default Authenticator MBean is Security:Name=myrealmDefaultAuthenticator. Using this information, we can quite easily use the Administration Tool to create a new WebLogic user from the command line:
java weblogic.Admin -username system -password pssst -url http://10.0.10.10:7001 INVOKE -mbean "Security:Name=myrealmDefaultAuthenticator" -method createUser username password password
Introduction
Web Applications
Managing the Web Server
Using JNDI and RMI
JDBC
Transactions
J2EE Connectors
JMS
JavaMail
Using EJBs
Using CMP and EJB QL
Packaging and Deployment
Managing Domains
Clustering
Performance, Monitoring, and Tuning
SSL
Security
XML
Web Services
JMX
Logging and Internationalization
SNMP