Java Code Examples for org.apache.rocketmq.common.message.MessageQueue#setBrokerName()

The following examples show how to use org.apache.rocketmq.common.message.MessageQueue#setBrokerName() . 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 DDMQ 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: DefaultMQPushConsumerTest.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 6 votes vote down vote up
private PullRequest createPullRequest() {
    PullRequest pullRequest = new PullRequest();
    pullRequest.setConsumerGroup(consumerGroup);
    pullRequest.setNextOffset(1024);

    MessageQueue messageQueue = new MessageQueue();
    messageQueue.setBrokerName(brokerName);
    messageQueue.setQueueId(0);
    messageQueue.setTopic(topic);
    pullRequest.setMessageQueue(messageQueue);
    ProcessQueue processQueue = new ProcessQueue();
    processQueue.setLocked(true);
    processQueue.setLastLockTimestamp(System.currentTimeMillis());
    pullRequest.setProcessQueue(processQueue);

    return pullRequest;
}
 
Example 3
Source File: DefaultMQPushConsumerTest.java    From DDMQ with Apache License 2.0 6 votes vote down vote up
private PullRequest createPullRequest() {
    PullRequest pullRequest = new PullRequest();
    pullRequest.setConsumerGroup(consumerGroup);
    pullRequest.setNextOffset(1024);

    MessageQueue messageQueue = new MessageQueue();
    messageQueue.setBrokerName(brokerName);
    messageQueue.setQueueId(0);
    messageQueue.setTopic(topic);
    pullRequest.setMessageQueue(messageQueue);
    ProcessQueue processQueue = new ProcessQueue();
    processQueue.setLocked(true);
    processQueue.setLastLockTimestamp(System.currentTimeMillis());
    pullRequest.setProcessQueue(processQueue);

    return pullRequest;
}
 
Example 4
Source File: PullConsumerTest.java    From rocketmq-all-4.1.0-incubating 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: DefaultMQPushConsumerTest.java    From rocketmq-4.3.0 with Apache License 2.0 6 votes vote down vote up
private PullRequest createPullRequest() {
    PullRequest pullRequest = new PullRequest();
    pullRequest.setConsumerGroup(consumerGroup);
    pullRequest.setNextOffset(1024);

    MessageQueue messageQueue = new MessageQueue();
    messageQueue.setBrokerName(brokerName);
    messageQueue.setQueueId(0);
    messageQueue.setTopic(topic);
    pullRequest.setMessageQueue(messageQueue);
    ProcessQueue processQueue = new ProcessQueue();
    processQueue.setLocked(true);
    processQueue.setLastLockTimestamp(System.currentTimeMillis());
    pullRequest.setProcessQueue(processQueue);

    return pullRequest;
}
 
Example 6
Source File: QueryMsgByOffsetSubCommand.java    From rocketmq-read 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 7
Source File: AdminBrokerProcessor.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 4 votes vote down vote up
private RemotingCommand getTopicStatsInfo(ChannelHandlerContext ctx, RemotingCommand request) throws RemotingCommandException {
    final RemotingCommand response = RemotingCommand.createResponseCommand(null);
    final GetTopicStatsInfoRequestHeader requestHeader =
        (GetTopicStatsInfoRequestHeader) request.decodeCommandCustomHeader(GetTopicStatsInfoRequestHeader.class);

    final String topic = requestHeader.getTopic();
    TopicConfig topicConfig = this.brokerController.getTopicConfigManager().selectTopicConfig(topic);
    if (null == topicConfig) {
        response.setCode(ResponseCode.TOPIC_NOT_EXIST);
        response.setRemark("topic[" + topic + "] not exist");
        return response;
    }

    TopicStatsTable topicStatsTable = new TopicStatsTable();
    for (int i = 0; i < topicConfig.getWriteQueueNums(); i++) {
        MessageQueue mq = new MessageQueue();
        mq.setTopic(topic);
        mq.setBrokerName(this.brokerController.getBrokerConfig().getBrokerName());
        mq.setQueueId(i);

        TopicOffset topicOffset = new TopicOffset();
        long min = this.brokerController.getMessageStore().getMinOffsetInQueue(topic, i);
        if (min < 0)
            min = 0;

        long max = this.brokerController.getMessageStore().getMaxOffsetInQueue(topic, i);
        if (max < 0)
            max = 0;

        long timestamp = 0;
        if (max > 0) {
            timestamp = this.brokerController.getMessageStore().getMessageStoreTimeStamp(topic, i, max - 1);
        }

        topicOffset.setMinOffset(min);
        topicOffset.setMaxOffset(max);
        topicOffset.setLastUpdateTimestamp(timestamp);

        topicStatsTable.getOffsetTable().put(mq, topicOffset);
    }

    byte[] body = topicStatsTable.encode();
    response.setBody(body);
    response.setCode(ResponseCode.SUCCESS);
    response.setRemark(null);
    return response;
}
 
