springBoot + ActiveMQ(消息队列搭建)

AciveMQ是Apache出品的目前最流行,能力强劲的开源消息总线

使用AciveMQ  搭建非常简单,使用起来很方便,接下来 我将会把整个流程记录下来

第一步,先到官网下载ActiveMQ :http://activemq.apache.org/download.html

下载完之后进行解压,注意解压的文件路径要全英文路径,不能带有中文

进入bin目录文件

选择相对应的系统位数 

 运行activemq.bat

 当运行后出现一下图片的文字,证明你已经启动成功了

启动成功之后,在你的浏览器中访问http://localhost:8161/ 就能访问到ActiveMq的页面咯

点击画红线的选项,会弹出一个登陆框,账号和密码都是 admin

 

登陆后我们可以看到两个队列:分别是Queue 队列 和 Topic广播

Queue 队列:是有生产者生产一个消息,只能由一个消费者进行消费,类似于 1对1

Topic 广播:广播顾名思义就是让大家都知道,生产者生产一个消息,可以有多个消费者进行消费 类似于1对多

 点击queue,这里可以实时看到生产者生产的消息条数和消费者消费条数

第一阶段已经完成了,接下来要做的是代码的编程,也是你们所关注的springboot整合ActiveMQ

首先pom.xml添加依赖:

       <!-- ActiveMQ的启动器 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-activemq</artifactId>
        </dependency>

路径配置:

使用application.properties:

# MQ所在的服务器的地址
spring.activemq.broker-url=tcp://localhost:61616
# 是否使用内置的MQ, true  使用; fale  不使用
spring.activemq.in-memory=false 
# 是否在回滚回滚消息之前停止消息传递。这意味着当启用此命令时,消息顺序不会被保留。
spring.activemq.non-blocking-redelivery=false
# 用户名
spring.activemq.password=admin
# 密码
spring.activemq.user=admin

使用application.yml 配置:

spring:
    activemq:
        broker-url: tcp://localhost:61616
        user: admin
        password: admin
        pool:
          enabled: false
          max-connections: 10

生产者配置  : 消费者根据 生产者 jazzy.queue 的名字起找到相对应的消息 (名字根据自己实际情况取)

package com.jazzychenz.myfirstspringboot.config;

import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

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


@Configuration
public class ActiveMQConfig {
    @Bean
    public Queue queue(){
        return new ActiveMQQueue("jazzy.queue");
    }

    @Bean
    public Topic topic(){
        return new ActiveMQTopic("jazzy.topic");
    }
}

接下来做一个简单的案例:

生产者:

package com.jazzychenz.myfirstspringboot.controller;

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;

import javax.jms.Queue;
import java.util.HashMap;

@Component
@EnableScheduling
public class QueueProducer {

    @Autowired
    private JmsMessagingTemplate jmsMessagingTemplate;

    @Autowired
    private Queue queue;

    @Scheduled(fixedDelay = 3000)
    public void send(){
        try {
            System.out.println("-------我发的MQ-------");
            //ActiveMQMapMessage mapMessage = new ActiveMQMapMessage();
            //mapMessage.setString("info","你好");
            HashMap<String, Object> mapMessage = new HashMap<>();
            mapMessage.put("info","你好");

            this.jmsMessagingTemplate.convertAndSend(this.queue,mapMessage);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

消费者:

package com.jazzychenz.myfirstspringboot.controller;

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

import java.util.Map;

@Component
public class QueueConsumer {

    @JmsListener(destination = "jazzy.queue")
    public void receiveQueue(Map<String,Object> map){
        try {
            String info = (String) map.get("info");
            System.out.println(info);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

}

启动项目看一下实际情况

搭建成功  大家可以尽情的玩耍!!~~~ 谢谢你的观看  希望对你有用

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值