Friday, January 27, 2006

Suns J2EE JMS Example in eclipse

I'm hastily trying to learn all things java. To add to the mix, its the enterprise stuff. Before this week I'd heard of "enterprise Java," but never really understood what it meant. It appears to mean "complicated, scalable Java."

I've been looking into all aspects of it, specifically Java Messaging Service (JMS) which is a scalable messaging system between java programs.

I ran through the tutorial from Sun [] (be sure to get the tut specific to your SDK, it seems to change quite a bit), and had a bit of trouble with it, as I didn't understand the need to package the code into jars, and run it via the "appclient" batch file provided by Sun [].

This sets up the environment to allow the "application client" to run under the control of the application server, and have full access to its queues etc. This is necessary because of something called Environment Naming Context (ENC) which the application needs to have access to to connect to the application server.

C:\JMS_Test\bin>appclient -client SimpleProducer.jar jms/Queue 4

Works perfectly.

With that running the next step was to get it running from within Eclipse. After many dead ends, I found someone who had had this problem before []. I mostly followed his instructions, not quite - here's what worked for me:
I set up a project in eclipse, just an ordinary project, and added a source folder to it. I then copied in the simplified code supplied by Sun. Running this leads to a series of problems. The first thing that needs set is command line arguments. Right click the project and select debug as -> debug.... - from the dialog select the arguments tab, and insert "jms/Queue 3" (without the quotes) into the top box. This specifies the command line arguments expected in the program.

This gets through the first part of the code, but then throws an NoInitialContextException Exception:
JNDI API lookup failed: javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial

This is because the environment is not set up properly. That's where This article comes in - I set up the project as suggested :
1) Set the VM Arguments (in the Arguments Tab [debug as -> debug...])
-Dcom.sun.aas.installRoot="%AS_INSTALL%" -Dcom.sun.aas.imqLib="%AS_IMQ_LIB%""%AS_INSTALL%\lib\appclient\client.policy""%AS_INSTALL%\lib\appclient\appclientlogin.conf" -Djava.endorsed.dirs="%AS_INSTALL%\lib\endorsed" -Djavax.xml.parsers.SAXParserFactory=org.apache.xerces.jaxp.SAXParserFactoryImpl -Djavax.xml.parsers.DocumentBuilderFactory=org.apache.xerces.jaxp.DocumentBuilderFactoryImpl -Djavax.xml.transform.TransformerFactory=org.apache.xalan.xsltc.trax.TransformerFactoryImpl -Dorg.xml.sax.parser=org.xml.sax.helpers.XMLReaderAdapter -Dorg.xml.sax.driver=org.apache.xerces.parsers.SAXParser
2) In the Classpath Tab, click on user entries and add the following External Jars:

From the directory C:\Sun\AppServer\libj2ee.jar
From the directory C:\Sun\AppServer\imq\lib\

Debugging this application now works. Phew.


jaded said...


Good day.
can i ask where did you get all those libraries?
i am currently working on a JMS project.


Simon said...

Its been so long, I really am not sure. From a quick scan of the post I can see that I probably used the Sun application server - so that would contain some of the files. Then I think the examples were just compiled versions of the tutorials found on the sun website.