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

The following examples show how to use org.apache.rocketmq.common.message.Message#putUserProperty() . 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: DeFiReplyMessageProcessorTest.java    From DeFiBus with Apache License 2.0 6 votes vote down vote up
private SendMessageRequestHeader createSendMsgRequestHeader() {
    SendMessageRequestHeader requestHeader = new SendMessageRequestHeader();
    requestHeader.setProducerGroup(group);
    requestHeader.setTopic(topic);
    requestHeader.setDefaultTopic(MixAll.AUTO_CREATE_TOPIC_KEY_TOPIC);
    requestHeader.setDefaultTopicQueueNums(3);
    requestHeader.setQueueId(1);
    requestHeader.setSysFlag(0);
    requestHeader.setBornTimestamp(System.currentTimeMillis());
    requestHeader.setFlag(124);
    Message msg = new Message();
    msg.putUserProperty(DeFiBusConstant.KEY, DeFiBusConstant.REPLY);
    msg.putUserProperty(DeFiBusConstant.PROPERTY_MESSAGE_REPLY_TO, clientId);
    msg.setBody(new String("abcd").getBytes());
    requestHeader.setProperties(MessageDecoder.messageProperties2String(msg.getProperties()));
    return requestHeader;
}
 
Example 2
Source File: MessageUserPropIT.java    From rocketmq-read with Apache License 2.0 6 votes vote down vote up
/**
 * @since version3.4.6
 */
@Test
public void testSendChinaUserProp() {
    Message msg = MessageFactory.getRandomMessage(topic);
    String msgKey = "jueyinKey";
    String msgValue = "jueyinzhi";
    msg.putUserProperty(msgKey, msgValue);

    RMQNormalConsumer consumer = getConsumer(nsAddr, topic, "*", new RMQNormalListener());

    producer.send(msg, null);
    assertThat(producer.getAllMsgBody().size()).isEqualTo(1);

    consumer.getListener().waitForMessageConsume(producer.getAllMsgBody(), consumeTime);

    Message sendMsg = (Message) producer.getFirstMsg();
    Message recvMsg = (Message) consumer.getListener().getFirstMsg();
    assertThat(recvMsg.getUserProperty(msgKey)).isEqualTo(sendMsg.getUserProperty(msgKey));
}
 
Example 3
Source File: MessageUserPropIT.java    From rocketmq with Apache License 2.0 6 votes vote down vote up
/**
 * @since version3.4.6
 */
@Test
public void testSendChinaUserProp() {
    Message msg = MessageFactory.getRandomMessage(topic);
    String msgKey = "jueyinKey";
    String msgValue = "jueyinzhi";
    msg.putUserProperty(msgKey, msgValue);

    RMQNormalConsumer consumer = getConsumer(nsAddr, topic, "*", new RMQNormalListener());

    producer.send(msg, null);
    assertThat(producer.getAllMsgBody().size()).isEqualTo(1);

    consumer.getListener().waitForMessageConsume(producer.getAllMsgBody(), consumeTime);

    Message sendMsg = (Message) producer.getFirstMsg();
    Message recvMsg = (Message) consumer.getListener().getFirstMsg();
    assertThat(recvMsg.getUserProperty(msgKey)).isEqualTo(sendMsg.getUserProperty(msgKey));
}
 
Example 4
Source File: MessageUserPropIT.java    From DDMQ with Apache License 2.0 6 votes vote down vote up
/**
 * @since version3.4.6
 */
@Test
public void testSendEnglishUserProp() {
    Message msg = MessageFactory.getRandomMessage(topic);
    String msgKey = "jueyinKey";
    String msgValue = "jueyinValue";
    msg.putUserProperty(msgKey, msgValue);

    RMQNormalConsumer consumer = getConsumer(nsAddr, topic, "*", new RMQNormalListener());

    producer.send(msg, null);
    assertThat(producer.getAllMsgBody().size()).isEqualTo(1);

    consumer.getListener().waitForMessageConsume(producer.getAllMsgBody(), consumeTime);

    Message sendMsg = (Message) producer.getFirstMsg();
    Message recvMsg = (Message) consumer.getListener().getFirstMsg();
    assertThat(recvMsg.getUserProperty(msgKey)).isEqualTo(sendMsg.getUserProperty(msgKey));
}
 
