Hi All,

Jay SenSharma

Jay SenSharma

Here is a Example of MDB 3.0 which gets deployed on WebLogic Server 10 and above and starts consuming messages from javax.jms.Queue . This Example demonstrates us, How we can develop MDB3.0 for Oracle WebLogic Application Server.

For More details on MDB3.0 Standard Annotations which makes our MDB Application container specific API independent please refer to the following link:   MDB With Non-Container-Specific-Annotations

Step 1). first of all we need to develop  the MDB class like below

package ejb30;
import javax.ejb.MessageDriven;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
import javax.ejb.ActivationConfigProperty;

activationConfig = {
@ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue") ,
@ActivationConfigProperty(propertyName="destinationJndiName", propertyValue="TestQ")

public class MyMDB implements MessageListener
public void onMessage(Message message)
TextMessage textMessage = (TextMessage) message;
try {
System.out.println("nnt(mdb) MyMDB Received n"+ textMessage.getText());
catch (JMSException e)

Step 2). Compile the Above MDB class and then create a JAR out of it.

javac    –d    .   MyMDB.java

jar   -cvf     mdb30.jar   ejb30     MyMDB.java

Step 3). Now Logic to WebLogic Admin console and create a ConnectionFactory with JNDI Name “QCF” and a Queue with JNDI Name “TestQ”.

Step 4). Deploy the MDB Jar (mdb30) file on the Server.

Step 5). Now start Writing the Client Queue Sender Program which is going to send some JMS messages to the TestQ and as a listener the MDB is going to consume these JMS messages.

import java.io.IOException;
import java.util.Hashtable;

import javax.jms.JMSException;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class QueueSend
// Defines the JNDI context factory.
public final static String JNDI_FACTORY="weblogic.jndi.WLInitialContextFactory";

// Defines the JMS context factory.
public final static String JMS_FACTORY="QCF";

// Defines the queue.
public final static String QUEUE="TestQ";

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();

public void send(String message,int counter) throws JMSException {
msg.setIntProperty("counter", counter);

public void close() throws JMSException {

public static void main(String[] args) throws Exception {
if (args.length != 1) {
System.out.println("Usage: java QueueSend WebLogicURL");
InitialContext ic = getInitialContext(args[0]);
QueueSend qs = new QueueSend();
qs.init(ic, QUEUE);

private static void readAndSend(QueueSend qs)    throws IOException, JMSException
String line="Test Message Body with counter = ";
for(int i=0;i<10;i++){
System.out.println("JMS Message Sent: "+line+i+"n");

private static InitialContext getInitialContext(String url)    throws NamingException
Hashtable env = new Hashtable();
env.put(Context.PROVIDER_URL, url);
return new InitialContext(env);


Step 6). Compile the above program and then run it.

javac        –d     .       QueueSend.java

java    QueueSend   t3://localhost:7001

Step 7). Now in Admin Console Go to the Monitoring Tab of the JMS Queue and see how many messages are getting consumed by MDB and how many messages are pending in the queue.


Jay SenSharma

If you enjoyed this post, please considerleaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.