Class PooledChannel
java.lang.Object
fr.codinbox.connector.commons.rabbitmq.PooledChannel
- All Implemented Interfaces:
AutoCloseable
A pooled RabbitMQ channel wrapper that implements
AutoCloseable to automatically
return the channel to the pool when closed.
This class provides both raw channel access and convenience methods for common RabbitMQ operations. It should always be used within a try-with-resources block:
try (PooledChannel ch = connection.borrowChannel()) {
ch.basicPublish("exchange", "routingKey", null, "hello".getBytes());
}
Closing this wrapper does not close the underlying channel; it returns it to the pool for reuse.
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionbasicConsume(@NotNull String queue, @NotNull DeliverCallback callback) Starts a non-auto-acknowledged consumer on the specified queue using a callback.voidbasicPublish(@NotNull String exchange, @NotNull String routingKey, AMQP.BasicProperties props, byte @NotNull [] body) Publishes a message to the specified exchange with the given routing key.voidclose()Returns the channel to the pool.Starts a non-auto-acknowledged consumer that delivers messages to aBlockingQueue.Returns the underlying RabbitMQChannelfor direct access.
-
Method Details
-
getChannel
Returns the underlying RabbitMQChannelfor direct access.- Returns:
- the raw channel
-
basicPublish
public void basicPublish(@NotNull @NotNull String exchange, @NotNull @NotNull String routingKey, @Nullable AMQP.BasicProperties props, byte @NotNull [] body) throws IOException Publishes a message to the specified exchange with the given routing key.- Parameters:
exchange- the exchange to publish toroutingKey- the routing keyprops- message properties, may benullbody- the message body- Throws:
IOException- if an I/O error occurs during publishing- See Also:
-
basicConsume
@NotNull public @NotNull String basicConsume(@NotNull @NotNull String queue, @NotNull @NotNull DeliverCallback callback) throws IOException Starts a non-auto-acknowledged consumer on the specified queue using a callback.- Parameters:
queue- the queue to consume fromcallback- the callback to invoke for each delivered message- Returns:
- the consumer tag
- Throws:
IOException- if an I/O error occurs- See Also:
-
consume
@NotNull public @NotNull BlockingQueue<Delivery> consume(@NotNull @NotNull String queue) throws IOException Starts a non-auto-acknowledged consumer that delivers messages to aBlockingQueue.Callers can poll or take from the returned queue to receive messages synchronously:
BlockingQueue<Delivery> deliveries = channel.consume("my-queue"); Delivery delivery = deliveries.take(); // blocks until a message arrives- Parameters:
queue- the queue to consume from- Returns:
- a blocking queue that receives deliveries
- Throws:
IOException- if an I/O error occurs
-
close
public void close()Returns the channel to the pool. Does not close the underlying channel.If the channel is no longer open, it is discarded rather than returned to the pool.
- Specified by:
closein interfaceAutoCloseable
-