Distributed Systems
Lecture -8-
Created by :
Eng. Ghadeer Al Hasan
JMS
Java Message Service
Messaging Domains 1
There are two types of messaging domains in JMS :
• Point-to-PointMessaging Domain
• Publisher/Subscriber Messaging Domain
Publisher/Subscriber (Pub/Sub) Messaging Domain 2
• In Pub/Sub model, one message is delivered to all the subscribers. It is like broadcasting.
• Topicis used as a messageorientedmiddleware that is responsible to hold and deliver messages.
• In PTP model, there is timing dependency between publisher and subscriber.
JMS Programming Model 3
JMS Queue Example
1st Create connection factory 5
2nd Create destination resource 6
3rd Create Sender application 7
InitialContext ctx = new InitialContext();
TopicConnectionFactory f = (TopicConnectionFactory) ctx.lookup("myTopicConnectionFactory");
TopicConnection con = f.createTopicConnection ();
con.start();
TopicSession session = con.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
Topic topic =(Topic) ctx.lookup("myTopic");
TopicPublisher publisher = session. createPublisher(topic);
TextMessage msg = session.createTextMessage();
3rd Create Sender application… 8
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
while(true) {
log("Enter Msg, end to terminate:");
String line = reader.readLine();
if (line.equals("end"))
break;
msg.setText(line);
publisher.publish(msg);
log("Message successfully sent.");
}
4th Create Receiver application 8
public class MsgListener implements MessageListener {
@Override
public void onMessage(Message m) {
try {
TextMessage msg = (TextMessage) m;
log("following message is received:" + msg.getText());
} catch (JMSException e) {
log(e);
}
}
}
4th Create Receiver application… 9
InitialContext ctx = new InitialContext();
TopicConnectionFactory f = (TopicConnectionFactory) ctx.lookup(" myTopicConnectionFactory");
TopicConnection con = f.createTopicConnection();
con.start();
TopicSession session = con.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
Topic topic = (Topic) ctx.lookup("myTopic");
TopicSubscriber receiver = session.createSubscriber(topic);
MsgListener listener = new MsgListener();
receiver.setMessageListener(listener);
Log("Subscriber is ready, waiting for messages... “);
while(true){
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
log(ex.getMessage());
}
}
References 10
- YouTube link
https://www.youtube.com/playlist?list=PLtDIUAtyP4lhV7CsYfLuIx26UeG4J-ujZ
- GitHub
https://github.com/Ghadeerof
End Lecture

#8 (Java Message Service)

  • 1.
    Distributed Systems Lecture -8- Createdby : Eng. Ghadeer Al Hasan JMS Java Message Service
  • 2.
    Messaging Domains 1 Thereare two types of messaging domains in JMS : • Point-to-PointMessaging Domain • Publisher/Subscriber Messaging Domain
  • 3.
    Publisher/Subscriber (Pub/Sub) MessagingDomain 2 • In Pub/Sub model, one message is delivered to all the subscribers. It is like broadcasting. • Topicis used as a messageorientedmiddleware that is responsible to hold and deliver messages. • In PTP model, there is timing dependency between publisher and subscriber.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
    3rd Create Senderapplication 7 InitialContext ctx = new InitialContext(); TopicConnectionFactory f = (TopicConnectionFactory) ctx.lookup("myTopicConnectionFactory"); TopicConnection con = f.createTopicConnection (); con.start(); TopicSession session = con.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); Topic topic =(Topic) ctx.lookup("myTopic"); TopicPublisher publisher = session. createPublisher(topic); TextMessage msg = session.createTextMessage();
  • 9.
    3rd Create Senderapplication… 8 BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); while(true) { log("Enter Msg, end to terminate:"); String line = reader.readLine(); if (line.equals("end")) break; msg.setText(line); publisher.publish(msg); log("Message successfully sent."); }
  • 10.
    4th Create Receiverapplication 8 public class MsgListener implements MessageListener { @Override public void onMessage(Message m) { try { TextMessage msg = (TextMessage) m; log("following message is received:" + msg.getText()); } catch (JMSException e) { log(e); } } }
  • 11.
    4th Create Receiverapplication… 9 InitialContext ctx = new InitialContext(); TopicConnectionFactory f = (TopicConnectionFactory) ctx.lookup(" myTopicConnectionFactory"); TopicConnection con = f.createTopicConnection(); con.start(); TopicSession session = con.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); Topic topic = (Topic) ctx.lookup("myTopic"); TopicSubscriber receiver = session.createSubscriber(topic); MsgListener listener = new MsgListener(); receiver.setMessageListener(listener); Log("Subscriber is ready, waiting for messages... “); while(true){ try { Thread.sleep(1000); } catch (InterruptedException ex) { log(ex.getMessage()); } }
  • 12.
    References 10 - YouTubelink https://www.youtube.com/playlist?list=PLtDIUAtyP4lhV7CsYfLuIx26UeG4J-ujZ - GitHub https://github.com/Ghadeerof
  • 13.