springboot整合activemq隊列和主題

   隊列模式是1對1模式,對於消費和生產者先啓動哪一個都可以,信息都會被消費,

    主題需要先啓動消費者,就如同公衆號一樣,只有訂閱後才能收到消息,並且訂閱之前的消息不會收到

1:隊列消費者

      首先是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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com</groupId>
    <artifactId>active</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>active</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <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.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

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

</project>

然後是application.yml配置

#端口號
server:
  port: 7778
spring:
  activemq:
    broker-url: tcp://ip:61616 #地址
    user: admin                             #用戶名
    password: admin                          #密碼
  jms:
    pub-sub-domain: false                  #false 隊列  true 主題
myqueue: boot-queuename                    #隊列名稱,myqueue隨意起,boot-queuename隨意起

編寫消費者代碼

package com.active.consumer;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

import javax.jms.TextMessage;

@Component
public class QueueConsumer {
    //myqueue 和配置文件名稱一致
    @Value("${myqueue}")
    public String queuename;

    //監聽者  用戶監聽是否有信息,有則打印
    @JmsListener(destination = "${myqueue}")
    public void reserve(TextMessage textMessage) throws Exception{
        System.out.println("消費了消息=="+textMessage.getText());
    }
}

然後啓動類啓動,注意啓動類SpringApplication.run(ActiveApplication.class, args)中的ActiveApplication.class必須是啓動類的名稱

package com.active;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ActiveApplication {

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

}

進入activemq網頁發現有一個消費者

 

2:生產者

    首先是pom文件,和消費者的一樣

    然後是application.yml配置文件,注意除了端口號不一致,其他的保持一致

server:
  port: 7777
spring:
  activemq:
    broker-url: tcp://ip:61616
    user: admin
    password: admin
  jms:
    pub-sub-domain: false
myqueue: boot-queuename

編寫生產者工具類

package com.active.util;

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
public class ConfigUtil {
    @Value("${myqueue}")
    private String queueName;

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

編寫生產者

package com.active.producer;

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;
import java.util.UUID;

@Component
public class QueueProducer {
    @Autowired
    public JmsMessagingTemplate jmsMessagingTemplate;
    @Autowired
    public Queue queue;

    @Scheduled(fixedDelay = 3000L)
    public void produceMessage(){
        jmsMessagingTemplate.convertAndSend(queue,"queue生產的消息=="+ UUID.randomUUID().toString().substring(0,6));
        System.out.println("生產了消息");
    }
}

啓動類啓動

package com.active;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
@EnableScheduling
public class ActiveApplication {

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

}

可以看到信息被消費了,也可通過後臺查看

3:Topic主題消費者

    首先是pom文件同上

    然後是配置文件,配置文件主要的修改地方是true和false

server:
  port: 7779
spring:
  activemq:
    broker-url: tcp://ip:61616
    user: admin
    password: admin
  jms:
    pub-sub-domain: true
mytopic: boot-topicName

編寫消費者代碼

package com.example.active.consumer;

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

import javax.jms.TextMessage;

@Component
public class Consumer {
     //mytopic和配置文件名稱相同
    @JmsListener(destination = "${mytopic}")
    public void reserve(TextMessage textMessage) throws Exception{
        System.out.println("消費了消息===="+textMessage.getText());
    }
}

最後啓動類啓動,查看activemq頁面中的Topics

4:編寫主題生產者

   pom文件同上,application.yml文件只需修改端口號即可

  然後是工具類

package com.example.active.util;

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

import javax.jms.Topic;

@Component
public class ConfigUtil {
    @Value("${mytopic}")
    private String topicName;

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

生產者代碼

package com.example.active.product;

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;
import java.util.UUID;

@Component
public class Producter {
    @Autowired
    private JmsMessagingTemplate jmsMessagingTemplate;
    @Autowired
    private Topic topic;

    @Scheduled(fixedDelay = 3000L)
    public void produceTopic(){
        jmsMessagingTemplate.convertAndSend(topic,"主題消息生產"+ UUID.randomUUID().toString().substring(0,6));
        System.out.println("主題生產了消息");
    }
}

最後啓動類啓動,可以看到生產了消息並且被消費

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章