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

The following examples show how to use org.apache.rocketmq.common.message.Message#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: 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: 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 3
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 4
Source File: DefaultMQProducerTest.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
@Test
public void testSendMessageAsync() throws RemotingException, MQClientException, InterruptedException {
    final AtomicInteger cc = new AtomicInteger(0);
    final CountDownLatch countDownLatch = new CountDownLatch(6);

    when(mQClientAPIImpl.getTopicRouteInfoFromNameServer(anyString(), anyLong())).thenReturn(createTopicRoute());
    SendCallback sendCallback = new SendCallback() {
        @Override
        public void onSuccess(SendResult sendResult) {
            countDownLatch.countDown();
        }

        @Override
        public void onException(Throwable e) {
            e.printStackTrace();
            cc.incrementAndGet();
            countDownLatch.countDown();
        }
    };
    MessageQueueSelector messageQueueSelector = new MessageQueueSelector() {
        @Override
        public MessageQueue select(List<MessageQueue> mqs, Message msg, Object arg) {
            return null;
        }
    };

    Message message = new Message();
    message.setTopic("test");
    message.setBody("hello world".getBytes());
    producer.send(new Message(), sendCallback);
    producer.send(message, new MessageQueue(), sendCallback);
    producer.send(new Message(), new MessageQueue(), sendCallback, 1000);
    producer.send(new Message(), messageQueueSelector, null, sendCallback);
    producer.send(message, messageQueueSelector, null, sendCallback, 1000);
    //this message is send success
    producer.send(message, sendCallback, 1000);

    countDownLatch.await(3000L, TimeUnit.MILLISECONDS);
    assertThat(cc.get()).isEqualTo(5);
}
 
Example 5
Source File: TransactionProducer.java    From rocketmq-read 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 6
Source File: AppendCallbackTest.java    From rocketmq-read with Apache License 2.0 5 votes vote down vote up
@Test
public void testAppendMessageBatchEndOfFile() 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);
    //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 7
Source File: TransactionProducer.java    From rocketmq-4.3.0 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 8
Source File: AppendCallbackTest.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
@Test
public void testAppendMessageBatchEndOfFile() 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);
    //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 9
Source File: TransactionProducer.java    From rocketmq_trans_message 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 10
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 11
Source File: BatchSendIT.java    From rocketmq-4.3.0 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 12
Source File: BatchPutMessageTest.java    From rocketmq with Apache License 2.0 4 votes vote down vote up
@Test
public void testPutMessages() throws Exception {
    List<Message> messages = new ArrayList<>();
    String topic = "batch-write-topic";
    int queue = 0;
    int[] msgLengthArr = new int[11];
    msgLengthArr[0] = 0;
    int j = 1;
    for (int i = 0; i < 10; i++) {
        Message msg = new Message();
        msg.setBody(("body" + i).getBytes());
        msg.setTopic(topic);
        msg.setTags("TAG1");
        msg.setKeys(String.valueOf(System.currentTimeMillis()));
        messages.add(msg);
        String properties = messageProperties2String(msg.getProperties());
        byte[] propertiesBytes = properties.getBytes(CHARSET_UTF8);
        short propertiesLength = (short) propertiesBytes.length;
        final byte[] topicData = msg.getTopic().getBytes(MessageDecoder.CHARSET_UTF8);
        final int topicLength = topicData.length;
        msgLengthArr[j] = calMsgLength(msg.getBody().length, topicLength, propertiesLength) + msgLengthArr[j - 1];
        j++;
    }
    byte[] batchMessageBody = MessageDecoder.encodeMessages(messages);
    MessageExtBatch messageExtBatch = new MessageExtBatch();
    messageExtBatch.setTopic(topic);
    messageExtBatch.setQueueId(queue);
    messageExtBatch.setBody(batchMessageBody);
    messageExtBatch.setBornTimestamp(System.currentTimeMillis());
    messageExtBatch.setStoreHost(new InetSocketAddress("127.0.0.1", 125));
    messageExtBatch.setBornHost(new InetSocketAddress("127.0.0.1", 126));

    PutMessageResult putMessageResult = messageStore.putMessages(messageExtBatch);
    assertThat(putMessageResult.isOk()).isTrue();
    
    Thread.sleep(3 * 1000);

    for (long i = 0; i < 10; i++) {
        MessageExt messageExt = messageStore.lookMessageByOffset(msgLengthArr[(int) i]);
        assertThat(messageExt).isNotNull();
        GetMessageResult result = messageStore.getMessage("batch_write_group", topic, queue, i, 1024 * 1024, null);
        assertThat(result).isNotNull();
        assertThat(result.getStatus()).isEqualTo(GetMessageStatus.FOUND);
        result.release();
    }

}
 
Example 13
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 14
Source File: AppendCallbackTest.java    From rocketmq with Apache License 2.0 4 votes vote down vote up
@Test
public void testAppendIPv6HostMessageBatchSucc() 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);
    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(56, 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 15
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());
    }

}
 