Example 8
Source File: AdminBrokerProcessor.java    From rocketmq with Apache License 2.0 4 votes vote down vote up
private RemotingCommand getTopicStatsInfo(ChannelHandlerContext ctx,
    RemotingCommand request) throws RemotingCommandException {
    final RemotingCommand response = RemotingCommand.createResponseCommand(null);
    final GetTopicStatsInfoRequestHeader requestHeader =
        (GetTopicStatsInfoRequestHeader) request.decodeCommandCustomHeader(GetTopicStatsInfoRequestHeader.class);

    final String topic = requestHeader.getTopic();
    TopicConfig topicConfig = this.brokerController.getTopicConfigManager().selectTopicConfig(topic);
    if (null == topicConfig) {
        response.setCode(ResponseCode.TOPIC_NOT_EXIST);
        response.setRemark("topic[" + topic + "] not exist");
        return response;
    }

    TopicStatsTable topicStatsTable = new TopicStatsTable();
    for (int i = 0; i < topicConfig.getWriteQueueNums(); i++) {
        MessageQueue mq = new MessageQueue();
        mq.setTopic(topic);
        mq.setBrokerName(this.brokerController.getBrokerConfig().getBrokerName());
        mq.setQueueId(i);

        TopicOffset topicOffset = new TopicOffset();
        long min = this.brokerController.getMessageStore().getMinOffsetInQueue(topic, i);
        if (min < 0)
            min = 0;

        long max = this.brokerController.getMessageStore().getMaxOffsetInQueue(topic, i);
        if (max < 0)
            max = 0;

        long timestamp = 0;
        if (max > 0) {
            timestamp = this.brokerController.getMessageStore().getMessageStoreTimeStamp(topic, i, max - 1);
        }

        topicOffset.setMinOffset(min);
        topicOffset.setMaxOffset(max);
        topicOffset.setLastUpdateTimestamp(timestamp);

        topicStatsTable.getOffsetTable().put(mq, topicOffset);
    }

    byte[] body = topicStatsTable.encode();
    response.setBody(body);
    response.setCode(ResponseCode.SUCCESS);
    response.setRemark(null);
    return response;
}
 
Example 9
Source File: AdminBrokerProcessor.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 4 votes vote down vote up
private RemotingCommand queryConsumeTimeSpan(ChannelHandlerContext ctx, RemotingCommand request) throws RemotingCommandException {
    final RemotingCommand response = RemotingCommand.createResponseCommand(null);
    QueryConsumeTimeSpanRequestHeader requestHeader =
        (QueryConsumeTimeSpanRequestHeader) request.decodeCommandCustomHeader(QueryConsumeTimeSpanRequestHeader.class);

    final String topic = requestHeader.getTopic();
    TopicConfig topicConfig = this.brokerController.getTopicConfigManager().selectTopicConfig(topic);
    if (null == topicConfig) {
        response.setCode(ResponseCode.TOPIC_NOT_EXIST);
        response.setRemark("topic[" + topic + "] not exist");
        return response;
    }

    List<QueueTimeSpan> timeSpanSet = new ArrayList<QueueTimeSpan>();
    for (int i = 0; i < topicConfig.getWriteQueueNums(); i++) {
        QueueTimeSpan timeSpan = new QueueTimeSpan();
        MessageQueue mq = new MessageQueue();
        mq.setTopic(topic);
        mq.setBrokerName(this.brokerController.getBrokerConfig().getBrokerName());
        mq.setQueueId(i);
        timeSpan.setMessageQueue(mq);

        long minTime = this.brokerController.getMessageStore().getEarliestMessageTime(topic, i);
        timeSpan.setMinTimeStamp(minTime);

        long max = this.brokerController.getMessageStore().getMaxOffsetInQueue(topic, i);
        long maxTime = this.brokerController.getMessageStore().getMessageStoreTimeStamp(topic, i, max - 1);
        timeSpan.setMaxTimeStamp(maxTime);

        long consumeTime;
        long consumerOffset = this.brokerController.getConsumerOffsetManager().queryOffset(
            requestHeader.getGroup(), topic, i);
        if (consumerOffset > 0) {
            consumeTime = this.brokerController.getMessageStore().getMessageStoreTimeStamp(topic, i, consumerOffset - 1);
        } else {
            consumeTime = minTime;
        }
        timeSpan.setConsumeTimeStamp(consumeTime);

        long maxBrokerOffset = this.brokerController.getMessageStore().getMaxOffsetInQueue(requestHeader.getTopic(), i);
        if (consumerOffset < maxBrokerOffset) {
            long nextTime = this.brokerController.getMessageStore().getMessageStoreTimeStamp(topic, i, consumerOffset);
            timeSpan.setDelayTime(System.currentTimeMillis() - nextTime);
        }
        timeSpanSet.add(timeSpan);
    }

    QueryConsumeTimeSpanBody queryConsumeTimeSpanBody = new QueryConsumeTimeSpanBody();
    queryConsumeTimeSpanBody.setConsumeTimeSpanSet(timeSpanSet);
    response.setBody(queryConsumeTimeSpanBody.encode());
    response.setCode(ResponseCode.SUCCESS);
    response.setRemark(null);
    return response;
}
 
