问题
具体报错内容如下:
2022-11-08 14:49:15,170 29895 [AMQP Connection xxx:5672] ERROR org.springframework.amqp.rabbit.connection.CachingConnectionFactory - Channel shutdown: channel error; protocol method: #method<channel.close>(reply-code=406,
reply-text=PRECONDITION_FAILED - invalid arg 'x-message-ttl' for queue 'xxx' in vhost '/': {unacceptable_type,longstr}, class-id=50, method-id=10)
2022-11-08 14:49:15,172 29897 [main] INFO org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer - Broker not available; cannot force queue declarations during start: java.io.IOException
使用bean方式创建延迟队列queue
@Bean
public Queue messageTtlQueue() {
return QueueBuilder.durable(ctDataTtl.getQueue())
// 配置到期后转发的交换
.withArgument("x-dead-letter-exchange", dataConsume.getExchange())
// 配置到期后转发的路由键
.withArgument("x-dead-letter-routing-key", dataConsume.getRoutingKey())
// 延迟30分钟
.withArgument("x-message-ttl", dataTtl.getTtl()).build();
}
其中dataTtl是配置类,配置ttl值
@Data
@Configuration
@ConfigurationProperties(prefix = "custom.rabbitmq.data-ttl")
public class DataTtl {
private String exchange;
private String queue;
private String routingKey;
private String ttl;
}
原因
问题就在配置类DataTtl 中, 其中ttl数据类型不能使用String类型,改为Long之后即可解决问题
解决
dataTtl配置类修改如下:
@Data
@Configuration
@ConfigurationProperties(prefix = "custom.rabbitmq.data-ttl")
public class DataTtl {
private String exchange;
private String queue;
private String routingKey;
private Long ttl;
}
重启jar包即可创建延迟队列。