Connecting to Glassfish/Sun JMS Using C++ / C API

Aim
The aim of this tutorial is to use Sun's Message Queue 4.3 C-API to connect to a JMS topic on a Glassfish server. This is a fairly simple as we will compile the examples in the package and sun them agaisnst a Queue created in Glassfish. This example is on Windows using cl.exe. Leave any questions or problems as comments and I will endeavour to answer them. I will not be creating any source files as the ones provided in the examples are the simplest and cannot be further reduced.

Assumptions
This article assumes that you have Galssfish 2 installed and a domain configured. Please see Installing and Setting up Glassfish 2 for more details. For purposes of this example the glassfish domain is running on port 8082 and the project directory is examples.

For those of you who are familiar with the JMS Example using Glassfish and Remote Client example, you can go directly to the Compiling and Running sections as we are going to connect to the "jms/MyFirstTopic" topic in the example. If not follow the steps to setup a JMS

Versions used in this example
Sofware/ComponentImage
Windows XP SP2N/A
Glassfish 2glassfish-installer-v2.1-b60e-windows.jar
Visual C++ 2008 Express EditionN/A
Message Queue 4.3mq4_3-installer-WINNT.zip
Links to these files can be found here


Download and Unzip/Install Message Queue 4.3
  1. Download and unzip Message Queue 4.3. Note that i'm going to download and unzip this package. I'm not going to install it as I don't want a full Message Queue. I'm using Glassfish for that. For this example, the archive is extracted to d:\mq4_3-installer.

  2. Navigate to d:\mq4_3-installer\Product\WINDOWS\WINDOWS XP\X86\5.1\Packages and extract the mq-capi.zip to your working diretory. You should see a structure such as below. We will be working inside the mq\examples directory.
    mq\bin
    mq\examples
    mq\include
    mq\lib

Create a Topic in Glassfish - if you haven't already
  1. Login to the Glassfish admin console.

  2. Now select Resources -> JMS Resources -> Destination Resources. Select New
  3. `
  4. Enter jms/MyFirstTopic as the JNDI name. Enter MyFirstTopic into the Physical Destination Name and select javax.jms.Topic from the "Resource Type" dropdown. Select OK. The physical destination is what you would use to connect outside of JNDI - forexample using the JMS C-API.


Compile the Consupmer and Producer
  1. Open a prompt into the mq\examples\C\producer_consumer directory you extracted in the previous section

  2. Compile the Consumer

    ..mq\examples\C\producer_consumer>cl /MD -DWIN32 -I..\..\..\include Consumer.c /NODEFAULTLIB ..\..\..\lib\mqcrt.lib

  3. Compile the Producer

    ..mq\examples\C\producer_consumer>cl /MD -DWIN32 -I..\..\..\include Producer.c /NODEFAULTLIB ..\..\..\lib\mqcrt.lib

Running the example
  1. Add the mq\lib directory to your PATH. For this example I have extracted the mq-capi.zip under c:\temp. Replace this path with your local ppath!

    ..mq\examples\C\producer_consumer>set PATH=%PATH%;d:\temp\mq\lib

  2. Now run the consumer.

    ..mq\examples\C\producer_consumer>consumer -h localhost -p 7676 -t topic -d MyFirstTopic

  3. Open a new prompt and navigate to the ..mq\examples\C\producer_consumer directory. Now run the producer from here.

    ..mq\examples\C\producer_consumer>producer -h localhost -p 7676 -t topic -d MyFirstTopic

  4. Type some text in the producer's promt and it will appear on the consumers console.

  5. If you already have the JMS Example using Glassfish and Remote Client tutorial configured, then you can send and receive messages via the JNDI to the C-API and vice versa. Now that's pretty cool.

2 comments:

Anitha said...

Will this API help us to write messages to JMS Queue residing in Weblogic server ?

righteous said...

Should work with any JMS implementation. Only the ports may be different