参考文档:
1、配置
spring.rabbitmq.host=localhostspring.rabbitmq.port=5672spring.rabbitmq.username=testspring.rabbitmq.password=testspring.rabbitmq.virtual-host=test
2、代码
@SpringBootApplication // 自动配置@RabbitListener(queues = "foo") // 监听foo队列@EnableScheduling // 开启定时任务public class SampleAmqpSimpleApplication { // 类似xml 定义 send bean @Bean public Sender mySender() { return new Sender(); } // 类似xml 定义 队列 @Bean public Queue fooQueue() { return new Queue("foo"); } // 消费者处理 @RabbitHandler public void process(@Payload String foo) { System.out.println(new Date() + ": " + foo); } // 程序入口 public static void main(String[] args) throws Exception { SpringApplication.run(SampleAmqpSimpleApplication.class, args); }}
生产者
public class Sender { @Autowired private RabbitTemplate rabbitTemplate; @Scheduled(fixedDelay = 1000L) public void send() { this.rabbitTemplate.convertAndSend("foo", "hello"); }}