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
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
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
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:
byte[] messageBodyBytes = "Hello, world!".getBytes();
AMQP.BasicProperties properties = new AMQP.BasicProperties.Builder()
.expiration("60000")
.build();
channel.basicPublish("my-exchange", "routing-key", properties, messageBodyBytes);
Örnek
channel.basicPublish("tickets", "payment_is_done", null,
paymentInfo.toString().getBytes(StandardCharsets.UTF_8));
exchangeDeclare metoduÖrnek
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");