The MBean server connection's getAttribute method accepts the object name of an MBean along with a string representing the name of one of its attributes, and returns the value of the designated attribute. Example 2–4 shows an example, obtaining and printing the value of a destination's MaxNumProducers attribute from its configuration MBean (described in Destination Configuration).
import javax.management.*; import javax.management.remote.*; import com.sun.messaging.AdminConnectionFactory; import com.sun.messaging.jms.management.server.*; public class GetAttrValue { public static void main (String[] args) { try { // Create administration connection factory AdminConnectionFactory acf = new AdminConnectionFactory(); // Get JMX connector, supplying user name and password JMXConnector jmxc = acf.createConnection("AliBaba", "sesame"); // Get MBean server connection MBeanServerConnection mbsc = jmxc.getMBeanServerConnection(); // Create object name ObjectName destConfigName = MQObjectName.createDestinationConfig(DestinationType.QUEUE, "MyQueue"); // Get and print attribute value Integer attrValue = (Integer)mbsc.getAttribute(destConfigName, DestinationAttributes.MAX_NUM_PRODUCERS); System.out.println( "Maximum number of producers: " + attrValue ); // Close JMX connector jmxc.close(); } catch (Exception e) { System.out.println( "Exception occurred: " + e.toString() ); e.printStackTrace(); } } }
There is also an MBeanServerConnection method named getAttributes, which accepts an MBean object name and an array of attribute name strings, and returns a result of class AttributeList. This is an array of Attribute objects, each of which provides methods (getName and getValue) for retrieving the name and value of one of the requested attributes. Example 2–5 shows a modified version of Example 2–4 that uses getAttributes to retrieve the values of a destination's MaxNumProducers and maxNumActiveConsumers attributes from its configuration MBean (see Destination Configuration).
import javax.management.*; import javax.management.remote.*; import com.sun.messaging.AdminConnectionFactory; import com.sun.messaging.jms.management.server.*; public class GetAttrValues { public static void main (String[] args) { try { // Create administration connection factory AdminConnectionFactory acf = new AdminConnectionFactory(); // Get JMX connector, supplying user name and password JMXConnector jmxc = acf.createConnection("AliBaba", "sesame"); // Get MBean server connection MBeanServerConnection mbsc = jmxc.getMBeanServerConnection(); // Create object name ObjectName destConfigName = MQObjectName.createDestinationConfig(DestinationType.QUEUE, "MyQueue"); // Create array of attribute names String attrNames[] = { DestinationAttributes.MAX_NUM_PRODUCERS, DestinationAttributes.MAX_NUM_ACTIVE_CONSUMERS }; // Get attributes AttributeList attrList = mbsc.getAttributes(destConfigName, attrNames); // Extract and print attribute values Object attrValue; attrValue = attrList.get(0).getValue(); System.out.println( "Maximum number of producers: " + attrValue.toString() ); attrValue = attrList.get(1).getValue(); System.out.println( "Maximum number of active consumers: " + attrValue.toString() ); // Close JMX connector jmxc.close(); } catch (Exception e) { System.out.println( "Exception occurred: " + e.toString() ); e.printStackTrace(); } } }
To set the value of an attribute, use the MBeanServerConnection method setAttribute. This takes an MBean object name and an Attribute object specifying the name and value of the attribute to be set. Example 2–6 uses this method to set a destination's MaxNumProducers attribute to 25.
import javax.management.*; import javax.management.remote.*; import com.sun.messaging.AdminConnectionFactory; import com.sun.messaging.jms.management.server.*; public class SetAttrValue { public static void main (String[] args) { try { // Create administration connection factory AdminConnectionFactory acf = new AdminConnectionFactory(); // Get JMX connector, supplying user name and password JMXConnector jmxc = acf.createConnection("AliBaba", "sesame"); // Get MBean server connection MBeanServerConnection mbsc = jmxc.getMBeanServerConnection(); // Create object name ObjectName destConfigName = MQObjectName.createDestinationConfig(DestinationType.QUEUE, "MyQueue"); // Create attribute object Attribute attr = new Attribute(DestinationAttributes.MAX_NUM_PRODUCERS, 25); // Set attribute value mbsc.setAttribute(destConfigName, attr); // Close JMX connector jmxc.close(); } catch (Exception e) { System.out.println( "Exception occurred: " + e.toString() ); e.printStackTrace(); } } }
Just as for getting attribute values, there is an MBeanServerConnection method named setAttributes for setting the values of multiple attributes at once. You supply an MBean object name and an attribute list giving the names and values of the attributes to be set. Example 2–7 illustrates the use of this method to set a destination's MaxNumProducers and MaxNumActiveConsumers attributes to 25 and 50, respectively.
import javax.management.*; import javax.management.remote.*; import com.sun.messaging.AdminConnectionFactory; import com.sun.messaging.jms.management.server.*; public class SetAttrValues { public static void main (String[] args) { try { // Create administration connection factory AdminConnectionFactory acf = new AdminConnectionFactory(); // Get JMX connector, supplying user name and password JMXConnector jmxc = acf.createConnection("AliBaba", "sesame"); // Get MBean server connection MBeanServerConnection mbsc = jmxc.getMBeanServerConnection(); // Create object name ObjectName destConfigName = MQObjectName.createDestinationConfig(DestinationType.QUEUE, "MyQueue"); // Create and populate attribute list AttributeList attrList = new AttributeList(); Attribute attr; attr = new Attribute(DestinationAttributes.MAX_NUM_PRODUCERS, 25); attrList.add(attr); attr = new Attribute(DestinationAttributes.MAX_NUM_ACTIVE_CONSUMERS, 50); attrList.add(attr); // Set attribute values mbsc.setAttributes(destConfigName, attrList); // Close JMX connector jmxc.close(); } catch (Exception e) { System.out.println( "Exception occurred: " + e.toString() ); e.printStackTrace(); } } }