Message bean demonstration


Mbean.java
package com.message;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
@MessageDriven(mappedName = “jms/dest”, activationConfig = {
@ActivationConfigProperty(propertyName = “acknowledgeMode”, propertyValue = “Auto-acknowledge”),
@ActivationConfigProperty(propertyName = “destinationType”, propertyValue = “javax.jms.Queue”)
})
public class MBean implements MessageListener {

public MBean() {
}
public void onMessage(Message message) {
TextMessage t = null;
t = (TextMessage)message;
try {
System.out.println(t.getText());
} catch (JMSException ex) {
Logger.getLogger(MBean.class.getName()).log(Level.SEVERE, null, ex);
}
}
}

index.jsp :-
<%@page contentType=”text/html” pageEncoding=”UTF-8″%>
<html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″>
<title>Test Page</title>
</head>
<body>
<form action=”NewServlet”>
<table border=”1″>
<thead>
</thead>
<tbody>
<tr>
<td>Your Message :</td>
<td><input type=”text” name=”msg” value=”” size=”50″ /></td>
</tr>
</tbody>
</table>
<input type=”submit” value=”Submit” />
</form>
</body>
</html>

NewServlet.java :-
package com.web;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Resource;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class NewServlet extends HttpServlet {
@Resource(name = “jms/dest”)
private Queue dest;
@Resource(name = “jms/queue”)
private ConnectionFactory queue;
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType(“text/html;charset=UTF-8”);
PrintWriter out = response.getWriter();
String str = request.getParameter(“msg”);
try {
sendJMSMessageToDest(str);
} catch (JMSException ex) {
Logger.getLogger(NewServlet.class.getName()).log(Level.SEVERE, null, ex);
}
try {

out.println(“<html>”);
out.println(“<head>”);
out.println(“<title>Servlet NewServlet</title>”);
out.println(“</head>”);
out.println(“<body>”);
out.println(“<h1>Your Message ” + str + ” has been sent please check logs !</h1>”);
out.println(“</body>”);
out.println(“</html>”);

} finally {
out.close();
}
}
private Message createJMSMessageForjmsDest(Session session, Object messageData) throws JMSException {
TextMessage tm = session.createTextMessage();
tm.setText(messageData.toString());
return tm;
}
private void sendJMSMessageToDest(Object messageData) throws JMSException {
Connection connection = null;
Session session = null;
try {
connection = queue.createConnection();
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer messageProducer = session.createProducer(dest);
messageProducer.send(createJMSMessageForjmsDest(session, messageData));
} finally {
if (session != null) {
try {
session.close();
} catch (JMSException e) {
Logger.getLogger(this.getClass().getName()).log(Level.WARNING, “Cannot close session”, e);
}
}
if (connection != null) {
connection.close();
}
}
}
}