I using spring boot rabbitMQ Sender.
Method returns Integer
try {
return rabbitTemplate.convertSendAndReceive(exchange, routingKey,
mapper.writeValueAsString(request),
correlationData);
} catch (Exception e) {
log.error(e.getMessage(), e);
}
and on the receiver side, reply:
@RabbitListener(queues = "testQueue", returnExceptions = "true")
public class TestReply {
@RabbitHandler
public Integer handle(String message) throws JsonProcessingException {
throw new IllegalArgumentException()
}
}
I want to handle IllegalArgumentException
in the sender. But the fact I get is
org.springframework.amqp.support.converter.MessageConversionException: Failed to convert Message content
Cannot deserialize value of type `java.lang.Integer` from Object value (token `JsonToken.START_OBJECT`)
Please help me!
@RabbitListener.returnExceptions()
.
/**
* Set to "true" to cause exceptions thrown by the listener to be sent to the sender
* using normal {@code replyTo/@SendTo} semantics. When false, the exception is thrown
* to the listener container and normal retry/DLQ processing is performed.
* @return true to return exceptions. If the client side uses a
* {@code RemoteInvocationAwareMessageConverterAdapter} the exception will be re-thrown.
* Otherwise, the sender will receive a {@code RemoteInvocationResult} wrapping the
* exception.
* @since 2.0
*/
String returnExceptions() default "";
However, this will only work with Java serialization (not JSON) because exceptions are generally not JSON-friendly.
The alternative is to add an errorHandler
and return some special value to tell the client that an exception occurred.
https://docs.spring.io/spring-amqp/docs/current/reference/html/#annotation-error-handling