95-702 Organizational Communications and Distributed Object Technologies Message Oriented Middleware Homework 6 JMS on Glassfish Due: No due date Optional exercise. Not for credit. Do not submit. ================================================= 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"%> Using a message driven bean is fun.

Messages entered travel over HTTP to a servlet.

The servlet writes the message to a queue

The onMessage method of an MDB is called by the queue.

Enter a message
We need a servlet to collect the text from the browser and deliver it to the Message Driven Bean. In the Project View, select MyCoolJEEProject-war. Right click and choose New Servlet. The servlet name is MyCoolServlet. The package name is mycoolservlet. Choose Next through to Finish. Replace code in the default servlet with the following: protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); try { Context ctx = new InitialContext(); ConnectionFactory cf = (ConnectionFactory)ctx.lookup("jms/myCoolConnectionFactory"); Queue q = (Queue)ctx.lookup("jms/myCoolQueue"); Connection con = cf.createConnection(); Session session = con.createSession(false, Session.AUTO_ACKNOWLEDGE); MessageProducer writer = session.createProducer(q); TextMessage msg = session.createTextMessage(); String val = request.getParameter("simpleTextMessage"); msg.setText(val); writer.send(msg); con.close(); out.println("

Wrote "+val + " to queue

"); out.println(""); System.out.println("Servlet sent " + val + " to queue"); } catch(Exception e){ System.out.println("Servlet through exception "+ e); } finally { out.close(); } } From the Project View, right click MyCoolJEEProject and select deploy. Run a browser and visit http://localhost:8080/MyCoolJEEProject-war/ Part Two Homework ================= (1) Write a message driven bean that takes a java text message off a JMS queue and computes a cryptographically secure message digest. The message driven bean will write the digest back to a second queue. It is required that you use the Java Crypto API to compute a SHA-1 hash. Note that when performing a synchronous read from a queue, you need to execute the start method on the connection object. Also note that these queues are persistent. The contents of the queue will survive server restarts. (2) Provide a web service front end to your message driven bean. The web service will write text messages to the queue and wait for the MDB to respond. The web service will return the digest wrapped in a SOAP document. It will provide one method with the following signature: public String doHash(String textToHash); (3) Provide a web front-end to this application. In other words, provide a JSP page and a servlet that allows browser users to compute message digests using your message driven bean. The servlet will make calls on the web service that you wrote in part 2. (4) Provide an application client that interacts with the user via the console. It too will read a string and use the web service to compute the hash. Submission ========== No submission for this optional exercise ======================================== Submit to the digital blackboard a zip file containing your projects.