ActiveMQ_與SpringBoot整合

首先創建一個SpringBoot工程,以下是依賴包

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


<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-activemq</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

ActiveMQ 隊列 生產者代碼

application.yml

server:
  port: 8080

spring:
  activemq:
    broker-url: tcp://192.168.253.132:61616   # activeMQ服務器地址
    user: admin
    password: admin
  jms:
    pub-sub-domain: false  # false = Queue, true = Topic

#自己隊列的名稱
queue-name: boot-activemq-queue

創建一個配置類,註冊Queue隊列

import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.annotation.EnableJms;

import javax.jms.Queue;

/**
 * @Author: lgz
 * @Date: 2019/11/30 1:15
 */
@Configuration
@EnableJms
public class QueueConfigBean {

    @Value("${queue-name}")
    private String queueName;

    @Bean
    public Queue getQueue() {
        return new ActiveMQQueue(queueName);
    }
}

producer類代碼,每隔5秒發送一條消息

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

import javax.jms.Queue;

/**
 * @Author: lgz
 * @Date: 2019/11/30 1:18
 */
@Component
public class QueueProducer {

    @Autowired
    private Queue queue;
    @Autowired
    private JmsMessagingTemplate jmsMessagingTemplate;

    @Scheduled(fixedDelay = 5000)
    public void sendMessage() {
        jmsMessagingTemplate.convertAndSend(queue, "hello activemq --> " + System.currentTimeMillis());
        System.out.println("send successfully......");
    }

}

因爲加了定時器,所以要在application啓動類加上開啓註解

@EnableScheduling

ActiveMQ 隊列 消費者代碼

依賴包和配置不變,只需要一個類和一個註解即可

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

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

/**
 * @Author: lgz
 * @Date: 2019/11/30 1:28
 */
@Component
public class QueueConsumer {

    @JmsListener(destination = "${queue-name}")
    public void getMessage(TextMessage textMessage) throws JMSException {
        String text = textMessage.getText();
        System.out.println("consumer 收到消息 --> " + text);
    }
}

ActiveMQ 主題 生產者代碼

application.yml

server:
  port: 8080

spring:
  activemq:
    broker-url: tcp://192.168.253.132:61616   # activeMQ服務器地址
    user: admin
    password: admin
  jms:
    pub-sub-domain: true  # false = Queue, true = Topic

#自己主題的名稱
topic-name: boot-activemq-topic

創建一個配置類,註冊Topic主題

import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.annotation.EnableJms;

import javax.jms.Topic;

/**
 * @Author: lgz
 * @Date: 2019/11/30 1:15
 */
@Configuration
@EnableJms
public class TopicConfigBean {

    @Value("${topic-name}")
    private String topicName;

    @Bean
    public Topic getTopic() {
        return new ActiveMQTopic(topicName);
    }
}

producer類代碼,每隔5秒發送一條消息

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

import javax.jms.Topic;

/**
 * @Author: lgz
 * @Date: 2019/11/30 1:18
 */
@Component
public class TopicProducer {

    @Autowired
    private Topic topic;
    @Autowired
    private JmsMessagingTemplate jmsMessagingTemplate;

    @Scheduled(fixedDelay = 5000)
    public void sendMessage() {
        jmsMessagingTemplate.convertAndSend(topic, "hello activemq topic --> " + System.currentTimeMillis());
        System.out.println("send successfully......");
    }

}

因爲加了定時器,所以要在application啓動類加上開啓註解

@EnableScheduling

ActiveMQ 主題 消費者代碼

依賴包和配置不變,只需要一個類和一個註解即可

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

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

/**
 * @Author: lgz
 * @Date: 2019/11/30 1:28
 */
@Component
public class TopicConsumer {

    @JmsListener(destination = "${topic-name}")
    public void getMessage(TextMessage textMessage) throws JMSException {
        String text = textMessage.getText();
        System.out.println("consumer 收到消息 --> " + text);
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章