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 org.jboss.mq.SpyConnectionFactory 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.

import org.jboss.mq.SpyConnectionFactory;
import org.jboss.mq.il.oil.OILServerILFactory;

....

Properties props = new Properties();
props.setProperty(OILServerILFactory.SERVER_IL_FACTORY_KEY,
OILServerILFactory.SERVER_IL_FACTORY);
props.setProperty(OILServerILFactory.CLIENT_IL_SERVICE_KEY,
OILServerILFactory.CLIENT_IL_SERVICE);
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.

import org.jboss.mq.SpyConnectionFactory;
import org.jboss.mq.il.oil2.OIL2ServerILFactory;

...

Properties props = new Properties();
props.setProperty(OIL2ServerILFactory.SERVER_IL_FACTORY_KEY,
OIL2ServerILFactory.SERVER_IL_FACTORY);
props.setProperty(OIL2ServerILFactory.CLIENT_IL_SERVICE_KEY,
OIL2ServerILFactory.CLIENT_IL_SERVICE);
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.

import org.jboss.mq.SpyConnectionFactory;
import org.jboss.mq.il.uil.UILServerILFactory;

...

Properties props = new Properties();
props.setProperty(UILServerILFactory.SERVER_IL_FACTORY_KEY,
UILServerILFactory.SERVER_IL_FACTORY);
props.setProperty(UILServerILFactory.CLIENT_IL_SERVICE_KEY,
UILServerILFactory.CLIENT_IL_SERVICE);
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.

import org.jboss.mq.SpyQueue;
import org.jboss.mq.SpyTopic;

...

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 jboss.mq:service=DestinationManager 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 java.net.*;
import javax.jms.*;
import javax.management.*;
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 jboss.mq:service=DestinationManager JMX MBean.

ObjectName dm = new ObjectName("jboss.mq:service=DestinationManager");
server.invoke(
   dm,
   "createQueue",
   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 jboss.mq:service=DestinationManager JMX MBean.