Java Code Examples for org.apache.rocketmq.common.message.MessageExt#setTopic()

The following examples show how to use org.apache.rocketmq.common.message.MessageExt#setTopic() . 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: AbstractTransactionalMessageCheckListener.java    From rocketmq-read with Apache License 2.0 6 votes vote down vote up
/**
 * 发送回查消息
 * @param msgExt ;
 * @throws Exception ;
 */
public void sendCheckMessage(MessageExt msgExt) throws Exception {
    CheckTransactionStateRequestHeader checkTransactionStateRequestHeader = new CheckTransactionStateRequestHeader();
    checkTransactionStateRequestHeader.setCommitLogOffset(msgExt.getCommitLogOffset());
    checkTransactionStateRequestHeader.setOffsetMsgId(msgExt.getMsgId());
    checkTransactionStateRequestHeader.setMsgId(msgExt.getUserProperty(MessageConst.PROPERTY_UNIQ_CLIENT_MESSAGE_ID_KEYIDX));
    checkTransactionStateRequestHeader.setTransactionId(checkTransactionStateRequestHeader.getMsgId());
    checkTransactionStateRequestHeader.setTranStateTableOffset(msgExt.getQueueOffset());
    msgExt.setTopic(msgExt.getUserProperty(MessageConst.PROPERTY_REAL_TOPIC));
    msgExt.setQueueId(Integer.parseInt(msgExt.getUserProperty(MessageConst.PROPERTY_REAL_QUEUE_ID)));
    msgExt.setStoreSize(0);
    String groupId = msgExt.getProperty(MessageConst.PROPERTY_PRODUCER_GROUP);
    Channel channel = brokerController.getProducerManager().getAvaliableChannel(groupId);
    if (channel != null) {
        brokerController.getBroker2Client().checkProducerTransactionState(groupId, channel, checkTransactionStateRequestHeader, msgExt);
    } else {
        LOGGER.warn("Check transaction failed, channel is null. groupId={}", groupId);
    }
}
 
Example 2
Source File: PushConsumerImplTest.java    From rocketmq with Apache License 2.0 6 votes vote down vote up
@Test
public void testConsumeMessage() {
    final byte[] testBody = new byte[] {'a', 'b'};

    MessageExt consumedMsg = new MessageExt();
    consumedMsg.setMsgId("NewMsgId");
    consumedMsg.setBody(testBody);
    consumedMsg.putUserProperty(NonStandardKeys.MESSAGE_DESTINATION, "TOPIC");
    consumedMsg.setTopic("HELLO_QUEUE");
    consumer.attachQueue("HELLO_QUEUE", new MessageListener() {
        @Override
        public void onReceived(Message message, Context context) {
            assertThat(message.sysHeaders().getString(Message.BuiltinKeys.MESSAGE_ID)).isEqualTo("NewMsgId");
            assertThat(((BytesMessage) message).getBody(byte[].class)).isEqualTo(testBody);
            context.ack();
        }
    });
    ((MessageListenerConcurrently) rocketmqPushConsumer
        .getMessageListener()).consumeMessage(Collections.singletonList(consumedMsg), null);
}
 
Example 3
Source File: PushConsumerImplTest.java    From DDMQ with Apache License 2.0 6 votes vote down vote up
@Test
public void testConsumeMessage() {
    final byte[] testBody = new byte[] {'a', 'b'};

    MessageExt consumedMsg = new MessageExt();
    consumedMsg.setMsgId("NewMsgId");
    consumedMsg.setBody(testBody);
    consumedMsg.putUserProperty(NonStandardKeys.MESSAGE_DESTINATION, "TOPIC");
    consumedMsg.setTopic("HELLO_QUEUE");
    consumer.attachQueue("HELLO_QUEUE", new MessageListener() {
        @Override
        public void onMessage(final Message message, final ReceivedMessageContext context) {
            assertThat(message.headers().getString(MessageHeader.MESSAGE_ID)).isEqualTo("NewMsgId");
            assertThat(((BytesMessage) message).getBody()).isEqualTo(testBody);
            context.ack();
        }
    });
    ((MessageListenerConcurrently) rocketmqPushConsumer
        .getMessageListener()).consumeMessage(Collections.singletonList(consumedMsg), null);
}
 
