Lab 9-Messaging: Objective
Lab 9-Messaging: Objective
Lab 9-Messaging: Objective
Objective
The purpose of this lab is to develop an EJB application: Message Driven Bean
Description
Task 1: Developing EJB using NetBeans
In this lab, you will develop a very simple EJB application, and then implement an EJB client
which is a JSP file in this case.
To do this , create a new project, from categories select Java EE and from Projects, select
Enterprise Application
Give a name to the Ejb project, MessageEJB.
Click on Next. Select the following options:
Click on “Next”. In the next page, we set the queue properties. The only property we set
here is “Name”. Select “Name” as the name of the property from menu an in the “Value”
field type myQueue and press enter. If you don’t enter key it gives you an error message.
Click “Finish” to end the wizard.
Give a name to your Ejb such as Messaging, type a package like msg and select
“Project Destinations:”, and enter “jms/myQueue”. This the name of the destination
in the JNDI.
Click “Next” and then “Finish”.
When, you click on Finish, you will see one class file: one EJB Message-Driven
Bean
Now, you should implement their methods. For this, double click on the bean,
MessagingBean.
@Override
public void onMessage(Message message) {
TextMessage msg = null;
try {
if (message instanceof TextMessage) {
msg = (TextMessage) message;
System.out.println("MESSAGE BEAN: Message received: "
+ msg.getText());
} else {
System.out.println("Message of wrong type: "
+ message.getClass().getName());
}
} catch (JMSException e) {
e.printStackTrace();
} catch (Throwable te) {
te.printStackTrace();
}
}
So far, you have developed your EJB application. Now, you should implement the
client. Client in this lab is a JSP file. For this, do the Task2.
<%@page import="javax.jms.TextMessage"%>
<%@page import="javax.jms.QueueSender"%>
<%@page import="javax.jms.Session"%>
<%@page import="javax.jms.QueueSession"%>
<%@page import="javax.jms.QueueConnection"%>
<%@page import="javax.jms.Queue"%>
<%@page import="javax.jms.QueueConnectionFactory"%>
<%@page import="javax.naming.InitialContext"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=UTF-8">
<title>Message Driven Bean Page Tester</title>
</head>
<body>
<p> Check the Glassfish output and try to explain what is
going on ....</p>
<%
InitialContext jndiContext = new InitialContext();
QueueConnectionFactory queueConnectionFactory
= (QueueConnectionFactory)
jndiContext.lookup("jms/__defaultConnectionFactory");
QueueConnection queueConnection
= queueConnectionFactory.createQueueConnection();
QueueSession queueSession =
queueConnection.createQueueSession(false,
Session.AUTO_ACKNOWLEDGE);
QueueSender queueSender =
queueSession.createSender(queue);
%>
</body>
</html>
Now run the application. Check the Glassfish output in Netbeans and try to explain the
output.
Deliverables
When your program is complete, show it to your instructor.