95-702 Organizational Communications and Distributed Object Technologies Message Oriented Middleware Homework 6 JMS on Glassfish Due: August 7, 2009 This homework has two parts. Part one consists of a short tutorial on writing and running a JMS application using Netbeans and Glassfish. Part two consists of a small JMS exercise involving message digests and JMS messaging. Part One Tutorial ================= Glassfish comes with Sun Message Queue. Sun Message Queue is a JMS provider. First, using the Glassfish adminstrative 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. Messages coming out of the Queue may be read synchronously with a receive method call or asynchronously by implementing a message listener. A message driven bean may also be used to handle incoming, asynchronous messages. Set up ====== We need to establish a connection factory and a queue. Run Netbeans Choose Services/Servers/Right Click and start Glassfish V2. View the administration console of Glassfish V2. Expand Resources/JMS Resources. Select Connection Factories and select New from the menu. Enter the JNDI Name:jms/myCoolConnectionFactory. From the drop down list, select the type javax.jms.ConnectionFactory Enter a short description. Click OK. Under JMS Resources, select Destination Resources. Select New from the menu. Enter the JNDI Name:jms/myCoolQueue. Enter the Physical Destination Name:myCoolQueue. From the drop down list, select the type javax.jms.Queue Enter a short description. Click OK. Build an application. We need to construct a web component and a Message driven bean. Close the browser that interacts with the administration server. Return to Netbeans and choose Projects. Select File/New Project Select Java EE and Enterprise Application. The project name is MyCoolJEEProject. Create an EJB Module and a Web Application Module. Click finish. We need to populate the EJB module with a Message Driven Bean. From the Project View, Right Click MyCoolJEEProject-ejb. Select New Message Driven Bean. The EJB Name is MyCoolMDB and the package name is mycoolmdb Select the server destination as jms/myCoolQueue. Select Finish and you should see a default Message Driven Bean. Modify the onMesssage method with the following. Note that you will need to add imports for TextMessage and JMSException. public void onMessage(Message message) { try { if(message instanceof TextMessage) { TextMessage tm = (TextMessage)message; System.out.println(tm.getText()); } else { System.out.println("I don't handle messages of this ttype"); } } catch(JMSException e){ System.out.println("JMS Exception thrown"+e); } catch(Throwable e) { System.out.println("Throwable thrown"+e); } } We need to build a web application that sends messages to the queue. In the Project View, expand the MyCoolJEEProject-war. Expand Web Pages and double click index.jsp. We want to have an introduction page that makes calls to a servlet. Change the page to read as follows: <%@page contentType="text/html" pageEncoding="UTF-8"%>