Jboss mdb example
if you decide to use jboss as your application server
components used
jboss-4.0.5.GA
eclipse 3.2
xdoclet 1.2
setup eclipse to use jboss application server and configure xdoclet
use eclipse to create mdb (MessageBean.java)
things to add to mdb (MessageBean.java)
* @jboss.destination-jndi-nameor use
* name="queue/DelMdbQueue"
* destination-jndi-name="Message"add code in onMessage
public void onMessage(javax.jms.Message message) {
// begin-user-code
System.out.println("Message Driven Bean got message " + message);
// TODO: do business logic here
System.out.println("mdb doing business logic");
}
create application client (can use dynamic web project) put lib files into web-inf/lib
commons-logging-1.0.3.jar
concurrent-1.3.4.jar
concurrent.jar
gnu-regexp.jar
JavaGroups.jar
jboss-j2ee.jar
jboss-messaging-client.jar
jboss-spine.jar
jboss.jar
jbossmq.jar
jms-1.1.jar
jmxtools.jar
jndi.jar
jnpserver.jar
log4j.jar
openjms-0.7.7-beta-1.jar
openjms-common-0.7.7-beta-1.jar
openjms-net-0.7.7-beta-1.jar
openjms-tools-0.7.7-beta-1.jar
spice-jndikit-1.2.jar
create MessageTest class
package com.mycompany.test;
import java.util.Hashtable;
import javax.jms.ObjectMessage;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.naming.InitialContext;
import javax.naming.NamingException;
public class MessageTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
MessageTest mdbean = new MessageTest ();
mdbean.testMDBBean() ;
}
private InitialContext getContext() throws NamingException {
//using jboss version
Hashtable props = new Hashtable();
props.put(
InitialContext.INITIAL_CONTEXT_FACTORY,
"org.jnp.interfaces.NamingContextFactory");
props.put(InitialContext.PROVIDER_URL, "jnp://127.0.0.1:1099");
InitialContext initialContext = new InitialContext(props);
return initialContext;
}
public void testMDBBean() {
try {
System.out.println("Looking up the factory ");
InitialContext ctx = getContext();
QueueConnectionFactory factory = (QueueConnectionFactory) ctx.lookup("QueueConnectionFactory");
//using jboss version
System.out.println("Looking up the queue ");
Queue queue = (Queue) ctx.lookup("queue/Message");
System.out.println("Creating the connection ");
QueueConnection connection = factory.createQueueConnection();
System.out.println("Creating the session ");
QueueSession session = connection.createQueueSession(true, 1);
System.out.println("Creating the sender ");
QueueSender sender = session.createSender(queue);
ObjectMessage message = session.createObjectMessage();
System.out.println ("Setting the object in message ");
message.setObject("di object");
System.out.println ("Sending the message ");
sender.send(message);
System.out.println ("Shuting down");
session.commit();
session.close();
connection.close();
System.out.println ("Done ! ");
} catch (Exception e) {
e.printStackTrace();
}
}
}
note the use of "queue/Message" and "QueueConnectionFactory"
Comments