An EJB application that demonstrates MDB (with appropriate business logic)
package mybeans;
Step 1: Project Application Creation
New -> New Folder -> Java
EE -> Enterprise Application -> Name the application as you wish ->
set Java EE Version as Java EE 5 -> Finish
Step 2: Creating connection factory and
destination.
Select services window -> Start Glassfish Server
-> right click on glassfishserver -> View admin console module -> JMS
Resources -> Connection Factories -> New -> Set Pool name as jms/Queue
and Resouce type as QueueConnectionFactory.Destination Resources -> New
-> Set JNDI name as jms/dest and Resource type as queue -> rest can be
anything
Step 3: Message Driven Bean Creation
Right click on ejb module ->
New -> Message Driven Bean -> Name the program ->Select destination as
Server Destination (with the destination created by you in admin console) -> Finish Type the following code under onMeassage
method
TextMessage tmsg=null;
tmsg=(TextMessage)message;
System.out.println(tmsg.getText());
Left Click the error icon on
first line and select “Add import for javax.jms.TextMessage;
Left
click the error icon on third line and select “Surround the statement with
try-catch”
Step 4: Servlet file creation
Right click source Packages
under war module -> New -> Servlet ->
Name the servlet -> next -> finish Right
click on the first line inside the class definition -> Insert code ->Send
JMS message -> Select Message Driven bean -> set Connection Factory as
“jms/queue”
Type the following code in
process request method
String
msg=request.getParameter("msg");
sendJMSMessageToDest(msg);
Left click the error icon on
third line and select “Surround the statement
with try-catch”
Step 5: JSP file Creation
Create a jsp file with one label
and one text box.
Step 6: Running the project
Clean and Bulid the Application
module -> Deploy the ejb module -> Right click Application module select
run
You can see the message
displayed on server which is sent by you.
//Lab12a.java in ejb module:
package
lab12;
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/dest2", activationConfig = {
@ActivationConfigProperty(propertyName
= "acknowledgeMode", propertyValue =
"Auto-acknowledge"),
@ActivationConfigProperty(propertyName = "destinationType",
propertyValue = "javax.jms.Queue")
})
public
class Lab12a implements MessageListener {
public
Lab12a() {
}
@Override
public
void onMessage(Message message) {
TextMessage tmsg=null;
tmsg=(TextMessage)message;
try
{
System.out.println(tmsg.getText());
} catch (JMSException ex) {
Logger.getLogger(Lab12a.class.getName()).log(Level.SEVERE,
null, ex);
}
}
}
//index.jsp
<%@page
contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE
html>
<html>
<head>
<meta
http-equiv="Content-Type" content="text/html;
charset=UTF-8">
<title>Lab
12 Message Driven Bean </title>
</head>
<body>
<form
action="mdbServlet" >
<h1>
Message Driven Bean </h1>
Enter the Message: <input type="textbox"
name="message" size="80"/><br/>
<input
type="submit" name="Send"/>
<input
type="reset" name="clear" value="clear">
</form>
</body>
</html>
//mdbServlet.java
package
lab12;
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 mdbServlet extends HttpServlet {
@Resource(mappedName
= "jms/dest2")
private
Queue dest2;
@Resource(mappedName
= "jms/qe2")
private
ConnectionFactory qe2;
protected
void processRequest(HttpServletRequest request, HttpServletResponse response)
throws
ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try
{
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet
mdbServlet</title>");
out.println("</head>");
out.println("<body>");
String
msg=request.getParameter("message");
try
{
sendJMSMessageToDest2(msg);
} catch (JMSException ex) {
Logger.getLogger(mdbServlet.class.getName()).log(Level.SEVERE,
null, ex);
}
out.println("</body>");
out.println("</html>");
} finally {
out.close();
}
}
@Override
protected
void doGet(HttpServletRequest request, HttpServletResponse response)
throws
ServletException, IOException {
processRequest(request,
response);
}
@Override
protected
void doPost(HttpServletRequest request, HttpServletResponse response)
throws
ServletException, IOException {
processRequest(request,
response);
}
@Override
public
String getServletInfo() {
return
"Short description";
}
private
Message createJMSMessageForjmsDest2(Session session, Object messageData) throws
JMSException {
TextMessage tm =
session.createTextMessage();
tm.setText(messageData.toString());
return
tm;
}
private
void sendJMSMessageToDest2(Object messageData) throws JMSException {
Connection connection = null;
Session session = null;
try
{
connection
= qe2.createConnection();
session
= connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer messageProducer =
session.createProducer(dest2);
messageProducer.send(createJMSMessageForjmsDest2(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();
}
}
}
}
No comments:
Post a Comment