activeMQ-10整合springboot

1)首先創(chuàng)建springboot項目,創(chuàng)建一個maven的web項目厕隧,然后引入依賴

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

然后修改打包方式

 <groupId>com.jjclub</groupId>
  <artifactId>springboot-activeMQ</artifactId>
  <packaging>jar</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>springboot-activeMQ Maven Webapp</name>
  <url>http://maven.apache.org</url>

完整pom文件如下:

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
   <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.4.RELEASE</version>
        <relativePath/>
    </parent>
  <groupId>com.jjclub</groupId>
  <artifactId>springboot-activeMQ</artifactId>
  <packaging>jar</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>springboot-activeMQ Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
   <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>
        </dependency>
        <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
<dependency>
     <groupId>org.apache.activemq</groupId>
     <artifactId>activemq-pool</artifactId>
     <version>5.14.5</version>
</dependency>
  </dependencies>
  <build>
    <finalName>springboot-activeMQ</finalName>
  </build>
</project>

添加啟動類

package com;

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


@SpringBootApplication
@Component("com")
@EnableScheduling
public class SpringStart {
    public static void main(String[] args) {
        SpringApplication.run(SpringStart.class, args);
    }
}

2)添加application.yml绰沥;配置服務(wù)端口以及activemq地址云挟,隊列和主題的名稱

server:
  port: 8080
  context-path: /pro
spring:
  activemq:
    user: admin
    password: admin
    broker-url: tcp://localhost:61616
    pool:
      enabled: true
      max-connections: 10
queueName: publish.queue
topicName: publish.topic

3)添加activeMQ配置類辫诅,對隊列和主題進行配置

package com.toll;

import javax.jms.Queue;
import javax.jms.Topic;

import org.apache.activemq.ActiveMQConnectionFactory;
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.context.annotation.Configuration;
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
import org.springframework.jms.config.JmsListenerContainerFactory;

@Configuration
public class ActiveMQConfig {

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

    @Value("${topicName}")
    private String topicName;
    @Value("${spring.activemq.user}")
    private String usrName;

    @Value("${spring.activemq.password}")
    private  String password;

    @Value("${spring.activemq.broker-url}")
    private  String brokerUrl;

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

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

    @Bean
    public ActiveMQConnectionFactory connectionFactory() {
        return new ActiveMQConnectionFactory(usrName, password, brokerUrl);
    }

    
    @Bean//
    public JmsListenerContainerFactory<?> jmsListenerContainerQueue(ActiveMQConnectionFactory connectionFactory){
        DefaultJmsListenerContainerFactory bean = new DefaultJmsListenerContainerFactory();
        bean.setConnectionFactory(connectionFactory);
        return bean;
    }

    @Bean
    public JmsListenerContainerFactory<?> jmsListenerContainerTopic(ActiveMQConnectionFactory connectionFactory){
        DefaultJmsListenerContainerFactory bean = new DefaultJmsListenerContainerFactory();
        //設(shè)置為發(fā)布訂閱方式, 默認情況下使用的生產(chǎn)消費者方式
        bean.setPubSubDomain(true);
        bean.setConnectionFactory(connectionFactory);
        return bean;
    }
}

4)編寫生產(chǎn)者

package com;


import java.util.Date;

import javax.jms.Queue;

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

@Component
public class PublishController {
    @Autowired
    private JmsMessagingTemplate jms;

    @Autowired
    private Queue queue;
    
    @Scheduled(fixedDelay = 5000) //上一次執(zhí)行完畢時間點之后5秒再執(zhí)行
    public void sendqueue(){
       jms.convertAndSend(queue,"消息是"+new Date());
       System.out.println("消息發(fā)送");
    }
}

5)編寫消費者

package com.toll;

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

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

@Service
public class BootSonsumer {

    @JmsListener(destination="${queueName}")
    public void msg(Message message) throws JMSException {
        System.out.println("收到的消息為:"+((TextMessage)message).getText());
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末法绵,一起剝皮案震驚了整個濱河市感混,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌礼烈,老刑警劉巖弧满,帶你破解...
    沈念sama閱讀 218,640評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異此熬,居然都是意外死亡庭呜,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,254評論 3 395
  • 文/潘曉璐 我一進店門犀忱,熙熙樓的掌柜王于貴愁眉苦臉地迎上來募谎,“玉大人,你說我怎么就攤上這事阴汇∈” “怎么了?”我有些...
    開封第一講書人閱讀 165,011評論 0 355
  • 文/不壞的土叔 我叫張陵搀庶,是天一觀的道長拐纱。 經(jīng)常有香客問我,道長哥倔,這世上最難降的妖魔是什么秸架? 我笑而不...
    開封第一講書人閱讀 58,755評論 1 294
  • 正文 為了忘掉前任,我火速辦了婚禮咆蒿,結(jié)果婚禮上东抹,老公的妹妹穿的比我還像新娘蚂子。我一直安慰自己,他們只是感情好缭黔,可當(dāng)我...
    茶點故事閱讀 67,774評論 6 392
  • 文/花漫 我一把揭開白布食茎。 她就那樣靜靜地躺著,像睡著了一般馏谨。 火紅的嫁衣襯著肌膚如雪董瞻。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,610評論 1 305
  • 那天田巴,我揣著相機與錄音,去河邊找鬼挟秤。 笑死壹哺,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的艘刚。 我是一名探鬼主播管宵,決...
    沈念sama閱讀 40,352評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼攀甚!你這毒婦竟也來了箩朴?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,257評論 0 276
  • 序言:老撾萬榮一對情侶失蹤秋度,失蹤者是張志新(化名)和其女友劉穎炸庞,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體荚斯,經(jīng)...
    沈念sama閱讀 45,717評論 1 315
  • 正文 獨居荒郊野嶺守林人離奇死亡埠居,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,894評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了事期。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片滥壕。...
    茶點故事閱讀 40,021評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖兽泣,靈堂內(nèi)的尸體忽然破棺而出绎橘,到底是詐尸還是另有隱情,我是刑警寧澤唠倦,帶...
    沈念sama閱讀 35,735評論 5 346
  • 正文 年R本政府宣布称鳞,位于F島的核電站,受9級特大地震影響稠鼻,放射性物質(zhì)發(fā)生泄漏胡岔。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,354評論 3 330
  • 文/蒙蒙 一枷餐、第九天 我趴在偏房一處隱蔽的房頂上張望靶瘸。 院中可真熱鬧苫亦,春花似錦、人聲如沸怨咪。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,936評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽诗眨。三九已至唉匾,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間匠楚,已是汗流浹背巍膘。 一陣腳步聲響...
    開封第一講書人閱讀 33,054評論 1 270
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留芋簿,地道東北人峡懈。 一個月前我還...
    沈念sama閱讀 48,224評論 3 371
  • 正文 我出身青樓,卻偏偏與公主長得像与斤,于是被迫代替她去往敵國和親肪康。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,974評論 2 355

推薦閱讀更多精彩內(nèi)容