Example 10
Source File: QueryMsgByUniqueKeySubCommandTest.java    From rocketmq with Apache License 2.0 4 votes vote down vote up
@Before
public void before() throws NoSuchFieldException, IllegalAccessException, InterruptedException, RemotingException, MQClientException, MQBrokerException {

    mQClientAPIImpl = mock(MQClientAPIImpl.class);
    mQAdminImpl = mock(MQAdminImpl.class);

    defaultMQAdminExt = new DefaultMQAdminExt();
    defaultMQAdminExtImpl = new DefaultMQAdminExtImpl(defaultMQAdminExt, 1000);

    Field field = DefaultMQAdminExtImpl.class.getDeclaredField("mqClientInstance");
    field.setAccessible(true);
    field.set(defaultMQAdminExtImpl, mqClientInstance);

    field = MQClientInstance.class.getDeclaredField("mQClientAPIImpl");
    field.setAccessible(true);
    field.set(mqClientInstance, mQClientAPIImpl);

    field = MQClientInstance.class.getDeclaredField("mQAdminImpl");
    field.setAccessible(true);
    field.set(mqClientInstance, mQAdminImpl);


    field = DefaultMQAdminExt.class.getDeclaredField("defaultMQAdminExtImpl");
    field.setAccessible(true);
    field.set(defaultMQAdminExt, defaultMQAdminExtImpl);

    ConsumeMessageDirectlyResult result = new ConsumeMessageDirectlyResult();
    result.setConsumeResult(CMResult.CR_SUCCESS);
    result.setRemark("customRemark_122333444");
    when(mQClientAPIImpl.consumeMessageDirectly(anyString(), anyString(), anyString(), anyString(), anyLong())).thenReturn(result);

    MessageExt retMsgExt = new MessageExt();
    retMsgExt.setMsgId("0A3A54F7BF7D18B4AAC28A3FA2CF0000");
    retMsgExt.setBody("this is message ext body".getBytes());
    retMsgExt.setTopic("testTopic");
    retMsgExt.setTags("testTags");
    retMsgExt.setStoreHost(new InetSocketAddress("127.0.0.1", 8899));
    retMsgExt.setBornHost(new InetSocketAddress("127.0.0.1", 7788));
    retMsgExt.setQueueId(1);
    retMsgExt.setQueueOffset(12L);
    retMsgExt.setCommitLogOffset(123);
    retMsgExt.setReconsumeTimes(2);
    retMsgExt.setBornTimestamp(System.currentTimeMillis());
    retMsgExt.setStoreTimestamp(System.currentTimeMillis());
    when(mQAdminImpl.viewMessage(anyString())).thenReturn(retMsgExt);

    when(mQAdminImpl.queryMessageByUniqKey(anyString(), anyString())).thenReturn(retMsgExt);

    TopicRouteData topicRouteData = new TopicRouteData();
    List<BrokerData> brokerDataList = new ArrayList<BrokerData>();
    BrokerData brokerData = new BrokerData();
    HashMap<Long, String> brokerAddrs = new HashMap<Long, String>();
    brokerAddrs.put(MixAll.MASTER_ID, "127.0.0.1:9876");
    brokerData.setBrokerAddrs(brokerAddrs);
    brokerDataList.add(brokerData);
    topicRouteData.setBrokerDatas(brokerDataList);
    when(mQClientAPIImpl.getTopicRouteInfoFromNameServer(anyString(), anyLong())).thenReturn(topicRouteData);

    GroupList groupList = new GroupList();
    HashSet<String> groupSets = new HashSet<String>();
    groupSets.add("testGroup");
    groupList.setGroupList(groupSets);
    when(mQClientAPIImpl.queryTopicConsumeByWho(anyString(), anyString(), anyLong())).thenReturn(groupList);


    ConsumeStats consumeStats = new ConsumeStats();
    consumeStats.setConsumeTps(100*10000);
    HashMap<MessageQueue, OffsetWrapper> offsetTable = new HashMap<MessageQueue, OffsetWrapper>();
    MessageQueue messageQueue = new MessageQueue();
    messageQueue.setBrokerName("messageQueue BrokerName testing");
    messageQueue.setTopic("messageQueue topic");
    messageQueue.setQueueId(1);
    OffsetWrapper offsetWrapper = new OffsetWrapper();
    offsetWrapper.setBrokerOffset(100);
    offsetWrapper.setConsumerOffset(200);
    offsetWrapper.setLastTimestamp(System.currentTimeMillis());
    offsetTable.put(messageQueue, offsetWrapper);
    consumeStats.setOffsetTable(offsetTable);
    when(mQClientAPIImpl.getConsumeStats(anyString(), anyString(), (String)isNull(), anyLong())).thenReturn(consumeStats);

    ClusterInfo clusterInfo = new ClusterInfo();
    HashMap<String, BrokerData> brokerAddrTable = new HashMap<String, BrokerData>();
    brokerAddrTable.put("key", brokerData);
    clusterInfo.setBrokerAddrTable(brokerAddrTable);
    HashMap<String, Set<String>> clusterAddrTable = new HashMap<String, Set<String>>();
    Set<String> addrSet = new HashSet<String>();
    addrSet.add("127.0.0.1:9876");
    clusterAddrTable.put("key", addrSet);
    clusterInfo.setClusterAddrTable(clusterAddrTable);
    when(mQClientAPIImpl.getBrokerClusterInfo(anyLong())).thenReturn(clusterInfo);

    field = QueryMsgByUniqueKeySubCommand.class.getDeclaredField("defaultMQAdminExt");
    field.setAccessible(true);
    field.set(cmd, defaultMQAdminExt);

}
 
