This is the second part that deals with WebSphere 6 and JMX. The first part can be found in yesterday’s blog post.
Here I will give a small code sample of creating a JMS connection factory using the WebSphere JMX API. This example can give you a general idea of how things should look like when trying to administrate WAS from Java. There are of course more resources that you can generate (basically, everything that can be generated from the admin console) but I have decided to concentrate on one example that is not available in the InfoCenter.
Here is the method:
private void createJMSConnectionFactory(String name,String jndiName,String busName) throws Exception {
ObjectName sibJmsAdapter = findSIBJMSResourceAdapter();
ObjectName pattern = new ObjectName("*:_Websphere_Config_Data_Type=Node");
ObjectName[] targets = configService.queryConfigObjects(null,pattern,null);
ObjectName node = targets[0];
// Find the connection definition
pattern = new ObjectName("*:_Websphere_Config_Data_Type=ConnectionDefinition");
ObjectName[] conDefs = configService.queryConfigObjects(node,pattern,null);
ObjectName curConDef = null;
if (conDefs != null) {
for (ObjectName conDef : conDefs) {
if (configService.getAttribute(conDef,"connectionFactoryInterface").equals("javax.jms.ConnectionFactory")) {
curConDef = conDef;
break;
}
}
} else {
System.out.println("No connection definitions");
}
AttributeList attrs = new AttributeList();
attrs.add(new Attribute("name", name));
attrs.add(new Attribute("jndiName", jndiName));
attrs.add(new Attribute("connectionDefinition", curConDef));
ObjectName conFactory = configService.createConfigData(sibJmsAdapter,"J2CConnectionFactory","J2CConnectionFactory",attrs); attrs.clear(); ObjectName propertySet = configService.createConfigData(conFactory, "propertySet", "",attrs);
attrs.clear();
attrs.add(new Attribute("name", "BusName"));
attrs.add(new Attribute("type", "java.lang.String"));
attrs.add(new Attribute("value", busName));
configService.addElement(propertySet, "resourceProperties", attrs, -1);
System.out.println("SIB JMS connection factory created");
}
Let me go over the major parts:
- First thing you should do is to locate the “SIB JMS Resource Adapter” ObjectName in the WAS configuration. This config object is available by default, when you install WAS. To do that (here it’s a private method), just get all the “J2CResourceAdapter” objects and find the one with the correct name.
- Next thing we get the node, assuming there is a single node configured. If you have multiple nodes, you should add some code that selects the correct one or work with a cell.
- Next, locate the “ConnectionDefintion” object. There are several connection definitions, one for each type of JMS resource (check them out in the resources.xml file). We need the one that is defined for javax.jms.ConnectionFactory.
- Next thing is to build the attributes list, with 3 needed attributes: name, jndiName and the connectionDefinition reference. There might be more attributes but those are the mandatory.
- And now, createConfigData for the J2CConnectionFactory (which is the type that is used for JMS connection factories in WAS 6).
- The J2CConnectionFactory must have a property set as a child element, with some more configurations, so we create the property set with createConfigData. As you can see, the AttributeList can be reused after it is cleared.
- For each “resourceProperties“, we must use addElement with an AttributeList that contains 3 attributes: name, type and value. Here we add one resourceProperties element that points to the bus used with this ConnectionFactory (in WAS 6, the whole JMS infrastructure was changed to use a Service Information Bus. Read about that in the InfoCenter).
- And we are done, a lot of lines but at the end you should get the ConnectionFactory configured and ready to work.
As I said before, this is only one example but you should get the sense of how to do other resource as well. The easiest way to knoq what config objects to use, is to look in the resources.xml files and find the declarations of different resources.
For any comments, questions or more examples, feel free to drop a comment.
Ran.