21 Temmuz 2020 Salı

JMS MessageProducer Arayüzü

Giriş
Şu satırı dahil ederiz.
import javax.jms.MessageProducer;
Mesaj gönderir. mesajı okumak için MessageConsumer arayüzü kullanılır.

setDeliveryMode metodu
DeliveryMode.PERSISTENT ise açıklaması şöyle
The PERSISTENT delivery mode, the default, instructs the JMS provider to take extra care to ensure that a message is not lost in transit in case of a JMS provider failure. A message sent with this delivery mode is logged to stable storage when it is sent.
send metodu - Destination + Message
Açıklaması şöyle. MessageProducer nesnesi session.createConsumer(null) çağrısı ile yaratılır
You can create an unidentified producer by specifying null as the argument to createProducer. With an unidentified producer, you do not specify a destination until you send a message.
Örnek
Şö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) 
send metodu - Message
Örnek - 
Elimizde şöyle bir kod olsun
class MyProducer extends Thread {
  private final Connection connection;
  private final Destination destination;

  MyProducer(Connection connection, Destination destination) {
    this.connection = connection;
    this.destination = destination;
  }

  @Override
  public void run() {
    try (Session session = connection.createSession(Session.AUTO_ACKNOWLEDGE)) {
      MessageProducer messageProducer = session.createProducer(destination);
      messageProducer.send(session.createTextMessage("My message"));
      System.out.println("Sent message");
    } catch (Throwable e) {
      e.printStackTrace();
      return;
    }
  }
}
Şöyle yaparız
InitialContext initialContext = new InitialContext();
Queue queue = (Queue) initialContext.lookup("queue/exampleQueue");
ConnectionFactory cf = (ConnectionFactory) initialContext.lookup("ConnectionFactory");
Connection connection = cf.createConnection();
Thread myConsumer = myMultiThreadedApp.runProducer(connection, queue);

Hiç yorum yok:

Yorum Gönder