springboot整合ActiveMQ(點對點+發佈訂閱)

生產者項目

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

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Finchley.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </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-activemq</artifactId>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.62</version>
        </dependency>

    </dependencies>

application.yml

spring:
  activemq:
    #Mq連接通訊地址
    broker-url: tcp://127.0.0.1:61616
    #賬號
    user: admin
    #密碼
    password: admin

#自定義隊列名稱
my_queue: springboot-queue
server:
  port: 8080

config類

package com.vhukze.config;

import org.apache.activemq.command.ActiveMQQueue;
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.Queue;
import javax.jms.Topic;


@Component
public class ConfigQueue {

    @Value("${my_queue}")
    private String myQueue;

    //首先將隊列注入到springboot容器中
    @Bean
    public Queue queue(){
        return new ActiveMQQueue(myQueue);
    }

//    //首先將主題注入到springboot容器中
//    @Bean
//    public Topic topic(){
//        return new ActiveMQTopic(myQueue);
//    }
}

生產者類

package com.vhukze.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 javax.jms.Topic;

@Component
public class P2pProducer {
    @Autowired
    private JmsMessagingTemplate jmsMessagingTemplate;

    @Autowired
    private Queue queue;

//    @Autowired
//    private Topic topic;

    //每隔五秒向隊列發送消息
    @Scheduled(fixedDelay = 5000)
    public void send(){
        String msg = System.currentTimeMillis() + "";
        jmsMessagingTemplate.convertAndSend(queue,msg);
//        jmsMessagingTemplate.convertAndSend(topic,str);
        System.out.println("採用點對點模式發送消息"+msg);
    }
}

注意:在啓動類添加開啓定時任務 

@EnableScheduling

 

消費者

依賴依然是上面的那些

配置文件把端口號改了,8080生產者用了

消費者類

package com.vhukze.consumer;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

@Component
public class P2pConsumer {

    //使用此註解即可監聽指定名稱的隊列
    @JmsListener(destination = "${my_queue}")
    public void recive(String msg){

        System.out.println("採用點對點模式消費者成功獲取到生產者的消息:"+msg);
    }

}

這是點對點模式,發佈訂閱模式只需要把代碼中註釋打開,換了Queue的即可

然後在配置文件中添加開啓發布訂閱模式

 

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