{"id":395,"date":"2018-07-27T11:39:47","date_gmt":"2018-07-27T11:39:47","guid":{"rendered":"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/?post_type=chapter&#038;p=395"},"modified":"2018-08-02T08:57:03","modified_gmt":"2018-08-02T08:57:03","slug":"jms-ii","status":"publish","type":"chapter","link":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/chapter\/jms-ii\/","title":{"rendered":"JMS &#8211; II"},"content":{"raw":"<strong>Learning Objectives<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">In the previous module, we learnt about the JMS Messaging Model, Communication Model and Programming Models and some introduction about the JMS Messages. In this module, let us continue the exploration of JMS Messages, its types, formats and properties along with the illustration of a small JMS Sender and Receiver application. Also, we are going to learn the significance and need of Model View Controller.<\/p>\r\n\r\n<ol>\r\n \t<li><strong>JMS \u2013 Messages<\/strong><\/li>\r\n<\/ol>\r\n<p style=\"text-align: justify\">JMS application produces and consumes message.In the earlier module, we have seen about the five different types of messages along with a session object to create a message. Now, let us look through the functionality of JMS Message Producer and Message Consumer.<\/p>\r\n&nbsp;\r\n\r\n<strong>1.1 JMS \u2013 MessageProducer<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">A Message Producer is an object created by a session and is used for sending messages to a destination.MessageProducer comes in two forms by implementing either the QueueSender or the TopicPublisher interface as shown in Figure 1.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: center\"><img class=\"size-full wp-image-396 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-255.png\" alt=\"\" width=\"451\" height=\"205\" \/><\/p>\r\n\r\n<div>\r\n<p style=\"text-align: justify\">Here, we use the Session object to create a message producer.The following snippet shows how to create a message producer using either a queue sender or a topic publisher interface.<\/p>\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">MessageProducer producer =session.createProducer (destination)<\/span><\/p>\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">QueueSender queueSender = queueSession.createSender(queue);<\/span><\/p>\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">TopicPublisher topicPublisher = topicSession.createPublisher(topic);<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">Once a message producer has been created, we can use it to send messages as shown below:<\/span><\/p>\r\n\r\n<\/div>\r\n<div>\r\n\r\n\u00a0 \u00a0 \u00a0queueSender.send(message);\r\n\r\ntopicPublisher.publish(message);\r\n\r\n&nbsp;\r\n\r\n<strong>1.2 JMS \u2013 MessageConsumer<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">A Message Consumer is an object that is created by a session and used for receiving messages sent to a destination. It is created by impleemnting the MessageConsumer interface.A message consumer allows a JMS client to register interest in a destination with a JMS provider. The JMS provider manages the delivery of messages from a destination to the registered consumers of the destination.The PTP form of message consumer implements the QueueReceiver interface and the publish\/subscribe form implements the TopicSubscriber interface.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">For example, you may use a QueueSession to create a receiver for the queue myQueue, and you may use a TopicSession to create a subscriber for the topic myTopic as given below:<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">QueueReceiverqueueReceiver = queueSession.createReceiver(myQueue); TopicSubscribertopicSubscriber = topicSession.createSubscriber(myTopic); (OR)<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">TopicSubscribertopicSubscriber = TopicSession.createDurableSubscriber(myTopic); Once a message consumer has been created, it becomes active, and we can use it to<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">receive messages. With either a QueueReceiver or a TopicSubscriber, receive method can be used to consume a message synchronously. This method can be invoked at any time after calling the start method as given below:<\/p>\r\n&nbsp;\r\n\r\nqueueConnection.start();\r\n\r\nMessage m = queueReceiver.receive( );\r\n\r\n(OR)\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">topicConnection.start();<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">Message m = topicSubscriber.receive(1000); \/\/ time out after a seccond.<\/span>\r\n\r\n&nbsp;\r\n\r\n<span style=\"text-align: justify;font-size: 1em\">In order to consume a message asynchronously, a message listener need to be used. In synchronous message consumption, receive method can be used to consume a message synchronously. Similarly, this can be invoked at any time after calling the start method:<\/span>\r\n\r\n<\/div>\r\n<div>\r\n\r\n&nbsp;\r\n\r\nqueueConnection.start();\r\n\r\nMessage m = queueReceiver.receive();\r\n\r\ntopicConnection.start();\r\n\r\nMessage m = topicSubscriber.receive(1000); \/\/ time out after a second\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">In <strong><em>Asynchronous message consumption,<\/em><\/strong> message listeners and message selectors objects are included. A message listener is an object that acts as an asynchronous event handler for messages.This implements MessageListener Interface, which has only one method \u201conMessage(Message msg)\u201d. MessageListener is not specific to a particular destination. The \u201conMessage\u201d method is responsible for handling all exceptions too. The code snippet for setting a message listener for Topic is given below:<\/p>\r\n&nbsp;\r\n\r\nTopicListenertopicListener = new TopicListener(); topicSubscriber.setMessageListener(topicListener);\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Message selectors are used for filtering out the messages received by the application.Message selectors assign the work of filtering messages to the JMS provider rather than to the application.A message selector is a SQL92 String that contains an expression.A message selector cannot select messages on the basis of the content of the message body. Examples for Message Selector is given below:<\/p>\r\n&nbsp;\r\n\r\n<strong>Examples:<\/strong>\r\n\r\n&nbsp;\r\n\r\nphone LIKE \u2018223\u2019\r\n\r\nJMSType IS NOT NULL\r\n\r\nname = \u2018Tom\u2019\r\n\r\n&nbsp;\r\n\r\n<strong>1.3 JMS \u2013 Message Format<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">The ultimate purpose of a JMS application is to produce and consume messages that can then be used by other software applications.JMS messages have a basic format that is simple but\u00a0<span style=\"text-align: initial;font-size: 1em\">highly flexible, allowing the users to create messages that match formats used by non-JMS applications on heterogeneous platforms.A JMS message consists of three parts:<\/span><\/p>\r\n\r\n<\/div>\r\n<ul>\r\n \t<li>Header consists of predefined fields<\/li>\r\n \t<li>Properties (optional) consisting additional fields<\/li>\r\n \t<li>Body (optional) - actual message content<\/li>\r\n<\/ul>\r\n<p style=\"text-align: justify\"><strong>1.4 JMS \u2013 Message Header: <\/strong>A JMS message header contains a number of predefined fields that contain the values used by both clients and providers to identify and route messages. For example, every message has a unique identifier, which is represented in the header field as JMSMessageID. The value of another header field, JMSDestination, represents the queue or the topic to which the message is sent. Other fields include a timestamp and a priority level.Each header field has associated setter and getter methods. Table 1 shows the different header fields used in JMS Message Header with their description and the methods associated with it for setting the corresponding information.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: center\"><img class=\"size-full wp-image-397 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-256.png\" alt=\"\" width=\"650\" height=\"745\" \/><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\"><strong>1.5 JMS \u2013 Message Properties: <\/strong>This message property consists of optional fields which are set by the client before the message is sent. Some properties are predefined (prefix JMSX). E.g. JMSXUserId specifies the user about sending the messages. It can be evaluated by message selectors. The values can be of different types such asboolean, byte, short, int, float, double, and String. The JMS API provides some predefined property names that a provider can support. The use of these predefined properties or of user-defined properties is optional.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\"><strong>1.6 JMS \u2013 Message Bodies: <\/strong>The JMS API defines five message body formats, also called message types. They are listed in Table 2.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: center\"><img class=\"size-full wp-image-398 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-257.png\" alt=\"\" width=\"593\" height=\"460\" \/><\/p>\r\n&nbsp;\r\n<div>\r\n<p style=\"text-align: justify\"><strong>1.7 JMS \u2013 Message Type: <\/strong>The JMS API provides methods for creating messages of different types and for filling in their contents. For example, to create and send a TextMessage, the following statements can be used:TextMessage message = queueSession.createTextMessage(); message.setText(msg_text); \/\/ msg_text is a String queueSender.send(message);<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">At the consuming end, a message gets arrived as a generic Message object and must be cast to the appropriate message type. Either one or more getter methods can be used to extract the message contents. The following code fragment demonstrates the usage of the getText method for receiving message:<\/p>\r\n&nbsp;\r\n\r\nMessage m = queueReceiver.receive();\r\n\r\nif (m instanceofTextMessage) {\r\n\r\nTextMessage message = (TextMessage) m; System.out.println(\"Reading message:\"\u00a0 + message.getText());\r\n\r\n}\r\n\r\nelse {\r\n\r\n\/\/\u00a0 Handle error\r\n\r\n}\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\"><strong>1.8 JMS \u2013 Queue Browsers:<\/strong> The messages that are sent to a queue remain in the queue until the message consumer for that queue consumes them. The JMS API provides a QueueBrowser object that allows to browse the messages in the queue and display the header values for each message. To create a QueueBrowser object, Session.createBrowser method can be used. For example:<\/p>\r\n\r\n<\/div>\r\nQueueBrowser browser = session.createBrowser(queue);\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\"><strong>1.9 JMS \u2013 Exception Handling: <\/strong>The root class for exceptions thrown by JMS API methods is JMSException. Catching JMSException provides a generic way of handling all exceptions related to the JMS API.The JMSException class includes the following subclasses as listed below, described in the API documentation:<\/p>\r\n&nbsp;\r\n\r\nIllegalStateException\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 InvalidClientIDException\r\n\r\n&nbsp;\r\n\r\nInvalidDestinationException\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 InvalidSelectorException\r\n\r\n&nbsp;\r\n\r\nJMSSecurityException\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 MessageEOFException\r\n\r\n&nbsp;\r\n\r\nMessageFormatException\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 MessageNotReadableException\r\n\r\n&nbsp;\r\n\r\nMessageNotWriteableException\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 ResourceAllocationException\r\n\r\n&nbsp;\r\n\r\nTransactionInProgressException\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 TransactionRolledBackException\r\n\r\n&nbsp;\r\n\r\n1.10\u00a0<strong>JMS \u2013 Queue:<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">To develop JMS queue, anyone of the application server needs to be installed. Here, a glassfish3 server has been used for creating two JNDI. First connection factory named \u201cmyQueueConnectionFactory\u201d need to be created and a destination resource named \u201cmyQueue\u201d has to be created. After creating JNDI, server and receiver applications need to be created. Later, server and receiver has to be executed in different consoles. The steps involved in developing a JMS Queue has been explained below:<\/p>\r\n&nbsp;\r\n\r\n<strong>1)\u00a0\u00a0 Create connection factory and destination resource<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">As a first step, server admin console need to be opened using the URL <a href=\"http:\/\/localhost:4848\/\">http:\/\/localhost:4848 <\/a>(localhost with port number)<strong>.<\/strong> Later, login process need to be completed with the username and passwordfollowed by clicking on the JMS Resource -&gt; Connection Factories -&gt; New. Next to it, pool name has to be written and the Resource Type has to be selected as QueueConnectionFactory as shown in Figure 2.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: center\"><img class=\"size-full wp-image-400 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-259.png\" alt=\"\" width=\"523\" height=\"363\" \/><\/p>\r\n<p style=\"text-align: center\">Figure 2. New JMS Connection Factory establishment using Glass Fish Server.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">Subsequently, on the JMS Resource -&gt; Destination Resources -&gt; New has to be clicked, and the JNDI name and physical destination name has to be written as shown in Figure 3.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: center\"><img class=\"size-full wp-image-401 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-260.png\" alt=\"\" width=\"447\" height=\"255\" \/><\/p>\r\n&nbsp;\r\n<div>\r\n\r\n<strong>\u00a0 \u00a0 2) Create sender and receiver application<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Next to it, Sender and Receiver code has been developed by using the QueueConnection Factory, Queue Connection, Session, Sender and Receiver Objects explained earlier in the previous module and in this module. Note that the Receiver is attached with listener which will be invoked only when the user sends message.During the execution, the receiver class has to be\u00a0<span style=\"text-align: initial;font-size: 1em\">executed first followed by the Sender class. An example for Sender and Receiver application for sending\/receiving messages using all the objects explained earlier has been shown below:<\/span><\/p>\r\n\r\n<\/div>\r\n<div>\r\n\r\n<strong>\u00a0 \u00a0 File: MySender.java<\/strong>\r\n\r\n&nbsp;\r\n\r\nimportjava.io.BufferedReader;\r\n\r\nimportjava.io.InputStreamReader;\r\n\r\nimportjavax.naming.*;\r\n\r\nimport javax.jms.*;\r\n\r\npublic class MySender {\r\n\r\npublic static void main(String[] args)\r\n\r\n{\r\n\r\ntry\r\n\r\n{ \/\/Create and start connection InitialContextctx=new InitialContext();\r\n\r\nQueueConnectionFactoryf=(QueueConnectionFactory)ctx.lookup (\"myQueueConnectionFactory\");\r\n\r\nQueueConnection con=f.createQueueConnection(); con.start();\r\n\r\n\/\/2) create queue session\r\n\r\nQueueSessionses=con.createQueueSession(false, Session.AUTO_ACKNOWLEDGE); \/\/3) get the Queue object\r\n\r\nQueue t=(Queue)ctx.lookup(\"myQueue\"); \/\/4)create QueueSender object\r\n\r\nQueueSender sender=ses.createSender(t); \/\/5) create TextMessage object TextMessagemsg=ses.createTextMessage(); \/\/6) write message\r\n\r\nBufferedReader b=new BufferedReader(new InputStreamReader(System.in)); while(true)\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">{<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">System.out.println(\"Enter Msg, end to terminate:\");<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">String s=b.readLine();<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">if (s.equals(\"end\"))<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">break;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">msg.setText(s);<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">\/\/7) send message<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">sender.send(msg);<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">System.out.println(\"Message successfully sent.\");<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">\/\/8) connection close<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">con.close();<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}catch(Exception e){System.out.println(e);}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span><span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<\/div>\r\n<div>\r\n\r\n<strong>\u00a0 \u00a0 File: MyReceiver.java<\/strong>\r\n\r\n&nbsp;\r\n\r\nimport javax.jms.*;\r\n\r\nimportjavax.naming.InitialContext;\r\n\r\npublic class MyReceiver {\r\n\r\npublic static void main(String[] args)\r\n\r\n{\r\n\r\ntry{\r\n\r\n\/\/1) Create and start connection\r\n\r\nInitialContextctx=new InitialContext();\r\n\r\nQueueConnectionFactoryf=(QueueConnectionFactory)ctx.lookup(\"myQueueC onnectionFactory\");\r\n\r\nQueueConnection con=f.createQueueConnection(); con.start();\r\n\r\n\/\/2) create Queue session\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">QueueSessionses=con.createQueueSession(false, Session.AUTO_ACKNOWLEDGE); \/\/3) get the Queue object<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">Queue t=(Queue)ctx.lookup(\"myQueue\");<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">\/\/4)create QueueReceiver<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">QueueReceiver receiver=ses.createReceiver(t);<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">\/\/5) create listener object<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">MyListener listener=new MyListener();<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">\/\/6) register the listener object with receiver<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">receiver.setMessageListener(listener);<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">System.out.println(\"Receiver1 is ready, waiting for messages...\");<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">System.out.println(\"press Ctrl+c to shutdown...\");<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">while(true){<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">Thread.sleep(1000);<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}catch(Exception e){System.out.println(e);}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">} }<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">File: MyListener.java<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">import javax.jms.*;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">public class MyListener implements MessageListener { public void onMessage(Message m) { try{<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">TextMessagemsg=(TextMessage)m;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">System.out.println(\"following message is received:\"+msg.getText()); }catch(JMSException e){System.out.println(e);}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n&nbsp;\r\n\r\n<\/div>\r\n<div>\r\n\r\n<strong>\u00a0 \u00a0 2.<\/strong>\u00a0<strong>Introduction to Design Patterns<\/strong>\r\n\r\n<\/div>\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">A pattern is a proven solution to a problem in a context.Christopher Alexander says that each pattern is a three-part rule which expresses a relation between a certain context, a problem, and a solution. Design patterns represents a solutions to problems that arise when developing software within a particular context i.e. Patterns = problems-solution pairs in a context<\/span><\/p>\r\n\r\n<div>\r\n\r\n<strong>\u00a0<\/strong>\r\n\r\n<strong> 2.1 Details of MVC Design Pattern<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Name (essence of the pattern)- Model View Controller MVC Context (where does this problem occur)- MVC is an architectural pattern that is used when developing interactive application such as a shopping cart on the Internet.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">Problem (definition of the reoccurrence) -User interfaces change often, especially on the internet where look-and-feel is a competitive issue. Also, the same information is presented in different ways. The core business logic and data is stable.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">One of the most common Design Patterns is Model-View-Controller (MVC). The model does all the computational work. It is input\/output free and all communication with the model is done via methods. In a typical application we will find these three fundamental parts in Model View Controller architecture:<\/p>\r\n&nbsp;\r\n\r\nData (Model) \u2013 This is used to model the data.\r\n\r\n&nbsp;\r\n\r\nAn interface to view and modify the data (View) \u2013 User Interface design to display and manipulate the data.\r\n\r\n&nbsp;\r\n\r\nOperations that can be performed on the data (Controller) \u2013 This is used to operate for the data.\r\n\r\n&nbsp;\r\n\r\nThe pictorial representation of the Model View Controller design is shown in the figure4.\r\n\r\n<\/div>\r\n<p style=\"text-align: center\"><img class=\"size-full wp-image-402 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-261.png\" alt=\"\" width=\"380\" height=\"228\" \/><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">The controller tells the model what to do and the user input goes to the controller. The view shows results which is a \u201cwindow\u201d into the model. The view can get results from the controller or the view can get results directly from the model (where the data resides).<\/p>\r\n&nbsp;\r\n\r\n<strong>Summary<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Therefore, through this module, we have learnt the JMS Messages along with the components of JMS Message Producer, Consumer, Message types, formats and its body. Also, we have learnt a JMS application of File Sender and Receiver which sends\/receives JMS Messages. In addition to this, we also studied the need of Design patterns and Model view controller in this module.<\/p>\r\n&nbsp;\r\n\r\n<strong>Web Links<\/strong>\r\n\r\n&nbsp;\r\n<ul>\r\n \t<li style=\"text-align: justify\">http:\/\/www-sop.inria.fr\/members\/Francoise.Baude\/AppRep\/JMS.pdf<\/li>\r\n \t<li style=\"text-align: justify\">http:\/\/theopentutorials.com\/tutorials\/java-ee\/ejb3\/mdb\/java-message-service-jms-api\/<\/li>\r\n \t<li style=\"text-align: justify\">http:\/\/www.computing.dcu.ie\/~mcrane\/CA4006\/CA4006%20Lecture%206%20Message%20Pas sing%20in%20Distd%20Systems.pdf<\/li>\r\n \t<li style=\"text-align: justify\">https:\/\/www.novell.com\/documentation\/extend5\/Docs\/help\/MP\/jms\/tutorial\/<\/li>\r\n \t<li style=\"text-align: justify\">http:\/\/www.indigoo.com\/dox\/wsmw\/1_Middleware\/JMS.pdf<\/li>\r\n \t<li style=\"text-align: justify\">http:\/\/www-sop.inria.fr\/members\/Francoise.Baude\/AppRep\/JMS.pdf<\/li>\r\n \t<li style=\"text-align: justify\"><a href=\"https:\/\/www.tutorialspoint.com\/jsp\/\">https:\/\/www.tutorialspoint.com\/jsp\/<\/a><\/li>\r\n \t<li style=\"text-align: justify\">Mark Richards, Richard Monson-Haefel, AndDavid A. Chappell,\u201c Java Message Service\u201d,Second Edition, O\u2019Reilly Media,2009.<\/li>\r\n<\/ul>","rendered":"<p><strong>Learning Objectives<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">In the previous module, we learnt about the JMS Messaging Model, Communication Model and Programming Models and some introduction about the JMS Messages. In this module, let us continue the exploration of JMS Messages, its types, formats and properties along with the illustration of a small JMS Sender and Receiver application. Also, we are going to learn the significance and need of Model View Controller.<\/p>\n<ol>\n<li><strong>JMS \u2013 Messages<\/strong><\/li>\n<\/ol>\n<p style=\"text-align: justify\">JMS application produces and consumes message.In the earlier module, we have seen about the five different types of messages along with a session object to create a message. Now, let us look through the functionality of JMS Message Producer and Message Consumer.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>1.1 JMS \u2013 MessageProducer<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">A Message Producer is an object created by a session and is used for sending messages to a destination.MessageProducer comes in two forms by implementing either the QueueSender or the TopicPublisher interface as shown in Figure 1.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-396 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-255.png\" alt=\"\" width=\"451\" height=\"205\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-255.png 451w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-255-300x136.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-255-65x30.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-255-225x102.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-255-350x159.png 350w\" sizes=\"auto, (max-width: 451px) 100vw, 451px\" \/><\/p>\n<div>\n<p style=\"text-align: justify\">Here, we use the Session object to create a message producer.The following snippet shows how to create a message producer using either a queue sender or a topic publisher interface.<\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">MessageProducer producer =session.createProducer (destination)<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">QueueSender queueSender = queueSession.createSender(queue);<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">TopicPublisher topicPublisher = topicSession.createPublisher(topic);<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">Once a message producer has been created, we can use it to send messages as shown below:<\/span><\/p>\n<\/div>\n<div>\n<p>\u00a0 \u00a0 \u00a0queueSender.send(message);<\/p>\n<p>topicPublisher.publish(message);<\/p>\n<p>&nbsp;<\/p>\n<p><strong>1.2 JMS \u2013 MessageConsumer<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">A Message Consumer is an object that is created by a session and used for receiving messages sent to a destination. It is created by impleemnting the MessageConsumer interface.A message consumer allows a JMS client to register interest in a destination with a JMS provider. The JMS provider manages the delivery of messages from a destination to the registered consumers of the destination.The PTP form of message consumer implements the QueueReceiver interface and the publish\/subscribe form implements the TopicSubscriber interface.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">For example, you may use a QueueSession to create a receiver for the queue myQueue, and you may use a TopicSession to create a subscriber for the topic myTopic as given below:<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">QueueReceiverqueueReceiver = queueSession.createReceiver(myQueue); TopicSubscribertopicSubscriber = topicSession.createSubscriber(myTopic); (OR)<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">TopicSubscribertopicSubscriber = TopicSession.createDurableSubscriber(myTopic); Once a message consumer has been created, it becomes active, and we can use it to<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">receive messages. With either a QueueReceiver or a TopicSubscriber, receive method can be used to consume a message synchronously. This method can be invoked at any time after calling the start method as given below:<\/p>\n<p>&nbsp;<\/p>\n<p>queueConnection.start();<\/p>\n<p>Message m = queueReceiver.receive( );<\/p>\n<p>(OR)<\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">topicConnection.start();<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">Message m = topicSubscriber.receive(1000); \/\/ time out after a seccond.<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"text-align: justify;font-size: 1em\">In order to consume a message asynchronously, a message listener need to be used. In synchronous message consumption, receive method can be used to consume a message synchronously. Similarly, this can be invoked at any time after calling the start method:<\/span><\/p>\n<\/div>\n<div>\n<p>&nbsp;<\/p>\n<p>queueConnection.start();<\/p>\n<p>Message m = queueReceiver.receive();<\/p>\n<p>topicConnection.start();<\/p>\n<p>Message m = topicSubscriber.receive(1000); \/\/ time out after a second<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">In <strong><em>Asynchronous message consumption,<\/em><\/strong> message listeners and message selectors objects are included. A message listener is an object that acts as an asynchronous event handler for messages.This implements MessageListener Interface, which has only one method \u201conMessage(Message msg)\u201d. MessageListener is not specific to a particular destination. The \u201conMessage\u201d method is responsible for handling all exceptions too. The code snippet for setting a message listener for Topic is given below:<\/p>\n<p>&nbsp;<\/p>\n<p>TopicListenertopicListener = new TopicListener(); topicSubscriber.setMessageListener(topicListener);<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Message selectors are used for filtering out the messages received by the application.Message selectors assign the work of filtering messages to the JMS provider rather than to the application.A message selector is a SQL92 String that contains an expression.A message selector cannot select messages on the basis of the content of the message body. Examples for Message Selector is given below:<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Examples:<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>phone LIKE \u2018223\u2019<\/p>\n<p>JMSType IS NOT NULL<\/p>\n<p>name = \u2018Tom\u2019<\/p>\n<p>&nbsp;<\/p>\n<p><strong>1.3 JMS \u2013 Message Format<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The ultimate purpose of a JMS application is to produce and consume messages that can then be used by other software applications.JMS messages have a basic format that is simple but\u00a0<span style=\"text-align: initial;font-size: 1em\">highly flexible, allowing the users to create messages that match formats used by non-JMS applications on heterogeneous platforms.A JMS message consists of three parts:<\/span><\/p>\n<\/div>\n<ul>\n<li>Header consists of predefined fields<\/li>\n<li>Properties (optional) consisting additional fields<\/li>\n<li>Body (optional) &#8211; actual message content<\/li>\n<\/ul>\n<p style=\"text-align: justify\"><strong>1.4 JMS \u2013 Message Header: <\/strong>A JMS message header contains a number of predefined fields that contain the values used by both clients and providers to identify and route messages. For example, every message has a unique identifier, which is represented in the header field as JMSMessageID. The value of another header field, JMSDestination, represents the queue or the topic to which the message is sent. Other fields include a timestamp and a priority level.Each header field has associated setter and getter methods. Table 1 shows the different header fields used in JMS Message Header with their description and the methods associated with it for setting the corresponding information.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-397 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-256.png\" alt=\"\" width=\"650\" height=\"745\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-256.png 650w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-256-262x300.png 262w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-256-65x75.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-256-225x258.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-256-350x401.png 350w\" sizes=\"auto, (max-width: 650px) 100vw, 650px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\"><strong>1.5 JMS \u2013 Message Properties: <\/strong>This message property consists of optional fields which are set by the client before the message is sent. Some properties are predefined (prefix JMSX). E.g. JMSXUserId specifies the user about sending the messages. It can be evaluated by message selectors. The values can be of different types such asboolean, byte, short, int, float, double, and String. The JMS API provides some predefined property names that a provider can support. The use of these predefined properties or of user-defined properties is optional.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\"><strong>1.6 JMS \u2013 Message Bodies: <\/strong>The JMS API defines five message body formats, also called message types. They are listed in Table 2.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-398 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-257.png\" alt=\"\" width=\"593\" height=\"460\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-257.png 593w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-257-300x233.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-257-65x50.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-257-225x175.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-257-350x272.png 350w\" sizes=\"auto, (max-width: 593px) 100vw, 593px\" \/><\/p>\n<p>&nbsp;<\/p>\n<div>\n<p style=\"text-align: justify\"><strong>1.7 JMS \u2013 Message Type: <\/strong>The JMS API provides methods for creating messages of different types and for filling in their contents. For example, to create and send a TextMessage, the following statements can be used:TextMessage message = queueSession.createTextMessage(); message.setText(msg_text); \/\/ msg_text is a String queueSender.send(message);<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">At the consuming end, a message gets arrived as a generic Message object and must be cast to the appropriate message type. Either one or more getter methods can be used to extract the message contents. The following code fragment demonstrates the usage of the getText method for receiving message:<\/p>\n<p>&nbsp;<\/p>\n<p>Message m = queueReceiver.receive();<\/p>\n<p>if (m instanceofTextMessage) {<\/p>\n<p>TextMessage message = (TextMessage) m; System.out.println(&#8220;Reading message:&#8221;\u00a0 + message.getText());<\/p>\n<p>}<\/p>\n<p>else {<\/p>\n<p>\/\/\u00a0 Handle error<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\"><strong>1.8 JMS \u2013 Queue Browsers:<\/strong> The messages that are sent to a queue remain in the queue until the message consumer for that queue consumes them. The JMS API provides a QueueBrowser object that allows to browse the messages in the queue and display the header values for each message. To create a QueueBrowser object, Session.createBrowser method can be used. For example:<\/p>\n<\/div>\n<p>QueueBrowser browser = session.createBrowser(queue);<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\"><strong>1.9 JMS \u2013 Exception Handling: <\/strong>The root class for exceptions thrown by JMS API methods is JMSException. Catching JMSException provides a generic way of handling all exceptions related to the JMS API.The JMSException class includes the following subclasses as listed below, described in the API documentation:<\/p>\n<p>&nbsp;<\/p>\n<p>IllegalStateException\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 InvalidClientIDException<\/p>\n<p>&nbsp;<\/p>\n<p>InvalidDestinationException\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 InvalidSelectorException<\/p>\n<p>&nbsp;<\/p>\n<p>JMSSecurityException\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 MessageEOFException<\/p>\n<p>&nbsp;<\/p>\n<p>MessageFormatException\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 MessageNotReadableException<\/p>\n<p>&nbsp;<\/p>\n<p>MessageNotWriteableException\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 ResourceAllocationException<\/p>\n<p>&nbsp;<\/p>\n<p>TransactionInProgressException\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 TransactionRolledBackException<\/p>\n<p>&nbsp;<\/p>\n<p>1.10\u00a0<strong>JMS \u2013 Queue:<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">To develop JMS queue, anyone of the application server needs to be installed. Here, a glassfish3 server has been used for creating two JNDI. First connection factory named \u201cmyQueueConnectionFactory\u201d need to be created and a destination resource named \u201cmyQueue\u201d has to be created. After creating JNDI, server and receiver applications need to be created. Later, server and receiver has to be executed in different consoles. The steps involved in developing a JMS Queue has been explained below:<\/p>\n<p>&nbsp;<\/p>\n<p><strong>1)\u00a0\u00a0 Create connection factory and destination resource<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">As a first step, server admin console need to be opened using the URL <a href=\"http:\/\/localhost:4848\/\">http:\/\/localhost:4848 <\/a>(localhost with port number)<strong>.<\/strong> Later, login process need to be completed with the username and passwordfollowed by clicking on the JMS Resource -&gt; Connection Factories -&gt; New. Next to it, pool name has to be written and the Resource Type has to be selected as QueueConnectionFactory as shown in Figure 2.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-400 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-259.png\" alt=\"\" width=\"523\" height=\"363\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-259.png 523w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-259-300x208.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-259-65x45.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-259-225x156.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-259-350x243.png 350w\" sizes=\"auto, (max-width: 523px) 100vw, 523px\" \/><\/p>\n<p style=\"text-align: center\">Figure 2. New JMS Connection Factory establishment using Glass Fish Server.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Subsequently, on the JMS Resource -&gt; Destination Resources -&gt; New has to be clicked, and the JNDI name and physical destination name has to be written as shown in Figure 3.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-401 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-260.png\" alt=\"\" width=\"447\" height=\"255\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-260.png 447w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-260-300x171.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-260-65x37.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-260-225x128.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-260-350x200.png 350w\" sizes=\"auto, (max-width: 447px) 100vw, 447px\" \/><\/p>\n<p>&nbsp;<\/p>\n<div>\n<p><strong>\u00a0 \u00a0 2) Create sender and receiver application<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Next to it, Sender and Receiver code has been developed by using the QueueConnection Factory, Queue Connection, Session, Sender and Receiver Objects explained earlier in the previous module and in this module. Note that the Receiver is attached with listener which will be invoked only when the user sends message.During the execution, the receiver class has to be\u00a0<span style=\"text-align: initial;font-size: 1em\">executed first followed by the Sender class. An example for Sender and Receiver application for sending\/receiving messages using all the objects explained earlier has been shown below:<\/span><\/p>\n<\/div>\n<div>\n<p><strong>\u00a0 \u00a0 File: MySender.java<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>importjava.io.BufferedReader;<\/p>\n<p>importjava.io.InputStreamReader;<\/p>\n<p>importjavax.naming.*;<\/p>\n<p>import javax.jms.*;<\/p>\n<p>public class MySender {<\/p>\n<p>public static void main(String[] args)<\/p>\n<p>{<\/p>\n<p>try<\/p>\n<p>{ \/\/Create and start connection InitialContextctx=new InitialContext();<\/p>\n<p>QueueConnectionFactoryf=(QueueConnectionFactory)ctx.lookup (&#8220;myQueueConnectionFactory&#8221;);<\/p>\n<p>QueueConnection con=f.createQueueConnection(); con.start();<\/p>\n<p>\/\/2) create queue session<\/p>\n<p>QueueSessionses=con.createQueueSession(false, Session.AUTO_ACKNOWLEDGE); \/\/3) get the Queue object<\/p>\n<p>Queue t=(Queue)ctx.lookup(&#8220;myQueue&#8221;); \/\/4)create QueueSender object<\/p>\n<p>QueueSender sender=ses.createSender(t); \/\/5) create TextMessage object TextMessagemsg=ses.createTextMessage(); \/\/6) write message<\/p>\n<p>BufferedReader b=new BufferedReader(new InputStreamReader(System.in)); while(true)<\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">{<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">System.out.println(&#8220;Enter Msg, end to terminate:&#8221;);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">String s=b.readLine();<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">if (s.equals(&#8220;end&#8221;))<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">break;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">msg.setText(s);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">\/\/7) send message<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">sender.send(msg);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">System.out.println(&#8220;Message successfully sent.&#8221;);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">\/\/8) connection close<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">con.close();<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}catch(Exception e){System.out.println(e);}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<\/div>\n<div>\n<p><strong>\u00a0 \u00a0 File: MyReceiver.java<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>import javax.jms.*;<\/p>\n<p>importjavax.naming.InitialContext;<\/p>\n<p>public class MyReceiver {<\/p>\n<p>public static void main(String[] args)<\/p>\n<p>{<\/p>\n<p>try{<\/p>\n<p>\/\/1) Create and start connection<\/p>\n<p>InitialContextctx=new InitialContext();<\/p>\n<p>QueueConnectionFactoryf=(QueueConnectionFactory)ctx.lookup(&#8220;myQueueC onnectionFactory&#8221;);<\/p>\n<p>QueueConnection con=f.createQueueConnection(); con.start();<\/p>\n<p>\/\/2) create Queue session<\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">QueueSessionses=con.createQueueSession(false, Session.AUTO_ACKNOWLEDGE); \/\/3) get the Queue object<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">Queue t=(Queue)ctx.lookup(&#8220;myQueue&#8221;);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">\/\/4)create QueueReceiver<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">QueueReceiver receiver=ses.createReceiver(t);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">\/\/5) create listener object<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">MyListener listener=new MyListener();<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">\/\/6) register the listener object with receiver<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">receiver.setMessageListener(listener);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">System.out.println(&#8220;Receiver1 is ready, waiting for messages&#8230;&#8221;);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">System.out.println(&#8220;press Ctrl+c to shutdown&#8230;&#8221;);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">while(true){<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">Thread.sleep(1000);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}catch(Exception e){System.out.println(e);}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">} }<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">File: MyListener.java<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">import javax.jms.*;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">public class MyListener implements MessageListener { public void onMessage(Message m) { try{<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">TextMessagemsg=(TextMessage)m;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">System.out.println(&#8220;following message is received:&#8221;+msg.getText()); }catch(JMSException e){System.out.println(e);}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p>&nbsp;<\/p>\n<\/div>\n<div>\n<p><strong>\u00a0 \u00a0 2.<\/strong>\u00a0<strong>Introduction to Design Patterns<\/strong><\/p>\n<\/div>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">A pattern is a proven solution to a problem in a context.Christopher Alexander says that each pattern is a three-part rule which expresses a relation between a certain context, a problem, and a solution. Design patterns represents a solutions to problems that arise when developing software within a particular context i.e. Patterns = problems-solution pairs in a context<\/span><\/p>\n<div>\n<p><strong>\u00a0<\/strong><\/p>\n<p><strong> 2.1 Details of MVC Design Pattern<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Name (essence of the pattern)- Model View Controller MVC Context (where does this problem occur)- MVC is an architectural pattern that is used when developing interactive application such as a shopping cart on the Internet.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Problem (definition of the reoccurrence) -User interfaces change often, especially on the internet where look-and-feel is a competitive issue. Also, the same information is presented in different ways. The core business logic and data is stable.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">One of the most common Design Patterns is Model-View-Controller (MVC). The model does all the computational work. It is input\/output free and all communication with the model is done via methods. In a typical application we will find these three fundamental parts in Model View Controller architecture:<\/p>\n<p>&nbsp;<\/p>\n<p>Data (Model) \u2013 This is used to model the data.<\/p>\n<p>&nbsp;<\/p>\n<p>An interface to view and modify the data (View) \u2013 User Interface design to display and manipulate the data.<\/p>\n<p>&nbsp;<\/p>\n<p>Operations that can be performed on the data (Controller) \u2013 This is used to operate for the data.<\/p>\n<p>&nbsp;<\/p>\n<p>The pictorial representation of the Model View Controller design is shown in the figure4.<\/p>\n<\/div>\n<p style=\"text-align: center\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-402 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-261.png\" alt=\"\" width=\"380\" height=\"228\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-261.png 380w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-261-300x180.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-261-65x39.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-261-225x135.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-261-350x210.png 350w\" sizes=\"auto, (max-width: 380px) 100vw, 380px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The controller tells the model what to do and the user input goes to the controller. The view shows results which is a \u201cwindow\u201d into the model. The view can get results from the controller or the view can get results directly from the model (where the data resides).<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Summary<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Therefore, through this module, we have learnt the JMS Messages along with the components of JMS Message Producer, Consumer, Message types, formats and its body. Also, we have learnt a JMS application of File Sender and Receiver which sends\/receives JMS Messages. In addition to this, we also studied the need of Design patterns and Model view controller in this module.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Web Links<\/strong><\/p>\n<p>&nbsp;<\/p>\n<ul>\n<li style=\"text-align: justify\">http:\/\/www-sop.inria.fr\/members\/Francoise.Baude\/AppRep\/JMS.pdf<\/li>\n<li style=\"text-align: justify\">http:\/\/theopentutorials.com\/tutorials\/java-ee\/ejb3\/mdb\/java-message-service-jms-api\/<\/li>\n<li style=\"text-align: justify\">http:\/\/www.computing.dcu.ie\/~mcrane\/CA4006\/CA4006%20Lecture%206%20Message%20Pas sing%20in%20Distd%20Systems.pdf<\/li>\n<li style=\"text-align: justify\">https:\/\/www.novell.com\/documentation\/extend5\/Docs\/help\/MP\/jms\/tutorial\/<\/li>\n<li style=\"text-align: justify\">http:\/\/www.indigoo.com\/dox\/wsmw\/1_Middleware\/JMS.pdf<\/li>\n<li style=\"text-align: justify\">http:\/\/www-sop.inria.fr\/members\/Francoise.Baude\/AppRep\/JMS.pdf<\/li>\n<li style=\"text-align: justify\"><a href=\"https:\/\/www.tutorialspoint.com\/jsp\/\">https:\/\/www.tutorialspoint.com\/jsp\/<\/a><\/li>\n<li style=\"text-align: justify\">Mark Richards, Richard Monson-Haefel, AndDavid A. Chappell,\u201c Java Message Service\u201d,Second Edition, O\u2019Reilly Media,2009.<\/li>\n<\/ul>\n","protected":false},"author":4,"menu_order":38,"template":"","meta":{"pb_show_title":"on","pb_short_title":"","pb_subtitle":"","pb_authors":["dr-s-abirami"],"pb_section_license":""},"chapter-type":[],"contributor":[59],"license":[],"class_list":["post-395","chapter","type-chapter","status-publish","hentry","contributor-dr-s-abirami"],"part":3,"_links":{"self":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-json\/pressbooks\/v2\/chapters\/395","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-json\/pressbooks\/v2\/chapters"}],"about":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-json\/wp\/v2\/types\/chapter"}],"author":[{"embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-json\/wp\/v2\/users\/4"}],"version-history":[{"count":5,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-json\/pressbooks\/v2\/chapters\/395\/revisions"}],"predecessor-version":[{"id":627,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-json\/pressbooks\/v2\/chapters\/395\/revisions\/627"}],"part":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-json\/pressbooks\/v2\/parts\/3"}],"metadata":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-json\/pressbooks\/v2\/chapters\/395\/metadata\/"}],"wp:attachment":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-json\/wp\/v2\/media?parent=395"}],"wp:term":[{"taxonomy":"chapter-type","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-json\/pressbooks\/v2\/chapter-type?post=395"},{"taxonomy":"contributor","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-json\/wp\/v2\/contributor?post=395"},{"taxonomy":"license","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-json\/wp\/v2\/license?post=395"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}