SpringBoot整合ActiveMQ:主題 之 生產消費者

第一步:創建Maven項目

Maven依賴:

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
    </dependency>
    <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-test</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.junit.vintage</groupId>
                <artifactId>junit-vintage-engine</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-activemq</artifactId>
        <version>2.2.6.RELEASE</version>
    </dependency>

    <!--Spring事務處理-->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-tx</artifactId>
        <version>5.2.5.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jms</artifactId>
        <version>5.2.5.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.apache.activemq</groupId>
        <artifactId>activemq-all</artifactId>
        <version>5.15.12</version>
    </dependency>
    <!--消息隊列連接池-->
    <dependency>
        <groupId>org.apache.activemq</groupId>
        <artifactId>activemq-pool</artifactId>
        <version>5.15.12</version>
    </dependency>

application.yml:

server:
  port: 80
  servlet:
    context-path: /am

spring:
  activemq:
    broker-url: tcp://hcmaster:61616  #ActiveMQ服務器地址及端口
    user: admin
    password: admin
    close-timeout: 5000
    send-timeout: 3000

    # 下面五行配置加上程序報錯,程序啓動不起來
#    in-memory: false # true表示使用內置的MQ,false表示連接服務器
#    pool:
#      enabled: true # true表示使用連接池,false表示每發送一條數據就創建一個連接
#      max-connections: 10 #連接池最大連接數
#      idle-timeout: 30000 #空閒的連接過期時間,默認爲30s

  jms:
    pub-sub-domain: false # 默認值false表示Queue,true表示Topic

topicName: springboot-activemq-topic

# debug: true #顯示Debug信息

第二步:配置主題Bean

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


    @Bean //在Spring中注入一個名稱爲activeMQConfig的Bean
    public Topic topic(){
        return  new ActiveMQTopic(topicName);
    }
}

第三步:創建Producer

@Component
public class TopicProducer {
    //注入springboot封裝的工具類,它是Jmstemplate的封裝類
    @Autowired
    private JmsMessagingTemplate jmsMessagingTemplate;

    @Autowired
    private Topic topic;

    public void publishTopic(String msg) {
        System.out.println("TopicProvider發送了消息 : " + msg);
        //添加消息到消息隊列
        jmsMessagingTemplate.convertAndSend(topic, msg);
    }

    @Scheduled(fixedDelay = 3000)
    public void publishTopicScheduled(){
        String msg ="xixi";
        System.out.println("TopicProvider定時發送了消息 : " + msg+" "+System.currentTimeMillis());
        //方法一:添加消息到消息隊列
        jmsMessagingTemplate.convertAndSend(topic, msg+System.currentTimeMillis());
    }
}

第三步:創建Consumer

@Component
public class TopicConsumer {

    @JmsListener(destination = "${topicName}" , containerFactory="jmsListenerContainerTopic")
    public void receiveQueue(String text) {
        System.out.println("TopicConsumer消費了消息: "+text);
    }

    //需要給topic定義獨立的JmsListenerContainer
    @Bean
    public JmsListenerContainerFactory<?> jmsListenerContainerTopic(ConnectionFactory activeMQConnectionFactory) {
        DefaultJmsListenerContainerFactory bean = new DefaultJmsListenerContainerFactory();
        bean.setPubSubDomain(true);
        bean.setConnectionFactory(activeMQConnectionFactory);
        return bean;
    }

}

3.測試代碼

@RunWith(SpringRunner.class)
@SpringBootTest
@EnableScheduling
public class TopicTests {
    //注入springboot封裝的工具類,它是Jmstemplate的封裝類
    @Autowired
    private TopicProducer topicProducer;

    @Test
    public void testPublishTopic() {
        topicProducer.publishTopic("hehe");
    }

    @Test
    public void testPublishTopicScheduled(){
        topicProducer.publishTopicScheduled();
        try {
            System.in.read(); //讓間隔消費發佈一直進行下去
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

結果

在這裏插入圖片描述

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