activeMq使用

一、Linux下ActiveMQ安装

1.下载并解压

wget https://mirrors.tuna.tsinghua.edu.cn/apache//activemq/5.15.9/apache-activemq-5.15.9-bin.tar.gz
tar zxvf apache-activemq-5.15.9-bin.tar.gz 

2.运行

cd bin/
./activemq start

3.进入管理界面

浏览器访问192.168.0.1:8161/admin/,默认用户名和密码为:admin/admin,控制台截图如下:

列表中信息含义如下:

  • Number Of Pending Messages:等待消费的消息 这个是当前未出队列的数量。

  • Number Of Consumers:消费者 这个是消费者端的消费者数量

  • Messages Enqueued:进入队列的消息 进入队列的总数量,包括出队列的。

  • Messages Dequeued:出了队列的消息 可以理解为是消费这消费掉的数量。

二、发送队列消息

队列模式特点:

  1. 客户端包括生产者和消费者。
  2. 队列中的一个消息只能被一个消费者使用。
  3. 消费者可以随时取消息。

application.properties配置如下:

#连接地址
spring.activemq.broker-url=tcp://192.168.0.1:61616
#如果是点对点(queue),那么此处默认应该是false,如果发布订阅,那么一定设置为true
spring.jms.pub-sub-domain=false

ActivemqConfig.java配置:

/**
 * 点对点
 */
@Bean
public Queue queue() {
    return new ActiveMQQueue("active.queue");
}

消息生产者SendController.java发送代码如下:

/*
 * 发送 队列消息
 */
@RequestMapping("/sendQueue")
public String sendQueue() {
    String message = UUID.randomUUID().toString();
    // 指定消息发送的目的地及内容
    this.jmsMessagingTemplate.convertAndSend(this.queue, message);
    return "消息发送成功!message=" + message;
}

消息消费者QueueCustomerController.java发送代码如下:

@RestController
public class QueueCustomerController {
/*
 * 监听和接收  队列消息
 */
@JmsListener(destination="active.queue")
public void readActiveQueue(String message) {
    System.out.println("接受到:" + message);
}
}

 

三、发送主题消息

主题模式特点:

  1. 客户端包括发布者和订阅者。
  2. 主题中的消息可以被所有订阅者消费。
  3. 消费者不能消费订阅之前发送的消息。

application.properties中修改属性:

spring.jms.pub-sub-domain=true

ActivemqConfig.java配置:

/**
 * 发布/订阅
 */
@Bean
public Topic topic() {
    return new ActiveMQTopic("active.topic");
}

消息生产者SendController.java发送代码如下:

/*
 * 发送 主题消息
 */
@RequestMapping("/sendTopic")
public String sendTopic() {
    String message = UUID.randomUUID().toString();
    // 指定消息发送的目的地及内容
    this.jmsMessagingTemplate.convertAndSend(this.topic, message);
    return "消息发送成功!message=" + message;
}

添加两个消息消费者,TopicCustomerController.java代码如下:

/*
 * 监听和接收  主题消息1
 */
@JmsListener(destination = "active.topic")
public void readActiveTopic1(String message) {
    System.out.println("Customer1接受到:" + message);
}

/*
 * 监听和接收  主题消息2
 */
@JmsListener(destination = "active.topic")
public void readActiveTopic2(String message) {
    System.out.println("Customer2接受到:" + message);
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值