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

The following examples show how to use org.apache.rocketmq.common.message.MessageExt#setBody() . 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: 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 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 rocketmq-4.3.0 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: ConsumeMessageCommandTest.java    From rocketmq with Apache License 2.0 6 votes vote down vote up
@BeforeClass
public static void init() throws MQClientException, RemotingException, MQBrokerException, InterruptedException,
    NoSuchFieldException, IllegalAccessException {
    consumeMessageCommand = new ConsumeMessageCommand();
    DefaultMQPullConsumer defaultMQPullConsumer = mock(DefaultMQPullConsumer.class);
    MessageExt msg = new MessageExt();
    msg.setBody(new byte[] {'a'});
    List<MessageExt> msgFoundList = new ArrayList<>();
    msgFoundList.add(msg);
    final PullResult pullResult = new PullResult(PullStatus.FOUND, 2, 0, 1, msgFoundList);

    when(defaultMQPullConsumer.pull(any(MessageQueue.class), anyString(), anyLong(), anyInt())).thenReturn(pullResult);
    when(defaultMQPullConsumer.minOffset(any(MessageQueue.class))).thenReturn(Long.valueOf(0));
    when(defaultMQPullConsumer.maxOffset(any(MessageQueue.class))).thenReturn(Long.valueOf(1));

    final Set<MessageQueue> mqList = new HashSet<>();
    mqList.add(new MessageQueue());
    when(defaultMQPullConsumer.fetchSubscribeMessageQueues(anyString())).thenReturn(mqList);

    Field producerField = ConsumeMessageCommand.class.getDeclaredField("defaultMQPullConsumer");
    producerField.setAccessible(true);
    producerField.set(consumeMessageCommand, defaultMQPullConsumer);
}
 
Example 5
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 6
Source File: ConsumeMessageCommandTest.java    From rocketmq-read with Apache License 2.0 6 votes vote down vote up
@BeforeClass
public static void init() throws MQClientException, RemotingException, MQBrokerException, InterruptedException,
    NoSuchFieldException, IllegalAccessException {
    consumeMessageCommand = new ConsumeMessageCommand();
    DefaultMQPullConsumer defaultMQPullConsumer = mock(DefaultMQPullConsumer.class);
    MessageExt msg = new MessageExt();
    msg.setBody(new byte[] {'a'});
    List<MessageExt> msgFoundList = new ArrayList<>();
    msgFoundList.add(msg);
    final PullResult pullResult = new PullResult(PullStatus.FOUND, 2, 0, 1, msgFoundList);

    when(defaultMQPullConsumer.pull(any(MessageQueue.class), anyString(), anyLong(), anyInt())).thenReturn(pullResult);
    when(defaultMQPullConsumer.minOffset(any(MessageQueue.class))).thenReturn(Long.valueOf(0));
    when(defaultMQPullConsumer.maxOffset(any(MessageQueue.class))).thenReturn(Long.valueOf(1));

    final Set<MessageQueue> mqList = new HashSet<>();
    mqList.add(new MessageQueue());
    when(defaultMQPullConsumer.fetchSubscribeMessageQueues(anyString())).thenReturn(mqList);

    Field producerField = ConsumeMessageCommand.class.getDeclaredField("defaultMQPullConsumer");
    producerField.setAccessible(true);
    producerField.set(consumeMessageCommand, defaultMQPullConsumer);
}
 
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: 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 9
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 10
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 11
Source File: ProcessQueueTest.java    From rocketmq-read with Apache License 2.0 5 votes vote down vote up
private List<MessageExt> createMessageList(int count) {
    List<MessageExt> messageExtList = new ArrayList<MessageExt>();
    for (int i = 0; i < count; i++) {
        MessageExt messageExt = new MessageExt();
        messageExt.setQueueOffset(i);
        messageExt.setBody(new byte[123]);
        messageExtList.add(messageExt);
    }
    return messageExtList;
}
 
Example 12
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 13
Source File: LocalMessageCacheTest.java    From rocketmq-read 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: ProcessQueueTest.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
private List<MessageExt> createMessageList(int count) {
    List<MessageExt> messageExtList = new ArrayList<MessageExt>();
    for (int i = 0; i < count; i++) {
        MessageExt messageExt = new MessageExt();
        messageExt.setQueueOffset(i);
        messageExt.setBody(new byte[123]);
        messageExtList.add(messageExt);
    }
    return messageExtList;
}
 
