More on Java and MSMQ



In my original post on Java interop with MSMQ, I gave a sample implementation of a Queue class for Java that was able to send and receive strings, and I suggested that it would be pretty simple to extend it to send whatever you want.  A couple of people have asked just what would be required, so here are some additional details. 


For example, suppose you want to extend the Queue class to be able to send and receive a byte array.  Here’s what you’d do:


First, Read The Fine Manual from Sun on sending arrays through JNI.  Then, using the newly-acquired knowledge from that page, add a couple of new native methods (one for send, one for receive) in the MsmqQueueNativeMethods.cpp file, that handle a jbyteArray as input instead of a jstring. Then expose those native methods in the Queue.java class, following the example set by the jstring methods.  Re-build, you’re done. 


I’ve heard back from one person that this works.


If you aren’t so comfortable with JNI, a simpler approach may be just to base64-encode the byte array into a string, on the sender and on the receiver side. This would require only Java programming.  But of course you would need a base64 encoding class, which is not included in the Java base class library, as far as I know.  I think Apache have one. Anyone?

Comments (4)

  1. Rory Winston says:

    Actually there are BASE64 (de/en)coders in the JDK – they are just under the com.sun.* namespace, so are not "officially supported", although they have been there for a long, long time.

  2. Prasad says:

    Dino,

    Could you please possibly add a method to remove a message after peek, that is a receive method given a label or correlationid.

    Thanks,

  3. Bert Szoghy says:

    Hello,

    Sun have been scrambling their web site so the link to the array reference no longer works. Fortunately, there is the Wayback:

    http://web.archive.org/web/20030813063216/java.sun.com/docs/books/tutorial/native1.1/implementing/array.html

    Fascinating subject: MSMQ with Java, thanks!

    A possible bridge to Linux might be a UDP multicasting server on the Windows box which would be receiving XML from MSMQ as you describe and then passing it on to the Linux Java UDP clients.

    The only gotcha I see is that if you do the UDP server in VB.NET, you might not be able to multicast to "groups" the way Java understands it. Someone, please contradict me with working code.

    So I believe the UDP Server on the Windows box looks like it would need to be coded in Java rather than in VB.NET.

    Refs with code for that:

    http://docs.rinet.ru/UJ11/ch25.htm

    and

    http://rigauxf.waika9.com/

    If others want to structure an Open Source project to try this, let me know.

    More JNI on my own website: how to hook up Java to the MS Speech API.

    Cheers,

    Bert

Skip to main content