Skip to content
Advertisement

Synchronous publish for rabbitmq

I want to have two sets API for the clients to publish messages

  • sync send(no retry, fail immediately and send status back to the clients synchronously)
  • async send (publisher and confirm callback, with retry, log and drop the message after certain retries). I am able to implement this by enabling confirm and return.

Is there any way to implement Synchronous publish API using rabbitTemplate which will block for confirm and return both?

rabbiTemplate.waitForConfirmsOrDie(mills) blocks for confirmation, but I want caller to know if routing is successful as well or throw exception.

Advertisement

Answer

See this API from RabbitOeprations:

/**
 * Send a message to a specific exchange with a specific routing key.
 *
 * @param exchange the name of the exchange
 * @param routingKey the routing key
 * @param message a message to send
 * @param correlationData data to correlate publisher confirms.
 * @throws AmqpException if there is a problem
 */
void send(String exchange, String routingKey, Message message, CorrelationData correlationData)

The return or confirm is set in the end in that provided CorrelationData. You have to wait for its getFuture() and then return to the caller a decision you make against Confirm reason. See more info in that CorrelationData JavaDocs and its implementation. See also some docs on the matter: https://docs.spring.io/spring-amqp/docs/current/reference/html/#template-confirms

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement