spring boot + active mq 简单使用示例

安装active mq

win版简单。官网下载 http://activemq.apache.org/components/classic/download/

win下启动

   

双击 apache-activemq-5.15.9\bin\win64 下的 activemq.bat启动active 。当然你电脑是 32位的。就去32位下启动这个bat

启动成功

访问地址  http://localhost:8161/admin/

你的账号密码在这里

登陆成功:

项目demo

引入依赖

<dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-activemq</artifactId>
</dependency>

这里这种方式会导致项目中。所有的queue和topic都是根据ture来判断其到底是ture还是false

同时支持两种写法看另一篇博客 activeMq同时支持topic和queue

activemq配置yml

server:
  port: 8090
spring:
  activemq:
    broker-url: tcp://localhost:61616
    user: admin
    password: admin
  jms:
    pub-sub-domain: true #配置为true 则会导致项目中创建的所有topic和queue都是 topic
                         #配置为false 则会导致项目中创建的所有topic和queue都是 queue

activemq 配置

queue和topic 区别

queue队列 不重复消费

消息生产者生产消息发送到queue中,然后消息消费者从queue中取出并且消费消息。
消息被消费以后,queue中不再有存储,所以消息消费者不可能消费到已经被消费的消息。
Queue支持存在多个消费者,但是对一个消息而言,只会有一个消费者可以消费、其它的则不能消费此消息了。
当消费者不存在时,消息会一直保存,直到有消费消费

订阅 Topic,可以重复消费

消息生产者(发布)将消息发布到topic中,同时有多个消息消费者(订阅)消费该消息。
和点对点方式不同,发布到topic的消息会被所有订阅者消费。
当生产者发布消息,不管是否有消费者。都不会保存消息

    配置queue

package com.example.demo.config;

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

import javax.jms.Queue;

@Configuration
public class ActiveMqQueueConfig {

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

    配置topic

package com.example.demo.config;

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

import javax.jms.Topic;

@Configuration
public class ActiveMqTopicConfig {

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

配置controller

import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.jms.*;

/**
 * @author 
 * @Date: 2019/11/22 17:59
 * @Description:
 */
@RestController
@RequestMapping("/api")
public class ActiveMqClientController {
    @Autowired
    private JmsTemplate jmsTemplate;

    @RequestMapping("/send_topic")
    public void sendTopic(){
        jmsTemplate.send("my_topic", new MessageCreator() {
            @Override
            public Message createMessage(Session session) throws JMSException {
                TextMessage textMessage = session.createTextMessage();
                textMessage.setText("send topic_data");
                return textMessage;
            }
        });
    }
    @RequestMapping("/send_queue")
    public void sendQueue(){
        jmsTemplate.send("my_queue", new MessageCreator() {
            @Override
            public Message createMessage(Session session) throws JMSException {
                TextMessage textMessage = session.createTextMessage();
                textMessage.setText("send queue_data");
                return textMessage;
            }
        });
    }

}

配置消息消费server

package com.example.demo.server;

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

/**
 * @author 
 * @Date: 2019/11/22 18:01
 * @Description:
 */
@Component
public class ActiveMqServer {
    @JmsListener(destination = "my_topic")
    public void receiveTopic(String message) {
        System.out.println("监听topic=============监听topic");
        System.out.println(message);

    }

    @JmsListener(destination = "my_queue")
    public void receiveQueue(String message) {
        System.out.println("监听queue=============监听queue");
        System.out.println(message);

    }
}

发送请求  http://localhost:8090/api/send_topic     

                http://localhost:8090/api/send_queue

获取到消息。进行消费

简单的使用就这样。其他的详细使用

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值