Using JBossMQ Features

You should always try your hardest to stick to the JMS spec so that your application can remain JMS provider independent. However, you sometimes don't need to say JMS provider independent or you just don't have another choice but to use a JMS provider specific feature.

This chapter will review some of APIs that JBossMQ provides that can be used to do a few things that are not in the JMS spec.

Creating Managed Objects

Sometimes you create a JMS application but do not want to use JNDI to lookup the connection factory and destination objects. You can use JBossMQ APIs to create those objects yourself.

Connection Factory Objects

You can create a JMS connection factory by using the constructor:

public SpyConnectionFactory(Properties props);

The properties passed into the constructor configures the Invocation Layer settings that generated connections will use.

Listing 5.1, Listing 5.2, and Listing 5.3 are code samples demonstrating how to creating a JBossMQ connection factory object over different Invocation Layers. The host name, port and ping period are the configurable variables that your client will need to know to connect to the JBossMQ server.

Listing 5.1 Creating a connection factory that uses the OIL Invocation Layer.



Properties props = new Properties();
props.setProperty(OILServerILFactory.PING_PERIOD_KEY, "1000");
props.setProperty(OILServerILFactory.OIL_ADDRESS_KEY, "localhost");
props.setProperty(OILServerILFactory.OIL_PORT_KEY, "8090");

QueueConnectionFactory cf = new SpyConnectionFactory(props);

Listing 5.2 Creating a connection factory that uses the OIL2 Invocation Layer.



Properties props = new Properties();
props.setProperty(OIL2ServerILFactory.PING_PERIOD_KEY, "1000");
props.setProperty(OIL2ServerILFactory.OIL2_ADDRESS_KEY, "localhost");
props.setProperty(OIL2ServerILFactory.OIL2_PORT_KEY, "8092");

QueueConnectionFactory cf = new SpyConnectionFactory(props);

Listing 5.3 Creating a connection factory that uses the UIL Invocation Layer.



Properties props = new Properties();
props.setProperty(UILServerILFactory.PING_PERIOD_KEY, "1000");
props.setProperty(UILServerILFactory.UIL_ADDRESS_KEY, "localhost");
props.setProperty(UILServerILFactory.UIL_PORT_KEY, "8091");

QueueConnectionFactory cf = new SpyConnectionFactory(props);

Destination Objects

To create a queue or a destination, you would respectively use the SpyQueue and SpyTopic constructors.



Queue queue = new SpyQueue("testQueue");
Topic topic = new SpyTopic("testTopic");

Remote JBossMQ Management

All remote management of the JBossMQ server is accomplished via JMX. There are several remote interfaces into the JBoss JMX server. This chapter will provide a few examples that use the org.jboss.jmx.adaptor.rmi.RMIAdaptor. The RMIAdaptor allows you to establish an RMI connection to the JMX server to issue JMX commands.

Dynamic Creation of a Destination

A common need of a Messaging client is to create a queue or topic on demand at runtime. This can be achieved using the createQueue(...) method of the JMX MBean. The code snippet that we will review below will create a queue if it already does not exist:

You first import the needed classes.

import javax.jms.*;
import javax.naming.*;
import org.jboss.jmx.adaptor.rmi.RMIAdaptor;

You then look to see if the queue already exists using JNDI.

ctx = new InitialContext();
try {

destination = (Queue)ctx.lookup("dynamicQueue");
System.out.println("Queue did exist."); 

If it does not exist, a NameNotFoundException is thrown and we handle it by using JMX to create the queue dynamic manner. First thing that you have to do, is get the RMIAdaptor for the remote JMX server:

} catch (NameNotFoundException e) {

System.out.println("Queue did not exist... creating.");
String serverName = InetAddress.getLocalHost().getHostName();
RMIAdaptor server = (RMIAdaptor)ctx.lookup("jmx:"+serverName+":rmi");

Once the RMIAdaptor has been located, you can use the JMX bus to invoke the createQueue method on the JMX MBean.

ObjectName dm = new ObjectName("");
   new Object[] { "dynamicQueue", "dynamicQueue" },
   new String[] { String.class.getName(), String.class.getName()}

destination = (Queue)ctx.lookup("dynamicQueue"); 

To create a topic you would similarly use the createTopic of the JMX MBean.