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.引入依赖

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

  <parent>

        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-starter-parent</artifactId>

        <version>1.5.4.RELEASE</version>

        <relativePath/> <!-- lookup parent from repository -->

  </parent>

  <modelVersion>4.0.0</modelVersion>

  <groupId>com.me.activeMQ</groupId>

  <artifactId>MyActiveMQProducer</artifactId>

  <version>0.0.1-SNAPSHOT</version>

  <packaging>war</packaging>

  <properties>

    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

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

  </properties>

  <dependencies>

        <dependency>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-starter</artifactId>

        </dependency>

        <!-- spring boot web支持:mvc,aop... -->

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

    </dependencies>

  <build>

        <plugins>

            <plugin>

                 <groupId>org.springframework.boot</groupId>

                <artifactId>spring-boot-maven-plugin</artifactId>

            </plugin>

            <plugin>

                <groupId>org.apache.maven.plugins</groupId>

                <artifactId>maven-war-plugin</artifactId>

                <configuration>

                    <failOnMissingWebXml>false</failOnMissingWebXml>

                </configuration>

            </plugin>

        </plugins>

    </build>

</project>

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;

/**

 * 生产者

 * @author Administrator

 *

 */

@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.引入依赖

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

  <groupId>com.me.activeMQ</groupId>

  <artifactId>MyActiveMQConsumer</artifactId>

  <version>0.0.1-SNAPSHOT</version>

  <packaging>war</packaging>

  <parent>

        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-starter-parent</artifactId>

        <version>1.5.4.RELEASE</version>

        <relativePath/>

  </parent>

  <properties>

    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

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

  </properties>

  <dependencies>

        <dependency>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-starter</artifactId>

        </dependency>

        <!-- spring boot web支持:mvc,aop... -->

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

    </dependencies>

  <build>

        <plugins>

            <plugin>

                <groupId>org.apache.maven.plugins</groupId>

                <artifactId>maven-war-plugin</artifactId>

                <configuration>

                    <failOnMissingWebXml>false</failOnMissingWebXml>

                </configuration>

            </plugin>

            <plugin>

                <groupId>org.springframework.boot</groupId>

                <artifactId>spring-boot-maven-plugin</artifactId>

            </plugin>

        </plugins>

    </build>

</project>

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;

/**

 * 消息消费者

 * @author Administrator

 *

 */

@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;

/**

 * 消费者

 * @author Administrator

 *

 */

@SpringBootApplication

public class MyActiveMQConsumer {

    public static void main(String[] args) {

        SpringApplication.run(MyActiveMQConsumer.class, args);

    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

任风雨

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值