本文整理汇总了Java中com.alibaba.rocketmq.client.consumer.PullResult.getNextBeginOffset方法的典型用法代码示例。如果您正苦于以下问题:Java PullResult.getNextBeginOffset方法的具体用法?Java PullResult.getNextBeginOffset怎么用?Java PullResult.getNextBeginOffset使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.alibaba.rocketmq.client.consumer.PullResult
的用法示例。
在下文中一共展示了PullResult.getNextBeginOffset方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: fetchOneBatch
import com.alibaba.rocketmq.client.consumer.PullResult; //导入方法依赖的package包/类
public List<MessageExt> fetchOneBatch() {
List<MessageExt> ret = new ArrayList<MessageExt>();
String subexpress = metaSpoutConfig.getSubExpress();
for(Entry<MessageQueue, Long>entry : currentOffsets.entrySet()) {
MessageQueue mq = entry.getKey();
Long offset = entry.getValue();
int fetchSize = 0;
int oneFetchSize = Math.min(oneQueueFetchSize, 32);
while(fetchSize < oneQueueFetchSize) {
PullResult pullResult = null;
try {
pullResult = consumer.pullBlockIfNotFound(mq, subexpress, offset, oneFetchSize);
offset = pullResult.getNextBeginOffset();
PullStatus status = pullResult.getPullStatus();
if (status == PullStatus.FOUND) {
List<MessageExt> msgList = pullResult.getMsgFoundList();
ret.addAll(msgList);
fetchSize += msgList.size();
continue;
}else if (status == PullStatus.NO_MATCHED_MSG) {
continue;
}else if (status == PullStatus.NO_NEW_MSG ) {
break;
}else if (status == PullStatus.OFFSET_ILLEGAL) {
break;
}else {
break;
}
}
catch (Exception e) {
LOG.warn("Failed to fetch messages of " + mq + ":" + offset, e);
break;
}
}
backendOffset.put(mq, offset);
}
return ret;
}
示例2: run
import com.alibaba.rocketmq.client.consumer.PullResult; //导入方法依赖的package包/类
@Override
public void run() {
while (this.isOK.get()) {
System.out.println("Consume from the queue: " + partition);
try {
// 获取上一次的消费进度
long nextOffset = getMessageQueueOffset(consumer, partition, true);
for (;;) {
// 根据消费进度,获取下一批消息
PullResult pullResult = consumer.pullBlockIfNotFound(partition, null, nextOffset, 32);
switch (pullResult.getPullStatus()) {
case FOUND:
// 处理消息列表
int index = 0;
for (MessageExt msg : pullResult.getMsgFoundList()) {
index++;
boolean isMsgFailed = false;
System.out.println(msg);
// mock
if (index % 2 ==0) {
isMsgFailed = true;
}
// the message failed, need to send back to wait for retry.
if (isMsgFailed) {
this.consumer.sendMessageBack(msg, 2);
}
}
// 处理完成,让broker保存消费进度
putMessageQueueOffset(consumer, partition, pullResult.getNextBeginOffset());
nextOffset = pullResult.getNextBeginOffset();
break;
case NO_MATCHED_MSG:
break;
case NO_NEW_MSG:
break;
case OFFSET_ILLEGAL:
break;
default:
break;
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
// current task is ready to stop
this.stopLatch.countDown();
System.out.println("notified to stop");
}