This post describes usage of Jboss JMS Feature using a Message Driven Bean (MDB) consumer.
Note: In this article , we would be using the default JMS queue ‘/queue/DLQ’.
1. Create a Message Driven Bean with bean class TestMDB.java ****************************************************************************
****************************************************************************
2. Generate the ejb-jar.xml as below.
****************************************************************************
<?xml version=”1.0″ encoding=”UTF-8″?>
<ejb-jar
xmlns=”http://java.sun.com/xml/ns/j2ee”
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
xsi:schemaLocation=”http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/ejb-jar_2_1.xsd”
version=”2.1″>
<enterprise-beans>
<message-driven>
<ejb-name>TestMDB</ejb-name>
<ejb-class>TestMDB</ejb-class>
<transaction-type>Container</transaction-type>
<activation-config>
<activation-config-property>
<activation-config-property-name>destinationType</activation-config-property-name>
<activation-config-property-value>javax.jms.Queue</activation-config-property-value>
</activation-config-property>
</activation-config>
</message-driven>
</enterprise-beans>
</ejb-jar>
Note: Specify the ejb bean class name in the attribute <ejb-class> of the element <message-driven>.
3. Generate the jboss.xml to specify the name of the Jboss Queue, which the MDB bean would be listening to.
****************************************************************************
<?xml version=”1.0″ encoding=”Cp1252″?>
<jboss>
<enterprise-beans>
<message-driven>
<ejb-name>TestMDB</ejb-name>
<configuration-name>Standard Message Driven Bean</configuration-name>
<destination-jndi-name>/queue/DLQ</destination-jndi-name>
</message-driven>
</enterprise-beans>
</jboss>
****************************************************************************
4. Create a jar file as below.
jar -cvf TestMDB.jar .
5. Copy the jar file and paste it under Deploy folder of any server profiles.
$JBOSS_HOME\server\default\deploy
The deployment is automatically picked up by the server and you would see the relevant messages in the server logs and the console.

****************************************************************************
21:09:18,968 INFO [EjbDeployer] installing bean: ejb/#TestMDB,uid6458583
21:09:18,968 INFO [EjbDeployer] with dependencies:
21:09:18,968 INFO [EjbDeployer] and supplies:
21:09:18,968 INFO [EjbDeployer] jndi:null
21:09:19,046 INFO [EjbModule] Deploying TestMDB
****************************************************************************
6. Open a command prompt and set the class path to the jbossall-client.jar file.
set classpath=%classpath%;G:\jboss-5.1.0.GA\client\jbossall-client.jar
7. Compile and execute the below QueueSend.java program ****************************************************************************
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Hashtable;
import javax.jms.*;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import java.util.Properties;
public class QueueSend
{
public final static String JNDI_FACTORY=”org.jboss.naming.HttpNamingContextFactory”;
public final static String JMS_FACTORY=”ConnectionFactory”;
public final static String QUEUE=”/queue/DLQ”;
private QueueConnectionFactory qconFactory;
private QueueConnection qcon;
private QueueSession qsession;
private QueueSender qsender;
private Queue queue;
private TextMessage msg;
public void init(Context ctx, String queueName)
throws NamingException, JMSException
{
qconFactory = (QueueConnectionFactory) ctx.lookup(JMS_FACTORY);
qcon = qconFactory.createQueueConnection();
qsession = qcon.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
queue = (Queue) ctx.lookup(queueName);
qsender = qsession.createSender(queue);
msg = qsession.createTextMessage();
qcon.start();
}
public void send(String message) throws JMSException {
msg.setText(message);
qsender.send(msg);
}
public void close() throws JMSException {
qsender.close();
qsession.close();
qcon.close();
}
public static void main(String[] args) throws Exception {
if (args.length != 1) {
System.out.println(“Usage: java examples.jms.queue.QueueSend JBossURL”);
return;
}
InitialContext ic = getInitialContext(args[0]);
QueueSend qs = new QueueSend();
qs.init(ic, QUEUE);
readAndSend(qs);
qs.close();
}
private static void readAndSend(QueueSend qs)
throws IOException, JMSException
{
BufferedReader msgStream = new BufferedReader(new InputStreamReader(System.in));
String line=null;
boolean quitNow = false;
do {
System.out.print(“Enter message (\”quit\” to quit): \n”);
line = msgStream.readLine();
if (line != null && line.trim().length() != 0) {
qs.send(line);
System.out.println(“JMS Message Sent: “+line+”\n”);
quitNow = line.equalsIgnoreCase(“quit”);
}
} while (! quitNow);
}
private static InitialContext getInitialContext(String url)
throws NamingException
{
Properties env = new Properties();
env.setProperty(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY);
env.setProperty(Context.PROVIDER_URL,url);
return new InitialContext(env);
}
}
****************************************************************************
java QueueSend http://127.0.0.1:8080/invoker/JNDIFactory
The deployed MDB will consume the messages from the Jboss Queue as shown below.
For reference I have uploaded the sample TestMDB.jar to the below link.
http://www.4shared.com/file/TjO6i_DR/TestMDB.html
Best Regards,
Wonders


Post a Comment