The main content of this chapter:
Mainly from the following aspects:
Mybatis and spring boot integration, redis (stand-alone version) and spring boot integration, redis (cluster) and spring boot integration, httpclient and spring boot integration, rabbitMQ/active MQ and spring boot integration.
Welcome to Kaige's personal blog website: www.kaigejava.com
Welcome to the public account of Kaige: Kaige Java (kaigejava)
Next, we will learn the second section: spring boot integrates redis to open cache
Main content of this section:
1: Spring boot integrates active mq scheme one
Solution 1 is simple, both the producer and the consumer are in the same application project.
One: active mq related
1: Active mq download:
2: start
Under the bin folder, there are 64 and 32-bit folders that find their own system responses to open. Kai is using 64 bit
Double-click the activemq.bat batch to start it.
Start as shown below:
2: Application of active mq in spring boot
2.1: Add mq-related dependencies to the pom.xml file.
org.springframework.boot
spring-boot-starter-activemq
org.apache.activemq
activemq-pool
2.2: MQ configuration class
Note: @Configuration or other spring annotations must be used. This class can be managed by spring.
setBrokerURL is to add url
The complete code is as follows:
package com.kaigejava.springconfig.mq;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.pool.PooledConnectionFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.connection.SingleConnectionFactory;
import org.springframework.jms.core.JmsTemplate;
@Configuration
public class ActiveMQConfig {
@Bean
public ActiveMQConnectionFactory targetConnectionFactory(){
ActiveMQConnectionFactory targetConnectionFactory = new ActiveMQConnectionFactory();
targetConnectionFactory.setBrokerURL("tcp://localhost:61616");
return targetConnectionFactory;
}
@Bean
public SingleConnectionFactory connectionFactory( PooledConnectionFactory targetConnectionFactory){
SingleConnectionFactory connectionFactory = new SingleConnectionFactory();
connectionFactory.setTargetConnectionFactory(targetConnectionFactory);
return connectionFactory;
}
@Bean
public PooledConnectionFactory pooledConnectionFactory(ActiveMQConnectionFactory targetConnectionFactory){
PooledConnectionFactory pooledConnectionFactory = new PooledConnectionFactory();
pooledConnectionFactory.setConnectionFactory(targetConnectionFactory);
pooledConnectionFactory.setMaxConnections(10);
return pooledConnectionFactory;
}
@Bean
public JmsTemplate jmsTemplate(SingleConnectionFactory connectionFactory){
JmsTemplate jmsTemplate = new JmsTemplate();
jmsTemplate.setConnectionFactory(connectionFactory);
return jmsTemplate;
}
}
2.3: Producer configuration:
Here is the MQ producer used when logging.
/**
*
* @ClassName: Producer
* @Description: The generator that records logs through MQ in the background
* @author Kai Ge Java
*
*
*/
@Service("adminLogProducer")
public class AdminLogProducer {
@Autowired // You can also inject JmsTemplate, JmsMessagingTemplate encapsulates JmsTemplate
private JmsMessagingTemplate jmsTemplate;
// Send a message, destination is the queue to send to, message is the message to be sent
public void sendMessage(Destination destination, final String message){
jmsTemplate.convertAndSend(destination, message);
}
}
2.4: Consumers: Consumers conduct consumption processing.
2.5: Use in the service layer:
Steps for usage:
2.5.1: inject mq producer
2.5.2: Preparing data ready for launch
2.5.3: Create a target and specify consumers
Destination destination = new ActiveMQQueue("adminLogConsumerQuenue.loggingClassName");
2.5.4: Call the method of the producer to send
adminLogProducer.sendMessage(destination,sendmsg);
2.6: The producer sends the class:
So far, the spring boot integration active MQ scheme is completed.
Other articles in this tutorial series:
Spring boot framework learns important annotations to master before school (1) - java configuration of sprng
Spring boot framework learns important annotations to master before school (2) - configure spring through java configuration.
Important annotations for boot framework learning before school (3) - reading external resource configuration files through annotations
Spring boot framework learns important annotations before school (4) - reading external resource configuration files through annotations 2
Boot Framework Learning 1 - Understanding Spring Boot and Quick Start
Spring boot framework learning 2-spring boot core (1)
Spring boot framework learning 3-spring boot core (2)
Spring boot framework learning 4-spring boot core (3)
Spring boot framework learning 5-spring boot web development (1)
Spring boot framework learning 6-spring boot web development (2)
Spring boot framework learning 7-spring boot web development (3) - custom message converter
Spring boot framework learning 8-[Dry goods] spring boot web development (4)-custom interceptor processing permissions
Spring boot framework learning 9-spring boot web development (5) - error resolution and jump pages
Spring boot framework learning 10-spring boot persistent data
Spring boot framework learning 11-spring boot integration redis