Java Code Examples for org.apache.rocketmq.client.consumer.DefaultMQPullConsumer#shutdown()

The following examples show how to use org.apache.rocketmq.client.consumer.DefaultMQPullConsumer#shutdown() . 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 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.printf("%s%n", System.currentTimeMillis() - beginTime);
        System.out.printf("%s%n", pullResult);
    } catch (Exception e) {
        e.printStackTrace();
    }

    consumer.shutdown();
}
 
Example 2
Source File: PullConsumerTest.java    From rocketmq_trans_message 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.printf("%s%n", System.currentTimeMillis() - beginTime);
        System.out.printf("%s%n", pullResult);
    } catch (Exception e) {
        e.printStackTrace();
    }

    consumer.shutdown();
}
 
Example 3
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.setNamesrvAddr("127.0.0.1:9876");
    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.printf("%s%n", System.currentTimeMillis() - beginTime);
        System.out.printf("%s%n", pullResult);
    } catch (Exception e) {
        e.printStackTrace();
    }

    consumer.shutdown();
}
 
Example 4
Source File: PullConsumerTest.java    From rocketmq-read 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.printf("%s%n", System.currentTimeMillis() - beginTime);
        System.out.printf("%s%n", pullResult);
    } catch (Exception e) {
        e.printStackTrace();
    }

    consumer.shutdown();
}
 
Example 5
Source File: PullConsumer.java    From rocketmq-read 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.setNamesrvAddr("127.0.0.1:9876");
    consumer.start();

    Set<MessageQueue> mqs = consumer.fetchSubscribeMessageQueues("face-manager-1");
    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 6
Source File: MQHelper.java    From DDMQ 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
 */
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: RmqFetcher.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
@Override
public synchronized boolean start() {
    DefaultMQPullConsumer consumer = new DefaultMQPullConsumer(config.getGroup());
    RocketmqConfiguration rmqConfig = config.getcProxyConfig().getRocketmqConfigs().get(config.getBrokerCluster());
    String nameServers = StringUtils.join(rmqConfig.getNamesrvAddrs().iterator(), ";");
    consumer.setNamesrvAddr(nameServers);
    //必须这样设置。一个instance只能有一个group
    consumer.setInstanceName(RmqCidMaker.makeCid(SECOND_LOWLEVEL_CONSUMER_INSTANCE_NUM, config.getGroup(), config.getBrokerCluster(), config.getcProxyConfig().getProxyCluster(), config.getInstance(), LOW_LEVEL_V2));//根据instancename 隔离highlevel和lowlevel的netty资源

    //fix 2018.6.14 lowlevel实例过多,netty线程未共享
    consumer.setClientCallbackExecutorThreads(1);
    consumer.setMessageQueueListener(this);
    consumer.setPersistConsumerOffsetInterval(rmqConfig.getPersistConsumerOffsetInterval());
    consumer.setPollNameServerInterval(rmqConfig.getPollNameServerInterval());
    consumer.setRegisterTopics(new HashSet<>(config.getTopicNames()));
    try {
        consumer.start();
    } catch (MQClientException e) {
        LogUtils.MAIN_LOGGER.error(String.format("start RmqFetcher failed. group@Cluster=%s,consumerId=%s",
                config.getGroupBrokerCluster(), cid), e);
        consumer.shutdown();
        return false;
    }
    this.consumer = consumer;
    LogUtils.logMainInfo("RmqFetcher.start, this={}", this);
    return true;
}
 
Example 8
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
 */
