21 Temmuz 2020 Salı

JMS Session Arayüzü

Giriş
Şu satırı dahil ederiz
import javax.jms.Session;
JMS — Session and two-phase commit (XA) transactions
Açıklaması şöyle
Most JMS message brokers have good support for transactional workloads.

A transacted session supports a single series of transactions. Each transaction groups a set of produced messages and a set of consumed messages into an atomic unit of work.

Two-phase commit transactions (XA transactions) work on a limited scale. They are used to integrate with other systems like Mainframe CICS / DB2 or Oracle database. But it is hard to operate and not possible to scale beyond a few transactions per second.

It is important to note that support for XA transactions is not mandatory with the JMS 2.0 specification. This differs from the session transaction.
constructor
Örnek - AUTO_ACKNOWLEDGE
Şöyle yaparız
private void createUnsharedConsumer(ConnectionFactory connectionFactory, Topic topic)
  throws JMSException {

  Connection connection = connectionFactory.createConnection();
  Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
  ... 
}
createConsumer metodu - Destination
MessageConsumer döndürür

createDurableSubscription metodu
Örnek ver

createMessage metodu
Şöyle yaparız.
Queue replyTo = session.createTemporaryQueue();
MessageConsumer consumer = session.createConsumer(replyTo);

Queue testQueue = session.createQueue("TEST.FOO");
MessageProducer producer = session.createProducer(null);


Message msg = session.createMessage();
producer.send(testQueue, msg) 
createProducer metodu
createProducer() her zaman createMessage(), createObjectMessage() ile birlikte kullanılır.

Örnek - Queue'ya Yazma
Şöyle yaparız
ConnectionFactory connectionFactory =
String QUEUE_NAME = "user_creation_queue";

void pushNewUserToQueue(User newUser) throws Exception {
  Session session = ...
  Destination destination = session.createQueue(QUEUE_NAME);
  MessageProducer producer = session.createProducer(destination);

  ObjectMessage message = session.createObjectMessage(newUser);
  producer.send(message);
}
createSharedConsumer metodu - Topic + String
Mesajı okumak için MessageConsuer döndürür. JMS 2.0 ile kullanılır. Destination olarak Queue, TemporaryQueue, TemporaryTopic, Topic kullanılır.

Örnek
Şöyle yaparız.
private void createSharedConsumer(ConnectionFactory connectionFactory, Topic topic)
  throws JMSException {

   Connection connection = connectionFactory.createConnection();

   Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

   MessageConsumer messageConsumer = session.createSharedConsumer(topic,"mySubscription");

   connection.start();

   Message message = messageConsumer.receive(10000);
   while (message != null) {
      System.out.println("Message received: " + ((TextMessage) message).getText());
      message = messageConsumer.receive(10000);
   }
   connection.close();
}

Hiç yorum yok:

Yorum Gönder