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 [http://java.sun.com/j2ee/1.4/docs/tutorial/doc/index.html] (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 [http://java.sun.com/j2ee/1.4/docs/relnotes/cliref/hman1m/appclient.1m.html].

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 [http://forum.java.sun.com/thread.jspa?threadID=484229&tstart=90]. 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%" -Djava.security.policy="%AS_INSTALL%\lib\appclient\client.policy" -Djava.security.auth.login.config="%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
appserv-rt.jar
appserv-ext.jar
mail.jar
activation.jar
jaxrpc-impl.jar
saaj-impl.jar
jaxr-impl.jar
relaxngDatatype.jar
xsdlib.jar
install\applications\jmsra\imqjmsra.jar
appserv-cmp.jar
appserv-admin.jar
From the directory C:\Sun\AppServer\imq\lib\
fscontext.jar


Debugging this application now works. Phew.

2 comments:

Unknown said...

hello,

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

tnx
-jaded

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.