Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Lab 9-Messaging: Objective

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 6

Lab 9- Messaging

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:

 Then, click on Finish.


 In the project navigator, you will see three projects have been created:
EnterpriseApplicationMessage, EnterpriseApplicationMessage-ejb,
EnterpriseApplicationMessage-war
 EnterpriseApplicationMessage contains all packages including war and jar files as well as
application descriptor xml file
 We need to create “Destination” in Glassfish. Note that the destination is where all
messages are stored by “Producer” and delivered to “Consumer”. To create
“Destination”, right click on EnterpriseApplicationMessage-ejb, go to “New->Other…”.
 From “Categories:” menu from left select “GlassFish” and from “File Types:” select
“JMS Resource” and click “Next”.
 Note that the next page should be like the following image. If it is different change items
to make it the same. For this lab, we make “Queue” (a point to point) messaging system.

 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.

 EnterpriseApplicationMessage-ejb contains all EJB files. In order to develop your EJB


application, first you should open this project. Then, right-click on Source packages, then
select New>Message-Driven Bean

 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.

 Now, you should implement onMessage method.


 Enter the following code for “onMessage”.

@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.

Task 2: Developing EJB client using NetBeans


 Open the EnterpriseApplicationMessage-war project, delete “index.html” and create
“index.jsp” using right click and “New”.

Add the following code to the index.jsp

<%@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");

Queue queue = (Queue) jndiContext.lookup("jms/myQueue");

QueueConnection queueConnection
= queueConnectionFactory.createQueueConnection();
QueueSession queueSession =
queueConnection.createQueueSession(false,
Session.AUTO_ACKNOWLEDGE);

QueueSender queueSender =
queueSession.createSender(queue);

TextMessage message = queueSession.createTextMessage();

for (int i = 0; i < 100; i++) {


message.setText("This is message " + (i + 1));
System.out.println("Sending message: " +
message.getText());
queueSender.send(message);
}

%>
</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.

You might also like