Ok, here is the solution.
First of all I don't really like the example on the WIKI, it leaves out too many
details that a newbie like me really needs to configure and test JMS on JBOSS. So I am
going to be verbose and offer the full example. Hopefully I can add this solution to
the WIKI as well.
1. Configure your project so that you have all the client JARS you will need, and make
sure they are all in the classpath (lib). This will allow you to test your JMS
producer and consumer remotely.
This is the project structure I use to test:
| .:
| .
| ..
| build-jbossmq.xml
| jbossmq.war
| lib
| .nbattrs
| src
| tree.txt
| web
|
|
| ./lib:
| .
| ..
| concurrent.jar
| j2ee.jar
| jboss-common.jar
| jbossmq.jar
| jnpserver.jar
|
| ./src:
| .
| ..
| com
|
| ./src/com:
| .
| ..
| noi
|
| ./src/com/noi:
| .
| ..
| jbossmq
|
| ./src/com/noi/jbossmq:
| .
| ..
| app
|
| ./src/com/noi/jbossmq/app:
| .
| ..
| message
| util
|
| ./src/com/noi/jbossmq/app/message:
| .
| ..
| SimpleConsumer.java
| SimpleProducer.java
|
| ./src/com/noi/jbossmq/app/util:
| .
| ..
| SysLogger.java
|
2. Create the producer.
| /*
| * SimpleProducer.java
| *
| * Created on July 22, 2004, 11:30 AM
| */
|
| package com.noi.jbossmq.app.message;
|
| import javax.jms.*;
| import javax.naming.*;
| import java.util.Properties;
| import com.noi.jbossmq.app.util.*;
|
| public class SimpleProducer {
|
| /**
| * Main method.
| *
| * @param args the destination used by the example,
| * its type, and, optionally, the number of
| * messages to send
| */
| public static void main(String[] args) {
|
| String messages[] = {"MESSAGE A" ,
| "MESSAGE B" ,
| "MESSAGE C" ,
| "MESSAGE D"};
|
| QueueConnectionFactory queueConnectionFactory = null;
| Queue testQueue = null;
|
| try {
| SysLogger.info("Creating jndi context - alternatively use a
jndi.properties");
| Properties properties = new Properties();
| properties.put(Context.INITIAL_CONTEXT_FACTORY,
"org.jnp.interfaces.NamingContextFactory");
| properties.put(Context.PROVIDER_URL, "jnp://192.168.1.7:1099");
| InitialContext jndiContext = new InitialContext(properties);
| queueConnectionFactory = (QueueConnectionFactory)
jndiContext.lookup("ConnectionFactory");
| testQueue = (Queue) jndiContext.lookup("queue/testQueue");
| } catch (NamingException nameEx) {
| SysLogger.info("Naming Exception: " + nameEx.toString());
| }
|
| SysLogger.info("made queue");
| QueueConnection queueConnection = null;
|
| try {
|
| SysLogger.info("trying to send message");
| queueConnection = queueConnectionFactory.createQueueConnection();
| QueueSession queueSession =
queueConnection.createQueueSession(false,Session.AUTO_ACKNOWLEDGE);
| QueueSender queueSender = queueSession.createSender(testQueue);
| TextMessage textMessage = queueSession.createTextMessage();
| SysLogger.info("send each message");
| for (int msgCount = 0; msgCount < messages.length; msgCount++)
| {
| textMessage.setText(messages[msgCount]);
| queueSender.send(textMessage);
| SysLogger.info(" sending line " + msgCount + " : " +
messages[msgCount]);
| }
|
| textMessage.setText("end of message");
| queueSender.send(textMessage);
| SysLogger.info(" sending last line " + " : " + textMessage.getText());
|
| queueConnection.close();
|
| SysLogger.info(" sender closed");
|
| } catch (javax.jms.JMSException jmsEx) {System.out.println("JMS Exception:
" + jmsEx.toString());
| } finally { if (queueConnection != null)
| { try
| { queueConnection.close();
| } catch (javax.jms.JMSException jmse) {
| SysLogger.info("jms exception.");
| }
| }
| }
|
|
| }
| }
|
3. Create the Consumer
| /*
| * SimpleConsumer.java
| *
| * Created on July 22, 2004, 12:30 PM
| */
|
| package com.noi.jbossmq.app.message;
| import javax.jms.*;
| import javax.naming.*;
| import java.util.Properties;
| import com.noi.jbossmq.app.util.*;
| /**
| *
| * @author clay
| */
| public class SimpleConsumer {
|
| public static void main(String[] args) {
|
| QueueConnectionFactory queueConnectionFactory = null;
| Queue testQueue = null;
|
| try {
| SysLogger.info("Creating jndi context - alternatively use a
jndi.properties");
| Properties properties = new Properties();
| properties.put(Context.INITIAL_CONTEXT_FACTORY,
"org.jnp.interfaces.NamingContextFactory");
| properties.put(Context.PROVIDER_URL, "jnp://192.168.1.7:1099");
| InitialContext jndiContext = new InitialContext(properties);
| queueConnectionFactory = (QueueConnectionFactory)
jndiContext.lookup("ConnectionFactory");
| testQueue = (Queue) jndiContext.lookup("queue/testQueue");
|
| } catch (NamingException nameEx) {
| SysLogger.info("Naming Exception: " + nameEx.toString());
| }
|
| QueueConnection queueConnection = null;
|
| try {
|
| queueConnection = queueConnectionFactory.createQueueConnection();
| QueueSession queueSession =
queueConnection.createQueueSession(false,Session.AUTO_ACKNOWLEDGE);
| QueueReceiver queueReceiver = queueSession.createReceiver(testQueue);
| queueConnection.start();
|
| TextMessage textMessage = null;
|
| while (true)
| {
| textMessage = (TextMessage) queueReceiver.receive(1);
| SysLogger.info(" receiving line " + " : " + textMessage.getText());
| if (textMessage.getText().equals("end of message")) {break;}
| }
|
| queueConnection.close();
|
| SysLogger.info(" receiver closed");
|
| } catch (javax.jms.JMSException jmsEx) {SysLogger.info("JMS Exception: " +
jmsEx.toString());
| } finally { if (queueConnection != null)
| { try
| { queueConnection.close();
| } catch (javax.jms.JMSException jmse) {}
| }
| }
| }
| }
|
|
4. Make a logger, this one just goes to system.out
| /*
| * SysLogger.java
| *
| * Created on July 21, 2004, 3:50 PM
| */
|
| package com.noi.jbossmq.app.util;
|
| /**
| *
| * @author clay
| */
| public class SysLogger {
|
| public static void info(String message)
| {
| System.out.println(message);
| }
|
| public static void error(String message, Throwable t)
| {
| System.err.println(message);
| t.printStackTrace();
| }
| }
|
5. create your ant build, if this is going to become a web application (mine is) you
will also need to create a base web directory with a WEB-INF.
| <?xml version="1.0" encoding="UTF-8"?>
| <project basedir="." default="all" name="jbossmq">
| <target name="init">
| <property name="name" value="jbossmq"/>
| <property name="build" value="build" />
| <property name="build.lib" value="lib" />
| <property name="warfile" value="${name}.war" />
| <property name="web.orig.dir" value="web" />
| <property name="web.lib" value="${web.orig.dir}/WEB-INF/lib" />
| <property name="build.classes" value="${build}/WEB-INF/classes" />
| <property name="src.dir" value="src" />
| <!-- Configure the custom Ant tasks for the Manager application -->
| <echo message="JAVA_HOME=${java.home}"/>
| <path id="project.class.path">
| <fileset dir="${java.home}/lib">
| <include name="*.jar"/>
| </fileset>
| <fileset dir="${build.lib}">
| <include name="*.jar"/>
| </fileset>
| <fileset dir="${web.lib}">
| <include name="*.jar"/>
| </fileset>
| </path>
| <tstamp/>
| </target>
|
| <target name="prepare" depends="init"
| description="Create build directories.">
| <mkdir dir="${build}" />
| <mkdir dir="${build}/WEB-INF" />
| <mkdir dir="${build.classes}" />
| <mkdir dir="${build}/WEB-INF/lib" />
| <mkdir dir="${build}/html" />
| <mkdir dir="${build}/images" />
| </target>
|
| <target name="compile" depends="init, prepare"
| description="Compile app Java files and copy HTML and JSP pages" >
| <javac srcdir="${src.dir}" destdir="${build.classes}" deprecation="on">
| <include name="**/*.java" />
| <classpath refid="project.class.path"/>
| </javac>
| </target>
|
| <target name="web-war" depends="init, prepare, compile"
| description="Compile app Java files and copy HTML and JSP pages" >
| <copy todir="${build}/WEB-INF">
| <fileset dir="web/WEB-INF" >
| <include name="jboss-web.xml" />
| <include name="*.tld" />
| </fileset>
| </copy>
| <copy todir="${build}/WEB-INF/lib">
| <fileset dir="web/WEB-INF/lib" >
| <include name="*.jar" />
| </fileset>
| </copy>
| <copy todir="${build}/WEB-INF/classes">
| <fileset dir="${src.dir}" >
| <include name="**/*.properties" />
| </fileset>
| </copy>
| <copy todir="${build}">
| <fileset dir="web">
| <include name="*.html" />
| <include name="*.jsp" />
| </fileset>
| </copy>
| <copy todir="${build}/html">
| <fileset dir="web/html">
| <include name="*.htm" />
| <include name="*.html" />
| <include name="*.txt" />
| <include name="*.css" />
| </fileset>
| </copy>
| <copy todir="${build}/images">
| <fileset dir="web/images">
| <include name="*.jpg" />
| <include name="*.gif" />
| </fileset>
| </copy>
| <war warfile="${name}.war" webxml="${web.orig.dir}/WEB-INF/web.xml">
| <fileset dir="${build}" >
| <include name="WEB-INF/classes/**/*.class" />
| <include name="WEB-INF/classes/**/*.properties" />
| <include name="WEB-INF/lib/*.jar" />
| <include name="WEB-INF/*.xml" />
| <include name="WEB-INF/*.tld" />
| <include name="META-INF/**" />
| <include name="html/**" />
| <include name="images/**" />
| <include name="*.jsp" />
| <include name="*.html" />
| <include name="*.css" />
| </fileset>
| </war>
| </target>
|
| <target name="clean" depends="init">
| <delete dir="${build}"/>
| </target>
|
|
| </project>
|
|
What I generate is a WAR, that I then extract to a deploy dir. If you dont want to do
that then make a JAR instead of a WAR and run it from the command line.
This is working for me with the following output:
| Producer
| Creating jndi context - alternatively use a jndi.properties
| made queue
| trying to send message
| send each message
| sending line 0 : MESSAGE A
| sending line 1 : MESSAGE B
| sending line 2 : MESSAGE C
| sending line 3 : MESSAGE D
| sending last line : end of message
| sender closed
|
| Consumer
| Creating jndi context - alternatively use a jndi.properties
| receiving line : MESSAGE A
| receiving line : MESSAGE B
| receiving line : MESSAGE C
| receiving line : MESSAGE D
| receiving line : end of message
| receiver closed
|
|
Good luck, I hope this helps.
Clay
View the original post :
http://www.jboss.org/index.html?module=bb&op=viewtopic&p=3843022#3843022
Reply to the post :
http://www.jboss.org/index.html?module=bb&op=posting&mode=reply&p=3843022
-------------------------------------------------------
This SF.Net email is sponsored by BEA Weblogic Workshop
FREE Java Enterprise J2EE developer tools!
Get your free copy of BEA WebLogic Workshop 8.1 today.
http://ads.osdn.com/?ad_id=4721&alloc_id=10040&op=click
_______________________________________________
JBoss-user mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/jboss-user