rabbitmq延迟消息

1.rabbitmq_delayed_message_exchange插件

   实现延迟消息需要在mq安装 rabbitmq_delayed_message_exchange插件

 

2.springboot集成mq


        org.springframework.boot
        spring-boot-starter-amqp

 接下来在application配置 不想安装的话可以用本人的可以使用(建议自己安装)

spring.rabbitmq.host=118.190.207.242
spring.rabbitmq.port=5672
spring.rabbitmq.addresses=118.190.207.242:5672
spring.rabbitmq.username=cxz
spring.rabbitmq.password=cxz
spring.rabbitmq.virtual-host=test
spring.rabbitmq.publisher-confirms=true
spring.rabbitmq.prefetch=100
spring.rabbitmq.concurrency=8
spring.rabbitmq.max-concurrency=56
spring.rabbitmq.template.receive-timeout=0
spring.rabbitmq.template.retry.enabled=false
spring.rabbitmq.template.retry.initial-interval=1000

3.定义ConnectionFactory和RabbitTemplate

@Configuration
@EnableRabbit
public class RabbitMqReceiveConfig implements RabbitListenerConfigurer {
    @Value("${spring.rabbitmq.host}")
    private String host;
    @Value("${spring.rabbitmq.port}")
    private int port;
    @Value("${spring.rabbitmq.username}")
    private String userName;
    @Value("${spring.rabbitmq.password}")
    private String password;
    @Value("${spring.rabbitmq.virtual-host}")
    private String virtualHost;
    @Value("${spring.rabbitmq.prefetch}")
    private int prefetch;
    @Value("${spring.rabbitmq.template.receive-timeout}")
    private int receiveTimeout;
    @Value("${spring.rabbitmq.template.retry.enabled}")
    private boolean retryEnabled;
    @Value("${spring.rabbitmq.template.retry.initial-interval}")
    private int retryInterval;
    @Value("${spring.rabbitmq.concurrency}")
    private int concurrentConsumers;
    @Value("${spring.rabbitmq.max-concurrency}")
    private int maxConcurrentConsumers;

    @Bean
    public ConnectionFactory connectionFactory() {
        CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
        connectionFactory.setHost(host);
        connectionFactory.setPort(port);
        connectionFactory.setUsername(userName);
        connectionFactory.setPassword(password);
        connectionFactory.setVirtualHost(virtualHost);
        connectionFactory.setPublisherConfirms(true); //必须要设置
        return connectionFactory;
    }

    @Bean
    public DefaultMessageHandlerMethodFactory myHandlerMethodFactory() {
        DefaultMessageHandlerMethodFactory factory = new DefaultMessageHandlerMethodFactory();
        factory.setMessageConverter(new MappingJackson2MessageConverter());
        return factory;
    }

    @Bean
    public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory() {
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory());
        factory.setConcurrentConsumers(concurrentConsumers);
        factory.setMaxConcurrentConsumers(maxConcurrentConsumers);
        factory.setPrefetchCount(prefetch);
        factory.setAcknowledgeMode(AcknowledgeMode.AUTO);
        return factory;
    }

    @Override
    public void configureRabbitListeners(RabbitListenerEndpointRegistrar rabbitListenerEndpointRegistrar) {
        rabbitListenerEndpointRegistrar.setMessageHandlerMethodFactory(myHandlerMethodFactory());
    }


}

4.Exchange和Queue配置

public class RabbitMqBind {

	
	 @Bean 
	 public CustomExchange delayExchange() {
		 Map args = new HashMap<>(); 
		 args.put("x-delayed-type", "direct");
		 return new CustomExchange("test_exchange", "x-delayed-message",true, false,args); 
		 }
	
	 @Bean
	    public Queue queue() {
	        Queue queue = new Queue("test_queue_1", true);
	        return queue;
	    }
	 @Bean
	    public Binding binding() {
	        return BindingBuilder.bind(queue()).to(delayExchange()).with("test_queue_1").noargs();
	    }
	 
}

 

5.实现消息发送

public class RabbitMqSend {
	 @Autowired
	  private RabbitTemplate rabbitTemplate;
	 
	 public void sendMsg(String queueName,String msg) { 
		 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		 System.out.println("消息发送时间:"+sdf.format(new Date())); 
		 rabbitTemplate.convertAndSend("test_exchange", queueName, msg, new MessagePostProcessor() { 
			 @Override 
			 public Message postProcessMessage(Message message) throws AmqpException { 
				 message.getMessageProperties().setHeader("x-delay",1000*60); 
				
				 return message; 
				 
				 } }); 
		 }

}

6.消费者

@Component
public class RabbitMqList {
	 @RabbitListener(queues = "test_queue_1")
	 public void receive(String msg) {
		 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
		 System.out.println("消息接收时间:"+sdf.format(new Date())); 
		 System.out.println("接收到的消息:"+msg); 
		 }
	 }

7.测试

@RunWith(SpringRunner.class)
@SpringBootTest
public class TestMq {
	@Autowired
	RabbitMqSend rabbitMqSend;
	@Test
	public Object m(){
		rabbitMqSend.sendMsg("test_queue_1", "HEHEH");
	}

}

 

8.测试结果

 

rabbitmq延迟消息_第1张图片

 

 

 

 

你可能感兴趣的:(rabbitmq延迟消息)