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

The following examples show how to use org.apache.rocketmq.common.message.Message#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: DeFiBusClientUtil.java    From DeFiBus with Apache License 2.0 6 votes vote down vote up
public static Message createReplyMessage(MessageExt sourceMsg, byte[] content) {
    String cluster = sourceMsg.getUserProperty(DeFiBusConstant.PROPERTY_MESSAGE_CLUSTER);
    String replyTopic = DeFiBusConstant.RR_REPLY_TOPIC;
    if (!StringUtils.isEmpty(cluster)) {
        replyTopic = cluster + "-" + replyTopic;
    } else {
        LOGGER.warn("no cluster info from message, can not reply");
        return null;
    }

    Message msg = new Message();
    msg.setTopic(replyTopic);//回程topic
    msg.setBody(content);//body
    msg.putUserProperty(DeFiBusConstant.PROPERTY_MESSAGE_REPLY_TO, sourceMsg.getUserProperty(DeFiBusConstant.PROPERTY_MESSAGE_REPLY_TO));//回给谁
    msg.putUserProperty(DeFiBusConstant.PROPERTY_RR_REQUEST_ID, sourceMsg.getUserProperty(DeFiBusConstant.PROPERTY_RR_REQUEST_ID));//原uniqueId
    String sourceBroker = sourceMsg.getUserProperty(DeFiBusConstant.PROPERTY_MESSAGE_BROKER);
    if (!StringUtils.isEmpty(sourceBroker)) {
        msg.putUserProperty(DeFiBusConstant.PROPERTY_MESSAGE_BROKER, sourceBroker);//消息从哪个broker来
    }

    return msg;
}
 
Example 2
Source File: DefaultMQProducerImpl.java    From rocketmq-4.3.0 with Apache License 2.0 6 votes vote down vote up
private boolean tryToCompressMessage(final Message msg) {
    if (msg instanceof MessageBatch) {
        //batch dose not support compressing right now
        return false;
    }
    byte[] body = msg.getBody();
    if (body != null) {
        if (body.length >= this.defaultMQProducer.getCompressMsgBodyOverHowmuch()) {
            try {
                byte[] data = UtilAll.compress(body, zipCompressLevel);
                if (data != null) {
                    msg.setBody(data);
                    return true;
                }
            } catch (IOException e) {
                log.error("tryToCompressMessage exception", e);
                log.warn(msg.toString());
            }
        }
    }

    return false;
}
 
Example 3
Source File: MqMessage.java    From paascloud-master with Apache License 2.0 5 votes vote down vote up
private static Message buildMessage(String body, String topic, String tag, String key) {
	Message message = new Message();
	try {
		message.setBody(body.getBytes(RemotingHelper.DEFAULT_CHARSET));
	} catch (UnsupportedEncodingException e) {
		log.error("编码转换,出现异常={}", e.getMessage(), e);
		throw new BusinessException(ErrorCodeEnum.TPC100500011);
	}
	message.setKeys(key);
	message.setTopic(topic);
	message.setTags(tag);
	return message;
}
 
Example 4
Source File: SendMsgStatusCommand.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
private static Message buildMessage(final String topic, final int messageSize) throws UnsupportedEncodingException {
    Message msg = new Message();
    msg.setTopic(topic);

    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < messageSize; i += 11) {
        sb.append("hello jodie");
    }
    msg.setBody(sb.toString().getBytes(MixAll.DEFAULT_CHARSET));
    return msg;
}
 
Example 5
Source File: Producer.java    From rocketmq_trans_message with Apache License 2.0 5 votes vote down vote up
private static Message buildMessage(final int messageSize, final String topic) throws UnsupportedEncodingException {
    Message msg = new Message();
    msg.setTopic(topic);

    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < messageSize; i += 10) {
        sb.append("hello baby");
    }

    msg.setBody(sb.toString().getBytes(RemotingHelper.DEFAULT_CHARSET));

    return msg;
}
 
Example 6
Source File: SendMsgStatusCommand.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
private static Message buildMessage(final String topic, final int messageSize) throws UnsupportedEncodingException {
    Message msg = new Message();
    msg.setTopic(topic);

    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < messageSize; i += 11) {
        sb.append("hello jodie");
    }
    msg.setBody(sb.toString().getBytes(MixAll.DEFAULT_CHARSET));
    return msg;
}
 
