RabbitMQ is a commonly used AMQP implementation. This article is a simple tutorial on integrating RabbitMQ with Spring boot.
Install ActiveMQ server (you can also not install it. If it is not installed, you will use memory mq)
Build Spring boot project and add dependencies. Just add this item
<!-- Add activemq dependency--><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-activemq</artifactId></dependency>
Add Application class
@SpringBootApplication@EnableScheduling //Send messages using a timed task public class MqTestApplication { public static void main(String[] args) { SpringApplication.run(MqTestApplication.class, args); }} Configure application.yml
spring: activemq: broker-url: tcp://127.0.01:61616 packages: trust-all: true
To build a data model, the data types that can be sent and consumed are: String, byte array, Map<String,?>, Serializable object.
// If the message sent is an object, it must implements Serializable interface public class TModel implements Serializable { private static final long serialVersionUID = -921008687184331557L; private int count; public TModel(int count) { this.count = count; } @Override public String toString() { return "TModel [count=" + count + "]"; }} Build a Producer
@Componentpublic class Producer { // Inject JmsTemplate into the Producer, we can send messages through this template private final JmsTemplate jmsTemplate; private int count = 0; @Autowired public Producer(JmsTemplate jmsTemplate) { this.jmsTemplate = jmsTemplate; } // Here we use Spring Boot's timed task to send messages @Scheduled(fixedRate = 1000) public void create() { // Use convertAndSend to send messages jmsTemplate.convertAndSend("queue1", new TModel(count++)); }} Build Consumer
@Componentpublic class Consumer { @JmsListener(destination = "queue1") public void comsume(TModel content) { System.out.println("recive message from queue1 [" + content + "]"); }}Special Note: If our producers and consumers are in different modules, it is best to abstract the data to be consumed into a public module. The program serializes and deserializes objects through Serializable. It is necessary to ensure that the serialVersionUID of the object model of the producer and consumer is consistent.
Project address: https://github.com/ldwqh0/active-mq-spring.git
Example: Configure rabbitmq and add a queue
@Configurationpublic class Aqueue {@Beanpublic Queue queue() {return new Queue("good");}}Define a producer.
When activemq is enabled, an AmqpTemplate will be automatically created, which can be injected anywhere you need. We can send messages to MQ through this AmqpTemplate
/*** Define a producer* @author LiDong*/@RestController@RequestMapping("/test")public class SendController {@Autowiredprivate AmqpTemplate template;@GetMappingpublic String testSend() {// Send message using AmqpTemplate template.convertAndSend("good", "good");return "success";}} Define the consumer, specify the queue of consumption by specifying RabbitListener(queues='good')
@Componentpublic class Consumer {/*** Define a consumer* @param message*/@RabbitListener(queues = "good")public void handler(String message) {System.out.println("recive message from " + message);}}Start the test and enter http://localhost:8080/test in the browser to send a message to the queue. This pair of columns can be processed by the consumer
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.