Accessing MSMQ from Java |
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.
This example assumes that you have installed
the JDK under C:\j2sdk1.4.2_01
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>
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.
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 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.
You have options to run your Java client either in DCOM mode or native 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:
You can use native mode if you are accessing the MSMQ on local machine.
C:\pure>java -DJINTEGRA_NATIVE_MODE MsmqExample
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: