Simple-JMS.docx - Andrew.cmu.edu

moodusroundoΛογισμικό & κατασκευή λογ/κού

15 Αυγ 2012 (πριν από 4 χρόνια και 10 μήνες)

299 εμφανίσεις

95
-
702 Distributed Systems

Lab
Exercise



Simple
JMS

GlassFish comes with the JMS Provider,
Open Message
Queue

(OpenMQ)
. First, using the GlassFish
administrative server, we need to administratively establish a ConnectionFactory

and a Queue. JNDI (the Java
Naming and Directory Interface) will be used to get access to these administrated objects.

Set up

a ConnectionFactory and Queue

1)

Run Netbeans

2)

Choose Services/Servers/Right Click and
start

GlassFish v3 Domain.

3)

Once GlassFish is r
unning, r
ight
-
click again on GlassFish v3 Domain and choose “View Admin Console”

a)

T
he default login is “admin”

b)

The default password is “adminadmin”

c)

Within the admin console, e
xpand Resources/JMS Resources.

d)

Select Connection Factories

i)

S
elect New from the me
nu.

ii)

Enter the Pool Name:

jms/myConnectionFactory.



It is a convention to name JMS resources as jms/someResourceName

iii)

From the drop down list, select the type javax.jms.ConnectionFactory

iv)

Click OK.

e)

Under JMS Resources, select Destination Resources.

i)

Select New
from the menu.

ii)

Ent
er the JNDI Name:

jms/my
Queue
One
.

iii)

Enter the
Physical Destination Name:

my
Queue
One
.



The JNDI name is the string used to lookup the physical destination queue in the message broker
(MOM).

iv)

From the drop down list, select the type javax.jms.Q
ueue

v)

Click OK.

f)

You can now
leave

of the admin console.

Build an application



a web component and a Message Driven bean

4)

Return to Netbeans and choose Projects.

5)

Select File/New Project

a)

Select Java EE and Enterprise Application.

b)

The project name is My
JMS
Project
, and click next
.

c)

Create an EJB Module and a Web Application Module.

d)

Click finish.

6)

Populate the EJB module with a Message Driven Bean.

a)

From the Project View, Right Click
My
JMS
Project
-
ejb.

b)

Select New Message Driven Bean.


-

The EJB Name is My
QueueL
istener and the package name is edu.cmu.heinz.ds.queuelistener

c)

Select the server
destination as jms/myQueueOne
.


-

Notice that this is the
JNDI name of the
queue that you created earlier.

d)

Select Finish and you should see a default Message Driven Bean.

7)

Repl
ace the
My
QueueListener
code with the code found in:

http://andrew.cmu
.edu/course/95
-
702/homework/Spring2011
/Project6/
My
QueueListener
.java

8)

Build a web application that sends messages to the queue.

a)

In the Project View, expand the
My
JMS
Project
-
war.

b)

Expand We
b Pages and double click index.jsp.

c)

Replace index.jsp with the code found in:

http://andrew.cmu.edu/course/95
-
702/homework/
Spring2011
/Project6/index.jsp

9)

Create
a
servlet to collect the text from the browser and deliver it to

t
he Message Driven Bean.

a)

In the Project View, select
My
JMS
Project
-
war.

b)

Right click and choose New Servlet.

c)

T
he servlet name is MyQueueWriter
.

d)

The package name is
edu.cmu.heinz.ds.
my
queuewriter
.

e)

Choose Next

f)

Select "Add information to deployment descriptor (web.xml)" and then

Finis
h.

g)

Replace MyQueueWriter
.java with the code found in:

http://andrew.cmu.edu/course/95
-
702/homework/
Spring2011
/
P
roject6/My
QueueWriter
.java

10)

From the Project View, right click
My
JMS
Project

and select deploy.

11)

Run a browser and visit http://localhost:8080/
My
JMS
Project
-
war/



Substitute the correct port number if your GlassFish does not use 8080

12)

Check your GlassFish console and verify the message you typed was receive by MyQueueListener.