spring bootrabbitmq死信队列配置不起作用

问题描述 投票:0回答:1

我配置了 Spring Boot Rabbit 的死信队列,但 ErrorHandler 从未收到任何消息。我搜索了有关死信队列的所有问题,但无法弄清楚。有人可以帮助我吗?

RabbitConfig.java 配置死信队列/交换:

@Configuration
public class RabbitConfig {

public final static String MAIL_QUEUE = "mail_queue";

public final static String DEAD_LETTER_EXCHANGE = "dead_letter_exchange";

public final static String DEAD_LETTER_QUEUE = "dead_letter_queue";

public static Map<String, Object> args = new HashMap<String, Object>();

static {
    args.put("x-dead-letter-exchange", DEAD_LETTER_EXCHANGE);
    //args.put("x-dead-letter-routing-key", DEAD_LETTER_QUEUE);
    args.put("x-message-ttl", 5000);
}

@Bean
public Queue mailQueue() {
    return new Queue(MAIL_QUEUE, true, false, false, args);
}

@Bean
public Queue deadLetterQueue() {
    return new Queue(DEAD_LETTER_QUEUE, true);
}

@Bean
public FanoutExchange deadLetterExchange() {
    return new FanoutExchange(DEAD_LETTER_EXCHANGE);
}

@Bean  
public Binding deadLetterBinding() {  
    return BindingBuilder.bind(deadLetterQueue()).to(deadLetterExchange());  
}  

}

ErrorHandler.java 处理死信队列:

@Component
@RabbitListener( queues = RabbitConfig.DEAD_LETTER_QUEUE)
public class ErrorHandler {

@RabbitHandler
public void handleError(Object message) {
    System.out.println("xxxxxxxxxxxxxxxxxx"+message);
}

}

MailServiceImpl.java 处理MAIL_QUEUE:

@Service
@RabbitListener(queues = RabbitConfig.MAIL_QUEUE)
@ConditionalOnProperty("spring.mail.host")
public class MailServiceImpl implements MailService {

@Autowired
private JavaMailSender mailSender;

@RabbitHandler
@Override
public void sendMail(TMessageMail form) {
    //......
    try {
       mailSender.save(form);
    }catch(Exception e) {
        logger.error("error in sending mail: {}", e.getMessage());
        throw new AmqpRejectAndDontRequeueException(e.getMessage());
    }
}
}
spring spring-boot rabbitmq dead-letter
1个回答
1
投票

天哪,我终于找到答案了!

所有配置都是正确的,问题是所有像mail_queue这样的队列都是在我配置死信队列之前创建的。所以当我创建队列后给队列设置x-dead-letter-exchange时,并没有生效。

如何删除队列,我按照答案。 删除 RabbitMQ 中的队列

© www.soinside.com 2019 - 2024. All rights reserved.