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

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 SP2N/A
Glassfish 2glassfish-installer-v2.1-b60e-windows.jar
Visual C++ 2008 Express EditionN/A
Message Queue
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 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
  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\examples\C\producer_consumer>cl /MD -DWIN32 -I..\..\..\include Consumer.c /NODEFAULTLIB ..\..\..\lib\mqcrt.lib

  3. Compile the Producer\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 under c:\temp. Replace this path with your local ppath!\examples\C\producer_consumer>set PATH=%PATH%;d:\temp\mq\lib

  2. Now run the consumer.\examples\C\producer_consumer>consumer -h localhost -p 7676 -t topic -d MyFirstTopic

  3. Open a new prompt and navigate to the\examples\C\producer_consumer directory. Now run the producer from here.\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.


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

Naviya Nair said...

Very interesting and good Explanation
ASP NET Training
ASP NET Training
ASP NET Online Training
C-Sharp Training
Dot Net Training in Chennai
Online .Net Training

MVC Training
WCF Training
Web-API Training
LINQ Training
Entity Framework

Dot Net Interview Questions

geethu said...

Really interesting content which is unique which provided me the required information.
Dot Net Training in Chennai | .Net training in Chennai | FITA Training | FITA Velachery .

Nikshitha S said...

Well Said, you have furnished the right information that will be useful to anyone at all time. Thanks for sharing your Ideas.
Python Training in Chennai | Python Course in Chennai

Jhon Mick said...

This technical post helps me to improve my skills set, thanks for this wonder article I expect your upcoming blog, so keep sharing...
web designing training|web designing training in chennai

Priya R said...

Well Said, you have furnished the right information that will be useful to anyone at all time. Thanks for sharing your Ideas.
Web Designing Course in Chennai | web designing training in chennai

geethu said...

Quite a useful post, I learned some new points here. Thanks admin please keep posting updates regularly to enlighten our knowledge.
PHP Training in Chennai | PHP Course in Chennai

Evelin Harriet said...

Nice Blog. Thank you for Sharing.
Enterprise Resource Planning Software Solutions in Chennai. For more details contact us
erp in chennai | erp software providers chennai.Brave Technologies Private Limited is a leading ERP software company in chennai. We provide best quality cost effective ERP software applications and end to end enterprise solutions to all the industry verticals with the affordable cost.

Shivani Yuvanjalin said...

Thank you for this valuable information. I have got some important suggestions from it. Get your business to the next level in simple steps.
erp software company in chennai | erp software providers in chennai.

sivanesan said...

Nice blog. Thank you for sharing. The information you shared is very effective for learners I have got some important suggestions from it. erp in chennai.