Accessing JMS from ASP

Java/J2EE COM Interoperability Products Page

Summary

This example demonstrates how to access Java Message Services (JMS) from your Active Server Pages.

Introduction

This example will show how easy it is to integrate an ASP application and JMS. Any ASP client can post messages to a JMS queue.

For example, you could have a Java order processing application that reads order requests from a JMS queue. It would be easy to apply this example to create an ASP application that provides the users with a web interface that allows them to enter orders into your Java system.

This example was developed using Windows 2000, JDK1.3, J2EE SDK and Visual Basic 6.0. It should also work with Windows NT4.0.

Prerequisites

  1. Install J-Integra®.

  2. Install a .

  3. Install J2EE SDK

  4. Step through the JMS tutorial. Make sure it is working as this example is based on it.
  5. Also ensure that you have installed the J-Integra® license.

The Steps Involved

  1. Configure your environment

  2. Create the COMtoJMS bridge and the MyWrappers class

  3. Start the J2EE server and check that the JMS queue “MyQueue?still exists

  4. Start the SimpleQueueReceiver

  5. Create and run the ASP client

Configure your environment

This description assumes that you have installed:

If you have installed your software in a different location then please take that into account when following these instructions.

  1. Set your PATH environment variable to include the JDK, J2EE SDK and J-Integra® bin directories and update your CLASSPATH environment variable to include the JDK, J2EE SDK and J-Integra® class libraries.

  2. You also need to configure your environment for the J2EE SDK. You may find it convenient to create a batch file in the J2EE SDK bin directory and execute it:

    set JAVA_HOME=d:\jdk1.3
    set J2EE_HOME= d:\j2sdkee1.3
    set PATH=%PATH%;%J2EE_HOME%\bin;d:\jintegra\bin
    set CLASSPATH=d:\java\lib\jintegra.jar;
    set CLASSPATH=%CLASSPATH%;d:\java\lib\;
    set CLASSPATH=%CLASSPATH%;%J2EE_HOME%\lib\j2ee.jar;
    set CLASSPATH=%CLASSPATH%;%J2EE_HOME%\lib\locale;.
    call setenv.bat

    Note that the setenv.bat file that is called at the end of the script is part of the J2EE SDK.

Create the COMtoJMS bridge and the MyWrappers class

  1. Create a file called COMtoJMS.java with the following contents:

    // This is the bridge.  The ASP client talks to this
    // process via DCOM, and this process talks to JMS.
    // If you have a Java server process you could add this
    // initialization there.
    
    import javax.naming.*;
    
    import com.linar.jintegra.*;
    
    public class COMtoJMS {
      public static void main(String[] args) throws Exception {
        try {
          // Generate a log just in case it all goes wrong
          com.linar.jintegra.Log.logImmediately(3, "jintegra.log");
    
          // Register the JVM name with the J-Integra® runtime
          Jvm.register("JMS");
    
          // Wait around for a while. When the process exits the objects
          // here will no longer be accessible from COM.
          while (true) {
            // do nothing.
            Thread.sleep(10000);
          }
        } catch (Exception e) {
          System.out.println(e.getMessage());
          e.printStackTrace();
        }
      }
    }

  2. Create a file called MyWrappers.java with the following contents:

    // This class provides wrappers to enable calling overloaded
    // member functions from ASP.
    
    import javax.naming.*;
    import javax.jms.*;
    
    
    public class MyWrappers {
    
      public void QueueSender_send(
        QueueSender cQueueSender,
        Message cMessage)
        throws javax.jms.JMSException {
        // print out something so we know the method was invoked.
        System.out.println("+QueueSender_send.");
        cQueueSender.send(cMessage);
        System.out.println("-QueueSender_send.");
      }
    
      public Object InitialContext_lookup(
        javax.naming.InitialContext cContext,
        String cClassName)
        throws NamingException {
        // print out something so we know the method was invoked.
        System.out.println("+InitialContext_lookup.");
        Object obj = cContext.lookup(cClassName);
        System.out.println("-InitialContext_lookup.");
        return obj;
      }
    }

    We need this file because the javax.jms.QueueSender and javax.naming.InitialContext classes have overloaded methods, which take the same number of parameters. Thus, J-Integra® is unable to determine which method you wish to call (if the methods took different number of parameters there would not be a problem).

    This wrapper class takes the required parameters and calls the appropriate send method.

  3. Compile the bridge and wrapper class:

    >javac COMtoJMS.java
    >javac MyWrappers.java
    
  4. Register the JVM. Note that 4000 is an arbitrarily chosen port number. You can use any available port number on your machine.

    >regjvmcmd JMS localhost[4000]
    
  5. Start the JVM. Note that we pass the same command line options as in the JMS tutorial. This is to ensure that the JMS classes are available to the JVM. Also, the port specified with JINTEGRA_PORT has to be the same used for registering the JVM.

    >java –Djms.properties=%J2EE_HOME%\config\jms_client.properties –DJINTEGRA_PORT=4000 COMtoJMS
    

Start the J2EE server and check that the JMS queue “MyQueue? still exists

  1. In another console configure the J2EE environment as shown above and start the J2EE server as per the JMS tutorial.

  2. Check that the MyQueue queue still exists with the command

    >j2eeadmin –listJmsDestination
    

Start the SimpleQueueReceiver

In another console configure the J2EE environment and start the SimpleQueueReceiver as per the JMS tutorial.

Create and run the ASP client

  1. Create a file called ASPtoJMS.asp with the following contents:

    <%@ LANGUAGE = VBSCRIPT %>
    <html><head><title>Posting A Message To A JMS Queue</title></head><body>
    <%
    'Get the initial context which will allow us to create objects
    Set objInitialContext = GetObject("JMS:javax.naming.InitialContext")
    
    'Get an instance of the MyWrappers class
    Set objMyWrappers = GetObject("JMS:MyWrappers")
    
    Set objQueueConnectionFactory = objMyWrappers.InitialContext_lookup(objInitialContext,"QueueConnectionFactory")
    
    'Get the queue. MyQueue is the name of the queue created in the JMS tutorial
    Set objQueue = objMyWrappers.InitialContext_lookup(objInitialContext, "MyQueue")
    
    Set objQueueConnection = objQueueConnectionFactory.createQueueConnection()
    
    'Note that the second parameter to createQueueSession should be
    'Session.AUTO_ACKNOWLEDGE, Session.CLIENT_ACKNOWLEDGE
    'or Session.DUPS_OK_ACKNOWLEDGE and I pass 0 here in the
    'assumption that they are defined 0, 1 and 2.
    Set objQueueSession = objQueueConnection.createQueueSession(False, 0)
    
    Set objQueueSender = objQueueSession.createSender(objQueue)
    Set objMessage = objQueueSession.createTextMessage()
    objMessage.SetText("Hello from ASP")
    
    'Again we use the MyWrapper class to call the overloaded member.
    call objMyWrappers.QueueSender_send(objQueueSender, objMessage)
    
    Response.write "Message Sent"
    
    %>
    </body></html>
     
  2. To make this file available on a web browser please follow the instructions in the Accessing a Simple Java Class from Active Server Pages example.

  3. Make sure that the Application Protection setting for the virtual directory holding the file is set to Low (IIS Process).

  4. When you execute the ASP application your browser will display the “Message Sent?and on the command prompt where the SimpleQueueReceiver you should see the following:

    You have successfully sent a message from an ASP application to a JMS queue and received it in a Java application.