Java弹簧引导整合RabbitMQ(一):Hello World -B2B2C小程序电子商务

  

  

  

  

     

  

  

  

  

  

  

  

  

  

  
 & lt; dependency>
  ,,,& lt; groupId> org.springframework.boot
  ,,,& lt; artifactId> spring-boot-starter-amqp
  & lt;/dependency> 
  

  

  
春:
  配置文件才能:
  ,,,活跃:usage_message
  rabbitmq才能:
  ,,,端口:5672
  教程:
  客户:才能
  ,,,时间:10000年,
  

生产者   

春AMQP让我们用少量的代码就能轻松实现消息的发送和接收。通过注入AmqpTemplate接口的实例来实现消息的发送,AmqpTemplate接口定义了一套针对AMQP协议的基础操作。在春天引导中会根据配置来注入其具体实现(AmqpTemplate的默认实现就是RabbitTemplate)。

  <>以前public  class  Tut1Sender  {   ,,@ autowired   ,,,private  AmqpTemplate 模板;   ,,@ autowired   ,,,private  Queue 队列;   ,,,/* *   ,,,,*,用定时任务来模拟生产者定时发送消息   ,,,*/,,,@Scheduled  (=fixedDelay  1000年,initialDelay =, 500)   ,,,public  void 发送(),{   ,,,,,,,String  message =,“Hello 世界!“,+,new 日期();   ,,,,,,,template.convertAndSend (queue.getName(),,消息);   ,,,,,,,System.out.println (“, [x], Sent  & # 39;“, +, message  +,“& # 39;“);   ,,,}   }   

  

     

  

  

  

  
 @RabbitListener (=queues “hello-world")
  public  class  Tut1Receiver  {
  ,,@RabbitHandler
  ,,,public  void 接收(String 在),{
  ,,,,,,,System.out.println (“, [x], Received  & # 39;“, +,拷贝+,“& # 39;“);
  ,,,}
  }
  

     
  

  <>以前@Profile ({“tut1",,“hello-world"})   @ configuration   public  class  Tut1Config  {   ,,@ bean   ,,,public  Queue 队列(),{   ,,,,,,,return  new 队列(“hello-world");   ,,,}   ,,@Profile (“receiver")   ,,@ bean   ,,,public  Tut1Receiver 接收机(),{   ,,,,,,,return  new  Tut1Receiver ();   ,,,}   ,,@Profile (“sender")   ,,@Bean    ,,,public  Tut1Sender 发送方(),{   ,,,,,,,return  new  Tut1Sender ();   ,,,}   }   

  

  

  

     

  

  

  

  <>以前@SpringBootApplication   @EnableScheduling   public  class  RabbitmqTutorialApplication  {   ,,,public  static  void  main (String [], args), {   ,,,,,,,new  SpringApplicationBuilder ()   ,,,,,,,,,,,,,,,.sources (RabbitmqTutorialApplication.class)   ,,,,,,,,,,,,,,,//,设置成非,web 环境   ,,,,,,,,,,,,,,了对,web这类(WebApplicationType.NONE)   ,,,,,,,,,,,,,,,.run (args);   ,,,}   ,,@Profile (“usage_message")   ,,@Bean    ,,,public  CommandLineRunner 使用(),{   ,,,,,,,return  arg0 →, {   ,,,,,,,,,,,System.out.println(“却;能够app  uses  Spring  Profiles 用control  its 行为。\ n");   ,,,,,,,,,,,System.out.println (“Sample 用法:,java  -jar 目标/rabbitmq-tutorial-0.0.1-SNAPSHOT.jar ——spring.profiles.active=hello world, sender");   ,,,,,,,};   ,,,}   ,,,@Profile (“! usage_message")   ,,@ bean   ,,,,public  CommandLineRunner 教程(),{   ,,,,,,,return  new  RabbitTutorialRunner ();   ,,,}   }   

  

     

  

  <>以前public  class  RabbitTutorialRunner  implements  CommandLineRunner  {   ,,@ value (“$ {tutorial.client.duration: 0}“)   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null

Java弹簧引导整合RabbitMQ(一):Hello World -B2B2C小程序电子商务