- Introduction to The Architecture
- JMS and the Web
- A Sample Application Using JMS Through a Web Client
- A Producer Web Client Application
- A Consumer Web Client Application
- Conclusion
A Consumer Web Client Application
A web consumer client works like a stand-alone client. It can receive a message from a queue or subscribe to a message from a topic. In Listing 2, I provide a web consumer client.
When you run the web client in Listing 2, the JSP file reads the message from the queue type destination "queue_Montreal" and displays some informative messages about the progress of receiving the message.
In the JSP part of the client application, the page import directive must include classes for a JMS application such as javax.naming.*, javax.rmi.*, java.util.*, and javax.jms.*.
The JMS part of the application consists of six parts:
Creating a JNDI InitialContext object
Looking up a connection factory and a queue
Creating connection, or session
Creating a receiver, and then starting message delivery
Receiving all text messages from the queue
Closing the connection
Listing 2 The Receiver Web Client Source Code
1. <%@page contentType="text/html"%> 2. <%@ page import= "javax.naming.*, javax.rmi.*, java.util.*, javax.jms.*" %> 3. <html> 4. <head><title> 5. This JSP Page receives a message from the queue without using a bean 6. </title></head> 7. <body> 8. <% 9. out.println("This JavaServer Page as a web component RECEIVES a message from a queue"); 10. out.println("<BR>"); 11. out.println("to demonstrate how you can combine JMS with web components"); 12. out.println("<BR>"); 13. out.println("<BR>"); 14. 15. // Creating JMS object variables 16. String cityQueueName = "queue_Montreal"; 17. Context jndiContext = null; 18. QueueConnectionFactory queueConnectionFactory = null; 19. QueueConnection queueConnection = null; 20. QueueSession queueSession = null; 21. Queue queue = null; 22. QueueReceiver queueReceiver = null; 23. TextMessage message = null; 24. 25 out.println("Queue name is " + cityQueueName); 26. out.println("<BR>"); 27. 28. // Creating a JNDI InitialContext object 29. try { 30. jndiContext = new InitialContext(); 31. } catch (NamingException e) { 32. out.println("Error while creating JNDI context: " + e.toString()); 33. } 34. 35. // Looking up connection factory and queue 36. try { 37. queueConnectionFactory = (QueueConnectionFactory) 38. jndiContext.lookup("QueueConnectionFactory"); 39. queue = (Queue) jndiContext.lookup(cityQueueName); 40. } catch (NamingException e) { 41. out.println("JNDI lookup queue failed: " + e.toString()); 42. } 43. 44. // Creating connection, session (not transacted) 45. try { 46. queueConnection = 47. queueConnectionFactory.createQueueConnection(); 48. queueSession = 49. queueConnection.createQueueSession(false, 50. Session.AUTO_ACKNOWLEDGE); 51. // Creating receiver, then starting message delivery 52. queueReceiver = queueSession.createReceiver(queue); 53. queueConnection.start(); 54. out.println("Ready to receive the message "); 55. out.println("<BR>"); 56. 57. // Receiving all text messages from the queue 58. Message m = queueReceiver.receiveNoWait(); 59. if (m instanceof TextMessage) { 60. message = (TextMessage) m; 61. out.println("Reading message from : '" + cityQueueName + "' "); 62. out.println("<BR>"); 63. out.println(message.getText()); 64. out.println("<BR>"); 65. out.println("Finished reading message "); 66. } else { 67. 68. // Stop receiving when the message is end of stream from the queue 69. out.println("There is no proper message from the queue"); 70. out.println("<BR>"); 71. out.println("Finished reading message "); 72. out.println("<BR>"); 73. } 74. } catch (JMSException e) { 75. System.out.println("Error while receiving the message: " + e.toString()); 76. } finally { 77. 78. // closing the connection 79. if (queueConnection != null) { 80. try { 81. queueConnection.close(); 82. } catch (JMSException e) {} 83. } //end of if 84. } //end of finally 85. %> 86. </body> 87. </html>
Running a Receiver Web Client
To run the JSP file on the JMS server side, you must start your JMS server. Refer to the "Running a Sender Web Client" section to obtain information on starting the JMS server. The queue type destination ("queue_Montreal" for this example) must have already been created administratively while sending the message. For a receiver application, you do not need to re-create the destination.
After you have successfully started the JMS server, you can call the JSP file from a web browser on your computer. The JSP file application provided in Listing 2 is a very simple file. It immediately reads a message from the destination whenever you call the JSP on the web server. It waits for a message delivery for 5000 milliseconds to avoid blocking the server's resources. This example is for demonstration purposes only. Depending on your project, you can modify and enhance this sample application.
Figure 3 illustrates the result of calling the sample web consumer client application if there is a message in the queue. If there is no message in the queue, calling the JSP file will display some informative messages as illustrated in Figure 4.
Figure 3 Running a web consumer client if there is a message in the queue.
Figure 4 Running a web consumer client if there is no message in the queue.