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

The following examples show how to use org.apache.rocketmq.common.message.MessageExt#setMsgId() . 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: TransactionalMessageServiceImpl.java    From rocketmq with Apache License 2.0 6 votes vote down vote up
private boolean putBackHalfMsgQueue(MessageExt msgExt, long offset) {
    PutMessageResult putMessageResult = putBackToHalfQueueReturnResult(msgExt);
    if (putMessageResult != null
        && putMessageResult.getPutMessageStatus() == PutMessageStatus.PUT_OK) {
        msgExt.setQueueOffset(
            putMessageResult.getAppendMessageResult().getLogicsOffset());
        msgExt.setCommitLogOffset(
            putMessageResult.getAppendMessageResult().getWroteOffset());
        msgExt.setMsgId(putMessageResult.getAppendMessageResult().getMsgId());
        log.debug(
            "Send check message, the offset={} restored in queueOffset={} "
                + "commitLogOffset={} "
                + "newMsgId={} realMsgId={} topic={}",
            offset, msgExt.getQueueOffset(), msgExt.getCommitLogOffset(), msgExt.getMsgId(),
            msgExt.getUserProperty(MessageConst.PROPERTY_UNIQ_CLIENT_MESSAGE_ID_KEYIDX),
            msgExt.getTopic());
        return true;
    } else {
        log.error(
            "PutBackToHalfQueueReturnResult write failed, topic: {}, queueId: {}, "
                + "msgId: {}",
            msgExt.getTopic(), msgExt.getQueueId(), msgExt.getMsgId());
        return false;
    }
}
 
Example 2
Source File: PushConsumerImplTest.java    From rocketmq-all-4.1.0-incubating 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 3
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 4
Source File: TransactionalMessageServiceImpl.java    From rocketmq-4.3.0 with Apache License 2.0 6 votes vote down vote up
private boolean putBackHalfMsgQueue(MessageExt msgExt, long offset) {
    PutMessageResult putMessageResult = putBackToHalfQueueReturnResult(msgExt);
    if (putMessageResult != null
        && putMessageResult.getPutMessageStatus() == PutMessageStatus.PUT_OK) {
        msgExt.setQueueOffset(
            putMessageResult.getAppendMessageResult().getLogicsOffset());
        msgExt.setCommitLogOffset(
            putMessageResult.getAppendMessageResult().getWroteOffset());
        msgExt.setMsgId(putMessageResult.getAppendMessageResult().getMsgId());
        log.info(
            "Send check message, the offset={} restored in queueOffset={} "
                + "commitLogOffset={} "
                + "newMsgId={} realMsgId={} topic={}",
            offset, msgExt.getQueueOffset(), msgExt.getCommitLogOffset(), msgExt.getMsgId(),
            msgExt.getUserProperty(MessageConst.PROPERTY_UNIQ_CLIENT_MESSAGE_ID_KEYIDX),
            msgExt.getTopic());
        return true;
    } else {
        log.error(
            "PutBackToHalfQueueReturnResult write failed, topic: {}, queueId: {}, "
                + "msgId: {}",
            msgExt.getTopic(), msgExt.getQueueId(), msgExt.getMsgId());
        return false;
    }
}
 
Example 5
Source File: PushConsumerImplTest.java    From rocketmq-read 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 6
Source File: PullConsumerImplTest.java    From DDMQ 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 7
Source File: LocalMessageCacheTest.java    From DDMQ 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 8
Source File: EndTransactionProcessorTest.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
private MessageExt createDefaultMessageExt() {
    MessageExt messageExt = new MessageExt();
    messageExt.setMsgId("12345678");
    messageExt.setQueueId(0);
    messageExt.setCommitLogOffset(123456789L);
    messageExt.setQueueOffset(1234);
    MessageAccessor.putProperty(messageExt, MessageConst.PROPERTY_REAL_QUEUE_ID, "0");
    MessageAccessor.putProperty(messageExt, MessageConst.PROPERTY_TRANSACTION_PREPARED, "true");
    MessageAccessor.putProperty(messageExt, MessageConst.PROPERTY_PRODUCER_GROUP, "testTransactionGroup");
    return messageExt;
}
 
Example 9
Source File: EndTransactionProcessorTest.java    From rocketmq-read with Apache License 2.0 5 votes vote down vote up
private MessageExt createDefaultMessageExt() {
    MessageExt messageExt = new MessageExt();
    messageExt.setMsgId("12345678");
    messageExt.setQueueId(0);
    messageExt.setCommitLogOffset(123456789L);
    messageExt.setQueueOffset(1234);
    MessageAccessor.putProperty(messageExt, MessageConst.PROPERTY_REAL_QUEUE_ID, "0");
    MessageAccessor.putProperty(messageExt, MessageConst.PROPERTY_TRANSACTION_PREPARED, "true");
    MessageAccessor.putProperty(messageExt, MessageConst.PROPERTY_PRODUCER_GROUP, "testTransactionGroup");
    return messageExt;
}
 
Example 10
Source File: LocalMessageCacheTest.java    From rocketmq 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 11
Source File: PullConsumerImplTest.java    From rocketmq-read 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.receive();
    assertThat(message.sysHeaders().getString(Message.BuiltinKeys.MESSAGE_ID)).isEqualTo("NewMsgId");
    assertThat(((BytesMessage) message).getBody(byte[].class)).isEqualTo(testBody);
}
 
Example 12
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 13
Source File: AdminBrokerProcessorTest.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
private MessageExt createDefaultMessageExt() {
    MessageExt messageExt = new MessageExt();
    messageExt.setMsgId("12345678");
    messageExt.setQueueId(0);
    messageExt.setCommitLogOffset(123456789L);
    messageExt.setQueueOffset(1234);
    MessageAccessor.putProperty(messageExt, MessageConst.PROPERTY_REAL_QUEUE_ID, "0");
    MessageAccessor.putProperty(messageExt, MessageConst.PROPERTY_REAL_TOPIC, "testTopic");
    MessageAccessor.putProperty(messageExt, MessageConst.PROPERTY_TRANSACTION_CHECK_TIMES, "15");
    return messageExt;
}
 
Example 14
Source File: DefaultMQAdminExtTest.java    From rocketmq_trans_message 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 15
Source File: PullConsumerImplTest.java    From rocketmq-4.3.0 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.receive();
    assertThat(message.sysHeaders().getString(Message.BuiltinKeys.MESSAGE_ID)).isEqualTo("NewMsgId");
    assertThat(((BytesMessage) message).getBody(byte[].class)).isEqualTo(testBody);
}
 
Example 16
Source File: PullConsumerImplTest.java    From rocketmq 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.receive();
    assertThat(message.sysHeaders().getString(Message.BuiltinKeys.MESSAGE_ID)).isEqualTo("NewMsgId");
    assertThat(((BytesMessage) message).getBody(byte[].class)).isEqualTo(testBody);
}
 
Example 17
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 18
Source File: PullConsumerImplTest.java    From DDMQ 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 19
Source File: LocalMessageCacheTest.java    From DDMQ 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 20
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);

}