Example 7
Source File: TransactionProducer.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
private static Message buildMessage(TxSendConfig config) {
    byte[] bs = new byte[config.messageSize];
    ThreadLocalRandom r = ThreadLocalRandom.current();
    r.nextBytes(bs);

    ByteBuffer buf = ByteBuffer.wrap(bs);
    buf.putLong(config.batchId);
    long sendMachineId = START_TIME << 32;
    long msgId = sendMachineId | MSG_COUNT.getAndIncrement();
    buf.putLong(msgId);

    // save send tx result in message
    if (r.nextDouble() < config.sendRollbackRate) {
        buf.put((byte) LocalTransactionState.ROLLBACK_MESSAGE.ordinal());
    } else if (r.nextDouble() < config.sendUnknownRate) {
        buf.put((byte) LocalTransactionState.UNKNOW.ordinal());
    } else {
        buf.put((byte) LocalTransactionState.COMMIT_MESSAGE.ordinal());
    }

    // save check tx result in message
    for (int i = 0; i < MAX_CHECK_RESULT_IN_MSG; i++) {
        if (r.nextDouble() < config.checkRollbackRate) {
            buf.put((byte) LocalTransactionState.ROLLBACK_MESSAGE.ordinal());
        } else if (r.nextDouble() < config.checkUnknownRate) {
            buf.put((byte) LocalTransactionState.UNKNOW.ordinal());
        } else {
            buf.put((byte) LocalTransactionState.COMMIT_MESSAGE.ordinal());
        }
    }

    Message msg = new Message();
    msg.setTopic(config.topic);

    msg.setBody(bs);
    return msg;
}
 
Example 8
Source File: TransactionProducer.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 5 votes vote down vote up
private static Message buildMessage(final int messageSize) throws UnsupportedEncodingException {
    Message msg = new Message();
    msg.setTopic("BenchmarkTest");

    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < messageSize; i += 10) {
        sb.append("hello baby");
    }

    msg.setBody(sb.toString().getBytes(RemotingHelper.DEFAULT_CHARSET));

    return msg;
}
 
Example 9
Source File: Producer.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 5 votes vote down vote up
private static Message buildMessage(final int messageSize, final String topic) throws UnsupportedEncodingException {
    Message msg = new Message();
    msg.setTopic(topic);

    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < messageSize; i += 10) {
        sb.append("hello baby");
    }

    msg.setBody(sb.toString().getBytes(RemotingHelper.DEFAULT_CHARSET));

    return msg;
}
 
Example 10
Source File: AppendCallbackTest.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
@Test
public void testAppendIPv6HostMessageBatchEndOfFile() throws Exception {
    List<Message> messages = new ArrayList<>();
    String topic = "test-topic";
    int queue = 0;
    for (int i = 0; i < 10; i++) {
        Message msg = new Message();
        msg.setBody("body".getBytes());
        msg.setTopic(topic);
        msg.setTags("abc");
        messages.add(msg);
    }
    MessageExtBatch messageExtBatch = new MessageExtBatch();
    messageExtBatch.setTopic(topic);
    messageExtBatch.setQueueId(queue);
    messageExtBatch.setBornTimestamp(System.currentTimeMillis());
    messageExtBatch.setMsgId("24084004018081003FAA1DDE2B3F898A00002A9F0000000000000CA0");
    messageExtBatch.setSysFlag(0);
    messageExtBatch.setBornHostV6Flag();
    messageExtBatch.setStoreHostAddressV6Flag();
    messageExtBatch.setBornHost(new InetSocketAddress("1050:0000:0000:0000:0005:0600:300c:326b", 123));
    messageExtBatch.setStoreHost(new InetSocketAddress("::1", 124));
    messageExtBatch.setBody(MessageDecoder.encodeMessages(messages));

    messageExtBatch.setEncodedBuff(batchEncoder.encode(messageExtBatch));
    ByteBuffer buff = ByteBuffer.allocate(1024 * 10);
    //encounter end of file when append half of the data
    AppendMessageResult result = callback.doAppend(0, buff, 1000, messageExtBatch);
    assertEquals(AppendMessageStatus.END_OF_FILE, result.getStatus());
    assertEquals(0, result.getWroteOffset());
    assertEquals(0, result.getLogicsOffset());
    assertEquals(1000, result.getWroteBytes());
    assertEquals(8, buff.position()); //write blank size and magic value

    assertTrue(result.getMsgId().length() > 0); //should have already constructed some message ids
}
 
Example 11
Source File: MessageExceptionIT.java    From DDMQ with Apache License 2.0 4 votes vote down vote up
@Test(expected = org.apache.rocketmq.client.exception.MQClientException.class)
public void testSynSendZeroSizeBodyMessage() throws Exception {
    Message msg = new Message(topic, RandomUtils.getStringByUUID().getBytes());
    msg.setBody(new byte[0]);
    producer.send(msg);
}
 