Example 11
Source File: ConsumeMessageOrderlyService.java    From rocketmq-read with Apache License 2.0 4 votes vote down vote up
/**
 * 直接消费消息
 * @param msg msg ;
 * @param brokerName brokerName ;
 * @return ;
 */
@Override
public ConsumeMessageDirectlyResult consumeMessageDirectly(MessageExt msg, String brokerName) {
    ConsumeMessageDirectlyResult result = new ConsumeMessageDirectlyResult();
    result.setOrder(true);

    List<MessageExt> msgs = new ArrayList<MessageExt>();
    msgs.add(msg);
    MessageQueue mq = new MessageQueue();
    mq.setBrokerName(brokerName);
    mq.setTopic(msg.getTopic());
    mq.setQueueId(msg.getQueueId());

    ConsumeOrderlyContext context = new ConsumeOrderlyContext(mq);

    final long beginTime = System.currentTimeMillis();

    log.info("consumeMessageDirectly receive new message: {}", msg);

    try {
        ConsumeOrderlyStatus status = this.messageListener.consumeMessage(msgs, context);
        if (status != null) {
            switch (status) {
                case COMMIT:
                    result.setConsumeResult(CMResult.CR_COMMIT);
                    break;
                case ROLLBACK:
                    result.setConsumeResult(CMResult.CR_ROLLBACK);
                    break;
                case SUCCESS:
                    result.setConsumeResult(CMResult.CR_SUCCESS);
                    break;
                case SUSPEND_CURRENT_QUEUE_A_MOMENT:
                    result.setConsumeResult(CMResult.CR_LATER);
                    break;
                default:
                    break;
            }
        } else {
            result.setConsumeResult(CMResult.CR_RETURN_NULL);
        }
    } catch (Throwable e) {
        result.setConsumeResult(CMResult.CR_THROW_EXCEPTION);
        result.setRemark(RemotingHelper.exceptionSimpleDesc(e));

        log.warn(String.format("consumeMessageDirectly exception: %s Group: %s Msgs: %s MQ: %s",
            RemotingHelper.exceptionSimpleDesc(e),
            ConsumeMessageOrderlyService.this.consumerGroup,
            msgs,
            mq), e);
    }

    result.setAutoCommit(context.isAutoCommit());
    result.setSpentTimeMills(System.currentTimeMillis() - beginTime);

    log.info("consumeMessageDirectly Result: {}", result);

    return result;
}
 
Example 12
Source File: ConsumeMessageConcurrentlyService.java    From rocketmq with Apache License 2.0 4 votes vote down vote up
/**
 * 直接消费单条消息
 * TODO 疑问:调用方
 *
 * @param msg        消息
 * @param brokerName broker名
 * @return 消费结果
 */