Example 15
Source File: LocalMessageCacheTest.java    From rocketmq-4.3.0 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 16
Source File: ProcessQueueTest.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
private List<MessageExt> createMessageList(int count) {
    List<MessageExt> messageExtList = new ArrayList<MessageExt>();
    for (int i = 0; i < count; i++) {
        MessageExt messageExt = new MessageExt();
        messageExt.setQueueOffset(i);
        messageExt.setBody(new byte[123]);
        messageExtList.add(messageExt);
    }
    return messageExtList;
}
 
Example 17
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 18
Source File: DefaultRequestProcessor.java    From rocketmq_trans_message with Apache License 2.0 4 votes vote down vote up
private ByteBuffer messageToByteBuffer(final MessageExt msg) throws IOException {
    int sysFlag = MessageSysFlag.clearCompressedFlag(msg.getSysFlag());
    if (msg.getBody() != null) {
        if (msg.getBody().length >= this.filtersrvController.getFiltersrvConfig().getCompressMsgBodyOverHowmuch()) {
            byte[] data = UtilAll.compress(msg.getBody(), this.filtersrvController.getFiltersrvConfig().getZipCompressLevel());
            if (data != null) {
                msg.setBody(data);
                sysFlag |= MessageSysFlag.COMPRESSED_FLAG;
            }
        }
    }

    final int bodyLength = msg.getBody() != null ? msg.getBody().length : 0;
    byte[] topicData = msg.getTopic().getBytes(MixAll.DEFAULT_CHARSET);
    final int topicLength = topicData.length;
    String properties = MessageDecoder.messageProperties2String(msg.getProperties());
    byte[] propertiesData = properties.getBytes(MixAll.DEFAULT_CHARSET);
    final int propertiesLength = propertiesData.length;
    final int msgLen = 4 // 1 TOTALSIZE
        + 4 // 2 MAGICCODE
        + 4 // 3 BODYCRC
        + 4 // 4 QUEUEID
        + 4 // 5 FLAG
        + 8 // 6 QUEUEOFFSET
        + 8 // 7 PHYSICALOFFSET
        + 4 // 8 SYSFLAG
        + 8 // 9 BORNTIMESTAMP
        + 8 // 10 BORNHOST
        + 8 // 11 STORETIMESTAMP
        + 8 // 12 STOREHOSTADDRESS
        + 4 // 13 RECONSUMETIMES
        + 8 // 14 Prepared Transaction Offset
        + 4 + bodyLength // 14 BODY
        + 1 + topicLength // 15 TOPIC
        + 2 + propertiesLength // 16 propertiesLength
        + 0;

    ByteBuffer msgStoreItemMemory = ByteBuffer.allocate(msgLen);

    final MessageExt msgInner = msg;

    // 1 TOTALSIZE
    msgStoreItemMemory.putInt(msgLen);
    // 2 MAGICCODE
    msgStoreItemMemory.putInt(CommitLog.MESSAGE_MAGIC_CODE);
    // 3 BODYCRC
    msgStoreItemMemory.putInt(UtilAll.crc32(msgInner.getBody()));
    // 4 QUEUEID
    msgStoreItemMemory.putInt(msgInner.getQueueId());
    // 5 FLAG
    msgStoreItemMemory.putInt(msgInner.getFlag());
    // 6 QUEUEOFFSET
    msgStoreItemMemory.putLong(msgInner.getQueueOffset());
    // 7 PHYSICALOFFSET
    msgStoreItemMemory.putLong(msgInner.getCommitLogOffset());
    // 8 SYSFLAG
    msgStoreItemMemory.putInt(sysFlag);
    // 9 BORNTIMESTAMP
    msgStoreItemMemory.putLong(msgInner.getBornTimestamp());
    // 10 BORNHOST
    msgStoreItemMemory.put(msgInner.getBornHostBytes());
    // 11 STORETIMESTAMP
    msgStoreItemMemory.putLong(msgInner.getStoreTimestamp());
    // 12 STOREHOSTADDRESS
    msgStoreItemMemory.put(msgInner.getStoreHostBytes());
    // 13 RECONSUMETIMES
    msgStoreItemMemory.putInt(msgInner.getReconsumeTimes());
    // 14 Prepared Transaction Offset
    msgStoreItemMemory.putLong(msgInner.getPreparedTransactionOffset());
    // 15 BODY
    msgStoreItemMemory.putInt(bodyLength);
    if (bodyLength > 0)
        msgStoreItemMemory.put(msgInner.getBody());
    // 16 TOPIC
    msgStoreItemMemory.put((byte) topicLength);
    msgStoreItemMemory.put(topicData);
    // 17 PROPERTIES
    msgStoreItemMemory.putShort((short) propertiesLength);
    if (propertiesLength > 0)
        msgStoreItemMemory.put(propertiesData);

    return msgStoreItemMemory;
}
 
