gogoWebsite

RabbitMQ learning (twelve) spring integration sends asynchronous messages (annotation implementation)

Updated to 2 days ago
  • package ;  
  •   
  • import ;  
  •   
  • import ;  
  • import ;  
  • import ;  
  • import ;  
  • import ;  
  • import ;  
  • import ;  
  • import ;  
  • import ;  
  •   
  • import ;  
  •   
  • @Configuration  
  • public class ProducerConfiguration {  
  •   
  •     // Specify the queue name The name of the routingkey defaults to the name of the Queue, and use the Exchange type to DirectExchange  
  •     protected final String helloWorldQueueName = "spring-queue-async";  
  •   
  •     // Create a link  
  •     @Bean  
  •     public ConnectionFactory connectionFactory() {  
  •         CachingConnectionFactory connectionFactory = new CachingConnectionFactory("192.168.36.102");  
  •         ("admin");  
  •         ("admin");  
  •         ();  
  •         return connectionFactory;  
  •     }  
  •   
  •     // Create rabbitTemplate message template class  
  •     @Bean  
  •     public RabbitTemplate rabbitTemplate() {  
  •         RabbitTemplate template = new RabbitTemplate(connectionFactory());  
  •         (this.helloWorldQueueName);  
  •         return template;  
  •     }  
  •   
  •     //Create a schedule  
  •     @Bean  
  •     public ScheduledProducer scheduledProducer() {  
  •         return new ScheduledProducer();  
  •     }  
  •   
  •     @Bean  
  •     public BeanPostProcessor postProcessor() {  
  •         return new ScheduledAnnotationBeanPostProcessor();  
  •     }  
  •   
  •       
  •     static class ScheduledProducer {  
  •   
  •         @Autowired  
  •         private volatile RabbitTemplate rabbitTemplate;  
  •   
  •         //Increment integer  
  •         private final AtomicInteger counter = new AtomicInteger();  
  •         /** 
  • * Send a message every 3 seconds 
  •          *  
  • * The use of annotations has been strengthened in Spring 3, and the planning tasks have also been enhanced. Now, creating a planning task only takes two steps to complete: 
  • Create a Java class, add a method with no parameters and no return value, and modify it with @Scheduled annotation on the method; 
  • Add three <task:****/> nodes to the Spring configuration file; 
  • Reference: /blog/949123 
  •          */  
  •         @Scheduled(fixedRate = 3000)  
  •         public void sendMessage() {  
  •             ("Hello World " + ());  
  •         }  
  •     }  
  •   
  • }