Example 4
Source File: ConsumeMessageConcurrentlyService.java    From rocketmq-4.3.0 with Apache License 2.0 5 votes vote down vote up
public void resetRetryTopic(final List<MessageExt> msgs) {
    final String groupTopic = MixAll.getRetryTopic(consumerGroup);
    for (MessageExt msg : msgs) {
        String retryTopic = msg.getProperty(MessageConst.PROPERTY_RETRY_TOPIC);
        if (retryTopic != null && groupTopic.equals(msg.getTopic())) {
            msg.setTopic(retryTopic);
        }
    }
}
 
Example 5
Source File: ConsumeMessageOrderlyService.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
public void resetRetryTopic(final List<MessageExt> msgs) {
    final String groupTopic = MixAll.getRetryTopic(consumerGroup);
    for (MessageExt msg : msgs) {
        String retryTopic = msg.getProperty(MessageConst.PROPERTY_RETRY_TOPIC);
        if (retryTopic != null && groupTopic.equals(msg.getTopic())) {
            msg.setTopic(retryTopic);
        }
    }
}
 
Example 6
Source File: DefaultMQPullConsumer.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
/**
 * This method will be removed or it's visibility will be changed in a certain version after April 5, 2020, so
 * please do not use this method.
 */
@Deprecated
@Override
public void sendMessageBack(MessageExt msg, int delayLevel)
    throws RemotingException, MQBrokerException, InterruptedException, MQClientException {
    msg.setTopic(withNamespace(msg.getTopic()));
    this.defaultMQPullConsumerImpl.sendMessageBack(msg, delayLevel, null);
}
 
Example 7
Source File: DeFiBusCLientUtilTest.java    From DeFiBus with Apache License 2.0 5 votes vote down vote up
@Test
public void createReplyMessage_Fail() {
    MessageExt msg = new MessageExt();
    msg.setTopic("Test");
    msg.putUserProperty(DeFiBusConstant.PROPERTY_MESSAGE_REPLY_TO, "reply_to");
    msg.putUserProperty(DeFiBusConstant.PROPERTY_RR_REQUEST_ID, "w/request_id");

    byte[] replyContent = "reply content".getBytes();
    Message replyMsg = DeFiBusClientUtil.createReplyMessage(msg, replyContent);

    assertThat(replyMsg).isNull();
}
 
Example 8
Source File: DefaultLitePullConsumerImpl.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
private void resetTopic(List<MessageExt> msgList) {
    if (null == msgList || msgList.size() == 0) {
        return;
    }

    //If namespace not null , reset Topic without namespace.
    for (MessageExt messageExt : msgList) {
        if (null != this.defaultLitePullConsumer.getNamespace()) {
            messageExt.setTopic(NamespaceUtil.withoutNamespace(messageExt.getTopic(), this.defaultLitePullConsumer.getNamespace()));
        }
    }

}
 
Example 9
Source File: DefaultMQAdminExtTest.java    From rocketmq-4.3.0 with Apache License 2.0 5 votes vote down vote up
@Test
public void testMessageTrackDetail() throws InterruptedException, RemotingException, MQClientException, MQBrokerException {
    MessageExt messageExt = new MessageExt();
    messageExt.setMsgId("msgId");
    messageExt.setTopic("unit-test");
    List<MessageTrack> messageTrackList = defaultMQAdminExt.messageTrackDetail(messageExt);
    assertThat(messageTrackList.size()).isEqualTo(2);
}
 