Example 19
Source File: DefaultRequestProcessor.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 4 votes vote down vote up
private ByteBuffer messageToByteBuffer(final MessageExt msg) throws IOException {
    int sysFlag = MessageSysFlag.clearCompressedFlag(msg.getSysFlag());
    if (msg.getBody() != null) {
        if (msg.getBody().length >= this.filtersrvController.getFiltersrvConfig().getCompressMsgBodyOverHowmuch()) {
            byte[] data = UtilAll.compress(msg.getBody(), this.filtersrvController.getFiltersrvConfig().getZipCompressLevel());
            if (data != null) {
                msg.setBody(data);
                sysFlag |= MessageSysFlag.COMPRESSED_FLAG;
            }
        }
    }

    final int bodyLength = msg.getBody() != null ? msg.getBody().length : 0;
    byte[] topicData = msg.getTopic().getBytes(MixAll.DEFAULT_CHARSET);
    final int topicLength = topicData.length;
    String properties = MessageDecoder.messageProperties2String(msg.getProperties());
    byte[] propertiesData = properties.getBytes(MixAll.DEFAULT_CHARSET);
    final int propertiesLength = propertiesData.length;
    final int msgLen = 4 // 1 TOTALSIZE
        + 4 // 2 MAGICCODE
        + 4 // 3 BODYCRC
        + 4 // 4 QUEUEID
        + 4 // 5 FLAG
        + 8 // 6 QUEUEOFFSET
        + 8 // 7 PHYSICALOFFSET
        + 4 // 8 SYSFLAG
        + 8 // 9 BORNTIMESTAMP
        + 8 // 10 BORNHOST
        + 8 // 11 STORETIMESTAMP
        + 8 // 12 STOREHOSTADDRESS
        + 4 // 13 RECONSUMETIMES
        + 8 // 14 Prepared Transaction Offset
        + 4 + bodyLength // 14 BODY
        + 1 + topicLength // 15 TOPIC
        + 2 + propertiesLength // 16 propertiesLength
        + 0;

    ByteBuffer msgStoreItemMemory = ByteBuffer.allocate(msgLen);

    final MessageExt msgInner = msg;

    // 1 TOTALSIZE
    msgStoreItemMemory.putInt(msgLen);
    // 2 MAGICCODE
    msgStoreItemMemory.putInt(CommitLog.MESSAGE_MAGIC_CODE);
    // 3 BODYCRC
    msgStoreItemMemory.putInt(UtilAll.crc32(msgInner.getBody()));
    // 4 QUEUEID
    msgStoreItemMemory.putInt(msgInner.getQueueId());
    // 5 FLAG
    msgStoreItemMemory.putInt(msgInner.getFlag());
    // 6 QUEUEOFFSET
    msgStoreItemMemory.putLong(msgInner.getQueueOffset());
    // 7 PHYSICALOFFSET
    msgStoreItemMemory.putLong(msgInner.getCommitLogOffset());
    // 8 SYSFLAG
    msgStoreItemMemory.putInt(sysFlag);
    // 9 BORNTIMESTAMP
    msgStoreItemMemory.putLong(msgInner.getBornTimestamp());
    // 10 BORNHOST
    msgStoreItemMemory.put(msgInner.getBornHostBytes());
    // 11 STORETIMESTAMP
    msgStoreItemMemory.putLong(msgInner.getStoreTimestamp());
    // 12 STOREHOSTADDRESS
    msgStoreItemMemory.put(msgInner.getStoreHostBytes());
    // 13 RECONSUMETIMES
    msgStoreItemMemory.putInt(msgInner.getReconsumeTimes());
    // 14 Prepared Transaction Offset
    msgStoreItemMemory.putLong(msgInner.getPreparedTransactionOffset());
    // 15 BODY
    msgStoreItemMemory.putInt(bodyLength);
    if (bodyLength > 0)
        msgStoreItemMemory.put(msgInner.getBody());
    // 16 TOPIC
    msgStoreItemMemory.put((byte) topicLength);
    msgStoreItemMemory.put(topicData);
    // 17 PROPERTIES
    msgStoreItemMemory.putShort((short) propertiesLength);
    if (propertiesLength > 0)
        msgStoreItemMemory.put(propertiesData);

    return msgStoreItemMemory;
}
 