Example 12
Source File: MessageExceptionIT.java    From rocketmq with Apache License 2.0 4 votes vote down vote up
@Test(expected = org.apache.rocketmq.client.exception.MQClientException.class)
public void testSynSendOutOfSizeBodyMessage() throws Exception {
    Message msg = new Message(topic, RandomUtils.getStringByUUID().getBytes());
    msg.setBody(new byte[1024 * 1024 * 4 + 1]);
    producer.send(msg);
}
 
Example 13
Source File: MessageExceptionIT.java    From rocketmq-4.3.0 with Apache License 2.0 4 votes vote down vote up
@Test(expected = org.apache.rocketmq.client.exception.MQClientException.class)
public void testSynSendZeroSizeBodyMessage() throws Exception {
    Message msg = new Message(topic, RandomUtils.getStringByUUID().getBytes());
    msg.setBody(new byte[0]);
    producer.send(msg);
}
 
Example 14
Source File: BatchSendIT.java    From rocketmq-read with Apache License 2.0 4 votes vote down vote up
@Test
public void testBatchSend_CheckProperties() throws Exception {
    List<Message> messageList = new ArrayList<>();
    Message message = new Message();
    message.setTopic(topic);
    message.setKeys("keys123");
    message.setTags("tags123");
    message.setWaitStoreMsgOK(false);
    message.setBuyerId("buyerid123");
    message.setFlag(123);
    message.setBody("body".getBytes());
    messageList.add(message);

    DefaultMQProducer producer = ProducerFactory.getRMQProducer(nsAddr);
    SendResult sendResult = producer.send(messageList);
    Assert.assertEquals(SendStatus.SEND_OK, sendResult.getSendStatus());

    String[] offsetIds = sendResult.getOffsetMsgId().split(",");
    String[] msgIds = sendResult.getMsgId().split(",");
    Assert.assertEquals(messageList.size(), offsetIds.length);
    Assert.assertEquals(messageList.size(), msgIds.length);

    Thread.sleep(2000);

    Message messageByOffset = producer.viewMessage(offsetIds[0]);
    Message messageByMsgId = producer.viewMessage(topic, msgIds[0]);

    System.out.println(messageByOffset);
    System.out.println(messageByMsgId);

    Assert.assertEquals(message.getTopic(), messageByMsgId.getTopic());
    Assert.assertEquals(message.getTopic(), messageByOffset.getTopic());

    Assert.assertEquals(message.getKeys(), messageByOffset.getKeys());
    Assert.assertEquals(message.getKeys(), messageByMsgId.getKeys());

    Assert.assertEquals(message.getTags(), messageByOffset.getTags());
    Assert.assertEquals(message.getTags(), messageByMsgId.getTags());

    Assert.assertEquals(message.isWaitStoreMsgOK(), messageByOffset.isWaitStoreMsgOK());
    Assert.assertEquals(message.isWaitStoreMsgOK(), messageByMsgId.isWaitStoreMsgOK());

    Assert.assertEquals(message.getBuyerId(), messageByOffset.getBuyerId());
    Assert.assertEquals(message.getBuyerId(), messageByMsgId.getBuyerId());

    Assert.assertEquals(message.getFlag(), messageByOffset.getFlag());
    Assert.assertEquals(message.getFlag(), messageByMsgId.getFlag());
}
 
