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.
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
|Windows XP SP2||N/A|
|Visual C++ 2008 Express Edition||N/A|
|Message Queue 4.3||mq4_3-installer-WINNT.zip|
Download and Unzip/Install Message Queue 4.3
- 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.
- 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.
Create a Topic in Glassfish - if you haven't already
- Login to the Glassfish admin console.
- Now select Resources -> JMS Resources -> Destination Resources. Select New
- 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
- Open a prompt into the mq\examples\C\producer_consumer directory you extracted in the previous section
- Compile the Consumer
..mq\examples\C\producer_consumer>cl /MD -DWIN32 -I..\..\..\include Consumer.c /NODEFAULTLIB ..\..\..\lib\mqcrt.lib
- Compile the Producer
..mq\examples\C\producer_consumer>cl /MD -DWIN32 -I..\..\..\include Producer.c /NODEFAULTLIB ..\..\..\lib\mqcrt.lib
Running the example
- 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!
- Now run the consumer.
..mq\examples\C\producer_consumer>consumer -h localhost -p 7676 -t topic -d MyFirstTopic
- 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
- Type some text in the producer's promt and it will appear on the consumers console.
- 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.