Search in sources :

Example 26 with SubCommandException

use of org.apache.rocketmq.tools.command.SubCommandException in project rocketmq by apache.

the class DecodeMessageIdCommond method execute.

@Override
public void execute(final CommandLine commandLine, final Options options, RPCHook rpcHook) throws SubCommandException {
    String messageId = commandLine.getOptionValue('i').trim();
    try {
        System.out.printf("ip=%s", MessageClientIDSetter.getIPStrFromID(messageId));
    } catch (Exception e) {
        e.printStackTrace();
    }
    try {
        String date = UtilAll.formatDate(MessageClientIDSetter.getNearlyTimeFromID(messageId), UtilAll.YYYY_MM_DD_HH_MM_SS_SSS);
        System.out.printf("date=%s", date);
    } catch (Exception e) {
        throw new SubCommandException(this.getClass().getSimpleName() + " command failed", e);
    }
}
Also used : SubCommandException(org.apache.rocketmq.tools.command.SubCommandException) SubCommandException(org.apache.rocketmq.tools.command.SubCommandException)

Example 27 with SubCommandException

use of org.apache.rocketmq.tools.command.SubCommandException in project rocketmq by apache.

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 28 with SubCommandException

use of org.apache.rocketmq.tools.command.SubCommandException 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 29 with SubCommandException

use of org.apache.rocketmq.tools.command.SubCommandException in project rocketmq by apache.

the class QueryMsgByIdSubCommand method execute.

@Override
public void execute(CommandLine commandLine, Options options, RPCHook rpcHook) throws SubCommandException {
    DefaultMQAdminExt defaultMQAdminExt = new DefaultMQAdminExt(rpcHook);
    defaultMQAdminExt.setInstanceName(Long.toString(System.currentTimeMillis()));
    DefaultMQProducer defaultMQProducer = new DefaultMQProducer("ReSendMsgById");
    defaultMQProducer.setInstanceName(Long.toString(System.currentTimeMillis()));
    try {
        defaultMQAdminExt.start();
        if (commandLine.hasOption('s')) {
            if (commandLine.hasOption('u')) {
                String unitName = commandLine.getOptionValue('u').trim();
                defaultMQProducer.setUnitName(unitName);
            }
            defaultMQProducer.start();
        }
        final String msgIds = commandLine.getOptionValue('i').trim();
        final String[] msgIdArr = StringUtils.split(msgIds, ",");
        if (commandLine.hasOption('g') && commandLine.hasOption('d')) {
            final String consumerGroup = commandLine.getOptionValue('g').trim();
            final String clientId = commandLine.getOptionValue('d').trim();
            for (String msgId : msgIdArr) {
                if (StringUtils.isNotBlank(msgId)) {
                    pushMsg(defaultMQAdminExt, consumerGroup, clientId, msgId.trim());
                }
            }
        } else if (commandLine.hasOption('s')) {
            boolean resend = Boolean.parseBoolean(commandLine.getOptionValue('s', "false").trim());
            if (resend) {
                for (String msgId : msgIdArr) {
                    if (StringUtils.isNotBlank(msgId)) {
                        sendMsg(defaultMQAdminExt, defaultMQProducer, msgId.trim());
                    }
                }
            }
        } else {
            for (String msgId : msgIdArr) {
                if (StringUtils.isNotBlank(msgId)) {
                    queryById(defaultMQAdminExt, msgId.trim());
                }
            }
        }
    } catch (Exception e) {
        throw new SubCommandException(this.getClass().getSimpleName() + " command failed", e);
    } finally {
        defaultMQProducer.shutdown();
        defaultMQAdminExt.shutdown();
    }
}
Also used : SubCommandException(org.apache.rocketmq.tools.command.SubCommandException) DefaultMQAdminExt(org.apache.rocketmq.tools.admin.DefaultMQAdminExt) DefaultMQProducer(org.apache.rocketmq.client.producer.DefaultMQProducer) IOException(java.io.IOException) SubCommandException(org.apache.rocketmq.tools.command.SubCommandException) MQClientException(org.apache.rocketmq.client.exception.MQClientException) MQBrokerException(org.apache.rocketmq.client.exception.MQBrokerException) RemotingException(org.apache.rocketmq.remoting.exception.RemotingException)

Example 30 with SubCommandException

use of org.apache.rocketmq.tools.command.SubCommandException in project rocketmq by apache.

the class QueryMsgByKeySubCommand method execute.

@Override
public void execute(CommandLine commandLine, Options options, RPCHook rpcHook) throws SubCommandException {
    DefaultMQAdminExt defaultMQAdminExt = new DefaultMQAdminExt(rpcHook);
    defaultMQAdminExt.setInstanceName(Long.toString(System.currentTimeMillis()));
    try {
        final String topic = commandLine.getOptionValue('t').trim();
        final String key = commandLine.getOptionValue('k').trim();
        this.queryByKey(defaultMQAdminExt, topic, key);
    } catch (Exception e) {
        throw new SubCommandException(this.getClass().getSimpleName() + " command failed", e);
    } finally {
        defaultMQAdminExt.shutdown();
    }
}
Also used : SubCommandException(org.apache.rocketmq.tools.command.SubCommandException) DefaultMQAdminExt(org.apache.rocketmq.tools.admin.DefaultMQAdminExt) SubCommandException(org.apache.rocketmq.tools.command.SubCommandException) MQClientException(org.apache.rocketmq.client.exception.MQClientException)

Aggregations

SubCommandException (org.apache.rocketmq.tools.command.SubCommandException)88 DefaultMQAdminExt (org.apache.rocketmq.tools.admin.DefaultMQAdminExt)76 MessageQueue (org.apache.rocketmq.common.message.MessageQueue)22 MQBrokerException (org.apache.rocketmq.client.exception.MQBrokerException)16 MQClientException (org.apache.rocketmq.client.exception.MQClientException)16 Set (java.util.Set)14 RemotingException (org.apache.rocketmq.remoting.exception.RemotingException)12 UnsupportedEncodingException (java.io.UnsupportedEncodingException)8 LinkedList (java.util.LinkedList)8 Map (java.util.Map)8 DefaultMQProducer (org.apache.rocketmq.client.producer.DefaultMQProducer)8 Connection (org.apache.rocketmq.common.protocol.body.Connection)8 ConsumerConnection (org.apache.rocketmq.common.protocol.body.ConsumerConnection)8 TopicRouteData (org.apache.rocketmq.common.protocol.route.TopicRouteData)8 Date (java.util.Date)6 Entry (java.util.Map.Entry)6 Properties (java.util.Properties)6 DefaultMQPullConsumer (org.apache.rocketmq.client.consumer.DefaultMQPullConsumer)6 PullResult (org.apache.rocketmq.client.consumer.PullResult)6 ConsumeStats (org.apache.rocketmq.common.admin.ConsumeStats)6