Search in sources :

Example 1 with PullTaskCallback

use of org.apache.rocketmq.client.consumer.PullTaskCallback in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.

the class PullScheduleService method main.

public static void main(String[] args) throws MQClientException {
    final MQPullConsumerScheduleService scheduleService = new MQPullConsumerScheduleService("GroupName1");
    scheduleService.setMessageModel(MessageModel.CLUSTERING);
    scheduleService.registerPullTaskCallback("TopicTest1", new PullTaskCallback() {

        @Override
        public void doPullTask(MessageQueue mq, PullTaskContext context) {
            MQPullConsumer consumer = context.getPullConsumer();
            try {
                long offset = consumer.fetchConsumeOffset(mq, false);
                if (offset < 0)
                    offset = 0;
                PullResult pullResult = consumer.pull(mq, "*", offset, 32);
                System.out.printf("%s%n", 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;
                }
                consumer.updateConsumeOffset(mq, pullResult.getNextBeginOffset());
                context.setPullNextDelayTimeMillis(100);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
    scheduleService.start();
}
Also used : MessageQueue(org.apache.rocketmq.common.message.MessageQueue) PullTaskContext(org.apache.rocketmq.client.consumer.PullTaskContext) MQPullConsumerScheduleService(org.apache.rocketmq.client.consumer.MQPullConsumerScheduleService) PullTaskCallback(org.apache.rocketmq.client.consumer.PullTaskCallback) MQPullConsumer(org.apache.rocketmq.client.consumer.MQPullConsumer) PullResult(org.apache.rocketmq.client.consumer.PullResult) MQClientException(org.apache.rocketmq.client.exception.MQClientException)

Example 2 with PullTaskCallback

use of org.apache.rocketmq.client.consumer.PullTaskCallback in project rocketmq-externals by apache.

the class RocketMQSource method run.

@Override
public void run(SourceContext context) throws Exception {
    LOG.debug("source run....");
    // The lock that guarantees that record emission and state updates are atomic,
    // from the view of taking a checkpoint.
    final Object lock = context.getCheckpointLock();
    int delayWhenMessageNotFound = getInteger(props, RocketMQConfig.CONSUMER_DELAY_WHEN_MESSAGE_NOT_FOUND, RocketMQConfig.DEFAULT_CONSUMER_DELAY_WHEN_MESSAGE_NOT_FOUND);
    String tag = props.getProperty(RocketMQConfig.CONSUMER_TAG, RocketMQConfig.DEFAULT_CONSUMER_TAG);
    int pullPoolSize = getInteger(props, RocketMQConfig.CONSUMER_PULL_POOL_SIZE, RocketMQConfig.DEFAULT_CONSUMER_PULL_POOL_SIZE);
    int pullBatchSize = getInteger(props, RocketMQConfig.CONSUMER_BATCH_SIZE, RocketMQConfig.DEFAULT_CONSUMER_BATCH_SIZE);
    pullConsumerScheduleService.setPullThreadNums(pullPoolSize);
    pullConsumerScheduleService.registerPullTaskCallback(topic, new PullTaskCallback() {

        @Override
        public void doPullTask(MessageQueue mq, PullTaskContext pullTaskContext) {
            try {
                long offset = getMessageQueueOffset(mq);
                if (offset < 0) {
                    return;
                }
                PullResult pullResult = consumer.pull(mq, tag, offset, pullBatchSize);
                boolean found = false;
                switch(pullResult.getPullStatus()) {
                    case FOUND:
                        List<MessageExt> messages = pullResult.getMsgFoundList();
                        for (MessageExt msg : messages) {
                            byte[] key = msg.getKeys() != null ? msg.getKeys().getBytes(StandardCharsets.UTF_8) : null;
                            byte[] value = msg.getBody();
                            OUT data = schema.deserializeKeyAndValue(key, value);
                            // output and state update are atomic
                            synchronized (lock) {
                                context.collectWithTimestamp(data, msg.getBornTimestamp());
                            }
                        }
                        found = true;
                        break;
                    case NO_MATCHED_MSG:
                        LOG.debug("No matched message after offset {} for queue {}", offset, mq);
                        break;
                    case NO_NEW_MSG:
                        break;
                    case OFFSET_ILLEGAL:
                        LOG.warn("Offset {} is illegal for queue {}", offset, mq);
                        break;
                    default:
                        break;
                }
                synchronized (lock) {
                    putMessageQueueOffset(mq, pullResult.getNextBeginOffset());
                }
                if (found) {
                    // no delay when messages were found
                    pullTaskContext.setPullNextDelayTimeMillis(0);
                } else {
                    pullTaskContext.setPullNextDelayTimeMillis(delayWhenMessageNotFound);
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    });
    try {
        pullConsumerScheduleService.start();
    } catch (MQClientException e) {
        throw new RuntimeException(e);
    }
    runningChecker.setRunning(true);
    awaitTermination();
}
Also used : TypeHint(org.apache.flink.api.common.typeinfo.TypeHint) PullResult(org.apache.rocketmq.client.consumer.PullResult) MQClientException(org.apache.rocketmq.client.exception.MQClientException) MessageExt(org.apache.rocketmq.common.message.MessageExt) MessageQueue(org.apache.rocketmq.common.message.MessageQueue) PullTaskContext(org.apache.rocketmq.client.consumer.PullTaskContext) PullTaskCallback(org.apache.rocketmq.client.consumer.PullTaskCallback) List(java.util.List) MQClientException(org.apache.rocketmq.client.exception.MQClientException)

Example 3 with PullTaskCallback

use of org.apache.rocketmq.client.consumer.PullTaskCallback in project rocketmq by apache.

the class PullScheduleService method main.

public static void main(String[] args) throws MQClientException {
    final MQPullConsumerScheduleService scheduleService = new MQPullConsumerScheduleService("GroupName1");
    scheduleService.setMessageModel(MessageModel.CLUSTERING);
    scheduleService.registerPullTaskCallback("TopicTest1", new PullTaskCallback() {

        @Override
        public void doPullTask(MessageQueue mq, PullTaskContext context) {
            MQPullConsumer consumer = context.getPullConsumer();
            try {
                long offset = consumer.fetchConsumeOffset(mq, false);
                if (offset < 0)
                    offset = 0;
                PullResult pullResult = consumer.pull(mq, "*", offset, 32);
                System.out.printf("%s%n", 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;
                }
                consumer.updateConsumeOffset(mq, pullResult.getNextBeginOffset());
                context.setPullNextDelayTimeMillis(100);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
    scheduleService.start();
}
Also used : MessageQueue(org.apache.rocketmq.common.message.MessageQueue) PullTaskContext(org.apache.rocketmq.client.consumer.PullTaskContext) MQPullConsumerScheduleService(org.apache.rocketmq.client.consumer.MQPullConsumerScheduleService) PullTaskCallback(org.apache.rocketmq.client.consumer.PullTaskCallback) MQPullConsumer(org.apache.rocketmq.client.consumer.MQPullConsumer) PullResult(org.apache.rocketmq.client.consumer.PullResult) MQClientException(org.apache.rocketmq.client.exception.MQClientException)

Aggregations

PullResult (org.apache.rocketmq.client.consumer.PullResult)3 PullTaskCallback (org.apache.rocketmq.client.consumer.PullTaskCallback)3 PullTaskContext (org.apache.rocketmq.client.consumer.PullTaskContext)3 MQClientException (org.apache.rocketmq.client.exception.MQClientException)3 MessageQueue (org.apache.rocketmq.common.message.MessageQueue)3 MQPullConsumer (org.apache.rocketmq.client.consumer.MQPullConsumer)2 MQPullConsumerScheduleService (org.apache.rocketmq.client.consumer.MQPullConsumerScheduleService)2 List (java.util.List)1 TypeHint (org.apache.flink.api.common.typeinfo.TypeHint)1 MessageExt (org.apache.rocketmq.common.message.MessageExt)1