样例
DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("organization");
consumer.setNamesrvAddr("172.22.0.64:9876");
consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET);
consumer.subscribe("my-topic", "*");
consumer.registerMessageListener(new MessageListenerOrderly() {
@Override
public ConsumeOrderlyStatus consumeMessage(List<MessageExt> msgs, ConsumeOrderlyContext context) {
for (MessageExt msg : msgs) {
System.out.println(new String(msg.getBody()));
}
return ConsumeOrderlyStatus.SUCCESS;
}
});
consumer.start();
主要参数
- ConsumeFromWhere 控制新的消费者组从哪个位置开始消费
枚举值 | 效果 |
---|
CONSUME_FROM_LAST_OFFSET | 从最新的消息开始消费 |
CONSUME_FROM_LAST_OFFSET_AND_FROM_MIN_WHEN_BOOT_FIRST | 废弃,效果同上 |
CONSUME_FROM_MIN_OFFSET | 废弃,效果同上 |
CONSUME_FROM_MAX_OFFSET | 废弃 ,效果同上 |
CONSUME_FROM_FIRST_OFFSET | 从最早的消息开始消费 |
CONSUME_FROM_TIMESTAMP | 从指定时间开始消费 |
注:如果使用CONSUME_FROM_TIMESTAMP ,需设置参数
DefaultMQPushConsumer.setConsumeTimestamp(“20131223171201”)
时间戳字符串格式为yyyyMMddHHmmss
- DefaultMQPushConsumer.subscribe(String topic, String subExpression)
subExpression参数为tag选择表达式
语法:
- 不过滤tag:"*" 或者null
- 根据多个tag过滤:“tag1 || tag2 || tag3”
- DefaultMQPushConsumer.registerMessageListener注册消费监听器
- 顺序消费监听器MessageListenerOrderly
返回ConsumeOrderlyStatus
枚举值 | 效果 |
---|
SUCCESS | 成功 |
ROLLBACK | 废弃 |
COMMIT | 废弃 |
SUSPEND_CURRENT_QUEUE_A_MOMENT | 暂停当前队列一会 |
- 并行消费监听器MessageListenerConcurrently
返回ConsumeConcurrentlyStatus
枚举值 | 效果 |
---|
CONSUME_SUCCESS | 成功 |
RECONSUME_LATER | 失败,稍后重试 |
源码
- org.apache.rocketmq.client.impl.consumer.RebalancePushImpl
switch (consumeFromWhere) {
case CONSUME_FROM_LAST_OFFSET_AND_FROM_MIN_WHEN_BOOT_FIRST:
case CONSUME_FROM_MIN_OFFSET:
case CONSUME_FROM_MAX_OFFSET:
case CONSUME_FROM_LAST_OFFSET: {
long lastOffset = offsetStore.readOffset(mq, ReadOffsetType.READ_FROM_STORE);
if (lastOffset >= 0) {
result = lastOffset;
}
else if (-1 == lastOffset) {
if (mq.getTopic().startsWith(MixAll.RETRY_GROUP_TOPIC_PREFIX)) {
result = 0L;
} else {
try {
result = this.mQClientFactory.getMQAdminImpl().maxOffset(mq);
} catch (MQClientException e) {
result = -1;
}
}
} else {
result = -1;
}
break;
}
case CONSUME_FROM_FIRST_OFFSET: {
long lastOffset = offsetStore.readOffset(mq, ReadOffsetType.READ_FROM_STORE);
if (lastOffset >= 0) {
result = lastOffset;
} else if (-1 == lastOffset) {
result = 0L;
} else {
result = -1;
}
break;
}
case CONSUME_FROM_TIMESTAMP: {
long lastOffset = offsetStore.readOffset(mq, ReadOffsetType.READ_FROM_STORE);
if (lastOffset >= 0) {
result = lastOffset;
} else if (-1 == lastOffset) {
if (mq.getTopic().startsWith(MixAll.RETRY_GROUP_TOPIC_PREFIX)) {
try {
result = this.mQClientFactory.getMQAdminImpl().maxOffset(mq);
} catch (MQClientException e) {
result = -1;
}
} else {
try {
long timestamp = UtilAll.parseDate(this.defaultMQPushConsumerImpl.getDefaultMQPushConsumer().getConsumeTimestamp(),
UtilAll.YYYYMMDDHHMMSS).getTime();
result = this.mQClientFactory.getMQAdminImpl().searchOffset(mq, timestamp);
} catch (MQClientException e) {
result = -1;
}
}
} else {
result = -1;
}
break;
}
default:
break;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
2020-04-11 spring 启动时自动运行
2020-04-11 spring cloud oauth2授权服务 默认tokenService配置源码
2020-04-11 spring cloud 搭建oauth2授权服务 使用redis存储令牌
2020-04-11 spring cloud oauth2授权服务 clientDetails配置源码
2020-04-11 spring 验证框架
2020-04-11 IDEA 插件整理
2020-04-11 spring security笔记 默认登陆页面源码