Example 15
Source File: AppendCallbackTest.java    From DDMQ with Apache License 2.0 4 votes vote down vote up
@Test
public void testAppendMessageBatchSucc() throws Exception {
    List<Message> messages = new ArrayList<>();
    String topic = "test-topic";
    int queue = 0;
    for (int i = 0; i < 10; i++) {
        Message msg = new Message();
        msg.setBody("body".getBytes());
        msg.setTopic(topic);
        msg.setTags("abc");
        messages.add(msg);
    }
    MessageExtBatch messageExtBatch = new MessageExtBatch();
    messageExtBatch.setTopic(topic);
    messageExtBatch.setQueueId(queue);
    messageExtBatch.setBornTimestamp(System.currentTimeMillis());
    messageExtBatch.setBornHost(new InetSocketAddress("127.0.0.1", 123));
    messageExtBatch.setStoreHost(new InetSocketAddress("127.0.0.1", 124));
    messageExtBatch.setBody(MessageDecoder.encodeMessages(messages));

    messageExtBatch.setEncodedBuff(batchEncoder.encode(messageExtBatch));
    ByteBuffer buff = ByteBuffer.allocate(1024 * 10);
    AppendMessageResult allresult = callback.doAppend(0, buff, 1024 * 10, messageExtBatch);

    assertEquals(AppendMessageStatus.PUT_OK, allresult.getStatus());
    assertEquals(0, allresult.getWroteOffset());
    assertEquals(0, allresult.getLogicsOffset());
    assertEquals(buff.position(), allresult.getWroteBytes());

    assertEquals(messages.size(), allresult.getMsgNum());

    Set<String> msgIds = new HashSet<>();
    for (String msgId : allresult.getMsgId().split(",")) {
        assertEquals(32, msgId.length());
        msgIds.add(msgId);
    }
    assertEquals(messages.size(), msgIds.size());

    List<MessageExt> decodeMsgs = MessageDecoder.decodes((ByteBuffer) buff.flip());
    assertEquals(decodeMsgs.size(), decodeMsgs.size());
    long queueOffset = decodeMsgs.get(0).getQueueOffset();
    long storeTimeStamp = decodeMsgs.get(0).getStoreTimestamp();
    for (int i = 0; i < messages.size(); i++) {
        assertEquals(messages.get(i).getTopic(), decodeMsgs.get(i).getTopic());
        assertEquals(new String(messages.get(i).getBody()), new String(decodeMsgs.get(i).getBody()));
        assertEquals(messages.get(i).getTags(), decodeMsgs.get(i).getTags());

        assertEquals(messageExtBatch.getBornHostNameString(), decodeMsgs.get(i).getBornHostNameString());

        assertEquals(messageExtBatch.getBornTimestamp(), decodeMsgs.get(i).getBornTimestamp());
        assertEquals(storeTimeStamp, decodeMsgs.get(i).getStoreTimestamp());
        assertEquals(queueOffset++, decodeMsgs.get(i).getQueueOffset());
    }

}
 
Example 16
Source File: MessageExceptionIT.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 4 votes vote down vote up
@Test(expected = org.apache.rocketmq.client.exception.MQClientException.class)
public void testSynSendZeroSizeBodyMessage() throws Exception {
    Message msg = new Message(topic, RandomUtils.getStringByUUID().getBytes());
    msg.setBody(new byte[0]);
    producer.send(msg);
}
 
Example 17
Source File: MessageExceptionIT.java    From DDMQ with Apache License 2.0 4 votes vote down vote up
@Test(expected = org.apache.rocketmq.client.exception.MQClientException.class)
public void testSynSendZeroSizeBodyMessage() throws Exception {
    Message msg = new Message(topic, RandomUtils.getStringByUUID().getBytes());
    msg.setBody(new byte[0]);
    producer.send(msg);
}
 
Example 18
Source File: MessageExceptionIT.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 4 votes vote down vote up
@Test(expected = org.apache.rocketmq.client.exception.MQClientException.class)
public void testSynSendNullBodyMessage() throws Exception {
    Message msg = new Message(topic, RandomUtils.getStringByUUID().getBytes());
    msg.setBody(null);
    producer.send(msg);
}
 
