Next example is a message driven bean deployed on glassfish that gets called by a stand alone client. Let start with creating the JMS Connection Factory and the JMS Queue on glassfish.
Issue the following statement to create the connection factory:
asadmin create-jms-resource –restype javax.jms.ConnectionFactory jms/ConnectionFactory
After this create the resource destination:
asadmin create-jmsdest -T queue sampleQueue
Create the JMS destination:
asadmin create-jms-resource –restype javax.jms.Queue –property Name=sampleQueue jms/SampleQueue
Now we can create the Message Driven Bean:
broersa@debian1:~/work/HelloApp/HelloMDB/src/com/bekijkhet$ cat HelloMDB.java
package com.bekijkhet;
import javax.ejb.MessageDriven;
import javax.jms.MessageListener;
import javax.jms.Message;
import javax.jms.TextMessage;
@MessageDriven(mappedName="jms/SampleQueue")
public class HelloMDB implements MessageListener {
public void onMessage(Message msg) {
try {
if (msg instanceof TextMessage) {
TextMessage txtmsg = (TextMessage) msg;
System.out.println("Got message: "+txtmsg.getText());
}
} catch (Exception e) { System.out.println(e.toString()); }
}
}
broersa@debian1:~/work/HelloApp/HelloMDB$ cat build.xml
<project name="HelloMDB" default="dist" basedir=".">
<description>
simple example build file
</description>
<!-- set global properties for this build -->
<property name="src" location="src"/>
<property name="build" location="build"/>
<property name="dist" location="dist"/>
<target name="init">
<!-- Create the time stamp -->
<tstamp/>
<!-- Create the build directory structure used by compile -->
<mkdir dir="${build}"/>
</target>
<target name="compile" depends="init"
description="compile the source " >
<!-- Compile the java code from ${src} into ${build} -->
<javac srcdir="${src}" destdir="${build}"/>
</target>
<target name="dist" depends="compile"
description="generate the distribution" >
<!-- Create the distribution directory -->
<mkdir dir="${dist}"/>
<!-- Put everything in ${build} into the MyProject-${DSTAMP}.jar file -->
<jar jarfile="${dist}/HelloMDB.jar" basedir="${build}"/>
</target>
<target name="clean"
description="clean up" >
<!-- Delete the ${build} and ${dist} directory trees -->
<delete dir="${build}"/>
<delete dir="${dist}"/>
</target>
</project>
Build and deploy the MDB:asant dist
asadmin deploy dist/HelloMDB.jar
Let’s go on creating the stand alone client to call the MDB.
broersa@debian1:~/work/HelloApp/HelloClient/src/com/bekijkhet/helloclient$ cat HelloMDBClient.java
package com.bekijkhet.helloclient;
import javax.naming.*;
import javax.jms.*;
public class HelloMDBClient {
public static void main(String args[]) {
try {
// will get the local default context from appserv-rt.jar
Context ctx = new InitialContext();
ConnectionFactory connectionFactory = (ConnectionFactory)ctx.lookup("jms/ConnectionFactory");
Queue queue = (Queue)ctx.lookup("jms/SampleQueue");
javax.jms.Connection connection = connectionFactory.createConnection();
javax.jms.Session session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);
MessageProducer messageProducer = session.createProducer(queue);
TextMessage message = session.createTextMessage();
message.setText("my own text message");
System.out.println( "Send: "+ message.getText());
messageProducer.send(message);
connection.close();
} catch (Exception e) {System.out.println(e.toString());}
}
}
Compile and run:
javac -cp $GLASSFISH_HOME/lib/appserv-rt.jar:$GLASSFISH_HOME/lib/javaee.jar:. -d . HelloMDBClient.java
java -cp $GLASSFISH_HOME/lib/appserv-rt.jar:$GLASSFISH_HOME/lib/javaee.jar:$GLASSFISH_HOME/lib/install/applications/jmsra/imqjmsra.jar:$GLASSFISH_HOME/lib/appserv-admin.jar:. com.bekijkhet.helloclient.HelloMDBClient
<snipped a lot of initialisation output..>
Send: my own text message
When we look at the server log file ($GLASSFISH_HOME/domains/domain1/logs/server.log) we can see the MDB got fired:
[#|2007-11-23T12:17:45.309+0100|INFO|sun-appserver9.1|javax.enterprise.system.stream.out|_ThreadID=23;_ThreadName=p: thread-pool-1; w: 34;|
Got message: my own text message|#]
After this my client app (HelloMDBClient) hangs….. Press ctrl-c to end the process. I can’t find a solution to get the program exit normally… It looks like another thread got starten to listen for incomming messages??
In the next sample I create an EJB to send messages which will be run from inside the container. This seams to work without hanging. Maybe someone can explain?
Thanks… This was very helpful.
I was missing the secret sauce.
Missing some of the Jar Files….
You should post on NB site.
Thanks again!
Peter
Comment by PEter — August 25, 2008 @ 7:41 pm
good site!
Comment by car insurance — February 8, 2009 @ 5:31 am
Hello webmaster
I would like to share with you a link to your site
write me here preonrelt@mail.ru
Comment by Alexwebmaster — March 3, 2009 @ 7:57 am
Good article to quick start without
hedges.
Thanks for briefing in nice way
Comment by Chandra — March 6, 2009 @ 2:12 pm