ActiveMQ - SpringBoot integration ActiveMQ queue

Integrated producers

Creating the Maven Project

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>sprinboot_activemq</artifactId>
    <version>1.0-SNAPSHOT</version>


    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.5.RELEASE</version>
        <relativePath/>
    </parent>

    <properties>
        <projet.build.sourceEncoding>UTF-8</projet.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-activemq</artifactId>
            <version>2.1.5.RELEASE</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

Configuration SpringBoot

application.yml

server:
  port: 7777
spring:
  activemq:
    broker-url: tcp://localhost:61616 #自己的MQ服务器地址
    user: admin
    password: admin

  jms:
    pub-sub-domain: false    # false: Queue  true:Topic


#自己定义队列名称
myqueue: boot-activemq-queue

Configuration Bean

package pers.zhang.config;

        import org.apache.activemq.command.ActiveMQQueue;
        import org.springframework.beans.factory.annotation.Value;
        import org.springframework.context.annotation.Bean;
        import org.springframework.stereotype.Component;

        import javax.jms.Queue;


@Component
@EnableJms//开启JMS适配注解
public class ConfigBean {

    @Value("${myqueue}")
    private String myqueue;

    @Bean
    public Queue queue(){
        return new ActiveMQQueue(myqueue);
    }
}

sender

package pers.zhang.test;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Component;

import javax.jms.Queue;
import java.util.UUID;

@Component
public class Queue_Produce {

    @Autowired
    private JmsMessagingTemplate jmsMessagingTemplate;

    @Autowired
    private Queue queue;

    public void produceMsg(){
        jmsMessagingTemplate.convertAndSend(queue, "*****" + UUID.randomUUID().toString().substring(0, 6));
    }
}

Master Boot class

@SpringBootApplication
public class MainApp_Produce {
    public static void main(String[] args) {
        SpringApplication.run(MainApp_Produce.class, args);
    }
}

Write test classes

package pers.zhang;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import pers.zhang.test.Queue_Produce;

import javax.annotation.Resource;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = MainApp_Produce.class)
@WebAppConfiguration
public class TestActive {

    @Resource
    private Queue_Produce queue_produce;

    @Test
    public void testSend() throws Exception{
        queue_produce.produceMsg();
    }
}

Running the test method:
Here Insert Picture Description

Push message every 3 seconds to MQ:
Timing increase Queue_Produce delivery methods:

//间隔3定时投递
    @Scheduled(fixedDelay = 3000)
    public void produceMdgScheduled(){
        jmsMessagingTemplate.convertAndSend(queue, "*****" + UUID.randomUUID().toString().substring(0, 6));
        System.out.println("3秒一次...");
    }

Modify the master boot class MainApp_Produce:

@SpringBootApplication
@EnableScheduling//开启
public class MainApp_Produce {
    public static void main(String[] args) {
        SpringApplication.run(MainApp_Produce.class, args);
    }
}

Run the main class starts, the console print:

3秒一次...
3秒一次...
3秒一次...

Here Insert Picture Description

Integration of consumer

Creating the Maven project, consistent with the pom.xml producer.

Placed application.yml

server:
  port: 8888
spring:
  activemq:
    broker-url: tcp://localhost:61616 #自己的MQ服务器地址
    user: admin
    password: admin

  jms:
    pub-sub-domain: false    # false: Queue  true:Topic


#自己定义队列名称
myqueue: boot-activemq-queue

consumer

Monitor news:

package pers.zhang.activemq;

import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

import javax.jms.JMSException;
import javax.jms.TextMessage;

@Component
public class Queue_Consumer {

    @JmsListener(destination = "${myqueue}")
    public void receive(TextMessage textMessage)throws JMSException {
        System.out.println("消费者收到消息:" + textMessage.getText());
    }
}

Master Boot class

@SpringBootApplication
public class MainApp_Consumer {
    public static void main(String[] args) {
        SpringApplication.run(MainApp_Consumer.class, args);
    }
}

Run, console print:

消费者收到消息:*****88d60d
消费者收到消息:*****b9c57e
消费者收到消息:*****5116ae
消费者收到消息:*****b59d0b
消费者收到消息:*****0806c6
消费者收到消息:*****af7056
消费者收到消息:*****9895bd

Here Insert Picture Description

Published 651 original articles · won praise 1884 · Views 230,000 +

Guess you like

Origin blog.csdn.net/cold___play/article/details/104064954