https://github.com/rabbitmq/rabbitmq-delayed-message-exchange/releases
rabbitmq-plugins enable rabbitmq_delayed_message_exchange
管理端新增交换机时可以看到这个类型说明启用成功
import com.great.common.core.constant.RabbitConstant;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.CustomExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.HashMap;
import java.util.Map;
@Configuration
public class GrabDelayedConfig {
@Bean
public Queue grabQueue() {
return new Queue(RabbitConstant.QUEUE_DELAYED_DISPATCH_GRAB);
}
@Bean
public CustomExchange delayedExchange() {
Map args = new HashMap<>(1);
args.put("x-delayed-type", "direct");
return new CustomExchange(RabbitConstant.EXCHANGE_DELAYED, "x-delayed-message", true, false, args);
}
@Bean
public Binding bindingNotify(@Qualifier("grabQueue") Queue queue,
@Qualifier("delayedExchange") CustomExchange customExchange) {
return BindingBuilder.bind(queue).to(customExchange).with(RabbitConstant.ROUTING_KEY_DISPATCH_GRAB).noargs();
}
}
2.生产者
public void sendDelayMessage(Object message, Integer delayTime) {
rabbitTemplate.convertAndSend(RabbitConstant.EXCHANGE_DELAYED, RabbitConstant.ROUTING_KEY_DISPATCH_GRAB, message, a -> {
a.getMessageProperties().setDelay(delayTime);
return a;
});
}
3.消费者
@RabbitListener(queuesToDeclare = @Queue(value = RabbitConstant.QUEUE_DELAYED_DISPATCH_GRAB))
public void receivedDispatchGrabMessage(String message) {
System.out.println("延迟消费:" + message);
}



