Accessing MSMQ from Java

Java/J2EE COM Interoperability Products Page

This example demonstrates how to use Java to send a message using MSMQ on Windows 2000. J-Integra® is a generic bi-directional bridge between any Java objects and any COM components. We do not provide the documentation of the generated Java proxies since the Java proxies are just mapped from the programming API of the COM component. For more information about MSMQ programming API, please refer to the Microsoft Message Queuing Reference. The quickest way to start programming Java-to-MSMQ applications is to find a VB example first, and then map the VB code to Java.

The steps involved

  1. Configure your environment

  2. Generate the Java proxies for MSMQ

  3. Create and compile the example pure Java client

  4. Run the example

  5. Remote access - configure DCOM access to remote MSMQ

Configure your environment

This example assumes that you have installed

We will be performing this example under C:\pure. Create that directory, and an msmq directory under it. Update your path environment variable to include the JDK and J-Integra® bin directories, and update your CLASSPATH environment variable to include the J-Integra® runtime:

C:\>mkdir pure
C:\>cd pure
C:\pure>mkdir msmq
C:\pure>set PATH=C:\jintegra\bin;C:\j2sdk1.4.2_01\bin;C:\j2sdk1.4.2_01\jre\bin;
C:\pure>set CLASSPATH=.;C:\jintegra\lib\jintegra.jar
C:\pure>

Generate the Java proxies for MSMQ

Type Libraries contain information about COM Components. We will be using the information contained in MSMQ's type library to generate pure Java proxies which can be used to access MSMQ from a standard JVM.

  1. Start the com2java tool:



    If you get an error about the command not being found, check that you set your path environment variable correctly above.
  2. Click Settings > Options... to check the options and click OK..

  3. Select C:\WINNT\System32\mqoa.dll as the type library
  4. Enter C:\pure into the output dir
  5. Enter msmq as the Java package name field in the com2java dialog box.
  6. Click on Generate Proxies...
  7. When you click OK, the following proxy files will be generated:


    Take a look at some of the generated files to get a feel for what is being generated.

    You will notice that comments are included in the generated files in order to make them easier to understand. You may wish to use the javadoc documentation generation tool on the files (the comments are compatible with that tool).

Create and compile the example pure Java client

Create the file c:\pure\MsmqExample.java, by cutting and pasting from your Web browser. In order to run this example, the MSMQ machine which MsmqExample.java accesses must belong to a Domain instead of a Workgroup. Otherwise you will get:

Exception in thread "main" AutomationException: 0xc00e006a - The operation is not supported for a WORKGROUP installation computer. in 'MSMQQueueInfo '
   at msmq.MSMQQueueInfo.create(MSMQQueueInfo.java:602)
   at MsmqExample.main(MsmqExample.java:17)

Refer to Microsoft Message Queuing Reference for more information about supported operations for Workgroup and Domain.

import msmq.*;
// Generated using 'com2java' from \WINNT\System32\mqoa.dll

import com.linar.jintegra.AuthInfo;

public class MsmqExample {
  public static void main(String[] args) throws Exception {
    try {
      String hostName = "localhost";
      String pathName = ".\\PRIVATE$\\Greeting2";
      // If running this Java client under MS windows then make sure the J-Integra® 'bin' directory
      // is in your PATH, otherwise run DCOMCNFG on the machine running MSMQ (Start|Run|DCOMCNFG),
      // grant a specific user default launch and access permissions, and uncomment the following
      // line, specifying the appropriate domain/user/password
      // com.linar.jintegra.AuthInfo.setDefault("NT DOMAIN", "NT USER", "NT PASSWORD");
      // Create the queue on the remote Windows machine using its IP address
      // MSMQQueueInfo qinfo = new MSMQQueueInfo("123.456.78.9");

      // Create the queue on the local Windows machine
      MSMQQueueInfo qinfo = new MSMQQueueInfo(hostName);
      qinfo.setPathName(pathName);
      System.out.println("pathName = " + pathName);
      qinfo.create(null, null);
      // Open the queue, and send a message
      IMSMQQueue q = qinfo.open(MQACCESS.MQ_SEND_ACCESS, MQSHARE.MQ_DENY_NONE);

      // Create the message
      IMSMQMessage msg = new MSMQMessage(hostName);

      msg.setLabel("Test Message");
      msg.setBody("This is a test message with a string Body.");
      msg.send(q, null);
      q.close();
      // Open the queue, and read the message
      q = qinfo.open(MQACCESS.MQ_RECEIVE_ACCESS, MQSHARE.MQ_DENY_NONE);
      msg = q.receive(null, null, null, null);
      System.out.println("Received: " + msg.getBody());
      q.close();
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      com.linar.jintegra.Cleaner.releaseAll();
    }
  }
}

Compile it using C:\pure>javac MsmqExample.java

If you get a The name specified is not recognized as an internal or external command ... error, then it is likely that you did not set you path environment variable correctly (see the first section of this example)

If you get a MsmqExample.java:1: Package msmq not found in import... error, then your CLASSPATH environment variable does not correctly include a dot: "."

If you get a Package com.linar.jintegra not found in import... error, then your CLASSPATH environment variable does not correctly include jintegra.jar

If you get an error like error: File .\msmq\MSMQQueueInfo.java does not contain type msmq.MSMQQueueInfo as expected. Please adjust the class path so that the file does not appear in the package msmq, then it is likely that you did not specify the package msmq when generating the proxy files from the type library.

Run the Java example

You have options to run your Java client either in DCOM mode or native mode.

DCOM Mode

You need to configure mqoa.dll using Setdllhost first if you do not run MsmqExample.java in the native mode:

C:\WINNT\System32\setdllhost mqoa.dll "MSMQ"

You should be able to see the following output when running the example:

Native Mode

You can use native mode if you are accessing the MSMQ on local machine.

C:\pure>java -DJINTEGRA_NATIVE_MODE MsmqExample

Remote access - configure DCOM access to remote MSMQ

When running under Windows, J-Integra® can use native code to pick up your current login identity. If you would rather that J-Integra® did not do this, or if your Java client is on another remote machine of any OS, then you must configure DCOM access to MSMQ or to other COM components, and you must specify the NT domain, user and password to be used by J-Integra®. For example, to run the MsmqExample.java on a remote UNIX box:

  1. Install J-Integra® (the jintegra.jar) on the UNIX machine. Install MSMQ on anther Windows machine.
  2. Move the Setdllhost tool to the Windows machine. Use the Setdllhost and DCOMCNFG to configure the MSMQ on the Windows machine.
  3. Move the com2java tool to the Windows machine to generate the Java proxies of MSMQ, and then move the Java proxies to the UNIX from the Windows.
  4. Create the MsmqExample.java on the UNIX machine, and uncomment the line:
    com.linar.jintegra.AuthInfo.setDefault("NT DOMAIN", "NT USER", "NT PASSWORD");
    and pass the correct login credentials that are used to access the Window.
  5. Compile and run the MsmqExample.java on the UNIX machine.