Example 16
Source File: DLedgerProduceAndConsumeIT.java    From rocketmq with Apache License 2.0 4 votes vote down vote up
@Test
public void testProduceAndConsume() throws Exception {
    String cluster = UUID.randomUUID().toString();
    String brokerName = UUID.randomUUID().toString();
    String selfId = "n0";
    String peers = String.format("n0-localhost:%d", nextPort());
    BrokerConfig brokerConfig = buildBrokerConfig(cluster, brokerName);
    MessageStoreConfig storeConfig = buildStoreConfig(brokerName, peers, selfId);
    BrokerController brokerController = IntegrationTestBase.createAndStartBroker(storeConfig, brokerConfig);
    Thread.sleep(3000);

    Assert.assertEquals(BrokerRole.SYNC_MASTER, storeConfig.getBrokerRole());


    String topic = UUID.randomUUID().toString();
    String consumerGroup = UUID.randomUUID().toString();
    IntegrationTestBase.initTopic(topic, BaseConf.nsAddr, cluster, 1);
    DefaultMQProducer producer = ProducerFactory.getRMQProducer(BaseConf.nsAddr);
    DefaultMQPullConsumer consumer = ConsumerFactory.getRMQPullConsumer(BaseConf.nsAddr, consumerGroup);

    for (int i = 0; i < 10; i++) {
        Message message = new Message();
        message.setTopic(topic);
        message.setBody(("Hello" + i).getBytes());
        SendResult sendResult = producer.send(message);
        Assert.assertEquals(SendStatus.SEND_OK, sendResult.getSendStatus());
        Assert.assertEquals(0, sendResult.getMessageQueue().getQueueId());
        Assert.assertEquals(brokerName, sendResult.getMessageQueue().getBrokerName());
        Assert.assertEquals(i, sendResult.getQueueOffset());
        Assert.assertNotNull(sendResult.getMsgId());
        Assert.assertNotNull(sendResult.getOffsetMsgId());
    }

    Thread.sleep(500);
    Assert.assertEquals(0, brokerController.getMessageStore().getMinOffsetInQueue(topic, 0));
    Assert.assertEquals(10, brokerController.getMessageStore().getMaxOffsetInQueue(topic, 0));

    MessageQueue messageQueue = new MessageQueue(topic, brokerName, 0);
    PullResult pullResult= consumer.pull(messageQueue, "*", 0, 32);
    Assert.assertEquals(PullStatus.FOUND, pullResult.getPullStatus());
    Assert.assertEquals(10, pullResult.getMsgFoundList().size());

    for (int i = 0; i < 10; i++) {
        MessageExt messageExt = pullResult.getMsgFoundList().get(i);
        Assert.assertEquals(i, messageExt.getQueueOffset());
        Assert.assertArrayEquals(("Hello" + i).getBytes(), messageExt.getBody());
    }

    producer.shutdown();
    consumer.shutdown();
    brokerController.shutdown();
}
 
Example 17
Source File: DefaultMQProducer.java    From rocketmq with Apache License 2.0 3 votes vote down vote up
/**
 * Same to {@link #sendOneway(Message)} with message queue selector specified.
 *
 * @param msg Message to send.
 * @param selector Message queue selector, through which to determine target message queue to deliver message
 * @param arg Argument used along with message queue selector.
 * @throws MQClientException if there is any client error.
 * @throws RemotingException if there is any network-tier error.
 * @throws InterruptedException if the sending thread is interrupted.
 */
@Override
public void sendOneway(Message msg, MessageQueueSelector selector, Object arg)
    throws MQClientException, RemotingException, InterruptedException {
    msg.setTopic(withNamespace(msg.getTopic()));
    this.defaultMQProducerImpl.sendOneway(msg, selector, arg);
}
 
Example 18
Source File: DefaultMQProducer.java    From rocketmq with Apache License 2.0 3 votes vote down vote up
/**
 * Same to {@link #send(Message, SendCallback)} with send timeout specified in addition.
 *
 * @param msg message to send.
 * @param sendCallback Callback to execute.
 * @param timeout send timeout.
 * @throws MQClientException if there is any client error.
 * @throws RemotingException if there is any network-tier error.
 * @throws InterruptedException if the sending thread is interrupted.
 */
@Override
public void send(Message msg, SendCallback sendCallback, long timeout)
    throws MQClientException, RemotingException, InterruptedException {
    msg.setTopic(withNamespace(msg.getTopic()));
    this.defaultMQProducerImpl.send(msg, sendCallback, timeout);
}
 
Example 19
Source File: DefaultMQProducer.java    From rocketmq with Apache License 2.0 3 votes vote down vote up
/**
 * Same to {@link #request(Message, long)}  with target message queue specified in addition.
 *
 * @param msg request message to send
 * @param mq target message queue.
 * @param timeout request timeout
 * @throws MQClientException if there is any client error.
 * @throws RemotingException if there is any network-tier error.
 * @throws MQBrokerException if there is any broker error.
 * @throws InterruptedException if the thread is interrupted.
 * @throws RequestTimeoutException if request timeout.
 */
@Override
public Message request(final Message msg, final MessageQueue mq, final long timeout)
    throws MQClientException, RemotingException, MQBrokerException, InterruptedException, RequestTimeoutException {
    msg.setTopic(withNamespace(msg.getTopic()));
    return this.defaultMQProducerImpl.request(msg, mq, timeout);
}
 
Example 20
Source File: DefaultMQProducer.java    From rocketmq with Apache License 2.0 2 votes vote down vote up
/**
 * Similar to <a href="https://en.wikipedia.org/wiki/User_Datagram_Protocol">UDP</a>, this method won't wait for
 * acknowledgement from broker before return. Obviously, it has maximums throughput yet potentials of message loss.
 *
 * @param msg Message to send.
 * @throws MQClientException if there is any client error.
 * @throws RemotingException if there is any network-tier error.
 * @throws InterruptedException if the sending thread is interrupted.
 */
@Override
public void sendOneway(Message msg) throws MQClientException, RemotingException, InterruptedException {
    msg.setTopic(withNamespace(msg.getTopic()));
    this.defaultMQProducerImpl.sendOneway(msg);
}