@Override
public ConsumeMessageDirectlyResult consumeMessageDirectly(MessageExt msg, String brokerName) {
    ConsumeMessageDirectlyResult result = new ConsumeMessageDirectlyResult();
    result.setOrder(false);
    result.setAutoCommit(true);

    // 消息列表
    List<MessageExt> msgs = new ArrayList<>();
    msgs.add(msg);

    // 消费队列
    MessageQueue mq = new MessageQueue();
    mq.setBrokerName(brokerName);
    mq.setTopic(msg.getTopic());
    mq.setQueueId(msg.getQueueId());

    // 消费Context
    ConsumeConcurrentlyContext context = new ConsumeConcurrentlyContext(mq);

    // 当消息为重试消息,设置Topic为原始Topic
    this.resetRetryTopic(msgs);

    final long beginTime = System.currentTimeMillis();

    log.info("consumeMessageDirectly receive new messge: {}", msg);

    // 执行消费
    try {
        ConsumeConcurrentlyStatus status = this.messageListener.consumeMessage(msgs, context);
        if (status != null) {
            switch (status) {
                case CONSUME_SUCCESS:
                    result.setConsumeResult(CMResult.CR_SUCCESS);
                    break;
                case RECONSUME_LATER:
                    result.setConsumeResult(CMResult.CR_LATER);
                    break;
                default:
                    break;
            }
        } else {
            result.setConsumeResult(CMResult.CR_RETURN_NULL);
        }
    } catch (Throwable e) {
        result.setConsumeResult(CMResult.CR_THROW_EXCEPTION);
        result.setRemark(RemotingHelper.exceptionSimpleDesc(e));

        log.warn(String.format("consumeMessageDirectly exception: %s Group: %s Msgs: %s MQ: %s", //
            RemotingHelper.exceptionSimpleDesc(e), //
            ConsumeMessageConcurrentlyService.this.consumerGroup, //
            msgs, //
            mq), e);
    }

    // 设置消费时长
    result.setSpentTimeMills(System.currentTimeMillis() - beginTime);

    log.info("consumeMessageDirectly Result: {}", result);

    return result;
}
 
Example 13
Source File: ConsumeMessageConcurrentlyService.java    From rocketmq-4.3.0 with Apache License 2.0 4 votes vote down vote up
@Override
    public ConsumeMessageDirectlyResult consumeMessageDirectly(MessageExt msg, String brokerName) {
        ConsumeMessageDirectlyResult result = new ConsumeMessageDirectlyResult();
        result.setOrder(false);
        result.setAutoCommit(true);

        List<MessageExt> msgs = new ArrayList<MessageExt>();
        msgs.add(msg);
        MessageQueue mq = new MessageQueue();
        mq.setBrokerName(brokerName);
        mq.setTopic(msg.getTopic());
        mq.setQueueId(msg.getQueueId());

        ConsumeConcurrentlyContext context = new ConsumeConcurrentlyContext(mq);

//        重置重试topic=》
        this.resetRetryTopic(msgs);

        final long beginTime = System.currentTimeMillis();

        log.info("consumeMessageDirectly receive new message: {}", msg);

        try {
//            消费消息,需要开发实现自己的消息消费逻辑
            ConsumeConcurrentlyStatus status = this.messageListener.consumeMessage(msgs, context);
            if (status != null) {
                switch (status) {
                    case CONSUME_SUCCESS:
                        result.setConsumeResult(CMResult.CR_SUCCESS);
                        break;
                    case RECONSUME_LATER:
                        result.setConsumeResult(CMResult.CR_LATER);
                        break;
                    default:
                        break;
                }
            } else {
                result.setConsumeResult(CMResult.CR_RETURN_NULL);
            }
        } catch (Throwable e) {
            result.setConsumeResult(CMResult.CR_THROW_EXCEPTION);
            result.setRemark(RemotingHelper.exceptionSimpleDesc(e));

            log.warn(String.format("consumeMessageDirectly exception: %s Group: %s Msgs: %s MQ: %s",
                RemotingHelper.exceptionSimpleDesc(e),
                ConsumeMessageConcurrentlyService.this.consumerGroup,
                msgs,
                mq), e);
        }

        result.setSpentTimeMills(System.currentTimeMillis() - beginTime);

        log.info("consumeMessageDirectly Result: {}", result);

        return result;
    }
 
