Java Code Examples for com.alibaba.rocketmq.client.consumer.DefaultMQPullConsumer#start()

The following examples show how to use com.alibaba.rocketmq.client.consumer.DefaultMQPullConsumer#start() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PullConsumerTest.java    From reading-and-annotate-rocketmq-3.4.6 with GNU General Public License v3.0 6 votes vote down vote up
public static void main(String[] args) throws MQClientException {
    DefaultMQPullConsumer consumer = new DefaultMQPullConsumer("please_rename_unique_group_name_5");
    consumer.start();

    try {
        MessageQueue mq = new MessageQueue();
        mq.setQueueId(0);
        mq.setTopic("TopicTest3");
        mq.setBrokerName("vivedeMacBook-Pro.local");

        long offset = 26;

        long beginTime = System.currentTimeMillis();
        PullResult pullResult = consumer.pullBlockIfNotFound(mq, null, offset, 32);
        System.out.println(System.currentTimeMillis() - beginTime);
        System.out.println(pullResult);
    }
    catch (Exception e) {
        e.printStackTrace();
    }

    consumer.shutdown();
}
 
Example 2
Source File: PullConsumerTest.java    From rocketmq with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws MQClientException {
    DefaultMQPullConsumer consumer = new DefaultMQPullConsumer("please_rename_unique_group_name_5");
    consumer.start();

    try {
        MessageQueue mq = new MessageQueue();
        mq.setQueueId(0);
        mq.setTopic("TopicTest3");
        mq.setBrokerName("vivedeMacBook-Pro.local");

        long offset = 26;

        long beginTime = System.currentTimeMillis();
        PullResult pullResult = consumer.pullBlockIfNotFound(mq, null, offset, 32);
        System.out.println(System.currentTimeMillis() - beginTime);
        System.out.println(pullResult);
    } catch (Exception e) {
        e.printStackTrace();
    }

    consumer.shutdown();
}
 
Example 3
Source File: PullConsumerTest.java    From RocketMQ-Master-analyze with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws MQClientException {
    DefaultMQPullConsumer consumer = new DefaultMQPullConsumer("please_rename_unique_group_name_5");
    consumer.start();

    try {
        MessageQueue mq = new MessageQueue();
        mq.setQueueId(0);
        mq.setTopic("TopicTest3");
        mq.setBrokerName("vivedeMacBook-Pro.local");

        long offset = 26;

        long beginTime = System.currentTimeMillis();
        PullResult pullResult = consumer.pullBlockIfNotFound(mq, null, offset, 32);
        System.out.println(System.currentTimeMillis() - beginTime);
        System.out.println(pullResult);
    }
    catch (Exception e) {
        e.printStackTrace();
    }

    consumer.shutdown();
}
 
Example 4
Source File: MQHelper.java    From reading-and-annotate-rocketmq-3.4.6 with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Reset consumer topic offset according to time
 * 
 * @param messageModel
 *            which model
 * @param instanceName
 *            which instance
 * @param consumerGroup
 *            consumer group
 * @param topic
 *            topic
 * @param timestamp
 *            time
 * @throws Exception
 */
public static void resetOffsetByTimestamp(//
        final MessageModel messageModel,//
        final String instanceName,//
        final String consumerGroup, //
        final String topic, //
        final long timestamp) throws Exception {
    final Logger log = ClientLogger.getLog();

    DefaultMQPullConsumer consumer = new DefaultMQPullConsumer(consumerGroup);
    consumer.setInstanceName(instanceName);
    consumer.setMessageModel(messageModel);
    consumer.start();

    Set<MessageQueue> mqs = null;
    try {
        mqs = consumer.fetchSubscribeMessageQueues(topic);
        if (mqs != null && !mqs.isEmpty()) {
            TreeSet<MessageQueue> mqsNew = new TreeSet<MessageQueue>(mqs);
            for (MessageQueue mq : mqsNew) {
                long offset = consumer.searchOffset(mq, timestamp);
                if (offset >= 0) {
                    consumer.updateConsumeOffset(mq, offset);
                    log.info("resetOffsetByTimestamp updateConsumeOffset success, {} {} {}",
                        consumerGroup, offset, mq);
                }
            }
        }
    }
    catch (Exception e) {
        log.warn("resetOffsetByTimestamp Exception", e);
        throw e;
    }
    finally {
        if (mqs != null) {
            consumer.getDefaultMQPullConsumerImpl().getOffsetStore().persistAll(mqs);
        }
        consumer.shutdown();
    }
}
 
