pom.xml
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId> </dependency>
application.yml
spring:
rabbitmq:
host: localhost port: 5672 username: admin password: 123456
RabbitConfig
@Configuration public class RabbitConfig { @Bean public Queue helloQueue() { return new Queue("hello"); } @Bean public Queue neoQueue() { return new Queue("neo"); } @Bean public Queue objectQueue() { return new Queue("object"); } }
Sender
@Component public class HelloSender { @Autowired private AmqpTemplate rabbitTemplate; public void send() { String context = "hello " + new Date(); System.out.println("Sender : " + context); this.rabbitTemplate.convertAndSend("hello", context); } }
Receiver
@Component @RabbitListener(queues = "hello") public class HelloReceiver { @RabbitHandler public void process(String hello) { System.out.println("Receiver : " + hello); } }