Example 14
Source File: ConsumeMessageOrderlyService.java    From rocketmq-4.3.0 with Apache License 2.0 4 votes vote down vote up
@Override
    public ConsumeMessageDirectlyResult consumeMessageDirectly(MessageExt msg, String brokerName) {
        ConsumeMessageDirectlyResult result = new ConsumeMessageDirectlyResult();
        result.setOrder(true);

        List<MessageExt> msgs = new ArrayList<MessageExt>();
        msgs.add(msg);
        MessageQueue mq = new MessageQueue();
        mq.setBrokerName(brokerName);
        mq.setTopic(msg.getTopic());
        mq.setQueueId(msg.getQueueId());

        ConsumeOrderlyContext context = new ConsumeOrderlyContext(mq);

        final long beginTime = System.currentTimeMillis();

        log.info("consumeMessageDirectly receive new message: {}", msg);

        try {
//            消费消息,需要开发实现自己的消息消费逻辑=》
            ConsumeOrderlyStatus status = this.messageListener.consumeMessage(msgs, context);
            if (status != null) {
                switch (status) {
                    case COMMIT:
                        result.setConsumeResult(CMResult.CR_COMMIT);
                        break;
                    case ROLLBACK:
                        result.setConsumeResult(CMResult.CR_ROLLBACK);
                        break;
                    case SUCCESS:
                        result.setConsumeResult(CMResult.CR_SUCCESS);
                        break;
                    case SUSPEND_CURRENT_QUEUE_A_MOMENT:
                        result.setConsumeResult(CMResult.CR_LATER);
                        break;
                    default:
                        break;
                }
            } else {
                result.setConsumeResult(CMResult.CR_RETURN_NULL);
            }
        } catch (Throwable e) {
            result.setConsumeResult(CMResult.CR_THROW_EXCEPTION);
            result.setRemark(RemotingHelper.exceptionSimpleDesc(e));

            log.warn(String.format("consumeMessageDirectly exception: %s Group: %s Msgs: %s MQ: %s",
                RemotingHelper.exceptionSimpleDesc(e),
                ConsumeMessageOrderlyService.this.consumerGroup,
                msgs,
                mq), e);
        }

        result.setAutoCommit(context.isAutoCommit());
        result.setSpentTimeMills(System.currentTimeMillis() - beginTime);

        log.info("consumeMessageDirectly Result: {}", result);

        return result;
    }
 
Example 15
Source File: AdminBrokerProcessor.java    From rocketmq_trans_message with Apache License 2.0 4 votes vote down vote up
private RemotingCommand getConsumeStats(ChannelHandlerContext ctx, RemotingCommand request) throws RemotingCommandException {
    final RemotingCommand response = RemotingCommand.createResponseCommand(null);
    final GetConsumeStatsRequestHeader requestHeader =
        (GetConsumeStatsRequestHeader) request.decodeCommandCustomHeader(GetConsumeStatsRequestHeader.class);

    ConsumeStats consumeStats = new ConsumeStats();

    Set<String> topics = new HashSet<String>();
    if (UtilAll.isBlank(requestHeader.getTopic())) {
        topics = this.brokerController.getConsumerOffsetManager().whichTopicByConsumer(requestHeader.getConsumerGroup());
    } else {
        topics.add(requestHeader.getTopic());
    }

    for (String topic : topics) {
        TopicConfig topicConfig = this.brokerController.getTopicConfigManager().selectTopicConfig(topic);
        if (null == topicConfig) {
            log.warn("consumeStats, topic config not exist, {}", topic);
            continue;
        }

        /**

         */
        {
            SubscriptionData findSubscriptionData =
                this.brokerController.getConsumerManager().findSubscriptionData(requestHeader.getConsumerGroup(), topic);

            if (null == findSubscriptionData //
                && this.brokerController.getConsumerManager().findSubscriptionDataCount(requestHeader.getConsumerGroup()) > 0) {
                log.warn("consumeStats, the consumer group[{}], topic[{}] not exist", requestHeader.getConsumerGroup(), topic);
                continue;
            }
        }

        for (int i = 0; i < topicConfig.getReadQueueNums(); i++) {
            MessageQueue mq = new MessageQueue();
            mq.setTopic(topic);
            mq.setBrokerName(this.brokerController.getBrokerConfig().getBrokerName());
            mq.setQueueId(i);

            OffsetWrapper offsetWrapper = new OffsetWrapper();

            long brokerOffset = this.brokerController.getMessageStore().getMaxOffsetInQuque(topic, i);
            if (brokerOffset < 0)
                brokerOffset = 0;

            long consumerOffset = this.brokerController.getConsumerOffsetManager().queryOffset(//
                requestHeader.getConsumerGroup(), //
                topic, //
                i);
            if (consumerOffset < 0)
                consumerOffset = 0;

            offsetWrapper.setBrokerOffset(brokerOffset);
            offsetWrapper.setConsumerOffset(consumerOffset);

            long timeOffset = consumerOffset - 1;
            if (timeOffset >= 0) {
                long lastTimestamp = this.brokerController.getMessageStore().getMessageStoreTimeStamp(topic, i, timeOffset);
                if (lastTimestamp > 0) {
                    offsetWrapper.setLastTimestamp(lastTimestamp);
                }
            }

            consumeStats.getOffsetTable().put(mq, offsetWrapper);
        }

        double consumeTps = this.brokerController.getBrokerStatsManager().tpsGroupGetNums(requestHeader.getConsumerGroup(), topic);

        consumeTps += consumeStats.getConsumeTps();
        consumeStats.setConsumeTps(consumeTps);
    }

    byte[] body = consumeStats.encode();
    response.setBody(body);
    response.setCode(ResponseCode.SUCCESS);
    response.setRemark(null);
    return response;
}
 
