Skip Headers
Oracle® Fusion Middleware Programming JMS for Oracle WebLogic Server
12c Release 1 (12.1.1)

Part Number E24387-03
Go to Documentation Home
Home
Go to Table of Contents
Contents
Go to Feedback page
Contact Us

Go to previous page
Previous
Go to next page
Next
PDF · Mobi · ePub

7 Using JMS Module Helper to Manage Applications

This chapter describes how to use the weblogic.jms.extensions.JMSModuleHelper to programmatically create and manage JMS servers, Store-and-Forward Agents, and JMS system resources.

Configuring JMS System Resources Using JMSModuleHelper

JMSModuleHelper provides the following API signatures to manage a system module and JMS resources, such as queues and topics:

You can manage a system module, including the JMS resources it contains by providing the domain MBean or by providing the initial context to the administration server in the API signature. For more information on JMS system resources, see "Configuring Basic JMS System Resources" in the Oracle WebLogic Server Administration Console Help.

Configuring JMS Servers and Store-and-Forward Agents

JMSModuleHelper provides the following method APIs to manage JMS servers and Store-and-Forward Agents:

You can manage JMS servers and Store-and-Forward Agents by providing the domain MBean or by providing the initial context to the administration server in the API signature. For more information, see:

JMSModuleHelper Sample Code

This section provides sample code to create and delete a JMS system resource module.

Creating a JMS System Resource

The module contains a connection factory and a topic.

Example 7-1 Create JMS System Resources

.
.
.
private static void createJMSUsingJMSModuleHelper(Context ctx){
System.out.println(
     "\n\n.... Configure JMS Resource for C API Topic Example ....\n\n"); 

     try {

     MBeanHome mbeanHome = 
        (MBeanHome) ctx.lookup(MBeanHome.ADMIN_JNDI_NAME);
        DomainMBean domainMBean = mbeanHome.getActiveDomain();
        String domainMBeanName = domainMBean.getName();
        ServerMBean[] servers = domainMBean.getServers();

      String jmsServerName = "examplesJMSServer";

//
// create a JMSSystemResource "CapiTopic-jms"
//
     String resourceName = "CapiTopic-jms";
     JMSModuleHelper.createJMSSystemResource(
        ctx,
         resourceName,
         servers[0].getName());
         JMSSystemResourceMBean jmsSR =
              JMSModuleHelper.findJMSSystemResource(
                    ctx,
                    resourceName);
          JMSBean jmsBean = jmsSR.getJMSResource();
     System.out.println("Created JMSSystemResource " + resourceName);

//
// create a JMSConnectionFactory "CConFac"
//
     String factoryName = "CConFac";
     String jndiName = "CConFac";
     JMSModuleHelper.createConnectionFactory(
        ctx,
        resourceName,
         factoryName, 
         jndiName,
          servers[0].getName());
     JMSConnectionFactoryBean factory =           jmsBean.lookupConnectionFactory(factoryName);
     System.out.println("Created Factory " + factory.getName());

//
// create a topic "CTopic"
//
     String topicName = "CTopic";
     String topicjndiName = "CTopic"; 
     JMSModuleHelper.createTopic(
        ctx,
        resourceName,
        jmsServerName,
        topicName, 
        topicjndiName);

     TopicBean topic = jmsBean.lookupTopic(topicName);
     System.out.println("Created Topic " + topic.getName());
     } catch (Exception e) {
         System.out.println("Example configuration failed :" + e.getMessage());
        e.printStackTrace();
     } 
}
.
.
.

Deleting a JMS System Resource

The following code removes JMS system resources.

Example 7-2 Delete JMS System Resources

.
.
.
private static void deleteJMSUsingJMSModuleHelper(Context ctx ) {

     System.out.println("\n\n.... Remove JMS System Resource for C API Topic Example ....\n\n");


     try {

        MBeanHome mbeanHome = 
           (MBeanHome) ctx.lookup(MBeanHome.ADMIN_JNDI_NAME);
           DomainMBean domainMBean = mbeanHome.getActiveDomain();
           String domainMBeanName = domainMBean.getName();
            ServerMBean[] servers = domainMBean.getServers();

     String jmsServerName = "examplesJMSServer";

//
// delete JMSSystemResource "CapiTopic-jms"
//
     String resourceName = "CapiTopic-jms";
     JMSModuleHelper.deleteJMSSystemResource(
        ctx,
        resourceName
         );
     } catch (Exception e) {
        System.out.println("Example configuration failed :" + e.getMessage());
        e.printStackTrace();
     } 
}
.
.
.

Best Practices when Using JMSModuleHelper

This section provides best practices information when using JMSModuleHelper to configure JMS servers and resources:

private static Queue findQueue (
 QueueSession queueSession,
 String jmsServerName, 
 String queueName, 
 int retryCount,
 long retryInterval
) throws JMSException
{
  String wlsQueueName = jmsServerName + "/" + queueName;
  String command = "QueueSession.createQueue(" + 
       wlsQueueName + ")";
  long startTimeMillis = System.currentTimeMillis();
  for (int i=retryCount; i>=0; i--) {
       try {
        System.out.println("Trying " + command);
        Queue queue = queueSession.createQueue(wlsQueueName);
        System.out.println(command + "succeeded after " +
               (retryCount - i + 1) + " tries in " +
               (System.currentTimeMillis() - startTimeMillis) + 
               " millis.");
        return queue;
       } catch (JMSException je) {
        if (retryCount == 0) throw je;
       }
       try {
        System.out.println(command + "> failed, pausing " +
               retryInterval + " millis.");
        Thread.sleep(retryInterval);
       } catch (InterruptedException ignore) {}
  }
  throw new JMSException("out of retries");
}

You can then call the findQueue() method after the JMSModuleHelper class method call to retrieve the dynamically created queue once it becomes available. For example:

JMSModuleHelper.createPermanentQueueAsync(ctx, domain, jmsServerName, 
 queueName, jndiName);
Queue queue = findQueue(qsess, jmsServerName, queueName,
 retry_count, retry_interval);