Search in sources :

Example 16 with PullResult

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

the class PrintMessageSubCommand method execute.

@Override
public void execute(CommandLine commandLine, Options options, RPCHook rpcHook) throws SubCommandException {
    DefaultMQPullConsumer consumer = new DefaultMQPullConsumer(MixAll.TOOLS_CONSUMER_GROUP, rpcHook);
    try {
        String topic = commandLine.getOptionValue('t').trim();
        String charsetName = !commandLine.hasOption('c') ? "UTF-8" : commandLine.getOptionValue('c').trim();
        String subExpression = !commandLine.hasOption('s') ? "*" : commandLine.getOptionValue('s').trim();
        boolean printBody = !commandLine.hasOption('d') || Boolean.parseBoolean(commandLine.getOptionValue('d').trim());
        consumer.start();
        Set<MessageQueue> mqs = consumer.fetchSubscribeMessageQueues(topic);
        for (MessageQueue mq : mqs) {
            long minOffset = consumer.minOffset(mq);
            long maxOffset = consumer.maxOffset(mq);
            if (commandLine.hasOption('b')) {
                String timestampStr = commandLine.getOptionValue('b').trim();
                long timeValue = timestampFormat(timestampStr);
                minOffset = consumer.searchOffset(mq, timeValue);
            }
            if (commandLine.hasOption('e')) {
                String timestampStr = commandLine.getOptionValue('e').trim();
                long timeValue = timestampFormat(timestampStr);
                maxOffset = consumer.searchOffset(mq, timeValue);
            }
            System.out.printf("minOffset=%s, maxOffset=%s, %s", minOffset, maxOffset, mq);
            READQ: for (long offset = minOffset; offset < maxOffset; ) {
                try {
                    PullResult pullResult = consumer.pull(mq, subExpression, offset, 32);
                    offset = pullResult.getNextBeginOffset();
                    switch(pullResult.getPullStatus()) {
                        case FOUND:
                            printMessage(pullResult.getMsgFoundList(), charsetName, printBody);
                            break;
                        case NO_MATCHED_MSG:
                            System.out.printf(mq + " no matched msg. status=%s, offset=%s", pullResult.getPullStatus(), offset);
                            break;
                        case NO_NEW_MSG:
                        case OFFSET_ILLEGAL:
                            System.out.printf(mq + " print msg finished. status=%s, offset=%s", pullResult.getPullStatus(), offset);
                            break READQ;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    break;
                }
            }
        }
    } catch (Exception e) {
        throw new SubCommandException(this.getClass().getSimpleName() + " command failed", e);
    } finally {
        consumer.shutdown();
    }
}
Also used : MessageQueue(org.apache.rocketmq.common.message.MessageQueue) SubCommandException(org.apache.rocketmq.tools.command.SubCommandException) DefaultMQPullConsumer(org.apache.rocketmq.client.consumer.DefaultMQPullConsumer) PullResult(org.apache.rocketmq.client.consumer.PullResult) SubCommandException(org.apache.rocketmq.tools.command.SubCommandException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 17 with PullResult

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

the class RocketMQDataSourceTest method checkTopicData.

private static int checkTopicData(String topic) throws Exception {
    DefaultMQPullConsumer consumer = new DefaultMQPullConsumer("test_consumer");
    consumer.start();
    int messageCount = 0;
    Set<MessageQueue> mqs = consumer.fetchSubscribeMessageQueues(topic);
    for (MessageQueue mq : mqs) {
        PullResult pullResult = consumer.pull(mq, null, 0, 1000);
        if (pullResult.getPullStatus() == PullStatus.FOUND) {
            for (int i = 0; i < pullResult.getMsgFoundList().size(); i++) {
                String messageBody = new String(pullResult.getMsgFoundList().get(i).getBody());
                logger.info("Got message: " + messageBody);
                assertEquals("\"Hello Rocket\"", messageBody.substring(0, 14));
                messageCount++;
            }
        }
    }
    consumer.shutdown();
    return messageCount;
}
Also used : MessageQueue(org.apache.rocketmq.common.message.MessageQueue) DefaultMQPullConsumer(org.apache.rocketmq.client.consumer.DefaultMQPullConsumer) PullResult(org.apache.rocketmq.client.consumer.PullResult)

Example 18 with PullResult

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

the class RocketMQSinkTest method testBatchEvent.

@Test
public void testBatchEvent() throws MQClientException, InterruptedException, EventDeliveryException, RemotingException, MQBrokerException, UnsupportedEncodingException {
    /*
        start sink
         */
    Context context = new Context();
    context.put(NAME_SERVER_CONFIG, nameServer);
    context.put(TAG_CONFIG, tag);
    context.put(BATCH_SIZE_CONFIG, String.valueOf(batchSize));
    RocketMQSink sink = new RocketMQSink();
    Configurables.configure(sink, context);
    MemoryChannel channel = new MemoryChannel();
    Configurables.configure(channel, context);
    sink.setChannel(channel);
    sink.start();
    /*
        mock flume source
         */
    Map<String, String> msgs = new HashMap<>();
    Transaction tx = channel.getTransaction();
    tx.begin();
    int sendNum = 0;
    for (int i = 0; i < batchSize; i++) {
        String sendMsg = "\"Hello RocketMQ\"" + "," + DateFormatUtils.format(new Date(), "yyyy-MM-DD hh:mm:ss:SSSS");
        Event event = EventBuilder.withBody(sendMsg.getBytes(), null);
        channel.put(event);
        log.info("publish message : {}", sendMsg);
        String[] sendMsgKv = sendMsg.split(",");
        msgs.put(sendMsgKv[1], sendMsgKv[0]);
        sendNum++;
        Thread.sleep(10);
    }
    log.info("send message num={}", sendNum);
    tx.commit();
    tx.close();
    Sink.Status status = sink.process();
    if (status == Sink.Status.BACKOFF) {
        fail("Error");
    }
    sink.stop();
    /*
        consumer message
         */
    consumer = new DefaultMQPullConsumer(consumerGroup);
    consumer.setNamesrvAddr(nameServer);
    consumer.setMessageModel(MessageModel.valueOf("BROADCASTING"));
    consumer.registerMessageQueueListener(TOPIC_DEFAULT, null);
    consumer.start();
    int receiveNum = 0;
    String receiveMsg = null;
    Set<MessageQueue> queues = consumer.fetchSubscribeMessageQueues(TOPIC_DEFAULT);
    for (MessageQueue queue : queues) {
        long offset = getMessageQueueOffset(queue);
        PullResult pullResult = consumer.pull(queue, tag, offset, batchSize);
        if (pullResult.getPullStatus() == PullStatus.FOUND) {
            for (MessageExt message : pullResult.getMsgFoundList()) {
                byte[] body = message.getBody();
                receiveMsg = new String(body, "UTF-8");
                String[] receiveMsgKv = receiveMsg.split(",");
                msgs.remove(receiveMsgKv[1]);
                log.info("receive message : {}", receiveMsg);
                receiveNum++;
            }
            long nextBeginOffset = pullResult.getNextBeginOffset();
            putMessageQueueOffset(queue, nextBeginOffset);
        }
    }
    log.info("receive message num={}", receiveNum);
    /*
        wait for processQueueTable init
         */
    Thread.sleep(1000);
    consumer.shutdown();
    assertEquals(msgs.size(), 0);
}
Also used : Context(org.apache.flume.Context) MemoryChannel(org.apache.flume.channel.MemoryChannel) HashMap(java.util.HashMap) Date(java.util.Date) DefaultMQPullConsumer(org.apache.rocketmq.client.consumer.DefaultMQPullConsumer) PullResult(org.apache.rocketmq.client.consumer.PullResult) MessageExt(org.apache.rocketmq.common.message.MessageExt) Transaction(org.apache.flume.Transaction) Sink(org.apache.flume.Sink) MessageQueue(org.apache.rocketmq.common.message.MessageQueue) Event(org.apache.flume.Event) Test(org.junit.Test)

Example 19 with PullResult

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

the class OffsetSyncStore method sync.

private boolean sync(Duration pullTimeout) throws RemotingException, MQClientException, InterruptedException, MQBrokerException {
    TopicRouteData route = adminExt.examineTopicRouteInfo(taskConfig.getOffsetSyncTopic());
    String brokerName = route.getQueueDatas().get(0).getBrokerName();
    MessageQueue mq = new MessageQueue(taskConfig.getOffsetSyncTopic(), brokerName, 0);
    PullResult pr = consumer.pull(mq, "", lastOffset, 0, pullTimeout.getNano() / Duration.ofMillis(1).getNano());
    if (pr.getPullStatus() != PullStatus.FOUND) {
        return false;
    }
    handle(pr);
    return true;
}
Also used : MessageQueue(org.apache.rocketmq.common.message.MessageQueue) PullResult(org.apache.rocketmq.client.consumer.PullResult) TopicRouteData(org.apache.rocketmq.common.protocol.route.TopicRouteData)

Example 20 with PullResult

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

the class PullTaskCallbackImpl method doPullTask.

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

Aggregations

PullResult (org.apache.rocketmq.client.consumer.PullResult)38 MessageQueue (org.apache.rocketmq.common.message.MessageQueue)29 DefaultMQPullConsumer (org.apache.rocketmq.client.consumer.DefaultMQPullConsumer)19 MQClientException (org.apache.rocketmq.client.exception.MQClientException)19 MessageExt (org.apache.rocketmq.common.message.MessageExt)12 MQBrokerException (org.apache.rocketmq.client.exception.MQBrokerException)8 RemotingException (org.apache.rocketmq.remoting.exception.RemotingException)8 HashMap (java.util.HashMap)7 PullCallback (org.apache.rocketmq.client.consumer.PullCallback)6 SubscriptionData (org.apache.rocketmq.common.protocol.heartbeat.SubscriptionData)6 SubCommandException (org.apache.rocketmq.tools.command.SubCommandException)6 ArrayList (java.util.ArrayList)5 UnsupportedEncodingException (java.io.UnsupportedEncodingException)4 List (java.util.List)4 PullMessageRequestHeader (org.apache.rocketmq.common.protocol.header.PullMessageRequestHeader)4 Test (org.junit.Test)4 Date (java.util.Date)3 Event (org.apache.flume.Event)3 MQPullConsumer (org.apache.rocketmq.client.consumer.MQPullConsumer)3 PullTaskCallback (org.apache.rocketmq.client.consumer.PullTaskCallback)3