3 Mayıs 2022 Salı

RAbbitMQ API

Channel Sınıfı
Şu satırı dahil ederiz. Bu sınıfın basicPublish(), basicConsume() metodları ile mesaj gönderilir ve alınır
import com.rabbitmq.client.Channel;
constructor
Örnek
Şöyle yaparız
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");

Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
basicConsume metodu
Örnek
Şöyle yaparız
DeliverCallback deliverCallbackDlx = (consumerTag, delivery) -> {
  String routingKey = delivery.getEnvelope().getRoutingKey();
  String message = new String(delivery.getBody(), StandardCharsets.UTF_8);

  System.out.println(" [x] Received WITH DLX! '" + routingKey + "':'" + message + "'");

  channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
};

channel.basicConsume("tickets_dlx", false, deliverCallbackDlx, consumerTag -> {});
basicPublish metodu
Örnek
Açıklaması şöyle
The following shows how RabbitMQ’s Java publisher sets the TTL of a message which can reside in the queue for at most 60 seconds:
Şöyle yaparız
byte[] messageBodyBytes = "Hello, world!".getBytes();
AMQP.BasicProperties properties = new AMQP.BasicProperties.Builder()
                                   .expiration("60000")
                                   .build();
channel.basicPublish("my-exchange", "routing-key", properties, messageBodyBytes);
Örnek
Şöyle yaparız
channel.basicPublish("tickets", "payment_is_done", null,
  paymentInfo.toString().getBytes(StandardCharsets.UTF_8));
exchangeDeclare metodu
Örnek
Şöyle yaparız
channel.exchangeDeclare("tickets", "direct");
queueDeclare metodu
Örnek
Şöyle yaparız. Burada kuyruk için dead letter exchange de tanımlanıyor
Map<String, args = new HashMap<>()
{
  {
    put("x-max-length", 10);
    put("x-message-ttl", 5000);
    put("x-dead-letter-exchange", "tickets_dlx";
    put("x-dead-letter-routing-key", "payment_is_done");
  }
};

channel.queueDeclare("TICKETS_AFTER_APPROVING_PAYMENT", 
  true, //Durable
  false,//Exclusive 
  false, //Auto delete
  args);
channel.queueBind("TICKETS_AFTER_APPROVING_PAYMENT", "tickets", "payment_is_done");



Hiç yorum yok:

Yorum Gönder