Example 5
Source File: PullConsumer.java    From reading-and-annotate-rocketmq-3.4.6 with GNU General Public License v3.0 5 votes vote down vote up
public static void main(String[] args) throws MQClientException {
    DefaultMQPullConsumer consumer = new DefaultMQPullConsumer("please_rename_unique_group_name_5");

    consumer.start();

    Set<MessageQueue> mqs = consumer.fetchSubscribeMessageQueues("TopicTest1");
    for (MessageQueue mq : mqs) {
        System.out.println("Consume from the queue: " + mq);
        SINGLE_MQ: while (true) {
            try {
                PullResult pullResult =
                        consumer.pullBlockIfNotFound(mq, null, getMessageQueueOffset(mq), 32);
                System.out.println(pullResult);
                putMessageQueueOffset(mq, pullResult.getNextBeginOffset());
                switch (pullResult.getPullStatus()) {
                case FOUND:
                    break;
                case NO_MATCHED_MSG:
                    break;
                case NO_NEW_MSG:
                    break SINGLE_MQ;
                case OFFSET_ILLEGAL:
                    break;
                default:
                    break;
                }
            }
            catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    consumer.shutdown();
}
 
Example 6
Source File: MQHelper.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
/**
 * Reset consumer topic offset according to time
 *
 * @param messageModel
 *         which model
 * @param instanceName
 *         which instance
 * @param consumerGroup
 *         consumer group
 * @param topic
 *         topic
 * @param timestamp
 *         time
 *
 * @throws Exception
 */
public static void resetOffsetByTimestamp(//
                                          final MessageModel messageModel,//
                                          final String instanceName,//
                                          final String consumerGroup, //
                                          final String topic, //
                                          final long timestamp) throws Exception {
    final Logger log = ClientLogger.getLog();

    DefaultMQPullConsumer consumer = new DefaultMQPullConsumer(consumerGroup);
    consumer.setInstanceName(instanceName);
    consumer.setMessageModel(messageModel);
    consumer.start();

    Set<MessageQueue> mqs = null;
    try {
        mqs = consumer.fetchSubscribeMessageQueues(topic);
        if (mqs != null && !mqs.isEmpty()) {
            TreeSet<MessageQueue> mqsNew = new TreeSet<MessageQueue>(mqs);
            for (MessageQueue mq : mqsNew) {
                long offset = consumer.searchOffset(mq, timestamp);
                if (offset >= 0) {
                    consumer.updateConsumeOffset(mq, offset);
                    log.info("resetOffsetByTimestamp updateConsumeOffset success, {} {} {}",
                            consumerGroup, offset, mq);
                }
            }
        }
    } catch (Exception e) {
        log.warn("resetOffsetByTimestamp Exception", e);
        throw e;
    } finally {
        if (mqs != null) {
            consumer.getDefaultMQPullConsumerImpl().getOffsetStore().persistAll(mqs);
        }
        consumer.shutdown();
    }
}
 
Example 7
Source File: PullConsumer.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws MQClientException {
    DefaultMQPullConsumer consumer = new DefaultMQPullConsumer("please_rename_unique_group_name_5");

    consumer.start();

    Set<MessageQueue> mqs = consumer.fetchSubscribeMessageQueues("TopicTest1");
    for (MessageQueue mq : mqs) {
        System.out.println("Consume from the queue: " + mq);
        SINGLE_MQ:
        while (true) {
            try {
                PullResult pullResult =
                        consumer.pullBlockIfNotFound(mq, null, getMessageQueueOffset(mq), 32);
                System.out.println(pullResult);
                putMessageQueueOffset(mq, pullResult.getNextBeginOffset());
                switch (pullResult.getPullStatus()) {
                    case FOUND:
                        break;
                    case NO_MATCHED_MSG:
                        break;
                    case NO_NEW_MSG:
                        break SINGLE_MQ;
                    case OFFSET_ILLEGAL:
                        break;
                    default:
                        break;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    consumer.shutdown();
}
 
Example 8
Source File: MQHelper.java    From RocketMQ-Master-analyze with Apache License 2.0 5 votes vote down vote up
/**
 * Reset consumer topic offset according to time
 * 
 * @param messageModel
 *            which model
 * @param instanceName
 *            which instance
 * @param consumerGroup
 *            consumer group
 * @param topic
 *            topic
 * @param timestamp
 *            time
 * @throws Exception
 */
public static void resetOffsetByTimestamp(//
        final MessageModel messageModel, //
        final String instanceName, //
        final String consumerGroup, //
        final String topic, //
        final long timestamp) throws Exception {
    final Logger log = ClientLogger.getLog();

    DefaultMQPullConsumer consumer = new DefaultMQPullConsumer(consumerGroup);
    consumer.setInstanceName(instanceName);
    consumer.setMessageModel(messageModel);
    consumer.start();

    Set<MessageQueue> mqs = null;
    try {
        mqs = consumer.fetchSubscribeMessageQueues(topic);
        if (mqs != null && !mqs.isEmpty()) {
            TreeSet<MessageQueue> mqsNew = new TreeSet<MessageQueue>(mqs);
            for (MessageQueue mq : mqsNew) {
                long offset = consumer.searchOffset(mq, timestamp);
                if (offset >= 0) {
                    consumer.updateConsumeOffset(mq, offset);
                    log.info("resetOffsetByTimestamp updateConsumeOffset success, {} {} {}",
                        consumerGroup, offset, mq);
                }
            }
        }
    }
    catch (Exception e) {
        log.warn("resetOffsetByTimestamp Exception", e);
        throw e;
    }
    finally {
        if (mqs != null) {
            consumer.getDefaultMQPullConsumerImpl().getOffsetStore().persistAll(mqs);
        }
        consumer.shutdown();
    }
}
 
Example 9
Source File: PullConsumer.java    From RocketMQ-Master-analyze with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws MQClientException {
    DefaultMQPullConsumer consumer = new DefaultMQPullConsumer("please_rename_unique_group_name_5");

    consumer.start();

    Set<MessageQueue> mqs = consumer.fetchSubscribeMessageQueues("TopicTest");
    for (MessageQueue mq : mqs) {
        System.out.println("Consume from the queue: " + mq);
        SINGLE_MQ: while (true) {
            try {
                PullResult pullResult =
                        consumer.pullBlockIfNotFound(mq, null, getMessageQueueOffset(mq), 32);
                System.out.println(pullResult);
                putMessageQueueOffset(mq, pullResult.getNextBeginOffset());
                switch (pullResult.getPullStatus()) {
                case FOUND:
                    // TODO
                    break;
                case NO_MATCHED_MSG:
                    break;
                case NO_NEW_MSG:
                    break SINGLE_MQ;
                case OFFSET_ILLEGAL:
                    break;
                default:
                    break;
                }
            }
            catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    consumer.shutdown();
}
 
Example 10
Source File: QueryMsgByOffsetSubCommand.java    From reading-and-annotate-rocketmq-3.4.6 with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void execute(CommandLine commandLine, Options options, RPCHook rpcHook) {
    DefaultMQAdminExt defaultMQAdminExt = new DefaultMQAdminExt(rpcHook);
    DefaultMQPullConsumer defaultMQPullConsumer = new DefaultMQPullConsumer(MixAll.TOOLS_CONSUMER_GROUP);

    defaultMQAdminExt.setInstanceName(Long.toString(System.currentTimeMillis()));
    defaultMQPullConsumer.setInstanceName(Long.toString(System.currentTimeMillis()));

    try {
        //解析 sh mqadmin  queryMsgByOffset 命令携带的各种参数信息
        String topic = commandLine.getOptionValue('t').trim();
        String brokerName = commandLine.getOptionValue('b').trim();
        String queueId = commandLine.getOptionValue('i').trim();
        String offset = commandLine.getOptionValue('o').trim();

        MessageQueue mq = new MessageQueue();
        mq.setTopic(topic);
        mq.setBrokerName(brokerName);
        mq.setQueueId(Integer.parseInt(queueId));

        defaultMQPullConsumer.start();
        defaultMQAdminExt.start();

        PullResult pullResult = defaultMQPullConsumer.pull(mq, "*", Long.parseLong(offset), 1);
        if (pullResult != null) {
            switch (pullResult.getPullStatus()) {
            case FOUND:
                QueryMsgByIdSubCommand.queryById(defaultMQAdminExt, pullResult.getMsgFoundList().get(0)
                    .getMsgId());
                break;
            case NO_MATCHED_MSG:
            case NO_NEW_MSG:
            case OFFSET_ILLEGAL:
            default:
                break;
            }
        }
    }
    catch (Exception e) {
        e.printStackTrace();
    }
    finally {
        defaultMQPullConsumer.shutdown();
        defaultMQAdminExt.shutdown();
    }
}
 
Example 11
Source File: MessageService.java    From rocketmq with Apache License 2.0 4 votes vote down vote up
@CmdTrace(cmdClazz = QueryMsgByOffsetSubCommand.class)
public Table queryMsgByOffset(String topicName, String brokerName, String queueId, String offset)
        throws Throwable {
    Throwable t = null;
    DefaultMQPullConsumer defaultMQPullConsumer = new DefaultMQPullConsumer(MixAll.TOOLS_CONSUMER_GROUP);

    defaultMQPullConsumer.setInstanceName(Long.toString(System.currentTimeMillis()));

    try {
        MessageQueue mq = new MessageQueue();
        mq.setTopic(topicName);
        mq.setBrokerName(brokerName);
        mq.setQueueId(Integer.parseInt(queueId));

        defaultMQPullConsumer.start();

        PullResult pullResult = defaultMQPullConsumer.pull(mq, "*", Long.parseLong(offset), 1);
        if (pullResult != null) {
            switch (pullResult.getPullStatus()) {
            case FOUND:
                Table table = queryMsgById(pullResult.getMsgFoundList().get(0).getMsgId());
                return table;
            case NO_MATCHED_MSG:
            case NO_NEW_MSG:
            case OFFSET_ILLEGAL:
            default:
                break;
            }
        }
        else {
            throw new IllegalStateException("pullResult is null");
        }
    }
    catch (Throwable e) {
        logger.error(e.getMessage(), e);
        t = e;
    }
    finally {
        defaultMQPullConsumer.shutdown();
    }
    throw t;
}
 
Example 12
Source File: QueryMsgByOffsetSubCommand.java    From rocketmq with Apache License 2.0 4 votes vote down vote up
@Override
public void execute(CommandLine commandLine, Options options, RPCHook rpcHook) {
    DefaultMQAdminExt defaultMQAdminExt = new DefaultMQAdminExt(rpcHook);
    DefaultMQPullConsumer defaultMQPullConsumer = new DefaultMQPullConsumer(MixAll.TOOLS_CONSUMER_GROUP,rpcHook);

    defaultMQAdminExt.setInstanceName(Long.toString(System.currentTimeMillis()));
    defaultMQPullConsumer.setInstanceName(Long.toString(System.currentTimeMillis()));

    try {
        String topic = commandLine.getOptionValue('t').trim();
        String brokerName = commandLine.getOptionValue('b').trim();
        String queueId = commandLine.getOptionValue('i').trim();
        String offset = commandLine.getOptionValue('o').trim();

        MessageQueue mq = new MessageQueue();
        mq.setTopic(topic);
        mq.setBrokerName(brokerName);
        mq.setQueueId(Integer.parseInt(queueId));

        defaultMQPullConsumer.start();
        defaultMQAdminExt.start();

        PullResult pullResult = defaultMQPullConsumer.pull(mq, "*", Long.parseLong(offset), 1);
        if (pullResult != null) {
            switch (pullResult.getPullStatus()) {
                case FOUND:
                    QueryMsgByIdSubCommand.queryById(defaultMQAdminExt, pullResult.getMsgFoundList().get(0)
                            .getMsgId());
                    break;
                case NO_MATCHED_MSG:
                case NO_NEW_MSG:
                case OFFSET_ILLEGAL:
                default:
                    break;
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        defaultMQPullConsumer.shutdown();
        defaultMQAdminExt.shutdown();
    }
}
 
Example 13
Source File: QueryMsgByOffsetSubCommand.java    From RocketMQ-Master-analyze with Apache License 2.0 4 votes vote down vote up
@Override
public void execute(CommandLine commandLine, Options options, RPCHook rpcHook) {
    DefaultMQAdminExt defaultMQAdminExt = new DefaultMQAdminExt(rpcHook);
    DefaultMQPullConsumer defaultMQPullConsumer = new DefaultMQPullConsumer(MixAll.TOOLS_CONSUMER_GROUP);

    defaultMQAdminExt.setInstanceName(Long.toString(System.currentTimeMillis()));
    defaultMQPullConsumer.setInstanceName(Long.toString(System.currentTimeMillis()));

    try {
        String topic = commandLine.getOptionValue('t').trim();
        String brokerName = commandLine.getOptionValue('b').trim();
        String queueId = commandLine.getOptionValue('i').trim();
        String offset = commandLine.getOptionValue('o').trim();

        MessageQueue mq = new MessageQueue();
        mq.setTopic(topic);
        mq.setBrokerName(brokerName);
        mq.setQueueId(Integer.parseInt(queueId));

        defaultMQPullConsumer.start();
        defaultMQAdminExt.start();

        PullResult pullResult = defaultMQPullConsumer.pull(mq, "*", Long.parseLong(offset), 1);
        if (pullResult != null) {
            switch (pullResult.getPullStatus()) {
            case FOUND:
                QueryMsgByIdSubCommand.queryById(defaultMQAdminExt,
                    pullResult.getMsgFoundList().get(0).getMsgId());
                break;
            case NO_MATCHED_MSG:
            case NO_NEW_MSG:
            case OFFSET_ILLEGAL:
            default:
                break;
            }
        }
    }
    catch (Exception e) {
        e.printStackTrace();
    }
    finally {
        defaultMQPullConsumer.shutdown();
        defaultMQAdminExt.shutdown();
    }
}