Example 20
Source File: DefaultRequestProcessor.java    From DDMQ with Apache License 2.0 4 votes vote down vote up
private ByteBuffer messageToByteBuffer(final MessageExt msg) throws IOException {
    int sysFlag = MessageSysFlag.clearCompressedFlag(msg.getSysFlag());
    if (msg.getBody() != null) {
        if (msg.getBody().length >= this.filtersrvController.getFiltersrvConfig().getCompressMsgBodyOverHowmuch()) {
            byte[] data = UtilAll.compress(msg.getBody(), this.filtersrvController.getFiltersrvConfig().getZipCompressLevel());
            if (data != null) {
                msg.setBody(data);
                sysFlag |= MessageSysFlag.COMPRESSED_FLAG;
            }
        }
    }

    final int bodyLength = msg.getBody() != null ? msg.getBody().length : 0;
    byte[] topicData = msg.getTopic().getBytes(MixAll.DEFAULT_CHARSET);
    final int topicLength = topicData.length;
    String properties = MessageDecoder.messageProperties2String(msg.getProperties());
    byte[] propertiesData = properties.getBytes(MixAll.DEFAULT_CHARSET);
    final int propertiesLength = propertiesData.length;
    final int msgLen = 4 // 1 TOTALSIZE
        + 4 // 2 MAGICCODE
        + 4 // 3 BODYCRC
        + 4 // 4 QUEUEID
        + 4 // 5 FLAG
        + 8 // 6 QUEUEOFFSET
        + 8 // 7 PHYSICALOFFSET
        + 4 // 8 SYSFLAG
        + 8 // 9 BORNTIMESTAMP
        + 8 // 10 BORNHOST
        + 8 // 11 STORETIMESTAMP
        + 8 // 12 STOREHOSTADDRESS
        + 4 // 13 RECONSUMETIMES
        + 8 // 14 Prepared Transaction Offset
        + 4 + bodyLength // 14 BODY
        + 1 + topicLength // 15 TOPIC
        + 2 + propertiesLength // 16 propertiesLength
        + 0;

    ByteBuffer msgStoreItemMemory = ByteBuffer.allocate(msgLen);

    final MessageExt msgInner = msg;

    // 1 TOTALSIZE
    msgStoreItemMemory.putInt(msgLen);
    // 2 MAGICCODE
    msgStoreItemMemory.putInt(CommitLog.MESSAGE_MAGIC_CODE);
    // 3 BODYCRC
    msgStoreItemMemory.putInt(UtilAll.crc32(msgInner.getBody()));
    // 4 QUEUEID
    msgStoreItemMemory.putInt(msgInner.getQueueId());
    // 5 FLAG
    msgStoreItemMemory.putInt(msgInner.getFlag());
    // 6 QUEUEOFFSET
    msgStoreItemMemory.putLong(msgInner.getQueueOffset());
    // 7 PHYSICALOFFSET
    msgStoreItemMemory.putLong(msgInner.getCommitLogOffset());
    // 8 SYSFLAG
    msgStoreItemMemory.putInt(sysFlag);
    // 9 BORNTIMESTAMP
    msgStoreItemMemory.putLong(msgInner.getBornTimestamp());
    // 10 BORNHOST
    msgStoreItemMemory.put(msgInner.getBornHostBytes());
    // 11 STORETIMESTAMP
    msgStoreItemMemory.putLong(msgInner.getStoreTimestamp());
    // 12 STOREHOSTADDRESS
    msgStoreItemMemory.put(msgInner.getStoreHostBytes());
    // 13 RECONSUMETIMES
    msgStoreItemMemory.putInt(msgInner.getReconsumeTimes());
    // 14 Prepared Transaction Offset
    msgStoreItemMemory.putLong(msgInner.getPreparedTransactionOffset());
    // 15 BODY
    msgStoreItemMemory.putInt(bodyLength);
    if (bodyLength > 0)
        msgStoreItemMemory.put(msgInner.getBody());
    // 16 TOPIC
    msgStoreItemMemory.put((byte) topicLength);
    msgStoreItemMemory.put(topicData);
    // 17 PROPERTIES
    msgStoreItemMemory.putShort((short) propertiesLength);
    if (propertiesLength > 0)
        msgStoreItemMemory.put(propertiesData);

    return msgStoreItemMemory;
}