需求:
消息消费者监听接收生产者发送的id,根据id查询数据库数据并将数据保存到redis中。
问题描述
已知@RabbitListener标识的方法是无法拥有返回值的,只能是void。
但当我想使用Spring Cache的@CachePut来存储缓存时,返回值又是必须定义的,这样的做法就导致了消息一直无法出列,而且后台不停报错:
Caused by: org.springframework.amqp.AmqpException: Cannot determine ReplyTo message property value: Request message does not contain reply-to property, and no default response Exchange was set.
@Component("queueListener")
public class QueueListener {
@Autowired
private UserMapper userMapper;
@RabbitListener(queues = {
"insert.queue"})
@CachePut(value = "user", key = "#id", condition = "#result != null")
public User insertQueueListener(Long id) {
return userMapper.selectById(id);
}