{"id":4743,"date":"2020-09-08T17:09:40","date_gmt":"2020-09-08T11:39:40","guid":{"rendered":"https:\/\/www.h2kinfosys.com\/blog\/?p=4743"},"modified":"2020-12-14T15:59:14","modified_gmt":"2020-12-14T10:29:14","slug":"what-is-jms-architecture","status":"publish","type":"post","link":"https:\/\/www.h2kinfosys.com\/blog\/what-is-jms-architecture\/","title":{"rendered":"What is JMS Architecture?"},"content":{"rendered":"\n<p>JMS (Java Message Service), also known as a messaging service, is an API that provides the facility to create, send, and read messages. It gives loosely coupled, reliable and asynchronous communication between two or more systems. If you want to send a message from one application to another, you need to use JMS API.<\/p>\n\n\n\n<p>For example, one application A is running in INDIA, and another application B is running in the USA. To transfer a message from application A to application B, we require <a href=\"https:\/\/www.h2kinfosys.com\/courses\/java-online-training-course-details\">Java Message Service.<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Advantages of JMS Architecture:<\/strong><\/h2>\n\n\n\n<p>1. Asynchronous: For receiving the message, the client is not required to send any request. The message will automatically arrive at the client.<\/p>\n\n\n\n<p>2. Reliable: It gives assurance that message is delivered.<\/p>\n\n\n\n<p>There are two types of models in JMS Architecture:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Point-to-Point Messaging Domain (PTP)<\/li><li>Publisher\/Subscriber Messaging Domain (Pub\/Sub)<\/li><\/ol>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Point-to-Point Messaging Domain:<\/strong><\/h2>\n\n\n\n<p>In the PTP model, one message is delivered to only one receiver. Here, the Queue acts as <a href=\"https:\/\/en.wikipedia.org\/wiki\/Message-oriented_middleware\" rel=\"nofollow noopener\" target=\"_blank\">message-oriented middleware<\/a> (MOM) and is responsible for holding the message until the receiver is ready.<\/p>\n\n\n\n<p>In the PTP model, there is no timing dependency available between the sender and receiver. A sender (producer) sends a message addressing a specific queue. A receiver (consumer) receives the message from the Queue established to hold its messages. Queues stores all messages sent to them until the messages are received or until the messages expire. The receiver can get the message whether or not it was running when the client sent the message. The receiver also acknowledges the successful processing of a message.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh4.googleusercontent.com\/s3vVIRZmEcTrq_ztBLlW7KFbuhhImh1Vpyo4wSJ0IsDRMzpX3MpDYOAb1IB5BQoF4u1uYUjmxm23XZTKNLCrHIwOiYp6SUddSOAdxXUzKXyE-2DI2mWku9D-3eguGOMqHtvKCIU\" alt=\"http:\/\/docs.oracle.com\/javaee\/6\/tutorial\/doc\/figures\/jms-pointToPoint.gif\" title=\"\"><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Publisher\/Subscriber Messaging Domain:<\/strong><\/h2>\n\n\n\n<p>In the Pub\/Sub model, one message is delivered to all the subscribers. It is similar to broadcasting. Here, Topic acts as a message-oriented middleware that is responsible for holding and delivering messages.<\/p>\n\n\n\n<p>In the Pub\/Sub model, there is timing dependency present between publisher and subscriber. Publisher clients publish the messages to one or more topics. Subscriber clients subscribe to one or more topics and receive the messages when they are sent to them. The topics persist the messages as long as it takes to deliver them to all currently subscribed clients. Each message may have multiple receivers.<\/p>\n\n\n\n<p>A client who has subscribed to a topic can consume published messages only after the client has created a subscription, and the subscriber must continue to be active for it to consume messages.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh4.googleusercontent.com\/Mi1CGInHpwKpBYITQ1z75vL2OOQeYzVPDhRWFfpJ9ElAF9n-EHVtK9tXUk47HYOhbgsKY7uHhHWJWk8gw1IoVQaIb6rZ4Y_5Yck-SIiwWf_wHJ21Ug9d6p0YS9ErnGAJyPMhVQY\" alt=\"http:\/\/docs.oracle.com\/javaee\/6\/tutorial\/doc\/figures\/jms-publishSubscribe.gif\" title=\"\"><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Message Sender:<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh4.googleusercontent.com\/RvfnNkWwER001CzMjOTts3fbbsxlR1mHSKQJwPK4nb6RZqh0oZFhqSDHckIkjXwU6nja4nPgr9SyYbrNGwp1CwF0URfjx00KklFimHje9a06h3WYkMX46E5uhqeU9INx36-sDyE\" alt=\"http:\/\/docs.oracle.com\/javaee\/6\/tutorial\/doc\/figures\/jms-programmingModel.gif\" title=\"\"><\/figure>\n\n\n\n<p>Message Sender is used for sending messages to a destination queue, and its object is created by a session. It implements the interface MessageProduce.<\/p>\n\n\n\n<p>First, we need to configure a connection object using the ActiveMQConnectionFactory factory object, and then we create a session object.&nbsp; With the session object&#8217;s help, we set the message broker, i.e., Queue, and configure the message sender object.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">public class MessageSender&nbsp;\n{\npublic static void main(String[] args)&nbsp;\n{\nConnection connection = null;\ntry\n{\nContext ctx = new InitialContext();\nActiveMQConnectionFactory cf = new ActiveMQConnectionFactory(\"tcp:\/\/localhost:61616\");\nconnection = cf.createConnection();\nSession session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);\nDestination destination = session.createQueue(\"test.message.queue\"); MessageProducer messageProducer = session.createProducer(destination);\nMapMessage message = session.createMapMessage();\nmessage.setString(\"Name\", \"Tim\");\nmessage.setString(\"Role\", \"Developer\");\nmessage.setDouble(\"Salary\", 850000);\nmessageProducer.send(message);\n}\ncatch (Exception e)\n{\nSystem.out.println(e);\n}\nfinally\n{\nif (connection != null)\n{\ntry\n{\nconnection.close();\n}\ncatch (JMSException e)\n{\nSystem.out.println(e);\n}\n}\nSystem.exit(0);\n}\n}\n}\n<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Message Receiver:<\/strong><\/h2>\n\n\n\n<p>Message Receiver is used for receiving messages from a queue, and its object is created by a session. It implements the interface MessageProducer. Please see the below code for the message receiver. The process is the same in message sender and receiver. In the case of the receiver, a Message Listener is used. Listener remains active and is called only when the receiver consumes any message from the broker.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">public class MessageReceiver&nbsp;\n{\npublic static void main(String[] args)&nbsp;\n{\ntry\n{\nInitialContext ctx = new InitialContext();\nActiveMQConnectionFactory cf = new ActiveMQConnectionFactory(\"tcp:\/\/localhost:61616\");\nConnection connection = cf.createConnection();\nSession session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);\nDestination destination = session.createQueue(\"test.prog.queue\"); MessageConsumer consumer = session.createConsumer(destination);\nconsumer.setMessageListener(new MapMessageListener());\nconnection.start();\n}\ncatch (Exception e)\n{\nSystem.out.println(e);\n}\n}\n}\nPlease see the below code for a message listener receiving map message object.\npublic class MapMessageListener implements MessageListener&nbsp;\n{\npublic void onMessage(Message message)\n{\nif (message instanceof MapMessage)\n{\nMapMessage mapMessage = (MapMessage)message;\ntry\n{\nString name = mapMessage.getString(\"Name\");\nSystem.out.println(\"Name : \" + name);\n}\ncatch (JMSException e)\n{\nthrow new RuntimeException(e);\n}\n}\nelse\n{\nSystem.out.println(\"Invalid Message Received\");\n}\n}\n}\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>JMS (Java Message Service), also known as a messaging service, is an API that provides the facility to create, send, and read messages. It gives loosely coupled, reliable and asynchronous communication between two or more systems. If you want to send a message from one application to another, you need to use JMS API. For [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":7196,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[42],"tags":[449,1333],"class_list":["post-4743","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java-tutorials","tag-advantages","tag-jms-architecture"],"_links":{"self":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/4743","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/comments?post=4743"}],"version-history":[{"count":0,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/4743\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media\/7196"}],"wp:attachment":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media?parent=4743"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/categories?post=4743"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/tags?post=4743"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}