未验证 提交 401dc8ea 编写于 作者: S sunhangda 提交者: GitHub

production level pull api demo

production level pull api demo
上级 7c7e9aca
...@@ -16,63 +16,131 @@ ...@@ -16,63 +16,131 @@
*/ */
package org.apache.rocketmq.example.simple; package org.apache.rocketmq.example.simple;
import java.util.HashMap; import java.util.HashSet;
import java.util.Map; import java.util.List;
import java.util.Set; import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import org.apache.rocketmq.client.consumer.DefaultMQPullConsumer; import org.apache.rocketmq.client.consumer.DefaultMQPullConsumer;
import org.apache.rocketmq.client.consumer.PullResult; import org.apache.rocketmq.client.consumer.PullResult;
import org.apache.rocketmq.client.consumer.store.ReadOffsetType;
import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException; import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.common.message.MessageQueue; import org.apache.rocketmq.common.message.MessageQueue;
import org.apache.rocketmq.remoting.exception.RemotingException;
@SuppressWarnings("deprecation")
public class PullConsumer { public class PullConsumer {
private static final Map<MessageQueue, Long> OFFSE_TABLE = new HashMap<MessageQueue, Long>();
public static void main(String[] args) throws MQClientException { public static void main(String[] args) throws MQClientException {
DefaultMQPullConsumer consumer = new DefaultMQPullConsumer("please_rename_unique_group_name_5"); DefaultMQPullConsumer consumer = new DefaultMQPullConsumer("please_rename_unique_group_name_5");
consumer.setNamesrvAddr("127.0.0.1:9876"); consumer.setNamesrvAddr("127.0.0.1:9876");
Set<String> topics = new HashSet<>();
//You would better to register topics,It will use in rebalance when starting
topics.add("TopicTest");
consumer.setRegisterTopics(topics);
consumer.start(); consumer.start();
Set<MessageQueue> mqs = consumer.fetchSubscribeMessageQueues("broker-a"); ExecutorService executors = Executors.newFixedThreadPool(topics.size(), new ThreadFactory() {
for (MessageQueue mq : mqs) { @Override
System.out.printf("Consume from the queue: %s%n", mq); public Thread newThread(Runnable r) {
SINGLE_MQ: return new Thread(r, "PullConsumerThread");
while (true) { }
});
for(String topic : consumer.getRegisterTopics()){
executors.execute(new Runnable() {
public void doSomething(List<MessageExt> msgs){
//do you business
System.out.println(msgs);
}
@Override
public void run() {
while(true){
try {
Set<MessageQueue> messageQueues = consumer.fetchMessageQueuesInBalance(topic);
if(messageQueues == null || messageQueues.isEmpty()){
Thread.sleep(1000);
continue;
}
PullResult pullResult = null;
for(MessageQueue messageQueue : messageQueues){
try { try {
PullResult pullResult = long offset = this.consumeFromOffset(messageQueue);
consumer.pullBlockIfNotFound(mq, null, getMessageQueueOffset(mq), 32); pullResult = consumer.pull(messageQueue, "*", offset, 32);
System.out.printf("%s%n", pullResult);
putMessageQueueOffset(mq, pullResult.getNextBeginOffset());
switch (pullResult.getPullStatus()) { switch (pullResult.getPullStatus()) {
case FOUND: case FOUND:
List<MessageExt> msgs = pullResult.getMsgFoundList();
if(msgs != null && !msgs.isEmpty()){
this.doSomething(msgs);
//update offset to broker
consumer.updateConsumeOffset(messageQueue, pullResult.getNextBeginOffset());
//print pull tps
this.incPullTPS(topic, pullResult.getMsgFoundList().size());
}
break; break;
case NO_MATCHED_MSG: case OFFSET_ILLEGAL:
consumer.updateConsumeOffset(messageQueue, pullResult.getNextBeginOffset());
break; break;
case NO_NEW_MSG: case NO_NEW_MSG:
break SINGLE_MQ; Thread.sleep(1);
case OFFSET_ILLEGAL: consumer.updateConsumeOffset(messageQueue, pullResult.getNextBeginOffset());
break; break;
default: case NO_MATCHED_MSG:
consumer.updateConsumeOffset(messageQueue, pullResult.getNextBeginOffset());
break; break;
default:
} }
} catch (Exception e) { } catch (RemotingException e) {
e.printStackTrace();
} catch (MQBrokerException e) {
e.printStackTrace(); e.printStackTrace();
} catch (Exception e){
e.printStackTrace();
}
} }
} catch (MQClientException e) {
//reblance error
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (Exception e){
e.printStackTrace();
} }
} }
consumer.shutdown();
} }
private static long getMessageQueueOffset(MessageQueue mq) { public long consumeFromOffset(MessageQueue messageQueue) throws MQClientException{
Long offset = OFFSE_TABLE.get(mq); //-1 when started
if (offset != null) long offset = consumer.getOffsetStore().readOffset(messageQueue, ReadOffsetType.READ_FROM_MEMORY);
if(offset < 0){
//query from broker
offset = consumer.getOffsetStore().readOffset(messageQueue, ReadOffsetType.READ_FROM_STORE);
}
if (offset < 0){
//first time start from last offset
offset = consumer.maxOffset(messageQueue);
}
//make sure
if (offset < 0){
offset = 0;
}
return offset; return offset;
return 0;
} }
public void incPullTPS(String topic, int pullSize) {
private static void putMessageQueueOffset(MessageQueue mq, long offset) { consumer.getDefaultMQPullConsumerImpl().getRebalanceImpl().getmQClientFactory()
OFFSE_TABLE.put(mq, offset); .getConsumerStatsManager().incPullTPS(consumer.getConsumerGroup(), topic, pullSize);
} }
});
}
// executors.shutdown();
// consumer.shutdown();
}
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册