Search in sources :

Example 1 with PullResult

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

the class PrintMessageByQueueCommand 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 charsetName = !commandLine.hasOption('c') ? "UTF-8" : commandLine.getOptionValue('c').trim();
        boolean printMsg = commandLine.hasOption('p') && Boolean.parseBoolean(commandLine.getOptionValue('p').trim());
        boolean printBody = commandLine.hasOption('d') && Boolean.parseBoolean(commandLine.getOptionValue('d').trim());
        boolean calByTag = commandLine.hasOption('f') && Boolean.parseBoolean(commandLine.getOptionValue('f').trim());
        String subExpression = !commandLine.hasOption('s') ? "*" : commandLine.getOptionValue('s').trim();
        String topic = commandLine.getOptionValue('t').trim();
        String brokerName = commandLine.getOptionValue('a').trim();
        int queueId = Integer.parseInt(commandLine.getOptionValue('i').trim());
        consumer.start();
        MessageQueue mq = new MessageQueue(topic, brokerName, queueId);
        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);
        }
        final Map<String, AtomicLong> tagCalmap = new HashMap<String, AtomicLong>();
        READQ: for (long offset = minOffset; offset < maxOffset; ) {
            try {
                PullResult pullResult = consumer.pull(mq, subExpression, offset, 32);
                offset = pullResult.getNextBeginOffset();
                switch(pullResult.getPullStatus()) {
                    case FOUND:
                        calculateByTag(pullResult.getMsgFoundList(), tagCalmap, calByTag);
                        printMessage(pullResult.getMsgFoundList(), charsetName, printMsg, printBody);
                        break;
                    case NO_MATCHED_MSG:
                    case NO_NEW_MSG:
                    case OFFSET_ILLEGAL:
                        break READQ;
                }
            } catch (Exception e) {
                e.printStackTrace();
                break;
            }
        }
        printCalculateByTag(tagCalmap, calByTag);
    } catch (Exception e) {
        throw new SubCommandException(this.getClass().getSimpleName() + " command failed", e);
    } finally {
        consumer.shutdown();
    }
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) MessageQueue(org.apache.rocketmq.common.message.MessageQueue) SubCommandException(org.apache.rocketmq.tools.command.SubCommandException) HashMap(java.util.HashMap) 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 2 with PullResult

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

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=" + minOffset + ", maxOffset=" + 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=" + pullResult.getPullStatus() + ", offset=" + offset);
                            break;
                        case NO_NEW_MSG:
                        case OFFSET_ILLEGAL:
                            System.out.printf(mq + " print msg finished. status=" + pullResult.getPullStatus() + ", offset=" + 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 3 with PullResult

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

the class PullScheduleService method main.

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

        @Override
        public void doPullTask(MessageQueue mq, PullTaskContext context) {
            MQPullConsumer consumer = context.getPullConsumer();
            try {
                long offset = consumer.fetchConsumeOffset(mq, false);
                if (offset < 0)
                    offset = 0;
                PullResult pullResult = consumer.pull(mq, "*", offset, 32);
                System.out.printf("%s%n", offset + "\t" + mq + "\t" + pullResult);
                switch(pullResult.getPullStatus()) {
                    case FOUND:
                        break;
                    case NO_MATCHED_MSG:
                        break;
                    case NO_NEW_MSG:
                    case OFFSET_ILLEGAL:
                        break;
                    default:
                        break;
                }
                consumer.updateConsumeOffset(mq, pullResult.getNextBeginOffset());
                context.setPullNextDelayTimeMillis(100);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
    scheduleService.start();
}
Also used : MessageQueue(org.apache.rocketmq.common.message.MessageQueue) PullTaskContext(org.apache.rocketmq.client.consumer.PullTaskContext) MQPullConsumerScheduleService(org.apache.rocketmq.client.consumer.MQPullConsumerScheduleService) PullTaskCallback(org.apache.rocketmq.client.consumer.PullTaskCallback) MQPullConsumer(org.apache.rocketmq.client.consumer.MQPullConsumer) PullResult(org.apache.rocketmq.client.consumer.PullResult) MQClientException(org.apache.rocketmq.client.exception.MQClientException)

Example 4 with PullResult

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

the class PullConsumer method main.

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();
}
Also used : MessageQueue(org.apache.rocketmq.common.message.MessageQueue) DefaultMQPullConsumer(org.apache.rocketmq.client.consumer.DefaultMQPullConsumer) PullResult(org.apache.rocketmq.client.consumer.PullResult) MQClientException(org.apache.rocketmq.client.exception.MQClientException)

Example 5 with PullResult

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

the class PullConsumerTest method main.

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();
}
Also used : MessageQueue(org.apache.rocketmq.common.message.MessageQueue) DefaultMQPullConsumer(org.apache.rocketmq.client.consumer.DefaultMQPullConsumer) PullResult(org.apache.rocketmq.client.consumer.PullResult) MQClientException(org.apache.rocketmq.client.exception.MQClientException)

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