Search in sources :

Example 1 with MQPullConsumer

use of org.apache.rocketmq.client.consumer.MQPullConsumer in project spring-boot-starter-samples by vindell.

the class PullTaskCallbackImpl method doPullTask.

@Override
public void doPullTask(MessageQueue mq, PullTaskContext context) {
    MQPullConsumer consumer = context.getPullConsumer();
    try {
        // 获取从哪里拉取
        long offset = consumer.fetchConsumeOffset(mq, false);
        if (offset < 0) {
            offset = 0;
        }
        // String subExpression, long offset, int maxNums
        PullResult pullResult = consumer.pull(mq, "*", offset, 32);
        System.out.println(offset + "\t" + mq + "\t" + pullResult);
        switch(pullResult.getPullStatus()) {
            case FOUND:
                break;
            case NO_MATCHED_MSG:
                break;
            case NO_NEW_MSG:
            case OFFSET_ILLEGAL:
                break;
            default:
                break;
        }
        // 存储Offset,客户端每隔5s会定时刷新到Broker
        consumer.updateConsumeOffset(mq, pullResult.getNextBeginOffset());
        // 设置隔多长时间进行下次拉去 (100ms后重新拉取)
        context.setPullNextDelayTimeMillis(100);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : MQPullConsumer(org.apache.rocketmq.client.consumer.MQPullConsumer) PullResult(org.apache.rocketmq.client.consumer.PullResult)

Aggregations

MQPullConsumer (org.apache.rocketmq.client.consumer.MQPullConsumer)1 PullResult (org.apache.rocketmq.client.consumer.PullResult)1