Example 19
Source File: AppendCallbackTest.java    From rocketmq with Apache License 2.0 4 votes vote down vote up
@Test
public void testAppendMessageBatchSucc() throws Exception {
    List<Message> messages = new ArrayList<>();
    String topic = "test-topic";
    int queue = 0;
    for (int i = 0; i < 10; i++) {
        Message msg = new Message();
        msg.setBody("body".getBytes());
        msg.setTopic(topic);
        msg.setTags("abc");
        messages.add(msg);
    }
    MessageExtBatch messageExtBatch = new MessageExtBatch();
    messageExtBatch.setTopic(topic);
    messageExtBatch.setQueueId(queue);
    messageExtBatch.setBornTimestamp(System.currentTimeMillis());
    messageExtBatch.setBornHost(new InetSocketAddress("127.0.0.1", 123));
    messageExtBatch.setStoreHost(new InetSocketAddress("127.0.0.1", 124));
    messageExtBatch.setBody(MessageDecoder.encodeMessages(messages));

    messageExtBatch.setEncodedBuff(batchEncoder.encode(messageExtBatch));
    ByteBuffer buff = ByteBuffer.allocate(1024 * 10);
    AppendMessageResult allresult = callback.doAppend(0, buff, 1024 * 10, messageExtBatch);

    assertEquals(AppendMessageStatus.PUT_OK, allresult.getStatus());
    assertEquals(0, allresult.getWroteOffset());
    assertEquals(0, allresult.getLogicsOffset());
    assertEquals(buff.position(), allresult.getWroteBytes());

    assertEquals(messages.size(), allresult.getMsgNum());

    Set<String> msgIds = new HashSet<>();
    for (String msgId : allresult.getMsgId().split(",")) {
        assertEquals(32, msgId.length());
        msgIds.add(msgId);
    }
    assertEquals(messages.size(), msgIds.size());

    List<MessageExt> decodeMsgs = MessageDecoder.decodes((ByteBuffer) buff.flip());
    assertEquals(decodeMsgs.size(), decodeMsgs.size());
    long queueOffset = decodeMsgs.get(0).getQueueOffset();
    long storeTimeStamp = decodeMsgs.get(0).getStoreTimestamp();
    for (int i = 0; i < messages.size(); i++) {
        assertEquals(messages.get(i).getTopic(), decodeMsgs.get(i).getTopic());
        assertEquals(new String(messages.get(i).getBody()), new String(decodeMsgs.get(i).getBody()));
        assertEquals(messages.get(i).getTags(), decodeMsgs.get(i).getTags());

        assertEquals(messageExtBatch.getBornHostNameString(), decodeMsgs.get(i).getBornHostNameString());

        assertEquals(messageExtBatch.getBornTimestamp(), decodeMsgs.get(i).getBornTimestamp());
        assertEquals(storeTimeStamp, decodeMsgs.get(i).getStoreTimestamp());
        assertEquals(queueOffset++, decodeMsgs.get(i).getQueueOffset());
    }

}
 
Example 20
Source File: AppendCallbackTest.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 4 votes vote down vote up
@Test
public void testAppendMessageBatchSucc() throws Exception {
    List<Message>  messages = new ArrayList<>();
    String topic = "test-topic";
    int queue= 0;
    for (int i = 0; i < 10; i++) {
        Message msg = new Message();
        msg.setBody("body".getBytes());
        msg.setTopic(topic);
        msg.setTags("abc");
        messages.add(msg);
    }
    MessageExtBatch messageExtBatch = new MessageExtBatch();
    messageExtBatch.setTopic(topic);
    messageExtBatch.setQueueId(queue);
    messageExtBatch.setBornTimestamp(System.currentTimeMillis());
    messageExtBatch.setBornHost(new InetSocketAddress("127.0.0.1",123));
    messageExtBatch.setStoreHost(new InetSocketAddress("127.0.0.1",124));
    messageExtBatch.setBody(MessageDecoder.encodeMessages(messages));

    messageExtBatch.setEncodedBuff(batchEncoder.encode(messageExtBatch));
    ByteBuffer buff = ByteBuffer.allocate(1024 * 10);
    AppendMessageResult allresult = callback.doAppend(0, buff, 1024 * 10, messageExtBatch);

    assertEquals(AppendMessageStatus.PUT_OK, allresult.getStatus());
    assertEquals(0, allresult.getWroteOffset());
    assertEquals(0, allresult.getLogicsOffset());
    assertEquals(buff.position(), allresult.getWroteBytes());

    assertEquals(messages.size(), allresult.getMsgNum());

    Set<String> msgIds = new HashSet<>();
    for (String msgId: allresult.getMsgId().split(",")) {
        assertEquals(32, msgId.length());
        msgIds.add(msgId);
    }
    assertEquals(messages.size(), msgIds.size());

    List<MessageExt> decodeMsgs = MessageDecoder.decodes((ByteBuffer) buff.flip());
    assertEquals(decodeMsgs.size(), decodeMsgs.size());
    long queueOffset = decodeMsgs.get(0).getQueueOffset();
    long storeTimeStamp = decodeMsgs.get(0).getStoreTimestamp();
    for (int i = 0; i < messages.size(); i++) {
        assertEquals(messages.get(i).getTopic(), decodeMsgs.get(i).getTopic());
        assertEquals(new String(messages.get(i).getBody()), new String(decodeMsgs.get(i).getBody()));
        assertEquals(messages.get(i).getTags(), decodeMsgs.get(i).getTags());

        assertEquals(messageExtBatch.getBornHostNameString(), decodeMsgs.get(i).getBornHostNameString());

        assertEquals(messageExtBatch.getBornTimestamp(), decodeMsgs.get(i).getBornTimestamp());
        assertEquals(storeTimeStamp, decodeMsgs.get(i).getStoreTimestamp());
        assertEquals(queueOffset++, decodeMsgs.get(i).getQueueOffset());
    }

}