10 Ağustos 2020 Pazartesi

JMS Connection Arayüzü

start metodu
Açıklaması şöyle
Also, keep in mind that there's no reason to call start() on the JMS connection if it's just sending a message. The start() method only impacts consumers.
Örnek
Şöyle yaparız. Burada bir MessageProducer ve MessageConsumer yaratılıyor.. MessageProducer.send(TextMessage) çağrısı ile mesaj gönderiliyor.
MessageConsumer.receive() çağrısı ile mesaj alınıyor  
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.qpid.jms.JmsConnectionFactory;

public static void main(String[] args) throws Exception {
  Connection connection = null;
  ConnectionFactory connectionFactory = new JmsConnectionFactory("amqp://localhost:5672");
  try {
    // Step 1. Create an amqp qpid 1.0 connection
    connection = connectionFactory.createConnection();
    // Step 2. Create a session
    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    // Step 3. Create a sender
    Queue queue = session.createQueue("jms.queue.exampleQueue");
    MessageProducer sender = session.createProducer(queue);
    // Step 4. send a few simple message
    sender.send(session.createTextMessage("Hello world "));
    connection.start();
    // Step 5. create a moving receiver, this means the message will be removed froma
// the queue
    MessageConsumer consumer = session.createConsumer(queue);
    // Step 7. receive the simple message
    TextMessage m = (TextMessage) consumer.receive(5000);
    System.out.println("message = " + m.getText());
  }
  finally {
    if (connection != null) {
      // Step 9. close the connection
      connection.close();
    }
  }
}
createSession metodu - boolean transacted, int acknowledgeMode
Şöyle yaparız
try {
  if (connection == null) {
    createConnection();
  }
  Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

  // Destination represents here our queue 'MessageProducerJMSV1' on the  JMS server
  Destination destination = session.createQueue(subject);

  MessageProducer producer = session.createProducer(destination);

  //Sending message to the queue
  TextMessage toSendMessage = session.createTextMessage(message);

  long delay = 300 * 1000;

  toSendMessage.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_DELAY, delay);

  producer.send(toSendMessage);
} catch (Throwable th) {
  th.printStackTrace();
               
  // if there are any problems close the connection and it will be re-created next time
  if (connection != null) {
    connection.close();
    connection = null;
  }
}
  
private void createConnection() {
  // Getting JMS connection from the server
  ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
  connection = connectionFactory.createConnection();
}

Hiç yorum yok:

Yorum Gönder