一、问题答案

  是不可以的

而且后注册的会替换前注册的,MqConsumer2会替换MqConsumer,并且只结束tag-2的消息

/**
* @date 2019/05/28
*/
@Component
@Slf4j
public class MqConsumer implements MessageConsumer { @Override
@Transactional(rollbackFor = Throwable.class, propagation = Propagation.REQUIRED)
public void onMessage(String msg) {
log.info("接收到的库存MQ消息:{}", msg);
log.info("接收到的库存MQ消息:{}", msg);
log.info("接收到的库存MQ消息:{}", msg);
} @Override
public String getTopic() {
return "topic-1";
} @Override
public String getTag() {
return "tag-1";
}
}
@Component
@Slf4j
public class MqConsumer2 implements MessageConsumer { @Override
@Transactional(rollbackFor = Throwable.class, propagation = Propagation.REQUIRED)
public void onMessage(String msg) {
log.info("接收到的库存MQ消息:{}", msg);
log.info("接收到的库存MQ消息:{}", msg);
log.info("接收到的库存MQ消息:{}", msg);
} @Override
public String getTopic() {
return "topic-1";
} @Override
public String getTag() {
return "tag-2";
}
}

二、为什么呢?

我们从源码的角度来分析下

1.订阅消息的方法 public void subscribe(String topic, String subExpression, MessageListener listener) ,其中subExpression即为tag

package com.aliyun.openservices.ons.api.impl.rocketmq;
....
@Generated("ons-client")
public class ConsumerImpl extends ONSConsumerAbstract implements Consumer {
private final ConcurrentHashMap<String, MessageListener> subscribeTable = new ConcurrentHashMap<String, MessageListener>(); public ConsumerImpl(final Properties properties) {
super(properties);
boolean postSubscriptionWhenPull = Boolean.parseBoolean(properties.getProperty(PropertyKeyConst.PostSubscriptionWhenPull, "false"));
this.defaultMQPushConsumer.setPostSubscriptionWhenPull(postSubscriptionWhenPull); String messageModel = properties.getProperty(PropertyKeyConst.MessageModel, PropertyValueConst.CLUSTERING);
this.defaultMQPushConsumer.setMessageModel(MessageModel.valueOf(messageModel));
} @Override
public void start() {
this.defaultMQPushConsumer.registerMessageListener(new MessageListenerImpl());
super.start();
} @Override
public void subscribe(String topic, String subExpression, MessageListener listener) {
if (null == topic) {
throw new ONSClientException("topic is null");
} if (null == listener) {
throw new ONSClientException("listener is null");
}
this.subscribeTable.put(topic, listener);
super.subscribe(topic, subExpression);
} .....
}

从上面的类中我们可以从this.subscribeTable.put(topic, listener);看到subscribeTable这样的一个Map,该Map与tag无关

2.我们再看super.subscribe(topic, subExpression)方法,属于ONSConsumerAbstract类中

protected void subscribe(String topic, String subExpression) {
try {
this.defaultMQPushConsumer.subscribe(topic, subExpression);
} catch (MQClientException e) {
throw new ONSClientException("defaultMQPushConsumer subscribe exception", e);
}
}

DefaultMQPushConsumer中:

@Override
public void subscribe(String topic, String subExpression) throws MQClientException {
this.defaultMQPushConsumerImpl.subscribe(withNamespace(topic), subExpression);
}

DefaultMQPushConsumerImpl中:

public void subscribe(String topic, String subExpression) throws MQClientException {
try {
      //此处用来构建订阅数据,并且指定了tag
SubscriptionData subscriptionData = FilterAPI.buildSubscriptionData(this.defaultMQPushConsumer.getConsumerGroup(),
topic, subExpression);
      //此处将topic和该topic的订阅数据存放到subscriptionInner这个Map中
   // protected final ConcurrentMap<String /* topic */, SubscriptionData> subscriptionInner = new ConcurrentHashMap<String, SubscriptionData>();
this.rebalanceImpl.getSubscriptionInner().put(topic, subscriptionData);
if (this.mQClientFactory != null) {
this.mQClientFactory.sendHeartbeatToAllBrokerWithLock();
}
} catch (Exception e) {
throw new MQClientException("subscription exception", e);
}
}

三、总结

从上面简单的源码可以看到,有用到两个Map,

subscribeTable 和 subscriptionInner ,并且Map的key都为topic。所以我们可以笃定,RocketMQ在同一个项目中,只支持注册一个topic消费者,那么也就只能指定一个tag