Example 5
Source File: DeFiBusProducerImpl.java    From DeFiBus with Apache License 2.0 6 votes vote down vote up
public void publish(final Message msg, final SendCallback sendCallback,
    final long timeout) throws MQClientException, RemotingException, InterruptedException {
    if (msg.getUserProperty(DeFiBusConstant.PROPERTY_MESSAGE_TTL) == null) {
        msg.putUserProperty(DeFiBusConstant.PROPERTY_MESSAGE_TTL, DeFiBusConstant.DEFAULT_TTL);
    }

    final AtomicReference<MessageQueue> selectorArgs = new AtomicReference<MessageQueue>();
    AsynCircuitBreakSendCallBack asynCircuitBreakSendCallBack = new AsynCircuitBreakSendCallBack();
    asynCircuitBreakSendCallBack.setMsg(msg);
    asynCircuitBreakSendCallBack.setProducer(this.deFiBusProducer);
    asynCircuitBreakSendCallBack.setSelectorArg(selectorArgs);
    asynCircuitBreakSendCallBack.setSendCallback(sendCallback);

    String topic = msg.getTopic();
    boolean hasRouteData = deFiBusProducer.getDefaultMQProducer().getDefaultMQProducerImpl().getmQClientFactory().getTopicRouteTable().containsKey(topic);
    if (!hasRouteData) {
        LOGGER.info("no topic route info for " + topic + ", send heartbeat to nameserver");
        deFiBusProducer.getDefaultMQProducer().getDefaultMQProducerImpl().getmQClientFactory().updateTopicRouteInfoFromNameServer(topic);
    }

    DeFiBusProducerImpl.this.deFiBusProducer.getDefaultMQProducer().send(msg, messageQueueSelector, selectorArgs, asynCircuitBreakSendCallBack, timeout);
}
 
Example 6
Source File: MessageUserPropIT.java    From rocketmq-read with Apache License 2.0 6 votes vote down vote up
/**
 * @since version3.4.6
 */
@Test
public void testSendEnglishUserProp() {
    Message msg = MessageFactory.getRandomMessage(topic);
    String msgKey = "jueyinKey";
    String msgValue = "jueyinValue";
    msg.putUserProperty(msgKey, msgValue);

    RMQNormalConsumer consumer = getConsumer(nsAddr, topic, "*", new RMQNormalListener());

    producer.send(msg, null);
    assertThat(producer.getAllMsgBody().size()).isEqualTo(1);

    consumer.getListener().waitForMessageConsume(producer.getAllMsgBody(), consumeTime);

    Message sendMsg = (Message) producer.getFirstMsg();
    Message recvMsg = (Message) consumer.getListener().getFirstMsg();
    assertThat(recvMsg.getUserProperty(msgKey)).isEqualTo(sendMsg.getUserProperty(msgKey));
}
 