public static void resetOffsetByTimestamp(
    final MessageModel messageModel,
    final String instanceName,
    final String consumerGroup,
    final String topic,
    final long timestamp) throws Exception {
    final InternalLogger 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: MQHelper.java    From rocketmq_trans_message 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 10
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 11
Source File: MQHelper.java    From rocketmq-4.3.0 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
 */
public static void resetOffsetByTimestamp(
    final MessageModel messageModel,
    final String instanceName,
    final String consumerGroup,
    final String topic,
    final long timestamp) throws Exception {
    final InternalLogger 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 12
Source File: RmqFetcher.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
@Override
public synchronized boolean start() {
    DefaultMQPullConsumer consumer = new DefaultMQPullConsumer(config.getGroup());
    RocketmqConfiguration rmqConfig = config.getcProxyConfig().getRocketmqConfigs().get(config.getBrokerCluster());
    String nameServers = StringUtils.join(rmqConfig.getNamesrvAddrs().iterator(), ";");
    consumer.setNamesrvAddr(nameServers);
    //必须这样设置。一个instance只能有一个group
    consumer.setInstanceName(RmqCidMaker.makeCid(SECOND_LOWLEVEL_CONSUMER_INSTANCE_NUM, config.getGroup(), config.getBrokerCluster(), config.getcProxyConfig().getProxyCluster(), config.getInstance(), LOW_LEVEL_V2));//根据instancename 隔离highlevel和lowlevel的netty资源

    //fix 2018.6.14 lowlevel实例过多,netty线程未共享
    consumer.setClientCallbackExecutorThreads(1);
    consumer.setMessageQueueListener(this);
    consumer.setPersistConsumerOffsetInterval(rmqConfig.getPersistConsumerOffsetInterval());
    consumer.setPollNameServerInterval(rmqConfig.getPollNameServerInterval());
    consumer.setRegisterTopics(new HashSet<>(config.getTopicNames()));
    try {
        consumer.start();
    } catch (MQClientException e) {
        LogUtils.MAIN_LOGGER.error(String.format("start RmqFetcher failed. group@Cluster=%s,consumerId=%s",
                config.getGroupBrokerCluster(), cid), e);
        consumer.shutdown();
        return false;
    }
    this.consumer = consumer;
    LogUtils.logMainInfo("RmqFetcher.start, this={}", this);
    return true;
}
 
Example 13
Source File: PullConsumer.java    From rocketmq_trans_message 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 14
Source File: MQHelper.java    From DDMQ 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
 */
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 15
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 16
Source File: PullConsumer.java    From DDMQ 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: %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 17
Source File: DLedgerProduceAndConsumeIT.java    From rocketmq with Apache License 2.0 4 votes vote down vote up
@Test
public void testProduceAndConsume() throws Exception {
    String cluster = UUID.randomUUID().toString();
    String brokerName = UUID.randomUUID().toString();
    String selfId = "n0";
    String peers = String.format("n0-localhost:%d", nextPort());
    BrokerConfig brokerConfig = buildBrokerConfig(cluster, brokerName);
    MessageStoreConfig storeConfig = buildStoreConfig(brokerName, peers, selfId);
    BrokerController brokerController = IntegrationTestBase.createAndStartBroker(storeConfig, brokerConfig);
    Thread.sleep(3000);

    Assert.assertEquals(BrokerRole.SYNC_MASTER, storeConfig.getBrokerRole());


    String topic = UUID.randomUUID().toString();
    String consumerGroup = UUID.randomUUID().toString();
    IntegrationTestBase.initTopic(topic, BaseConf.nsAddr, cluster, 1);
    DefaultMQProducer producer = ProducerFactory.getRMQProducer(BaseConf.nsAddr);
    DefaultMQPullConsumer consumer = ConsumerFactory.getRMQPullConsumer(BaseConf.nsAddr, consumerGroup);

    for (int i = 0; i < 10; i++) {
        Message message = new Message();
        message.setTopic(topic);
        message.setBody(("Hello" + i).getBytes());
        SendResult sendResult = producer.send(message);
        Assert.assertEquals(SendStatus.SEND_OK, sendResult.getSendStatus());
        Assert.assertEquals(0, sendResult.getMessageQueue().getQueueId());
        Assert.assertEquals(brokerName, sendResult.getMessageQueue().getBrokerName());
        Assert.assertEquals(i, sendResult.getQueueOffset());
        Assert.assertNotNull(sendResult.getMsgId());
        Assert.assertNotNull(sendResult.getOffsetMsgId());
    }

    Thread.sleep(500);
    Assert.assertEquals(0, brokerController.getMessageStore().getMinOffsetInQueue(topic, 0));
    Assert.assertEquals(10, brokerController.getMessageStore().getMaxOffsetInQueue(topic, 0));

    MessageQueue messageQueue = new MessageQueue(topic, brokerName, 0);
    PullResult pullResult= consumer.pull(messageQueue, "*", 0, 32);
    Assert.assertEquals(PullStatus.FOUND, pullResult.getPullStatus());
    Assert.assertEquals(10, pullResult.getMsgFoundList().size());

    for (int i = 0; i < 10; i++) {
        MessageExt messageExt = pullResult.getMsgFoundList().get(i);
        Assert.assertEquals(i, messageExt.getQueueOffset());
        Assert.assertArrayEquals(("Hello" + i).getBytes(), messageExt.getBody());
    }

    producer.shutdown();
    consumer.shutdown();
    brokerController.shutdown();
}
 
Example 18
Source File: PullConsumerDemo.java    From Sentinel with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws MQClientException {
    // First we init the flow control rule for Sentinel.
    initFlowControlRule();

    DefaultMQPullConsumer consumer = new DefaultMQPullConsumer(Constants.TEST_GROUP_NAME);

    consumer.start();

    Set<MessageQueue> mqs = consumer.fetchSubscribeMessageQueues(Constants.TEST_TOPIC_NAME);
    for (MessageQueue mq : mqs) {
        System.out.printf("Consuming messages from the queue: %s%n", mq);
        SINGLE_MQ:
        while (true) {
            try {
                PullResult pullResult =
                    consumer.pullBlockIfNotFound(mq, null, getMessageQueueOffset(mq), 32);
                if (pullResult.getMsgFoundList() != null) {
                    for (MessageExt msg : pullResult.getMsgFoundList()) {
                        doSomething(msg);
                    }
                }

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

    consumer.shutdown();
}
 
Example 19
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 20
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) 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();
    }
}