Java Code Examples for org.apache.rocketmq.client.consumer.PullResult#getPullStatus()

The following examples show how to use org.apache.rocketmq.client.consumer.PullResult#getPullStatus() . 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: PullConsumer.java    From blog with BSD 2-Clause "Simplified" License 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.setNamesrvAddr("127.0.0.1:9876");
    consumer.start();

    Set<MessageQueue> mqs = consumer.fetchSubscribeMessageQueues("broker-a");
    for (MessageQueue mq : mqs) {
        System.out.printf("Consume from the queue: %s%n", mq);
        SINGLE_MQ:
        while (true) {
            try {
                PullResult pullResult =
                    consumer.pullBlockIfNotFound(mq, null, getMessageQueueOffset(mq), 32);
                System.out.printf("%s%n", 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 2
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.printf("Consume from the queue: " + mq + "%n");
        SINGLE_MQ:
        while (true) {
            try {
                PullResult pullResult =
                    consumer.pullBlockIfNotFound(mq, null, getMessageQueueOffset(mq), 32);
                System.out.printf("%s%n", 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 3
Source File: ConsumeMessageCommand.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
private void pullMessageByQueue(MessageQueue mq, long minOffset, long maxOffset) {
    READQ:
    for (long offset = minOffset; offset <= maxOffset; ) {
        PullResult pullResult = null;
        try {
            pullResult = defaultMQPullConsumer.pull(mq, "*", offset, (int)(maxOffset - offset + 1));
        } catch (Exception e) {
            e.printStackTrace();
            return;
        }
        if (pullResult != null) {
            offset = pullResult.getNextBeginOffset();
            switch (pullResult.getPullStatus()) {
                case FOUND:
                    System.out.print("Consume ok\n");
                    PrintMessageByQueueCommand.printMessage(pullResult.getMsgFoundList(), "UTF-8",
                        true, true);
                    break;
                case NO_MATCHED_MSG:
                    System.out.printf("%s no matched msg. status=%s, offset=%s\n", mq, pullResult.getPullStatus(),
                        offset);
                    break;
                case NO_NEW_MSG:
                case OFFSET_ILLEGAL:
                    System.out.printf("%s print msg finished. status=%s, offset=%s\n", mq,
                        pullResult.getPullStatus(), offset);
                    break READQ;
                default:
                    break;
            }
        }
    }
}
 
Example 4
Source File: PullConsumerWithNamespace.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    DefaultMQPullConsumer pullConsumer = new DefaultMQPullConsumer("InstanceTest", "cidTest");
    pullConsumer.setNamesrvAddr("127.0.0.1:9876");
    pullConsumer.start();

    Set<MessageQueue> mqs = pullConsumer.fetchSubscribeMessageQueues("topicTest");
    for (MessageQueue mq : mqs) {
        System.out.printf("Consume from the topic: %s, queue: %s%n", mq.getTopic(), mq);
        SINGLE_MQ:
        while (true) {
            try {
                PullResult pullResult =
                    pullConsumer.pullBlockIfNotFound(mq, null, getMessageQueueOffset(mq), 32);
                System.out.printf("%s%n", pullResult);

                putMessageQueueOffset(mq, pullResult.getNextBeginOffset());
                switch (pullResult.getPullStatus()) {
                    case FOUND:
                        dealWithPullResult(pullResult);
                        break;
                    case NO_MATCHED_MSG:
                        break;
                    case NO_NEW_MSG:
                        break SINGLE_MQ;
                    case OFFSET_ILLEGAL:
                        break;
                    default:
                        break;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    pullConsumer.shutdown();
}
 
Example 5
Source File: AclClient.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
public static void pullConsumer() throws MQClientException {
    DefaultMQPullConsumer consumer = new DefaultMQPullConsumer("please_rename_unique_group_name_6", getAclRPCHook());
    consumer.setNamesrvAddr("127.0.0.1:9876");
    consumer.start();

    Set<MessageQueue> mqs = consumer.fetchSubscribeMessageQueues("TopicTest");
    for (MessageQueue mq : mqs) {
        System.out.printf("Consume from the queue: %s%n", mq);
        SINGLE_MQ:
        while (true) {
            try {
                PullResult pullResult =
                    consumer.pullBlockIfNotFound(mq, null, getMessageQueueOffset(mq), 32);
                System.out.printf("%s%n", pullResult);
                putMessageQueueOffset(mq, pullResult.getNextBeginOffset());
                printBody(pullResult);
                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: ConsumerLagMonitor.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
private long getRmqConsumeDelay(RocketMQProduceOffsetFetcher fetcher, MessageQueue mq, long produceOffset, long consumeOffset, String group) {
    try {
        if (produceOffset == 0L || produceOffset <= consumeOffset) {
            // 未生产 or 消费完毕.
            return 0L;
        }

        PullResult consumePullResult = fetcher.queryMsgByOffset(mq, consumeOffset);

        if (consumePullResult != null && consumePullResult.getPullStatus() == PullStatus.FOUND) {
            return TimeUtils.getElapseTime(consumePullResult.getMsgFoundList().get(0).getStoreTimestamp());
        } else if (consumePullResult.getPullStatus() == PullStatus.NO_MATCHED_MSG) {
            LOGGER.error("failed to getRmqConsumeDelay. mq: {}, namesvr: {}, group: {}, produceOffset: {}, consumeOffset: {}, consumePullResult: {}", mq, fetcher.getNamesrvAddr(), group, produceOffset, consumeOffset, consumePullResult);
            return 0; // 拿不到消息,认为没有延迟.
        } else if (consumePullResult.getPullStatus() == PullStatus.OFFSET_ILLEGAL) {
            LOGGER.error("failed to getRmqConsumeDelay. mq: {}, namesvr: {}, group: {}, produceOffset: {}, consumeOffset: {}, consumePullResult: {}", mq, fetcher.getNamesrvAddr(), group, produceOffset, consumeOffset, consumePullResult);

            PullResult pullResult = fetcher.queryMsgByOffset(mq, consumePullResult.getMinOffset());
            if (pullResult != null && pullResult.getPullStatus() == PullStatus.FOUND) {
                return TimeUtils.getElapseTime(pullResult.getMsgFoundList().get(0).getStoreTimestamp());
            }

            return MSG_NOT_FOUND;
        } else {
            return 0;
        }
    } catch (Exception e) {
        LOGGER.error("queryMsgByOffset failed,", e);
        return -1L;
    }
}
 
Example 7
Source File: ConsumeMessageCommand.java    From rocketmq-4.3.0 with Apache License 2.0 5 votes vote down vote up
private void pullMessageByQueue(MessageQueue mq, long minOffset, long maxOffset) {
    READQ:
    for (long offset = minOffset; offset <= maxOffset; ) {
        PullResult pullResult = null;
        try {
            pullResult = defaultMQPullConsumer.pull(mq, "*", offset, (int)(maxOffset - offset + 1));
        } catch (Exception e) {
            e.printStackTrace();
            return;
        }
        if (pullResult != null) {
            offset = pullResult.getNextBeginOffset();
            switch (pullResult.getPullStatus()) {
                case FOUND:
                    System.out.print("Consume ok\n");
                    PrintMessageByQueueCommand.printMessage(pullResult.getMsgFoundList(), "UTF-8",
                        true, true);
                    break;
                case NO_MATCHED_MSG:
                    System.out.printf("%s no matched msg. status=%s, offset=%s\n", mq, pullResult.getPullStatus(),
                        offset);
                    break;
                case NO_NEW_MSG:
                case OFFSET_ILLEGAL:
                    System.out.printf("%s print msg finished. status=%s, offset=%s\n", mq,
                        pullResult.getPullStatus(), offset);
                    break READQ;
                default:
                    break;
            }
        }
    }
}
 
Example 8
Source File: PullConsumer.java    From rocketmq-all-4.1.0-incubating 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.printf("Consume from the queue: " + mq + "%n");
        SINGLE_MQ:
        while (true) {
            try {
                PullResult pullResult =
                    consumer.pullBlockIfNotFound(mq, null, getMessageQueueOffset(mq), 32);
                System.out.printf("%s%n", 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 9
Source File: MonitorService.java    From rocketmq with Apache License 2.0 4 votes vote down vote up
private void computeUndoneMsgs(final UndoneMsgs undoneMsgs, final ConsumeStats consumeStats) {
    long total = 0;
    long singleMax = 0;
    long delayMax = 0;
    Iterator<Entry<MessageQueue, OffsetWrapper>> it = consumeStats.getOffsetTable().entrySet().iterator();
    while (it.hasNext()) {
        Entry<MessageQueue, OffsetWrapper> next = it.next();
        MessageQueue mq = next.getKey();
        OffsetWrapper ow = next.getValue();
        long diff = ow.getBrokerOffset() - ow.getConsumerOffset();

        if (diff > singleMax) {
            singleMax = diff;
        }

        if (diff > 0) {
            total += diff;
        }

        // Delay
        if (ow.getLastTimestamp() > 0) {
            try {
                long maxOffset = this.defaultMQPullConsumer.maxOffset(mq);
                if (maxOffset > 0) {
                    PullResult pull = this.defaultMQPullConsumer.pull(mq, "*", maxOffset - 1, 1);
                    switch (pull.getPullStatus()) {
                        case FOUND:
                            long delay =
                                pull.getMsgFoundList().get(0).getStoreTimestamp() - ow.getLastTimestamp();
                            if (delay > delayMax) {
                                delayMax = delay;
                            }
                            break;
                        case NO_MATCHED_MSG:
                        case NO_NEW_MSG:
                        case OFFSET_ILLEGAL:
                            break;
                        default:
                            break;
                    }
                }
            } catch (Exception e) {
            }
        }
    }

    undoneMsgs.setUndoneMsgsTotal(total);
    undoneMsgs.setUndoneMsgsSingleMQ(singleMax);
    undoneMsgs.setUndoneMsgsDelayTimeMills(delayMax);
}
 
Example 10
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.printMsg(defaultMQAdminExt, pullResult.getMsgFoundList().get(0));
                    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: MonitorService.java    From rocketmq-read with Apache License 2.0 4 votes vote down vote up
private void computeUndoneMsgs(final UndoneMsgs undoneMsgs, final ConsumeStats consumeStats) {
    long total = 0;
    long singleMax = 0;
    long delayMax = 0;
    Iterator<Entry<MessageQueue, OffsetWrapper>> it = consumeStats.getOffsetTable().entrySet().iterator();
    while (it.hasNext()) {
        Entry<MessageQueue, OffsetWrapper> next = it.next();
        MessageQueue mq = next.getKey();
        OffsetWrapper ow = next.getValue();
        long diff = ow.getBrokerOffset() - ow.getConsumerOffset();

        if (diff > singleMax) {
            singleMax = diff;
        }

        if (diff > 0) {
            total += diff;
        }

        // Delay
        if (ow.getLastTimestamp() > 0) {
            try {
                long maxOffset = this.defaultMQPullConsumer.maxOffset(mq);
                if (maxOffset > 0) {
                    PullResult pull = this.defaultMQPullConsumer.pull(mq, "*", maxOffset - 1, 1);
                    switch (pull.getPullStatus()) {
                        case FOUND:
                            long delay =
                                pull.getMsgFoundList().get(0).getStoreTimestamp() - ow.getLastTimestamp();
                            if (delay > delayMax) {
                                delayMax = delay;
                            }
                            break;
                        case NO_MATCHED_MSG:
                        case NO_NEW_MSG:
                        case OFFSET_ILLEGAL:
                            break;
                        default:
                            break;
                    }
                }
            } catch (Exception e) {
            }
        }
    }

    undoneMsgs.setUndoneMsgsTotal(total);
    undoneMsgs.setUndoneMsgsSingleMQ(singleMax);
    undoneMsgs.setUndoneMsgsDelayTimeMills(delayMax);
}
 
Example 12
Source File: QueryMsgByOffsetSubCommand.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 4 votes vote down vote up
@Override
public void execute(CommandLine commandLine, Options options, RPCHook rpcHook) throws SubCommandException {
    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.printMsg(defaultMQAdminExt, pullResult.getMsgFoundList().get(0));
                    break;
                case NO_MATCHED_MSG:
                case NO_NEW_MSG:
                case OFFSET_ILLEGAL:
                default:
                    break;
            }
        }
    } catch (Exception e) {
        throw new SubCommandException(this.getClass().getSimpleName() + " command failed", e);
    } finally {
        defaultMQPullConsumer.shutdown();
        defaultMQAdminExt.shutdown();
    }
}
 
Example 13
Source File: QueryMsgByOffsetSubCommand.java    From DDMQ with Apache License 2.0 4 votes vote down vote up
@Override
public void execute(CommandLine commandLine, Options options, RPCHook rpcHook) throws SubCommandException {
    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();
        String isSlaveFirstStr = commandLine.getOptionValue('s').trim();
        boolean isSlaveFirst = StringUtils.isEmpty(isSlaveFirstStr) ? true : Boolean.valueOf(isSlaveFirstStr);

        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, isSlaveFirst);
        if (pullResult != null) {
            switch (pullResult.getPullStatus()) {
                case FOUND:
                    QueryMsgByIdSubCommand.printMsg(defaultMQAdminExt, pullResult.getMsgFoundList().get(0));
                    break;
                case NO_MATCHED_MSG:
                case NO_NEW_MSG:
                case OFFSET_ILLEGAL:
                default:
                    break;
            }
        }
    } catch (Exception e) {
        throw new SubCommandException(this.getClass().getSimpleName() + " command failed", e);
    } finally {
        defaultMQPullConsumer.shutdown();
        defaultMQAdminExt.shutdown();
    }
}
 
Example 14
Source File: QueryMsgByOffsetSubCommand.java    From rocketmq-4.3.0 with Apache License 2.0 4 votes vote down vote up
@Override
public void execute(CommandLine commandLine, Options options, RPCHook rpcHook) throws SubCommandException {
    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.printMsg(defaultMQAdminExt, pullResult.getMsgFoundList().get(0));
                    break;
                case NO_MATCHED_MSG:
                case NO_NEW_MSG:
                case OFFSET_ILLEGAL:
                default:
                    break;
            }
        }
    } catch (Exception e) {
        throw new SubCommandException(this.getClass().getSimpleName() + " command failed", e);
    } finally {
        defaultMQPullConsumer.shutdown();
        defaultMQAdminExt.shutdown();
    }
}
 
Example 15
Source File: MonitorService.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 4 votes vote down vote up
private void computeUndoneMsgs(final UndoneMsgs undoneMsgs, final ConsumeStats consumeStats) {
    long total = 0;
    long singleMax = 0;
    long delayMax = 0;
    Iterator<Entry<MessageQueue, OffsetWrapper>> it = consumeStats.getOffsetTable().entrySet().iterator();
    while (it.hasNext()) {
        Entry<MessageQueue, OffsetWrapper> next = it.next();
        MessageQueue mq = next.getKey();
        OffsetWrapper ow = next.getValue();
        long diff = ow.getBrokerOffset() - ow.getConsumerOffset();

        if (diff > singleMax) {
            singleMax = diff;
        }

        if (diff > 0) {
            total += diff;
        }

        // Delay
        if (ow.getLastTimestamp() > 0) {
            try {
                long maxOffset = this.defaultMQPullConsumer.maxOffset(mq);
                if (maxOffset > 0) {
                    PullResult pull = this.defaultMQPullConsumer.pull(mq, "*", maxOffset - 1, 1);
                    switch (pull.getPullStatus()) {
                        case FOUND:
                            long delay =
                                pull.getMsgFoundList().get(0).getStoreTimestamp() - ow.getLastTimestamp();
                            if (delay > delayMax) {
                                delayMax = delay;
                            }
                            break;
                        case NO_MATCHED_MSG:
                        case NO_NEW_MSG:
                        case OFFSET_ILLEGAL:
                            break;
                        default:
                            break;
                    }
                }
            } catch (Exception e) {
            }
        }
    }

    undoneMsgs.setUndoneMsgsTotal(total);
    undoneMsgs.setUndoneMsgsSingleMQ(singleMax);
    undoneMsgs.setUndoneMsgsDelayTimeMills(delayMax);
}
 
Example 16
Source File: Utils.java    From DDMQ with Apache License 2.0 4 votes vote down vote up
/**
 * 检查消费
 *
 * @param nameSvr
 * @param address
 * @throws MQClientException
 * @throws NoSuchFieldException
 * @throws SecurityException
 * @throws IllegalArgumentException
 * @throws IllegalAccessException
 * @throws InterruptedException
 * @throws RemotingException
 * @throws MQBrokerException
 */
public static long checkReceive(String cluster, String nameSvr, String address)
        throws MQClientException, NoSuchFieldException, SecurityException, IllegalArgumentException,
        IllegalAccessException, InterruptedException, RemotingException, MQBrokerException {

    DefaultMQPullConsumer consumer = getReceiveCheckConsumer(nameSvr, cluster, address);
    Field f1 = DefaultMQPullConsumerImpl.class.getDeclaredField("mQClientFactory");
    f1.setAccessible(true);

    MQClientInstance instance = (MQClientInstance) f1.get(consumer.getDefaultMQPullConsumerImpl());

    Field f = MQClientInstance.class.getDeclaredField("brokerAddrTable");
    f.setAccessible(true);

    Field f2 = MQClientInstance.class.getDeclaredField("scheduledExecutorService");
    f2.setAccessible(true);

    ScheduledExecutorService service = (ScheduledExecutorService) f2.get(instance);
    service.shutdown();
    service.awaitTermination(1000, TimeUnit.SECONDS);

    ConcurrentHashMap<String, HashMap<Long, String>> map = (ConcurrentHashMap<String, HashMap<Long, String>>) f.get(instance);
    HashMap<Long, String> addresses = new HashMap<>();
    addresses.put(0L, address);
    map.put("rmqmonitor_" + address, addresses);

    MessageQueue queue = new MessageQueue("SELF_TEST_TOPIC", "rmqmonitor_" + address, 0);

    boolean pullOk = false;
    long maxOffset = -1;
    for (int i = 0; i < 2; ++i) {
        try {
            maxOffset = consumer.getDefaultMQPullConsumerImpl().maxOffset(queue);
            PullResult result = consumer.pull(queue, "*", maxOffset > 100 ? maxOffset - 10 : 0, 1);
            if (result.getPullStatus() == PullStatus.FOUND) {
                pullOk = true;
                break;
            } else if(result.getPullStatus() == PullStatus.NO_NEW_MSG) {
                checkSend(cluster, nameSvr, address);
                continue;
            }

            logger.warn("pull result failed, PullResult={}, cluster={}, namesvr={}, address={}", result, cluster, nameSvr, address);
        } catch (Throwable e) {
            logger.error("pull exception, cluster={}, namesvr={}, address={}", cluster, nameSvr, address, e);
        }
        Thread.sleep(1000);
    }
    if (!pullOk) {
        logger.error(String.format("[AlarmPullErr] cluster=%s, broker=%s", cluster, address));
    } else {
        logger.info("AlarmPullCheck cluster={}, broker={}", cluster, address);
    }
    return maxOffset;
}
 
Example 17
Source File: QueryMsgByOffsetSubCommand.java    From rocketmq_trans_message 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.printMsg(defaultMQAdminExt, pullResult.getMsgFoundList().get(0));
                    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 18
Source File: Utils.java    From DDMQ with Apache License 2.0 4 votes vote down vote up
/**
 * 检查消费
 *
 * @param nameSvr
 * @param address
 * @throws MQClientException
 * @throws NoSuchFieldException
 * @throws SecurityException
 * @throws IllegalArgumentException
 * @throws IllegalAccessException
 * @throws InterruptedException
 * @throws RemotingException
 * @throws MQBrokerException
 */
public static long checkReceive(String cluster, String nameSvr, String address)
        throws MQClientException, NoSuchFieldException, SecurityException, IllegalArgumentException,
        IllegalAccessException, InterruptedException, RemotingException, MQBrokerException {

    DefaultMQPullConsumer consumer = getReceiveCheckConsumer(nameSvr, cluster, address);
    Field f1 = DefaultMQPullConsumerImpl.class.getDeclaredField("mQClientFactory");
    f1.setAccessible(true);

    MQClientInstance instance = (MQClientInstance) f1.get(consumer.getDefaultMQPullConsumerImpl());

    Field f = MQClientInstance.class.getDeclaredField("brokerAddrTable");
    f.setAccessible(true);

    Field f2 = MQClientInstance.class.getDeclaredField("scheduledExecutorService");
    f2.setAccessible(true);

    ScheduledExecutorService service = (ScheduledExecutorService) f2.get(instance);
    service.shutdown();
    service.awaitTermination(1000, TimeUnit.SECONDS);

    ConcurrentHashMap<String, HashMap<Long, String>> map = (ConcurrentHashMap<String, HashMap<Long, String>>) f.get(instance);
    HashMap<Long, String> addresses = new HashMap<>();
    addresses.put(0L, address);
    map.put("rmqmonitor_" + address, addresses);

    MessageQueue queue = new MessageQueue("SELF_TEST_TOPIC", "rmqmonitor_" + address, 0);

    boolean pullOk = false;
    long maxOffset = -1;
    for (int i = 0; i < 2; ++i) {
        try {
            maxOffset = consumer.getDefaultMQPullConsumerImpl().maxOffset(queue);
            PullResult result = consumer.pull(queue, "*", maxOffset > 100 ? maxOffset - 10 : 0, 1);
            if (result.getPullStatus() == PullStatus.FOUND) {
                pullOk = true;
                break;
            } else if(result.getPullStatus() == PullStatus.NO_NEW_MSG) {
                checkSend(cluster, nameSvr, address);
                continue;
            }

            logger.warn("pull result failed, PullResult={}, cluster={}, namesvr={}, address={}", result, cluster, nameSvr, address);
        } catch (Throwable e) {
            logger.error("pull exception, cluster={}, namesvr={}, address={}", cluster, nameSvr, address, e);
        }
        Thread.sleep(1000);
    }
    if (!pullOk) {
        logger.error(String.format("[AlarmPullErr] cluster=%s, broker=%s", cluster, address));
    } else {
        logger.info("AlarmPullCheck cluster={}, broker={}", cluster, address);
    }
    return maxOffset;
}
 
Example 19
Source File: MonitorService.java    From rocketmq_trans_message with Apache License 2.0 4 votes vote down vote up
private void computeUndoneMsgs(final UndoneMsgs undoneMsgs, final ConsumeStats consumeStats) {
    long total = 0;
    long singleMax = 0;
    long delayMax = 0;
    Iterator<Entry<MessageQueue, OffsetWrapper>> it = consumeStats.getOffsetTable().entrySet().iterator();
    while (it.hasNext()) {
        Entry<MessageQueue, OffsetWrapper> next = it.next();
        MessageQueue mq = next.getKey();
        OffsetWrapper ow = next.getValue();
        long diff = ow.getBrokerOffset() - ow.getConsumerOffset();

        if (diff > singleMax) {
            singleMax = diff;
        }

        if (diff > 0) {
            total += diff;
        }

        // Delay
        if (ow.getLastTimestamp() > 0) {
            try {
                long maxOffset = this.defaultMQPullConsumer.maxOffset(mq);
                if (maxOffset > 0) {
                    PullResult pull = this.defaultMQPullConsumer.pull(mq, "*", maxOffset - 1, 1);
                    switch (pull.getPullStatus()) {
                        case FOUND:
                            long delay =
                                pull.getMsgFoundList().get(0).getStoreTimestamp() - ow.getLastTimestamp();
                            if (delay > delayMax) {
                                delayMax = delay;
                            }
                            break;
                        case NO_MATCHED_MSG:
                        case NO_NEW_MSG:
                        case OFFSET_ILLEGAL:
                            break;
                        default:
                            break;
                    }
                }
            } catch (Exception e) {
            }
        }
    }

    undoneMsgs.setUndoneMsgsTotal(total);
    undoneMsgs.setUndoneMsgsSingleMQ(singleMax);
    undoneMsgs.setUndoneMsgsDelayTimeMills(delayMax);
}
 
Example 20
Source File: PullTaskCallbackImpl.java    From spring-boot-starter-samples with Apache License 2.0 2 votes vote down vote up
@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();

	}

}