Amqp starter introduced under item 1. springBoot
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
amqp-starter will be introduced below the jar package
[INFO] \- org.springframework.boot:spring-boot-starter-amqp:jar:2.1.5.RELEASE:compile
[INFO] +- org.springframework:spring-messaging:jar:5.1.7.RELEASE:compile
[INFO] \- org.springframework.amqp:spring-rabbit:jar:2.1.6.RELEASE:compile
[INFO] +- org.springframework.amqp:spring-amqp:jar:2.1.6.RELEASE:compile
[INFO] | \- org.springframework.retry:spring-retry:jar:1.2.4.RELEASE:compile
[INFO] +- com.rabbitmq:amqp-client:jar:5.4.3:compile
[INFO] | \- org.slf4j:slf4j-api:jar:1.7.26:compile
[INFO] \- org.springframework:spring-tx:jar:5.1.7.RELEASE:compile
Configuration rabbitMQ attributes 2. springBoot profile
spring.rabbitmq.host=127.0.0.1
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
3. Create Queue and injected into the spring container
@Configuration
public class QueueConfig {
@Bean
public Queue queue() {
return new Queue("test_queue");
}
}
4. Sender sends data to the Queue
@Component
public class Sender {
@Autowired
private AmqpTemplate template;
public void send(String message) {
template.convertAndSend("test_queue",message);
}
}
Wherein AmqpTemplate SpringBoot is generated default implementation used can be introduced directly inside the code.
5. Receiver consumption data in the Queue
@Component
@RabbitListener(queues = "test_queue")
public class Receiver {
Logger logger = LoggerFactory.getLogger(Receiver.class.getName());
@RabbitHandler
public void process(String message) {
logger.info(message);
}
}
Add listener Queue, the message is received and processed. Note, receiver to be poured into the container (@Component) to listen to the message.
Direct use of the above ways to use queue. Then use the Topic, Fanout ...
Reproduced in: https: //www.jianshu.com/p/9cf3e883b4c0