Example 10
Source File: ConsumeMessageOrderlyServiceTest.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
@Test
public void testConsumeMessageDirectly_WithException() {
    MessageListenerOrderly listenerOrderly = new MessageListenerOrderly() {
        @Override
        public ConsumeOrderlyStatus consumeMessage(List<MessageExt> msgs, ConsumeOrderlyContext context) {
            throw new RuntimeException();
        }
    };

    ConsumeMessageOrderlyService consumeMessageOrderlyService = new ConsumeMessageOrderlyService(pushConsumer.getDefaultMQPushConsumerImpl(), listenerOrderly);
    MessageExt msg = new MessageExt();
    msg.setTopic(topic);
    assertTrue(consumeMessageOrderlyService.consumeMessageDirectly(msg, brokerName).getConsumeResult().equals(CMResult.CR_THROW_EXCEPTION));
}
 
Example 11
Source File: DefaultMQPushConsumerImpl.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
public void resetRetryAndNamespace(final List<MessageExt> msgs, String consumerGroup) {
    final String groupTopic = MixAll.getRetryTopic(consumerGroup);
    for (MessageExt msg : msgs) {
        String retryTopic = msg.getProperty(MessageConst.PROPERTY_RETRY_TOPIC);
        if (retryTopic != null && groupTopic.equals(msg.getTopic())) {
            msg.setTopic(retryTopic);
        }

        if (StringUtils.isNotEmpty(this.defaultMQPushConsumer.getNamespace())) {
            msg.setTopic(NamespaceUtil.withoutNamespace(msg.getTopic(), this.defaultMQPushConsumer.getNamespace()));
        }
    }
}
 
Example 12
Source File: ClientRemotingProcessor.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
public RemotingCommand checkTransactionState(ChannelHandlerContext ctx,
    RemotingCommand request) throws RemotingCommandException {
    final CheckTransactionStateRequestHeader requestHeader =
        (CheckTransactionStateRequestHeader) request.decodeCommandCustomHeader(CheckTransactionStateRequestHeader.class);
    final ByteBuffer byteBuffer = ByteBuffer.wrap(request.getBody());
    final MessageExt messageExt = MessageDecoder.decode(byteBuffer);
    if (messageExt != null) {
        if (StringUtils.isNotEmpty(this.mqClientFactory.getClientConfig().getNamespace())) {
            messageExt.setTopic(NamespaceUtil
                .withoutNamespace(messageExt.getTopic(), this.mqClientFactory.getClientConfig().getNamespace()));
        }
        String transactionId = messageExt.getProperty(MessageConst.PROPERTY_UNIQ_CLIENT_MESSAGE_ID_KEYIDX);
        if (null != transactionId && !"".equals(transactionId)) {
            messageExt.setTransactionId(transactionId);
        }
        final String group = messageExt.getProperty(MessageConst.PROPERTY_PRODUCER_GROUP);
        if (group != null) {
            MQProducerInner producer = this.mqClientFactory.selectProducer(group);
            if (producer != null) {
                final String addr = RemotingHelper.parseChannelRemoteAddr(ctx.channel());
                producer.checkTransactionState(addr, messageExt, requestHeader);
            } else {
                log.debug("checkTransactionState, pick producer by group[{}] failed", group);
            }
        } else {
            log.warn("checkTransactionState, pick producer group failed");
        }
    } else {
        log.warn("checkTransactionState, decode message failed");
    }

    return null;
}
 
Example 13
Source File: LocalMessageCacheTest.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 5 votes vote down vote up
@Test
public void testSubmitConsumeRequest() throws Exception {
    byte [] body = new byte[]{'1', '2', '3'};
    MessageExt consumedMsg = new MessageExt();
    consumedMsg.setMsgId("NewMsgId");
    consumedMsg.setBody(body);
    consumedMsg.putUserProperty(NonStandardKeys.MESSAGE_DESTINATION, "TOPIC");
    consumedMsg.setTopic("HELLO_QUEUE");

    when(consumeRequest.getMessageExt()).thenReturn(consumedMsg);
    localMessageCache.submitConsumeRequest(consumeRequest);
    assertThat(localMessageCache.poll()).isEqualTo(consumedMsg);
}
 