【RocketMQ】同一个项目中,同一个topic,可以存在多个消费者么?的更多相关文章

  1. django同一个项目中连接多个数据库

    一.场景与思路 同一个项目中需要连接多个数据库. 二.代码 代码中主要是三个部分,settings.models以及自己写的一个类. 1.自己写的文件:database_app_router.py  ...

  2. Swift & Objc 在同一个项目中的使用

    在WWDC大会中发布了Swift让人眼前一亮.终于加了很多的现代编程语言该有的东西.很早年以前玩C#3.0+的时候这些差不多类似的 已经用的烂熟的东西终于一点一点的在看Swift Programmin ...

  3. 项目中同一个页面引入不同的jQuery版本的不冲突问题

    在写项目的过程中,如果需要使用jQuery时,时长会遇到需要引入不同版本的jQuery,可能上一个负责该项目的人用到的是老版本的jQuery,而你去添加功能时用的是新版本的,这个问题很难避免掉,如果去 ...

  4. 在同一个项目中灵活运用application/json 和application/x-www-form-urlencoded 两种传输格式(配合axios,同时配置loading)

    'use strict' import axios from 'axios' // import qs from 'qs' import { Notification} from 'element-u ...

  5. 项目中同一个dll的x86和x64同时引用

    <ItemGroup Condition=" '$(Platform)' == 'x86' "> <Reference Include="System. ...

  6. IntelliJ IDEA导入多个eclipse项目到同一个workspace下

    IntelliJ IDEA 与eclipse在新建项目上工作区的叫法略有不同,区别见下图. 我们在eclipse都是在新建的workspace目录下新建我们的项目,但是在IDEA中没有workspac ...

  7. 如何在 ETL 项目中统一管理上百个 SSIS 包的日志和包配置框架

    一直准备写这么一篇有关 SSIS 日志系统的文章,但是发现很难一次写的很完整.因为这篇文章的内容可扩展的性太强,每多扩展一部分就意味着需要更多代码,示例和理论支撑.因此,我选择我觉得比较通用的 LOG ...

  8. MVC3中在同一解决方案的不同项目中实现Area功能

    1.背景      微软在MVC中引入了Area概念,用于复杂项目的分工开发.如一个MVC项目中Controller过多时,就会导致项目中包含大量的Controller+View+Model,无论是查 ...

  9. class类名在webpack项目中的两种引用方式

    一.问题描述 在项目工程中,我们通常既用到css module,也用到普通的less文件引用方式,代码及webpack配置如下,运行时,发现只有css module起作用,如何让两者都起作用呢? // ...

随机推荐

  1. LeetCode 101. Symmetric Tree(镜像树)

    Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...

  2. [转]解决 gem install 安装失败

    链接地址:https://blog.csdn.net/weixin_42414461/article/details/85337465

  3. 移芯EC616修改记录

    1. FOTA升级的不用修改了,发布的版本已经修改过. 2. 添加AT+LPNM和AT+LGMR

  4. 腾讯云直播+点播全线产品支持AV1,带来极致视频体验

    日前,腾讯视频云直播.点播.媒体处理全线产品均已支持AV1标准,据悉,腾讯云也是国内首家直播+点播同时支持AV1视频处理业务的公有云厂商. 据悉,AV1(Alliance for Open Media ...

  5. sizeof(类名字)

    析构函数,跟构造函数这些成员函数,是跟sizeof无关的,因为我们的sizeof是针对实例,而普通成员函数,是针对类体的,一个类的成员函数,多个实例也共用相同的函数指针,所以自然不能归为实例的大小. ...

  6. understanding backpropagation

    几个有助于加深对反向传播算法直观理解的网页,包括普通前向神经网络,卷积神经网络以及利用BP对一般性函数求导 A Visual Explanation of the Back Propagation A ...

  7. CentOS7-Docker 安装 Gitlab

    官方教程 https://docs.gitlab.com/omnibus/docker/ 搜索镜像 [root@master ~]# docker search gitlab 拉取镜像 [root@m ...

  8. 嵌入式02 STM32 实验08 外部中断

    一.中断 由于某个事件的发生,CPU暂停当前正在执行的程序,转而执行处理事件的一个程序.该程序执行完成后,CPU接着执行被暂停的程序.这个过程称为中断.(我正在捉泥鳅,但是我妈喊我回家吃饭,我必须回家 ...

  9. 关于Django数据库mysql连接错误问题Connection to api@localhost failed. [08001] Could not create connection to d

    Connection to api@localhost failed. [08001] Could not create connection to d 错误类型 django连接mysql数据库错误 ...

  10. cloudera cdh6.3 离线安装 经典大数据平台视频教程(含网盘下载地址)

    cdh6.3企业级大数据视频教程 链接:https://pan.baidu.com/s/1bLGrIwzpFQB-pQRb6KOmNg 提取码:i8h8 系统和软件版本1,操作系统:Centos7.6 ...