Create Message Driven Bean for Queue destination and send message from web application

Here I am creating Enterprise Application with EJB and Web Module. In the EJB module create Message Driven Bean (MDB) for Queue. And send message from Web Application.The client message displays on Server clonsole.

Below are steps to create Message Driven Bean on Server :

  • Install Oracle WebLogic Server 10.3.2
  • Create Domain on Orcale WebLogic Server 10.3.2
  • Configure Domain in Eclipse
  • Create administrator objects for Queue on Server
  • Create Enterprise Application Project "EJB3MDBQueue.ear". with EJB and Web Module
  • Create Message Driven Bean
  • Create user interface and Servlet to send message
  • Run application and send message

Step 1: Install Oracle WebLogic Server 10.3.2 [ Click here - How to install WebLogic Server ]

Step 2: Create Domain on Orcale WebLogic Server 10.3.2 [ Click here - How to create Domain on WebLogic Server ]

Step 3: Configure Domain in Eclipse [ Click here - How to Configure Domain in Eclipse ]

Step 4: Create administrator objects for Queue on Server [ Click here - How to create JMS administrator objects on Server]

Step 5: Create Enterprise Application Project with EJB and Web Module
File -> New -> Enterprise Appliction Project
Create Enterprise Project
Specify the Enterprise Project name EJB3MDBQueue and click Next.

Below screen is used to create EJB and Web module in Enterprise project. Click New Module
MDB Queue

Check EJB Module and Web Module and Click Finish.
MDB Queue

You will get below screen and Click Finish
MDB Queue
This step will create Enterprise Project with EJB and Web project.

Step 6: Create Message Driven Bean
Create MDB : MessageQueue1.java
package techmyguru.mdb;
import javax.annotation.Resource;
import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.ejb.MessageDrivenContext;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

@MessageDriven(activationConfig = 
@ActivationConfigProperty(propertyName = "destinationType"
propertyValue = "javax.jms.Queue") }, mappedName = "qjndi1")
public class MessageQueue1 implements MessageListener {
@Resource
private MessageDrivenContext mdc;

@Override
public void onMessage(Message inMessage) {
  System.out.println("onMessage method");
  TextMessage msg = null;
  try {
  if (inMessage instanceof TextMessage) {
  msg = (TextMessageinMessage;
  System.out.println("MDB Queue1: " + msg.getText());
  else {
  System.out.println("Message of wrong type: "
  + inMessage.getClass().getName());
  }
  catch (JMSException e) {
    e.printStackTrace();
    mdc.setRollbackOnly();
  catch (Throwable te) {
    te.printStackTrace();
  }
}
}

Step 7: Create user interface and Servlet to send message
User interface : index.jsp
"-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">


MDB Queue Example


 Queue Demo


"QueueSenderServlet">
Enter your message "text" name="msg">
"submit" value="Send Message">



Servlet : QueueSenderServlet.java
package techmyguru.web;
// all imports
public class QueueSenderServlet extends HttpServlet 
{
private static final long serialVersionUID = 1L;

@Resource(mappedName = "cfjndi")
private QueueConnectionFactory queueconnectionFactory;

@Resource(mappedName = "qjndi1")
private Queue queue;

protected void doGet(HttpServletRequest request,HttpServletResponse response)
 throws ServletException, IOException {
QueueConnection queueconnection = null;
QueueSession qsession = null;
TextMessage message = null;
PrintWriter pw = response.getWriter();
pw.println("Message seding from Web client
"
);
try {
  queueconnection = queueconnectionFactory.createQueueConnection();
  qsession = queueconnection.createQueueSession(false,Session.AUTO_ACKNOWLEDGE);
  QueueSender qp = qsession.createSender(queue);
  queueconnection.start();
  TextMessage msg1 = qsession.createTextMessage();
  msg1.setText(request.getParameter("msg"));
  qp.send(msg1);
  pw.println("To see message go to server console
"

       "Try Again");
catch (JMSException e
{
        System.out.println("Exception occurred: " + e.toString());
finally {
  if (queueconnection != null) {
  try {
  queueconnection.close();
  catch (JMSException e) {
  e.printStackTrace();
  }
  }
}
}
}

Step 8: Run application and send message
Verify the project directory structure :


Run Enterprise application and get output :


Type message and click Send Message button and see server console.

Previous Download Source Code Next


Write your Comment

Name :
Email Id : [ We will not publish your email id ]
Your Comment : [ Maximum 200 chars ]
Enter Security code