logo

JMS Eğitimi

JMS (Java Mesaj Hizmeti), mesaj oluşturma, gönderme ve okuma olanağı sağlayan bir API'dir. Gevşek bağlı, güvenilir ve asenkron iletişim sağlar.

c'deki ascii tablosu

JMS aynı zamanda mesajlaşma servisi olarak da bilinir.


Mesajlaşmayı Anlamak

Mesajlaşma, uygulamaları veya yazılım bileşenlerini iletmek için kullanılan bir tekniktir.

JMS esas olarak bir uygulamadan diğerine mesaj göndermek ve almak için kullanılır.


JMS gerekliliği

Genellikle kullanıcı uygulamaya mesaj gönderir. Ancak bir uygulamadan diğerine mesaj göndermek istiyorsak JMS API kullanmamız gerekir.

Bir senaryo düşünün, bir A uygulaması HİNDİSTAN'da çalışıyor ve başka bir B uygulaması ABD'de çalışıyor. A uygulamasından B'ye mesaj göndermek için JMS kullanmamız gerekiyor.


JMS'nin Avantajı

1) Asenkron: Mesajı almak için istemcinin istek göndermesine gerek yoktur. Mesaj müşteriye otomatik olarak ulaşacaktır.

2) Güvenilir: Mesajın iletildiğine dair güvence sağlar.


Mesajlaşma Alanları

JMS'de iki tür mesajlaşma alanı vardır.

  1. Noktadan Noktaya Mesajlaşma Alanı
  2. Yayıncı/Abone Mesajlaşma Alanı

1) Noktadan Noktaya (PTP) Mesajlaşma Alanı

PTP modelinde bir mesaj bir alıcıya teslim edildi sadece. Burada, Sıra mesaj odaklı ara katman yazılımı (MOM) olarak kullanılır.

Kuyruk, alıcı hazır olana kadar mesajı tutmakla sorumludur.

PTP modelinde, zamanlama bağımlılığı yok gönderen ve alıcı arasında.


2) Yayıncı/Abone (Pub/Sub) Mesajlaşma Alanı

Pub/Sub modelinde bir mesaj tüm abonelere dağıtıldı . Yayın yapmak gibidir. Burada, Başlık mesajları tutmak ve iletmekten sorumlu olan mesaj odaklı bir ara yazılım olarak kullanılır.

PTP modelinde, zamanlama bağımlılığı Yayıncı ve abone arasında.



JMS Programlama Modeli


JMS Kuyruk Örneği

JMS kuyruk örneği geliştirmek için herhangi bir uygulama sunucusunu kurmanız gerekir. Burada kullanıyoruz cam balığı3 iki JNDI oluşturduğumuz sunucu.

  1. Adlı bağlantı fabrikası oluştur myQueueConnectionFactory
  2. Adlandırılan hedef kaynağı oluştur benim sıram

JNDI oluşturduktan sonra sunucu ve alıcı uygulamasını oluşturun. Sunucuyu ve alıcıyı farklı konsolda çalıştırmanız gerekir. Burada Eclipse IDE kullanıyoruz, varsayılan olarak farklı konsollarda açılıyor.

1) Bağlantı fabrikası ve hedef kaynağı oluşturun

Sunucu yönetici konsolunu URL'ye göre açın http://localhost:4848

Kullanıcı adı ve şifreyle giriş yapın.

Tıkla JMS Kaynağı -> Bağlantı Fabrikaları -> Yeni Şimdi havuz adını yazıp Kaynak Tipini QueueConnectionFactory olarak seçip tamam butonuna tıklayın.

Tıkla JMS Kaynağı -> Hedef Kaynaklar -> Yeni , şimdi JNDI adını ve fiziksel hedef adını yazın ve ardından tamam düğmesine tıklayın.

2) Gönderen ve alıcı uygulamasını oluşturun

Gönderici ve Alıcı kodunu görelim. Alıcının, kullanıcı mesaj gönderdiğinde çağrılacak dinleyiciye eklendiğini unutmayın.

