Thursday, November 15, 2007

Integrating Java and C apps on Linux.

Abstract: This article consists of several parts that describe my experience with integrating C programs into Java-based infrastructure like Java Messaging.

Part 1. How to build C client for JMS on Fedora Core 6


In this part I'll show how to build C client for Java Message Queue on Fedora Core 6. The story began with some cool application in C with needed functionality, while I had no time and resources to re-write it in Java.We'll need to install following packages:


-bash-3.1# yum install nss.i386 compat-libstdc++-33.i386 screen


This will install C compatibilty libraries, NSS and dependent libraries.
Now we need to get Open Message Queue server and client code (community version).These commands will download and extract distro into /opt/sun/mq directory.


-bash-3.1#wget --no-check-certificate https://mq.dev.java.net/files/documents/5002/66518/mq4_1-binary-Linux_X86-20070816.jar
-bash-3.1#mkdir -p /opt/sun
-bash-3.1#cd /opt/sun
-bash-3.1#unzip mq4_1-binary-Linux_X86-20070816.jar
-bash-3.1#cd mq

Lets run messaging server by issuing following commands:

-bash-3.1#screen

This will create a screen session.

-bash-3.1#/opt/sun/mq/bin/imbrokerd -tty

This will start the server outputting a bunch of information into terminal.

To detach from screen session press Ctrl + A + D (so your server will be still running and you'll be able to return to it later by hitting screen -r)


In this tutorial I would limit it to single example, while you have three available in the directory. Let's use producer_consumer for the sake of simplicity and just change working directory:


-bash-3.1#cd demo/C/producer_consumer

There you'll see two C files - Consumer.c and Producer.c

At this point you are ready to start building your clients. Let's issue following commands:

-bash-3.1# g++ -DLINUX -D_REENTRANT -I/opt/sun/mq/include -o Producer -L/opt/sun/mq/lib -lmqcrt

You may see warning that complaints on possible conflict:

Producer.c/usr/bin/ld: warning: libstdc++.so.5, needed by /opt/sun/mq/lib/libmqcrt.so, may conflict with libstdc++.so.6

If we look at directory contents now, it should contain binary with file name Producer. If you try running it, it might spit out following error:

-bash-3.1# ./Producer

./Producer: error while loading shared libraries: libmqcrt.so.1: cannot open shared object file: No such file or directory

This is easily fixed by adding libraries location to the path. Lets create file

-bash-3.1# nano /etc/ld.so.conf.d/mq.conf

and add /opt/sun/mq/lib to it.

Then just run following command:

-bash-3.1#ldconfig

Now you should be able to build both source files and run it without errors:

-bash-3.1#g++ -DLINUX -D_REENTRANT -I/opt/sun/mq/include -o Consumer -L/opt/sun/mq/lib -lmqcrt Consumer.c

-bash-3.1#g++ -DLINUX -D_REENTRANT -I/opt/sun/mq/include -o Producer -L/opt/sun/mq/lib -lmqcrt Producer.c

-bash-3.1#ln -s Producer p; ln -s Consumer c; ls -al

At this point you should have binaries of C client that are able to publish and read messages to Messaging server.

For usage options run

-bash-3.1#./Producer -help

or

-bash-3.1#./Consumer help

If you need to know more about messaging server, that we used in this tutorial, visit this url http://www.sun.com/software/products/message_queue/index.xml. In the next part of tutorial I'll describe integration of C client into existing application. Stay tuned.

1 comment:

AnusPlus said...

We spent a lot of time in getting a 64-bit version of libmqcrt.so as there are no official instructions on how to build libmqcrt.so yourself (just an incomplete cbuild.xml ant script).

As it may be helpfull for others I posted a guide on how to compile libmqcrt.so here:
http://forums.sun.com/thread.jspa?messageID=10985782

Cloud Computing Google Group