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
[sourcecode language="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()); }
}
}[/sourcecode]
broersa@debian1:~/work/HelloApp/HelloMDB$ cat build.xml
[sourcecode language="XML"]
simple example build file
[/sourcecode]
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
[sourcecode language="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());}
}
}[/sourcecode]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?