Dosya: MySender.java
 import java.io.BufferedReader; import java.io.InputStreamReader; import javax.naming.*; import javax.jms.*; public class MySender { public static void main(String[] args) { try { //Create and start connection InitialContext ctx=new InitialContext(); QueueConnectionFactory f=(QueueConnectionFactory)ctx.lookup('myQueueConnectionFactory'); QueueConnection con=f.createQueueConnection(); con.start(); //2) create queue session QueueSession ses=con.createQueueSession(false, Session.AUTO_ACKNOWLEDGE); //3) get the Queue object Queue t=(Queue)ctx.lookup('myQueue'); //4)create QueueSender object QueueSender sender=ses.createSender(t); //5) create TextMessage object TextMessage msg=ses.createTextMessage(); //6) write message BufferedReader b=new BufferedReader(new InputStreamReader(System.in)); while(true) { System.out.println('Enter Msg, end to terminate:'); String s=b.readLine(); if (s.equals('end')) break; msg.setText(s); //7) send message sender.send(msg); System.out.println('Message successfully sent.'); } //8) connection close con.close(); }catch(Exception e){System.out.println(e);} } } 
Dosya: MyReceiver.java
 import javax.jms.*; import javax.naming.InitialContext; public class MyReceiver { public static void main(String[] args) { try{ //1) Create and start connection InitialContext ctx=new InitialContext(); QueueConnectionFactory f=(QueueConnectionFactory)ctx.lookup('myQueueConnectionFactory'); QueueConnection con=f.createQueueConnection(); con.start(); //2) create Queue session QueueSession ses=con.createQueueSession(false, Session.AUTO_ACKNOWLEDGE); //3) get the Queue object Queue t=(Queue)ctx.lookup('myQueue'); //4)create QueueReceiver QueueReceiver receiver=ses.createReceiver(t); //5) create listener object MyListener listener=new MyListener(); //6) register the listener object with receiver receiver.setMessageListener(listener); System.out.println('Receiver1 is ready, waiting for messages...'); System.out.println('press Ctrl+c to shutdown...'); while(true){ Thread.sleep(1000); } }catch(Exception e){System.out.println(e);} } } 
Dosya: MyListener.java
 import javax.jms.*; public class MyListener implements MessageListener { public void onMessage(Message m) { try{ TextMessage msg=(TextMessage)m; System.out.println('following message is received:'+msg.getText()); }catch(JMSException e){System.out.println(e);} } } 

Önce Alıcı sınıfını, ardından Gönderen sınıfını çalıştırın.


JMS Konu Örneği

JMS Kuyruğu ile aynıdır, ancak Kuyruğu Konuya, Göndereni Yayıncıya ve Alıcıyı Aboneye değiştirmeniz gerekir.

Adlı 2 JNDI oluşturmanız gerekir myTopicConnectionFactory Ve benim konum .

Dosya: MySender.java
 import java.io.BufferedReader; import java.io.InputStreamReader; import javax.naming.*; import javax.jms.*; public class MySender { public static void main(String[] args) { try { //Create and start connection InitialContext ctx=new InitialContext(); TopicConnectionFactory f=(TopicConnectionFactory)ctx.lookup('myTopicConnectionFactory'); TopicConnection con=f.createTopicConnection(); con.start(); //2) create queue session TopicSession ses=con.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); //3) get the Topic object Topic t=(Topic)ctx.lookup('myTopic'); //4)create TopicPublisher object TopicPublisher publisher=ses.createPublisher(t); //5) create TextMessage object TextMessage msg=ses.createTextMessage(); //6) write message BufferedReader b=new BufferedReader(new InputStreamReader(System.in)); while(true) { System.out.println('Enter Msg, end to terminate:'); String s=b.readLine(); if (s.equals('end')) break; msg.setText(s); //7) send message publisher.publish(msg); System.out.println('Message successfully sent.'); } //8) connection close con.close(); }catch(Exception e){System.out.println(e);} } } 
Dosya: MyReceiver.java
 import javax.jms.*; import javax.naming.InitialContext; public class MyReceiver { public static void main(String[] args) { try { //1) Create and start connection InitialContext ctx=new InitialContext(); TopicConnectionFactory f=(TopicConnectionFactory)ctx.lookup('myTopicConnectionFactory'); TopicConnection con=f.createTopicConnection(); con.start(); //2) create topic session TopicSession ses=con.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); //3) get the Topic object Topic t=(Topic)ctx.lookup('myTopic'); //4)create TopicSubscriber TopicSubscriber receiver=ses.createSubscriber(t); //5) create listener object MyListener listener=new MyListener(); //6) register the listener object with subscriber receiver.setMessageListener(listener); System.out.println('Subscriber1 is ready, waiting for messages...'); System.out.println('press Ctrl+c to shutdown...'); while(true){ Thread.sleep(1000); } }catch(Exception e){System.out.println(e);} } } 
Dosya: MyListener.java
 import javax.jms.*; public class MyListener implements MessageListener { public void onMessage(Message m) { try{ TextMessage msg=(TextMessage)m; System.out.println('following message is received:'+msg.getText()); }catch(JMSException e){System.out.println(e);} } }