RabbitMQ学习(十二)之spring整合发送异步消息(注解实现)

时间:2025-04-23 14:34:39
  • package ;  
  •   
  • import ;  
  •   
  • import ;  
  • import ;  
  • import ;  
  • import ;  
  • import ;  
  • import ;  
  • import ;  
  • import ;  
  • import ;  
  •   
  • import ;  
  •   
  • @Configuration  
  • public class ProducerConfiguration {  
  •   
  •     // 指定队列名称 routingkey的名称默认为Queue的名称,使用Exchange类型为DirectExchange  
  •     protected final String helloWorldQueueName = "spring-queue-async";  
  •   
  •     // 创建链接  
  •     @Bean  
  •     public ConnectionFactory connectionFactory() {  
  •         CachingConnectionFactory connectionFactory = new CachingConnectionFactory("192.168.36.102");  
  •         ("admin");  
  •         ("admin");  
  •         ();  
  •         return connectionFactory;  
  •     }  
  •   
  •     // 创建rabbitTemplate 消息模板类  
  •     @Bean  
  •     public RabbitTemplate rabbitTemplate() {  
  •         RabbitTemplate template = new RabbitTemplate(connectionFactory());  
  •         (this.helloWorldQueueName);  
  •         return template;  
  •     }  
  •   
  •     //创建一个调度  
  •     @Bean  
  •     public ScheduledProducer scheduledProducer() {  
  •         return new ScheduledProducer();  
  •     }  
  •   
  •     @Bean  
  •     public BeanPostProcessor postProcessor() {  
  •         return new ScheduledAnnotationBeanPostProcessor();  
  •     }  
  •   
  •       
  •     static class ScheduledProducer {  
  •   
  •         @Autowired  
  •         private volatile RabbitTemplate rabbitTemplate;  
  •   
  •         //自增整数  
  •         private final AtomicInteger counter = new AtomicInteger();  
  •         /** 
  •          * 每3秒发送一条消息 
  •          *  
  •          * Spring3中加强了注解的使用,其中计划任务也得到了增强,现在创建一个计划任务只需要两步就完成了: 
  •         创建一个Java类,添加一个无参无返回值的方法,在方法上用@Scheduled注解修饰一下; 
  •         在Spring配置文件中添加三个<task:**** />节点; 
  •         参考:/blog/949123 
  •          */  
  •         @Scheduled(fixedRate = 3000)  
  •         public void sendMessage() {  
  •             ("Hello World " + ());  
  •         }  
  •     }  
  •   
  • }