91-9990449935 0120-4256464 |
JMS TutorialJMS (Java Message Service) is an API that provides the facility to create, send and read messages. It provides loosely coupled, reliable and asynchronous communication. JMS is also known as a messaging service. Understanding MessagingMessaging is a technique to communicate applications or software components. JMS is mainly used to send and receive message from one application to another. Requirement of JMSGenerally, user sends message to application. But, if we want to send message from one application to another, we need to use JMS API. Consider a scenario, one application A is running in INDIA and another application B is running in USA. To send message from A application to B, we need to use JMS. Advantage of JMS1) Asynchronous: To receive the message, client is not required to send request. Message will arrive automatically to the client. 2) Reliable: It provides assurance that message is delivered. Messaging DomainsThere are two types of messaging domains in JMS.
1) Point-to-Point (PTP) Messaging DomainIn PTP model, one message is delivered to one receiver only. Here, Queue is used as a message oriented middleware (MOM). The Queue is responsible to hold the message until receiver is ready. In PTP model, there is no timing dependency between sender and receiver. 2) Publisher/Subscriber (Pub/Sub) Messaging DomainIn Pub/Sub model, one message is delivered to all the subscribers. It is like broadcasting. Here, Topic is used as a message oriented middleware that is responsible to hold and deliver messages. In PTP model, there is timing dependency between publisher and subscriber. JMS Programming ModelJMS Queue ExampleTo develop JMS queue example, you need to install any application server. Here, we are using glassfish3 server where we are creating two JNDI.
After creating JNDI, create server and receiver application. You need to run server and receiver in different console. Here, we are using eclipse IDE, it is opened in different console by default. 1) Create connection factory and destination resourceOpen server admin console by the URL http://localhost:4848 Login with the username and password. Click on the JMS Resource -> Connection Factories -> New, now write the pool name and select the Resource Type as QueueConnectionFactory then click on ok button. Click on the JMS Resource -> Destination Resources -> New, now write the JNDI name and physical destination name then click on ok button. 2) Create sender and receiver applicationLet's see the Sender and Receiver code. Note that Receiver is attached with listener which will be invoked when user sends message. File: MySender.java File: MyReceiver.java File: MyListener.javaRun the Receiver class first then Sender class. JMS Topic ExampleIt is same as JMS Queue, but you need to change Queue to Topic, Sender to Publisher and Receiver to Subscriber. You need to create 2 JNDI named myTopicConnectionFactory and myTopic. File: MySender.java File: MyReceiver.java File: MyListener.java
Next TopicMessage Driven Bean
|