Spring boot framework learning 12-spring boot integration active mq method 1

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


network anomaly Cancel re-upload

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:


network anomaly Cancel re-upload

2: start


network anomaly Cancel re-upload

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:


network anomaly Cancel re-upload

2: Application of active mq in spring boot

2.1: Add mq-related dependencies to the pom.xml file.


network anomaly Cancel re-upload

 

 

org.springframework.boot

spring-boot-starter-activemq

 

 

org.apache.activemq

activemq-pool

 

 

2.2: MQ configuration class


network anomaly Cancel re-upload

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.


network anomaly Cancel re-upload

2.5: Use in the service layer:


network anomaly Cancel re-upload

network anomaly Cancel re-upload

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:


network anomaly Cancel re-upload

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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326480573&siteId=291194637