Example 16
Source File: AdminBrokerProcessor.java    From DDMQ with Apache License 2.0 4 votes vote down vote up
private RemotingCommand getTopicStatsInfo(ChannelHandlerContext ctx,
    RemotingCommand request) throws RemotingCommandException {
    final RemotingCommand response = RemotingCommand.createResponseCommand(null);
    final GetTopicStatsInfoRequestHeader requestHeader =
        (GetTopicStatsInfoRequestHeader) request.decodeCommandCustomHeader(GetTopicStatsInfoRequestHeader.class);

    final String topic = requestHeader.getTopic();
    TopicConfig topicConfig = this.brokerController.getTopicConfigManager().selectTopicConfig(topic);
    if (null == topicConfig) {
        response.setCode(ResponseCode.TOPIC_NOT_EXIST);
        response.setRemark("topic[" + topic + "] not exist");
        return response;
    }

    TopicStatsTable topicStatsTable = new TopicStatsTable();
    for (int i = 0; i < topicConfig.getWriteQueueNums(); i++) {
        MessageQueue mq = new MessageQueue();
        mq.setTopic(topic);
        mq.setBrokerName(this.brokerController.getBrokerConfig().getBrokerName());
        mq.setQueueId(i);

        TopicOffset topicOffset = new TopicOffset();
        long min = this.brokerController.getMessageStore().getMinOffsetInQueue(topic, i);
        if (min < 0)
            min = 0;

        long max = this.brokerController.getMessageStore().getMaxOffsetInQueue(topic, i);
        if (max < 0)
            max = 0;

        long timestamp = 0;
        if (max > 0) {
            timestamp = this.brokerController.getMessageStore().getMessageStoreTimeStamp(topic, i, max - 1);
        }

        topicOffset.setMinOffset(min);
        topicOffset.setMaxOffset(max);
        topicOffset.setLastUpdateTimestamp(timestamp);

        topicStatsTable.getOffsetTable().put(mq, topicOffset);
    }

    byte[] body = topicStatsTable.encode();
    response.setBody(body);
    response.setCode(ResponseCode.SUCCESS);
    response.setRemark(null);
    return response;
}
 
Example 17
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 18
Source File: AdminBrokerProcessor.java    From rocketmq with Apache License 2.0 4 votes vote down vote up
private RemotingCommand getTopicStatsInfo(ChannelHandlerContext ctx, RemotingCommand request) throws RemotingCommandException {
    final RemotingCommand response = RemotingCommand.createResponseCommand(null);
    final GetTopicStatsInfoRequestHeader requestHeader =
        (GetTopicStatsInfoRequestHeader)request.decodeCommandCustomHeader(GetTopicStatsInfoRequestHeader.class);

    final String topic = requestHeader.getTopic();
    TopicConfig topicConfig = this.brokerController.getTopicConfigManager().selectTopicConfig(topic);
    if (null == topicConfig) {
        response.setCode(ResponseCode.TOPIC_NOT_EXIST);
        response.setRemark("topic[" + topic + "] not exist");
        return response;
    }

    TopicStatsTable topicStatsTable = new TopicStatsTable();
    for (int i = 0; i < topicConfig.getWriteQueueNums(); i++) {
        MessageQueue mq = new MessageQueue();
        mq.setTopic(topic);
        mq.setBrokerName(this.brokerController.getBrokerConfig().getBrokerName());
        mq.setQueueId(i);

        TopicOffset topicOffset = new TopicOffset();
        long min = this.brokerController.getMessageStore().getMinOffsetInQuque(topic, i);
        if (min < 0) { min = 0; }

        long max = this.brokerController.getMessageStore().getMaxOffsetInQuque(topic, i);
        if (max < 0) { max = 0; }

        long timestamp = 0;
        if (max > 0) {
            timestamp = this.brokerController.getMessageStore().getMessageStoreTimeStamp(topic, i, max - 1);
        }

        topicOffset.setMinOffset(min);
        topicOffset.setMaxOffset(max);
        topicOffset.setLastUpdateTimestamp(timestamp);

        topicStatsTable.getOffsetTable().put(mq, topicOffset);
    }

    byte[] body = topicStatsTable.encode();
    response.setBody(body);
    response.setCode(ResponseCode.SUCCESS);
    response.setRemark(null);
    return response;
}
 
