Application scenarios for current projects
Use activeMQ to send messages asynchronously
Modification depending on the actual application of the project
ActiveMQ Basic Concepts
ActiveMQ is an open source message middleware based on JMS.
Message middleware has many uses and advantages:
1. Transfer data from one application to another, or from one module of the software to another;
2. Be responsible for the establishment of network communication channels and the reliable transmission of data.
3. Guarantee data not to be retransmitted and lost
4. It can realize cross-platform operation and provide data transmission service for software integration technicians on different operating systems.
The theoretical knowledge of JMS can be viewed as follows, which is not described here.
http://blog.csdn.net/jiuqiyuliang/article/details/46701559
ActiveMQ feature list
- Multilingualism and protocol writing client. Language: Java, C, C++, C#, Ruby, Perl, Python, PHP. Application protocols: OpenWire,Stomp REST,WS Notification,XMPP,AMQP
- Full support for JMS 1.1 and J2EE 1.4 specifications (persistence, XA messages, transactions)
- With Spring support, ActiveMQ can be easily embedded in systems that use Spring, and it also supports Spring 2.0 features.
- Through the common J2EE servers (such as Geronimo,JBoss 4, GlassFish,WebLogic) test, which through the JCA 1.5 resource adaptors configuration, ActiveMQ can be automatically deployed to any J2EE 1.4-compatible commercial server.
- Support multiple transport protocols: in-VM,TCP,SSL,NIO,UDP,JGroups,JXTA
- Supporting high-speed message persistence through JDBC and journal
- Design guarantees high performance cluster, client-server, point-to-point
- Support Ajax
- Support for integration with Axis
- It's easy to call an embedded JMS provider for testing
Under what circumstances will ActiveMQ be used?
- Integration among multiple projects
(1) Cross-platform
(2) Multilingualism
(3) Multiple projects - Decoupling and Decoupling of Modules between Systems
(1) Software extensibility - Front-end and back-end isolation of the system
(1) Front and rear end isolation, shielding high safety zone
Integration of ActeMQ and SpringBook
ActiveMQ Installation and Operation
Unzip after downloading 5.15.9, double-click the activemq.bat file under bin directory or run activeme.bat under your own computer version (win64) to see the effect.
If active MQ webConsole available at http://0.0.0.0:8161/ That is, it runs successfully and goes to http://127.0.0.1:8161/. You can see the following page
ActiveMQ configuration
Note: Versions 5.15 and above require jdk1.8 and above support. The author uses the latest version 5.15.9 on the official website.
First configure dependencies in pom.xml
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-activemq</artifactId> </dependency> <dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-pool</artifactId> </dependency>
Then the yml configuration of ActiveMQ:
activemq: user: admin password: admin broker-url: tcp://127.0.0.1:61616 in-memory: true pool: enabled: false max-connections: 50
Problems encountered in configuration
The following candidates were found but could not be injected: - Bean method 'jmsMessagingTemplate' in 'JmsAutoConfiguration.MessagingTemplateConfiguration' not loaded because Ancestor org.springframework.boot.autoconfigure.jms.JmsAutoConfiguration did not match Action: Consider revisiting the entries above or defining a bean of type 'org.springframework.jms.core.JmsMessagingTemplate' in your configuration.
This error occurred in the configuration. I checked a lot of information on the Internet and said that there was one space missing. However, there was not much in the actual configuration.
After many tests, it was found that activemq-pool-enabled used true, and it did not appear after changing to false.
In the current configuration, the value of the custom message queue is emailQueue: the message queue used to process sending mail
Injecting Queue examples into startup classes
@EnableJms @SpringBootApplication public class MQApplication{ public static void main(String[] args) { SpringApplication.run(MQApplication.class, args); } @Bean public ActiveMQQueue emailQueue() { return new ActiveMQQueue("emailQueue"); } }
Producer java code
@Component public class MQProducer implements CommandLineRunner { @Resource private JmsMessagingTemplate jmsMessagingTemplate; @Resource private ActiveMQQueue queue; @Override public void run(String... args) { // send("Connect"); System.out.println("Establish a connection"); } public void send(Map<String,Object> map) { this.jmsMessagingTemplate.convertAndSend(this.queue, map); } }
Consumer Monitoring Method
@Component public class MQConsumer { @Resource private IEmailService emailService; @JmsListener(destination ="emailQueue") public void receiveQueue(Map<String,Object> map){ System.out.println(JSONObject.toJSONString(map)); emailService.sendEmail(map); } }
Writing the logic of sending short messages in receiveQueue method can complete message queue and realize the method of sending mail.
Ending
At present, only the function of sending mail has been implemented in p2p mode, only the most obvious way to use ActiveMQ. In the future, other functions of ActiveMQ, such as information transfer between different systems, will be used only after the function is extended.