Example 14
Source File: DefaultMQPullConsumer.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
/**
 * This method will be removed or it's visibility will be changed in a certain version after April 5, 2020, so
 * please do not use this method.
 */
@Deprecated
@Override
public void sendMessageBack(MessageExt msg, int delayLevel, String brokerName)
    throws RemotingException, MQBrokerException, InterruptedException, MQClientException {
    msg.setTopic(withNamespace(msg.getTopic()));
    this.defaultMQPullConsumerImpl.sendMessageBack(msg, delayLevel, brokerName);
}
 
Example 15
Source File: DefaultMQAdminExtTest.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
@Test
public void testMessageTrackDetail() throws InterruptedException, RemotingException, MQClientException, MQBrokerException {
    MessageExt messageExt = new MessageExt();
    messageExt.setMsgId("msgId");
    messageExt.setTopic("unit-test");
    List<MessageTrack> messageTrackList = defaultMQAdminExt.messageTrackDetail(messageExt);
    assertThat(messageTrackList.size()).isEqualTo(2);
}
 
Example 16
Source File: PullConsumerImplTest.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 5 votes vote down vote up
@Test
public void testPoll() {
    final byte[] testBody = new byte[] {'a', 'b'};
    MessageExt consumedMsg = new MessageExt();
    consumedMsg.setMsgId("NewMsgId");
    consumedMsg.setBody(testBody);
    consumedMsg.putUserProperty(NonStandardKeys.MESSAGE_DESTINATION, "TOPIC");
    consumedMsg.setTopic(queueName);

    when(localMessageCache.poll()).thenReturn(consumedMsg);

    Message message = consumer.poll();
    assertThat(message.headers().getString(MessageHeader.MESSAGE_ID)).isEqualTo("NewMsgId");
    assertThat(((BytesMessage) message).getBody()).isEqualTo(testBody);
}
 
Example 17
Source File: DefaultMQAdminExtTest.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
@Test
public void testMessageTrackDetail() throws InterruptedException, RemotingException, MQClientException, MQBrokerException {
    MessageExt messageExt = new MessageExt();
    messageExt.setMsgId("msgId");
    messageExt.setTopic("unit-test");
    List<MessageTrack> messageTrackList = defaultMQAdminExt.messageTrackDetail(messageExt);
    assertThat(messageTrackList.size()).isEqualTo(2);
}
 
Example 18
Source File: DefaultMQPullConsumerImpl.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
public void resetTopic(List<MessageExt> msgList) {
    if (null == msgList || msgList.size() == 0) {
        return;
    }

    //If namespace not null , reset Topic without namespace.
    for (MessageExt messageExt : msgList) {
        if (null != this.getDefaultMQPullConsumer().getNamespace()) {
            messageExt.setTopic(NamespaceUtil.withoutNamespace(messageExt.getTopic(), this.defaultMQPullConsumer.getNamespace()));
        }
    }

}
 