Example 19
Source File: AdminBrokerProcessor.java    From DDMQ with Apache License 2.0 4 votes vote down vote up
private RemotingCommand getTopicStatsInfo(ChannelHandlerContext ctx,
    RemotingCommand request) throws RemotingCommandException {
    final RemotingCommand response = RemotingCommand.createResponseCommand(null);
    final GetTopicStatsInfoRequestHeader requestHeader =
        (GetTopicStatsInfoRequestHeader) request.decodeCommandCustomHeader(GetTopicStatsInfoRequestHeader.class);

    final String topic = requestHeader.getTopic();
    TopicConfig topicConfig = this.brokerController.getTopicConfigManager().selectTopicConfig(topic);
    if (null == topicConfig) {
        response.setCode(ResponseCode.TOPIC_NOT_EXIST);
        response.setRemark("topic[" + topic + "] not exist");
        return response;
    }

    TopicStatsTable topicStatsTable = new TopicStatsTable();
    for (int i = 0; i < topicConfig.getWriteQueueNums(); i++) {
        MessageQueue mq = new MessageQueue();
        mq.setTopic(topic);
        mq.setBrokerName(this.brokerController.getBrokerConfig().getBrokerName());
        mq.setQueueId(i);

        TopicOffset topicOffset = new TopicOffset();
        long min = this.brokerController.getMessageStore().getMinOffsetInQueue(topic, i);
        if (min < 0)
            min = 0;

        long max = this.brokerController.getMessageStore().getMaxOffsetInQueue(topic, i);
        if (max < 0)
            max = 0;

        long timestamp = 0;
        if (max > 0) {
            timestamp = this.brokerController.getMessageStore().getMessageStoreTimeStamp(topic, i, max - 1);
        }

        topicOffset.setMinOffset(min);
        topicOffset.setMaxOffset(max);
        topicOffset.setLastUpdateTimestamp(timestamp);

        topicStatsTable.getOffsetTable().put(mq, topicOffset);
    }

    byte[] body = topicStatsTable.encode();
    response.setBody(body);
    response.setCode(ResponseCode.SUCCESS);
    response.setRemark(null);
    return response;
}
 
Example 20
Source File: ConsumeMessageOrderlyService.java    From DDMQ with Apache License 2.0 4 votes vote down vote up
@Override
public ConsumeMessageDirectlyResult consumeMessageDirectly(MessageExt msg, String brokerName) {
    ConsumeMessageDirectlyResult result = new ConsumeMessageDirectlyResult();
    result.setOrder(true);

    List<MessageExt> msgs = new ArrayList<MessageExt>();
    msgs.add(msg);
    MessageQueue mq = new MessageQueue();
    mq.setBrokerName(brokerName);
    mq.setTopic(msg.getTopic());
    mq.setQueueId(msg.getQueueId());

    ConsumeOrderlyContext context = new ConsumeOrderlyContext(mq);

    final long beginTime = System.currentTimeMillis();

    log.info("consumeMessageDirectly receive new message: {}", msg);

    try {
        ConsumeOrderlyStatus status = this.messageListener.consumeMessage(msgs, context);
        if (status != null) {
            switch (status) {
                case COMMIT:
                    result.setConsumeResult(CMResult.CR_COMMIT);
                    break;
                case ROLLBACK:
                    result.setConsumeResult(CMResult.CR_ROLLBACK);
                    break;
                case SUCCESS:
                    result.setConsumeResult(CMResult.CR_SUCCESS);
                    break;
                case SUSPEND_CURRENT_QUEUE_A_MOMENT:
                    result.setConsumeResult(CMResult.CR_LATER);
                    break;
                default:
                    break;
            }
        } else {
            result.setConsumeResult(CMResult.CR_RETURN_NULL);
        }
    } catch (Throwable e) {
        result.setConsumeResult(CMResult.CR_THROW_EXCEPTION);
        result.setRemark(RemotingHelper.exceptionSimpleDesc(e));

        log.warn(String.format("consumeMessageDirectly exception: %s Group: %s Msgs: %s MQ: %s",
            RemotingHelper.exceptionSimpleDesc(e),
            ConsumeMessageOrderlyService.this.consumerGroup,
            msgs,
            mq), e);
    }

    result.setAutoCommit(context.isAutoCommit());
    result.setSpentTimeMills(System.currentTimeMillis() - beginTime);

    log.info("consumeMessageDirectly Result: {}", result);

    return result;
}