In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article shows you how to operate ActiveMQ in spring boot. The content is concise and easy to understand. It will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
Preface
Message queuing middleware is an important component in distributed system, which mainly solves the problems of application coupling, asynchronous message and traffic sharpening, and realizes high performance, high availability, scalability and ultimate consistency architecture. it is an indispensable middleware for large-scale distributed systems.
At present, the message queues commonly used in production environment are ActiveMQ, RabbitMQ, ZeroMQ, Kafka, MetaMQ, RocketMQ and so on.
Characteristics
Asynchrony: the time-consuming synchronization operation is asynchronized by sending a message, which reduces the time of synchronization waiting.
Loose coupling: message queuing reduces the coupling between services, and different services can communicate through message queues without paying attention to each other's implementation details, as long as the format of the message is defined.
Distributed: through horizontal expansion to consumers, the risk of message queue blocking is reduced, as well as the possibility of a single point of failure for a single consumer (of course, the message queue itself can also be made into a distributed cluster).
Reliability: message queuing generally stores the received messages on the local hard disk (when the messages are processed, the stored information may be deleted according to different message queuing implementations). In this way, even if the application hangs or the message queue itself dies, the message can be reloaded.
JMS specification
JMS, the Java message service (Java Message Service) application program interface, is an API oriented to message middleware (MOM) in a Java platform, which is used to send messages between two applications or in distributed systems for asynchronous communication. Java message service is a platform-independent API, and most MOM providers provide support for JMS.
There are two models of JMS message mechanism, one is Point to Point, which is in the form of queue, and the message sent can only be picked up by one receiver; the other is Topic, which can be subscribed by multiple subscribers, similar to group sending.
ActiveMQ is an implementation of JMS.
ActiveMQ introduction
ActiveMQ is an open source software under the Apache Software Foundation. It follows the JMS1.1 specification (Java Message Service) and is a message-driven middleware software (MOM). It provides high availability, excellent performance, scalability, stability, and security for enterprise messaging. ActiveMQ uses the Apache license agreement, so anyone can use and modify it without feedback on any changes.
The goal of ActiveMQ is to provide a standard, message-driven application integration on as many platforms and languages as possible. ActiveMQ implements the JMS specification and provides a large number of additional features on top of it. ActiveMQ supports message delivery in both queue and subscription modes.
Spring Boot provides the ActiveMQ component spring-boot-starter-activemq, which is used to support the use of ActiveMQ within the Spring Boot system. Let's take a closer look at how to use it.
Add dependency
The main add component: spring-boot-starter-activemq.
Org.springframework.boot spring-boot-starter-activemq
Configuration file
Add a configuration in application.properties.
# memory-based ActiveMQspring.activemq.in-memory=true# is not suitable for connection pool spring.activemq.pool.enabled=false # standalone installation of ActiveMQ#spring.activemq.broker-url=tcp://192.168.0.1:61616#spring.activemq.user=admin#spring.activemq.password=admin
There are two ways to use ActiveMQ, one is to use a standalone installation of ActiveMQ, which is recommended in a production environment, and the other is to use memory-based ActiveMQ, which is recommended during debugging.
Queue (Queue)
Messages sent by the queue can only be received by one consumer.
Create a queue
@ Configurationpublic class MqConfig {@ Bean public Queue queue () {return new ActiveMQQueue ("neo.queue");}}
Using the @ Configuration annotation, when the project starts, a queue queue is defined and named: neo.queue.
Message producer
Create a producer of the message:
@ Componentpublic class Producer {@ Autowired private JmsMessagingTemplate jmsMessagingTemplate; @ Autowired private Queue queue; public void sendQueue (String msg) {System.out.println ("send queue msg:" + msg); this.jmsMessagingTemplate.convertAndSend (this.queue, msg);}}
JmsMessagingTemplate is a utility class that Spring provides to send messages, using JmsMessagingTemplate and the created queue to send messages.
Message consumer
@ Componentpublic class Consumer {@ JmsListener (destination = "neo.queue") public void receiveQueue (String text) {System.out.println ("Consumer queue msg:" + text);}}
Use the annotation @ JmsListener (destination = "neo.queue") to indicate that this method monitors a queue named neo.queue. The execution of this method is triggered when a message is sent in the queue neo.queue, and the text is the message content.
test
Create a SampleActiveMqTests test class and inject the created message producer.
RunWith (SpringRunner.class) @ SpringBootTestpublic class SampleActiveMqTests {@ Autowired private Producer producer; @ Rule public OutputCapture outputCapture = new OutputCapture ();}
OutputCapture is a test class provided by Spring Boot, which can capture the output of System.out and System.err. We can use this feature to determine whether the output in the program is executed or not.
@ Testpublic void sendSimpleQueueMessage () throws InterruptedException {this.producer.sendQueue ("Test queue message"); Thread.sleep (1000L); assertThat (this.outputCapture.toString () .contains ("Test queue")) .isTrue ();}
Create a test method and use producer to send messages. In order to ensure that the container can receive the message, let the test method wait for 1 second, and finally use outputCapture to determine whether the execution is successful.
Test multi-consumer
The above case is just one producer and one consumer, and we are simulating the implementation of a producer and multiple consumer queues. Let's copy the above consumer Consumer and rename it to Consumer2, and add the keyword of 2 to the output, as follows:
@ Componentpublic class Consumer2 {@ JmsListener (destination = "neo.queue") public void receiveQueue (String text) {System.out.println ("Consumer2 queue msg:" + text);}}
Add a send100QueueMessage () method to the test class just now to see how the two consumers consume the message when the pattern sends 100 messages.
@ Testpublic void send100QueueMessage () throws InterruptedException {for (int iTuno connectionFactory I topicListenerFactory (ConnectionFactory connectionFactory) {DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory (); factory.setConnectionFactory (connectionFactory); factory.setPubSubDomain (true); return factory;}}
Then, in the consumer receiving method, it is indicated that the message is received using containerFactory.
@ Componentpublic class Consumer {@ JmsListener (destination = "neo.queue", containerFactory = "queueListenerFactory") public void receiveQueue (String text) {System.out.println ("Consumer queue msg:" + text);} @ JmsListener (destination = "neo.topic", containerFactory = "topicListenerFactory") public void receiveTopic (String text) {System.out.println ("Consumer topic msg:" + text);}}
After the transformation is completed, execute the queue and broadcast test methods again, and you will find that the project supports both types of message sending and receiving at the same time.
Message middleware is widely used in large-scale Internet architectures. using the respective characteristics of message middleware queue and broadcast, it can support many services, such as sending text messages in groups, sending e-mails to individual users and so on. ActiveMQ is a very popular message middleware, which is characterized by simple deployment, easy to use, and more suitable for small and medium-sized teams. Spring Boot provides components for integrating ActiveMQ, and you only need to add relevant annotations to use ActiveMQ in Spring Boot.
The above is how to operate ActiveMQ in spring boot. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.