Example 19
Source File: SendMessageProcessor.java    From DDMQ with Apache License 2.0 4 votes vote down vote up
private boolean handleRetryAndDLQ(SendMessageRequestHeader requestHeader, RemotingCommand response,
    RemotingCommand request,
    MessageExt msg, TopicConfig topicConfig) {
    String newTopic = requestHeader.getTopic();
    if (null != newTopic && newTopic.startsWith(MixAll.RETRY_GROUP_TOPIC_PREFIX)) {
        String groupName = newTopic.substring(MixAll.RETRY_GROUP_TOPIC_PREFIX.length());
        SubscriptionGroupConfig subscriptionGroupConfig =
            this.brokerController.getSubscriptionGroupManager().findSubscriptionGroupConfig(groupName);
        if (null == subscriptionGroupConfig) {
            response.setCode(ResponseCode.SUBSCRIPTION_GROUP_NOT_EXIST);
            response.setRemark(
                "subscription group not exist, " + groupName + " " + FAQUrl.suggestTodo(FAQUrl.SUBSCRIPTION_GROUP_NOT_EXIST));
            return false;
        }

        int maxReconsumeTimes = subscriptionGroupConfig.getRetryMaxTimes();
        if (request.getVersion() >= MQVersion.Version.V3_4_9.ordinal()) {
            maxReconsumeTimes = requestHeader.getMaxReconsumeTimes();
        }
        int reconsumeTimes = requestHeader.getReconsumeTimes() == null ? 0 : requestHeader.getReconsumeTimes();
        if (reconsumeTimes >= maxReconsumeTimes) {
            newTopic = MixAll.getDLQTopic(groupName);
            int queueIdInt = Math.abs(this.random.nextInt() % 99999999) % DLQ_NUMS_PER_GROUP;
            topicConfig = this.brokerController.getTopicConfigManager().createTopicInSendMessageBackMethod(newTopic,
                DLQ_NUMS_PER_GROUP,
                PermName.PERM_WRITE, 0
            );
            msg.setTopic(newTopic);
            msg.setQueueId(queueIdInt);
            if (null == topicConfig) {
                response.setCode(ResponseCode.SYSTEM_ERROR);
                response.setRemark("topic[" + newTopic + "] not exist");
                return false;
            }
        }
    }
    int sysFlag = requestHeader.getSysFlag();
    if (TopicFilterType.MULTI_TAG == topicConfig.getTopicFilterType()) {
        sysFlag |= MessageSysFlag.MULTI_TAGS_FLAG;
    }
    msg.setSysFlag(sysFlag);
    return true;
}
 
Example 20
Source File: SendMessageProcessor.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 4 votes vote down vote up
private boolean handleRetryAndDLQ(SendMessageRequestHeader requestHeader, RemotingCommand response, RemotingCommand request,
    MessageExt msg, TopicConfig topicConfig) {
    String newTopic = requestHeader.getTopic();
    if (null != newTopic && newTopic.startsWith(MixAll.RETRY_GROUP_TOPIC_PREFIX)) {
        String groupName = newTopic.substring(MixAll.RETRY_GROUP_TOPIC_PREFIX.length());
        SubscriptionGroupConfig subscriptionGroupConfig =
            this.brokerController.getSubscriptionGroupManager().findSubscriptionGroupConfig(groupName);
        if (null == subscriptionGroupConfig) {
            response.setCode(ResponseCode.SUBSCRIPTION_GROUP_NOT_EXIST);
            response.setRemark(
                "subscription group not exist, " + groupName + " " + FAQUrl.suggestTodo(FAQUrl.SUBSCRIPTION_GROUP_NOT_EXIST));
            return false;
        }

        int maxReconsumeTimes = subscriptionGroupConfig.getRetryMaxTimes();
        if (request.getVersion() >= MQVersion.Version.V3_4_9.ordinal()) {
            maxReconsumeTimes = requestHeader.getMaxReconsumeTimes();
        }
        int reconsumeTimes = requestHeader.getReconsumeTimes() == null ? 0 : requestHeader.getReconsumeTimes();
        if (reconsumeTimes >= maxReconsumeTimes) {
            newTopic = MixAll.getDLQTopic(groupName);
            int queueIdInt = Math.abs(this.random.nextInt() % 99999999) % DLQ_NUMS_PER_GROUP;
            topicConfig = this.brokerController.getTopicConfigManager().createTopicInSendMessageBackMethod(newTopic, //
                DLQ_NUMS_PER_GROUP, //
                PermName.PERM_WRITE, 0
            );
            msg.setTopic(newTopic);
            msg.setQueueId(queueIdInt);
            if (null == topicConfig) {
                response.setCode(ResponseCode.SYSTEM_ERROR);
                response.setRemark("topic[" + newTopic + "] not exist");
                return false;
            }
        }
    }
    int sysFlag = requestHeader.getSysFlag();
    if (TopicFilterType.MULTI_TAG == topicConfig.getTopicFilterType()) {
        sysFlag |= MessageSysFlag.MULTI_TAGS_FLAG;
    }
    msg.setSysFlag(sysFlag);
    return true;
}