栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringBoot整合ActiveMQ

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot整合ActiveMQ

1.生产者

目录

1.生产者

1.1.引入依赖

1.2.引入YML配置(application.yml)

1.3.创建QueueConfig配置

1.4.创建Producer(消费者)

1.5.启动

2.消费者

2.1.引入依赖

2.2.引入YML配置(application.yml)

2.3.创建Consumer(消费者)

2.4.启动


1.1.引入依赖

"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">

 

        org.springframework.boot

        spring-boot-starter-parent

        1.5.4.RELEASE

       

 

  4.0.0

  com.me.activeMQ

  MyActiveMQProducer

  0.0.1-SNAPSHOT

  war

 

    UTF-8

    UTF-8

    1.8

 

 

       

            org.springframework.boot

            spring-boot-starter

       

       

       

            org.springframework.boot

            spring-boot-starter-web

       

       

            org.springframework.boot

            spring-boot-starter-test

            test

       

       

            org.springframework.boot

            spring-boot-starter-activemq

       

   

 

       

           

                 org.springframework.boot

                spring-boot-maven-plugin

           

           

                org.apache.maven.plugins

                maven-war-plugin

               

                    false

               

           

       

   

1.2.引入YML配置(application.yml)

spring:

  activemq:

    broker-url: tcp://127.0.0.1:61616

    user: admin

    password: admin

queue: kmx.pas.job.sgtest

1.3.创建QueueConfig配置

package com.me.homesickness.activeMQ.config;

import javax.jms.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;

@Configuration

public class QueueConfig {

    @Value("${queue}")

    private String queue;

   

    @Bean

    public Queue logQueue() {

        return new ActiveMQQueue(queue);

    }

   

}

1.4.创建Producer(消费者)

package com.me.homesickness.activeMQ;

import javax.jms.Queue;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.jms.core.JmsMessagingTemplate;

import org.springframework.scheduling.annotation.EnableScheduling;

import org.springframework.scheduling.annotation.Scheduled;

import org.springframework.stereotype.Component;

@Component

@EnableScheduling

public class Producer {

    @Autowired

    private JmsMessagingTemplate jmsMessagingTemplate;

    @Autowired

    private Queue queue;

    @Scheduled(fixedDelay = 5000)

    public void send() {

        jmsMessagingTemplate.convertAndSend(queue, "测试消息队列" + System.currentTimeMillis());

    }

   

}

1.5.启动

package com.me.homesickness;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication

public class MyActiveMQProducer {

    public static void main(String[] args) {

        SpringApplication.run(MyActiveMQProducer.class, args);

    }

}

2.消费者

2.1.引入依赖

"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">

  4.0.0

  com.me.activeMQ

  MyActiveMQConsumer

  0.0.1-SNAPSHOT

  war

 

        org.springframework.boot

        spring-boot-starter-parent

        1.5.4.RELEASE

       

 

 

    UTF-8

    UTF-8

    1.8

 

 

       

            org.springframework.boot

            spring-boot-starter

       

       

       

            org.springframework.boot

            spring-boot-starter-web

       

       

            org.springframework.boot

            spring-boot-starter-test

            test

       

       

            org.springframework.boot

            spring-boot-starter-activemq

       

   

 

       

           

                org.apache.maven.plugins

                maven-war-plugin

               

                    false

               

           

           

                org.springframework.boot

                spring-boot-maven-plugin

           

       

   

2.2.引入YML配置(application.yml)

spring:

  activemq:

    broker-url: tcp://127.0.0.1:61616

    user: admin

    password: admin

queue: kmx.pas.job.sgtest

2.3.创建Consumer(消费者)

package com.me.homesickness.activeMQ;

import org.springframework.jms.annotation.JmsListener;

import org.springframework.stereotype.Component;

@Component

public class Consumer {

    @JmsListener(destination = "${queue}")

    public void receive(String msg) {

        System.out.println("监听器收到msg:" + msg);

    }

   

}

2.4.启动

package com.me.homesickness.activeMQ;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication

public class MyActiveMQConsumer {

    public static void main(String[] args) {

        SpringApplication.run(MyActiveMQConsumer.class, args);

    }

}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/351435.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号