Example 7
Source File: Producer.java    From rocketmq-4.3.0 with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws MQClientException, InterruptedException {
    DefaultMQProducer producer = new DefaultMQProducer("ProducerGroupName");
    producer.start();

    try {
        for (int i = 0; i < 6000000; i++) {
            Message msg = new Message("TopicFilter7",
                "TagA",
                "OrderID001",
                "Hello world".getBytes(RemotingHelper.DEFAULT_CHARSET));

            msg.putUserProperty("SequenceId", String.valueOf(i));
            SendResult sendResult = producer.send(msg);
            System.out.printf("%s%n", sendResult);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    producer.shutdown();
}
 
Example 8
Source File: Producer.java    From DDMQ with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws MQClientException, InterruptedException {
    DefaultMQProducer producer = new DefaultMQProducer("ProducerGroupName");
    producer.start();

    try {
        for (int i = 0; i < 6000000; i++) {
            Message msg = new Message("TopicFilter7",
                "TagA",
                "OrderID001",
                "Hello world".getBytes(RemotingHelper.DEFAULT_CHARSET));

            msg.putUserProperty("SequenceId", String.valueOf(i));
            SendResult sendResult = producer.send(msg);
            System.out.printf("%s%n", sendResult);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    producer.shutdown();
}
 
Example 9
Source File: Producer.java    From DDMQ with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws MQClientException, InterruptedException {
    DefaultMQProducer producer = new DefaultMQProducer("ProducerGroupName");
    producer.start();

    try {
        for (int i = 0; i < 6000000; i++) {
            Message msg = new Message("TopicFilter7",
                "TagA",
                "OrderID001",
                "Hello world".getBytes(RemotingHelper.DEFAULT_CHARSET));

            msg.putUserProperty("SequenceId", String.valueOf(i));
            SendResult sendResult = producer.send(msg);
            System.out.printf("%s%n", sendResult);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    producer.shutdown();
}
 
Example 10
Source File: MessageUserPropIT.java    From rocketmq-4.3.0 with Apache License 2.0 6 votes vote down vote up
/**
 * @since version3.4.6
 */
@Test
public void testSendChinaUserProp() {
    Message msg = MessageFactory.getRandomMessage(topic);
    String msgKey = "jueyinKey";
    String msgValue = "jueyinzhi";
    msg.putUserProperty(msgKey, msgValue);

    RMQNormalConsumer consumer = getConsumer(nsAddr, topic, "*", new RMQNormalListener());

    producer.send(msg, null);
    assertThat(producer.getAllMsgBody().size()).isEqualTo(1);

    consumer.getListener().waitForMessageConsume(producer.getAllMsgBody(), consumeTime);

    Message sendMsg = (Message) producer.getFirstMsg();
    Message recvMsg = (Message) consumer.getListener().getFirstMsg();
    assertThat(recvMsg.getUserProperty(msgKey)).isEqualTo(sendMsg.getUserProperty(msgKey));
}
 
Example 11
Source File: MessageExceptionIT.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
@Test
public void testSendMsgWithUserProperty() {
    Message msg = MessageFactory.getRandomMessage(topic);
    msg.putUserProperty("key", RandomUtils.getCheseWord(10 * 1024));
    SendResult sendResult = null;
    try {
        sendResult = producer.send(msg);
    } catch (Exception e) {
    }
    assertThat(sendResult.getSendStatus()).isEqualTo(SendStatus.SEND_OK);
}
 
Example 12
Source File: DeFiBusProducerImpl.java    From DeFiBus with Apache License 2.0 5 votes vote down vote up
public void reply(
    Message replyMsg,
    final SendCallback sendCallback) throws InterruptedException, RemotingException, MQClientException, MQBrokerException {
    replyMsg.putUserProperty(DeFiBusConstant.KEY, DeFiBusConstant.REPLY);
    replyMsg.putUserProperty(DeFiBusConstant.PROPERTY_MESSAGE_TTL, String.valueOf(deFiBusProducer.getDefaultMQProducer().getSendMsgTimeout()));

    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("Reply message: {} ", replyMsg.toString());
    }
    final String requestId = replyMsg.getUserProperty(DeFiBusConstant.PROPERTY_RR_REQUEST_ID);
    if (requestId == null) {
        LOGGER.warn("rr request id is null, can not reply");
    }
    publish(replyMsg, new SendCallback() {
        @Override
        public void onSuccess(SendResult sendResult) {
            if (sendCallback != null) {
                sendCallback.onSuccess(sendResult);
            }
        }

        @Override
        public void onException(Throwable e) {
            LOGGER.warn("Reply message fail, requestId={}", requestId);
            if (sendCallback != null) {
                sendCallback.onException(e);
            }
        }
    });
}
 
Example 13
Source File: ChinaPropIT.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 5 votes vote down vote up
/**
 * @since version3.4.6
 */
@Test(expected = org.apache.rocketmq.client.exception.MQBrokerException.class)
public void testSend20kChinaPropMsg() throws Exception {
    Message msg = MessageFactory.getRandomMessage(topic);
    msg.putUserProperty("key", RandomUtils.getCheseWord(32 * 1024 + 1));
    producer.send(msg);
}
 
Example 14
Source File: MessageExceptionIT.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 5 votes vote down vote up
@Test
public void testSendMsgWithUserProperty() {
    Message msg = MessageFactory.getRandomMessage(topic);
    msg.putUserProperty("key", RandomUtils.getCheseWord(10 * 1024));
    SendResult sendResult = null;
    try {
        sendResult = producer.send(msg);
    } catch (Exception e) {
    }
    assertThat(sendResult.getSendStatus()).isEqualTo(SendStatus.SEND_OK);
}
 
Example 15
Source File: MessageEncodeDecodeTest.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
@Test
public void testEncodeDecodeSingle() throws Exception {
    Message message = new Message("topic", "body".getBytes());
    message.setFlag(12);
    message.putUserProperty("key", "value");
    byte[] bytes = MessageDecoder.encodeMessage(message);
    ByteBuffer buffer = ByteBuffer.allocate(bytes.length);
    buffer.put(bytes);
    buffer.flip();
    Message newMessage = MessageDecoder.decodeMessage(buffer);

    assertTrue(message.getFlag() == newMessage.getFlag());
    assertTrue(newMessage.getProperty("key").equals(newMessage.getProperty("key")));
    assertTrue(Arrays.equals(newMessage.getBody(), message.getBody()));
}
 
Example 16
Source File: MessageExceptionIT.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
@Test
public void testSendMsgWithUserProperty() {
    Message msg = MessageFactory.getRandomMessage(topic);
    msg.putUserProperty("key", RandomUtils.getCheseWord(10 * 1024));
    SendResult sendResult = null;
    try {
        sendResult = producer.send(msg);
    } catch (Exception e) {
    }
    assertThat(sendResult.getSendStatus()).isEqualTo(SendStatus.SEND_OK);
}
 
Example 17
Source File: ChinaPropIT.java    From rocketmq_trans_message with Apache License 2.0 5 votes vote down vote up
/**
 * @since version3.4.6
 */
@Test
public void testSend10kChinaPropMsg() {

    Message msg = MessageFactory.getRandomMessage(topic);
    msg.putUserProperty("key", RandomUtils.getCheseWord(10 * 1024));
    SendResult sendResult = null;
    try {
        sendResult = producer.send(msg);
    } catch (Exception e) {
    }
    assertThat(sendResult.getSendStatus()).isEqualTo(SendStatus.SEND_OK);
}
 
Example 18
Source File: ChinaPropIT.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
/**
 * @since version3.4.6
 */
@Test
public void testSend10kChinaPropMsg() {

    Message msg = MessageFactory.getRandomMessage(topic);
    msg.putUserProperty("key", RandomUtils.getCheseWord(10 * 1024));
    SendResult sendResult = null;
    try {
        sendResult = producer.send(msg);
    } catch (Exception e) {
    }
    assertThat(sendResult.getSendStatus()).isEqualTo(SendStatus.SEND_OK);
}
 
Example 19
Source File: MessageEncodeDecodeTest.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 5 votes vote down vote up
@Test
public void testEncodeDecodeSingle() throws Exception {
    Message message = new Message("topic", "body".getBytes());
    message.setFlag(12);
    message.putUserProperty("key", "value");
    byte[] bytes = MessageDecoder.encodeMessage(message);
    ByteBuffer buffer = ByteBuffer.allocate(bytes.length);
    buffer.put(bytes);
    buffer.flip();
    Message newMessage = MessageDecoder.decodeMessage(buffer);

    assertTrue(message.getFlag() == newMessage.getFlag());
    assertTrue(newMessage.getProperty("key").equals(newMessage.getProperty("key")));
    assertTrue(Arrays.equals(newMessage.getBody(), message.getBody()));
}
 
Example 20
Source File: ChinaPropIT.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
/**
 * @since version3.4.6
 */
@Test(expected = org.apache.rocketmq.client.exception.MQBrokerException.class)
public void testSend20kChinaPropMsg() throws Exception {
    Message msg = MessageFactory.getRandomMessage(topic);
    msg.putUserProperty("key", RandomUtils.getCheseWord(32 * 1024 + 1));
    producer.send(msg);
}