RabbitMQ的延迟功能不起作用

时间:2018-07-29 11:02:39

标签: spring-boot rabbitmq microservices spring-rabbitmq rabbitmq-exchange

我在春季引导中使用RabbitMQ通过微服务发送消息。我需要为每条消息设置延迟选项。在这种情况下,我将MessageProperties文件与Message一起使用,就像这样。

 MessageProperties properties = new MessageProperties();
    properties.setDelay(15000);
    properties.setContentType(ContentType.APPLICATION_JSON.getMimeType());
    properties.setType("direct");
    properties.setHeader("x-delay",15000);
    properties.setHeader("x-delayed-type","direct");
    properties.setHeader("x-delayed-message",true);
    Message forSms = MessageBuilder.withBody(SerializationUtils.serialize(updatedAppointment.getId())).andProperties(properties).setCorrelationId(correlationIdForSms.getBytes()).setHeader("x-delay", 20000).setHeaderIfAbsent("x-delay", 20000).build();
rabbitTemplate.send("auto.exch", "orderRoutingKey", forSms);
and I handle it in this function:

@RabbitListener(bindings = @QueueBinding(
        value = @Queue(value = "myQueue", durable = "true"),arguments = @Argument(name = "x-delay",value = "15000",type = "java.lang.Integer"),
        exchange = @Exchange(value = "auto.exch", delayed ="true",ignoreDeclarationExceptions = "true",arguments = @Argument(name = "x-delayed-type",value = "direct"), type = ExchangeTypes.HEADERS),
         key = "orderRoutingKey")
)

public void sendMessagePatientPhone(final Message message) throws EntityNotFoundException {
    Long appointmentId = (Long) SerializationUtils.deserialize(message.getBody());
    String correlationId = new String(message.getMessageProperties().getCorrelationId());


    Appointment appointment = appointmentService.findById(appointmentId);
    User user = appointment.getPatient().getUser();
    String msg = "Teleconference is started after " + formattedTime(user.getNotificationPeriodForSms());
    sendSms(user, msg);

}

但是我只得到空的标题,消息不会延迟。也许我使用了错误的方式,但是我需要正确的解决方案。我的基本目的是在特定时间延迟每条消息

2 个答案:

答案 0 :(得分:0)

您似乎有些困惑:

>>> test = eval("[" + (open("C:/testdata1.txt" , 'r').read().replace("\n","").rstrip(",") + "]"))
>>> print(hex(test[1][2]))
0x7c

这些属性是交换属性,而不是消息属性。

要设置properties.setHeader("x-delayed-type","direct"); properties.setHeader("x-delayed-message",true); 属性,只需使用x-delay

答案 1 :(得分:0)

我已经做到了。该问题与 rabbitmq_delayed_message_exchange 的RabbitMQ插件有关。我安装并启用了它。在此 x-delay 功能正常工作