Search in sources :

Example 11 with MessageQueueSelector

use of org.apache.rocketmq.client.producer.MessageQueueSelector in project rocketmq by apache.

the class AsyncSendExceptionIT method testSendSelectorNull.

@Test(expected = org.apache.rocketmq.client.exception.MQClientException.class)
public void testSendSelectorNull() throws Exception {
    Message msg = new Message(topic, RandomUtils.getStringByUUID().getBytes());
    DefaultMQProducer producer = ProducerFactory.getRMQProducer(nsAddr);
    MessageQueueSelector selector = null;
    producer.send(msg, selector, 100, SendCallBackFactory.getSendCallBack());
}
Also used : MessageQueueSelector(org.apache.rocketmq.client.producer.MessageQueueSelector) Message(org.apache.rocketmq.common.message.Message) DefaultMQProducer(org.apache.rocketmq.client.producer.DefaultMQProducer) Test(org.junit.Test)

Example 12 with MessageQueueSelector

use of org.apache.rocketmq.client.producer.MessageQueueSelector in project rocketmq by apache.

the class OneWaySendWithSelectorIT method testSendWithSelector.

@Test
public void testSendWithSelector() {
    int msgSize = 20;
    final int queueId = 0;
    RMQNormalConsumer consumer = getConsumer(nsAddr, topic, "*", new RMQNormalListener());
    producer.sendOneWay(msgSize, new MessageQueueSelector() {

        @Override
        public MessageQueue select(List<MessageQueue> list, Message message, Object o) {
            for (MessageQueue mq : list) {
                if (mq.getQueueId() == queueId && mq.getBrokerName().equals(broker1Name)) {
                    return mq;
                }
            }
            return list.get(0);
        }
    });
    assertThat(producer.getAllMsgBody().size()).isEqualTo(msgSize);
    consumer.getListener().waitForMessageConsume(producer.getAllMsgBody(), consumeTime);
    assertThat(VerifyUtils.getFilterdMessage(producer.getAllMsgBody(), consumer.getListener().getAllMsgBody())).containsExactlyElementsIn(producer.getAllMsgBody());
    VerifyUtils.verifyMessageQueueId(queueId, consumer.getListener().getAllOriginMsg());
    producer.clearMsg();
    consumer.clearMsg();
    producer.sendOneWay(msgSize, new MessageQueueSelector() {

        @Override
        public MessageQueue select(List<MessageQueue> list, Message message, Object o) {
            for (MessageQueue mq : list) {
                if (mq.getQueueId() == queueId && mq.getBrokerName().equals(broker2Name)) {
                    return mq;
                }
            }
            return list.get(8);
        }
    });
    assertThat(producer.getAllMsgBody().size()).isEqualTo(msgSize);
    consumer.getListener().waitForMessageConsume(producer.getAllMsgBody(), consumeTime);
    assertThat(VerifyUtils.getFilterdMessage(producer.getAllMsgBody(), consumer.getListener().getAllMsgBody())).containsExactlyElementsIn(producer.getAllMsgBody());
    VerifyUtils.verifyMessageQueueId(queueId, consumer.getListener().getAllOriginMsg());
}
Also used : MessageQueueSelector(org.apache.rocketmq.client.producer.MessageQueueSelector) MessageQueue(org.apache.rocketmq.common.message.MessageQueue) Message(org.apache.rocketmq.common.message.Message) RMQNormalConsumer(org.apache.rocketmq.test.client.rmq.RMQNormalConsumer) RMQNormalListener(org.apache.rocketmq.test.listener.rmq.concurrent.RMQNormalListener) Test(org.junit.Test)

Example 13 with MessageQueueSelector

use of org.apache.rocketmq.client.producer.MessageQueueSelector in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.

the class AsyncSendExceptionIT method testSelectorThrowsException.

@Test(expected = org.apache.rocketmq.client.exception.MQClientException.class)
public void testSelectorThrowsException() throws Exception {
    Message msg = new Message(topic, RandomUtils.getStringByUUID().getBytes());
    DefaultMQProducer producer = ProducerFactory.getRMQProducer(nsAddr);
    producer.send(msg, new MessageQueueSelector() {

        @Override
        public MessageQueue select(List<MessageQueue> list, Message message, Object o) {
            String str = null;
            return list.get(str.length());
        }
    }, null, SendCallBackFactory.getSendCallBack());
}
Also used : MessageQueueSelector(org.apache.rocketmq.client.producer.MessageQueueSelector) Message(org.apache.rocketmq.common.message.Message) MessageQueue(org.apache.rocketmq.common.message.MessageQueue) DefaultMQProducer(org.apache.rocketmq.client.producer.DefaultMQProducer) Test(org.junit.Test)

Example 14 with MessageQueueSelector

use of org.apache.rocketmq.client.producer.MessageQueueSelector in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.

the class AsyncSendWithMessageQueueSelectorIT method testSendWithSelector.

@Test
public void testSendWithSelector() {
    int msgSize = 20;
    final int queueId = 0;
    RMQNormalConsumer consumer = getConsumer(nsAddr, topic, "*", new RMQNormalListner());
    producer.asyncSend(msgSize, new MessageQueueSelector() {

        @Override
        public MessageQueue select(List<MessageQueue> list, Message message, Object o) {
            for (MessageQueue mq : list) {
                if (mq.getQueueId() == queueId && mq.getBrokerName().equals(broker1Name)) {
                    return mq;
                }
            }
            return list.get(0);
        }
    });
    producer.waitForResponse(5 * 1000);
    assertThat(producer.getSuccessMsgCount()).isEqualTo(msgSize);
    consumer.getListner().waitForMessageConsume(producer.getAllMsgBody(), consumeTime);
    assertThat(VerifyUtils.getFilterdMessage(producer.getAllMsgBody(), consumer.getListner().getAllMsgBody())).containsExactlyElementsIn(producer.getAllMsgBody());
    VerifyUtils.verifyMessageQueueId(queueId, consumer.getListner().getAllOriginMsg());
    producer.clearMsg();
    consumer.clearMsg();
    producer.getSuccessSendResult().clear();
    producer.asyncSend(msgSize, new MessageQueueSelector() {

        @Override
        public MessageQueue select(List<MessageQueue> list, Message message, Object o) {
            for (MessageQueue mq : list) {
                if (mq.getQueueId() == queueId && mq.getBrokerName().equals(broker2Name)) {
                    return mq;
                }
            }
            return list.get(8);
        }
    });
    producer.waitForResponse(5 * 1000);
    assertThat(producer.getSuccessMsgCount()).isEqualTo(msgSize);
    consumer.getListner().waitForMessageConsume(producer.getAllMsgBody(), consumeTime);
    assertThat(VerifyUtils.getFilterdMessage(producer.getAllMsgBody(), consumer.getListner().getAllMsgBody())).containsExactlyElementsIn(producer.getAllMsgBody());
    VerifyUtils.verifyMessageQueueId(queueId, consumer.getListner().getAllOriginMsg());
}
Also used : RMQNormalListner(org.apache.rocketmq.test.listener.rmq.concurrent.RMQNormalListner) MessageQueueSelector(org.apache.rocketmq.client.producer.MessageQueueSelector) MessageQueue(org.apache.rocketmq.common.message.MessageQueue) Message(org.apache.rocketmq.common.message.Message) RMQNormalConsumer(org.apache.rocketmq.test.client.rmq.RMQNormalConsumer) Test(org.junit.Test)

Example 15 with MessageQueueSelector

use of org.apache.rocketmq.client.producer.MessageQueueSelector in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.

the class OneWaySendWithSelectorIT method testSendWithSelector.

@Test
public void testSendWithSelector() {
    int msgSize = 20;
    final int queueId = 0;
    RMQNormalConsumer consumer = getConsumer(nsAddr, topic, "*", new RMQNormalListner());
    producer.sendOneWay(msgSize, new MessageQueueSelector() {

        @Override
        public MessageQueue select(List<MessageQueue> list, Message message, Object o) {
            for (MessageQueue mq : list) {
                if (mq.getQueueId() == queueId && mq.getBrokerName().equals(broker1Name)) {
                    return mq;
                }
            }
            return list.get(0);
        }
    });
    assertThat(producer.getAllMsgBody().size()).isEqualTo(msgSize);
    consumer.getListner().waitForMessageConsume(producer.getAllMsgBody(), consumeTime);
    assertThat(VerifyUtils.getFilterdMessage(producer.getAllMsgBody(), consumer.getListner().getAllMsgBody())).containsExactlyElementsIn(producer.getAllMsgBody());
    VerifyUtils.verifyMessageQueueId(queueId, consumer.getListner().getAllOriginMsg());
    producer.clearMsg();
    consumer.clearMsg();
    producer.sendOneWay(msgSize, new MessageQueueSelector() {

        @Override
        public MessageQueue select(List<MessageQueue> list, Message message, Object o) {
            for (MessageQueue mq : list) {
                if (mq.getQueueId() == queueId && mq.getBrokerName().equals(broker2Name)) {
                    return mq;
                }
            }
            return list.get(8);
        }
    });
    assertThat(producer.getAllMsgBody().size()).isEqualTo(msgSize);
    consumer.getListner().waitForMessageConsume(producer.getAllMsgBody(), consumeTime);
    assertThat(VerifyUtils.getFilterdMessage(producer.getAllMsgBody(), consumer.getListner().getAllMsgBody())).containsExactlyElementsIn(producer.getAllMsgBody());
    VerifyUtils.verifyMessageQueueId(queueId, consumer.getListner().getAllOriginMsg());
}
Also used : RMQNormalListner(org.apache.rocketmq.test.listener.rmq.concurrent.RMQNormalListner) MessageQueueSelector(org.apache.rocketmq.client.producer.MessageQueueSelector) MessageQueue(org.apache.rocketmq.common.message.MessageQueue) Message(org.apache.rocketmq.common.message.Message) RMQNormalConsumer(org.apache.rocketmq.test.client.rmq.RMQNormalConsumer) Test(org.junit.Test)

Aggregations

MessageQueueSelector (org.apache.rocketmq.client.producer.MessageQueueSelector)17 Message (org.apache.rocketmq.common.message.Message)17 DefaultMQProducer (org.apache.rocketmq.client.producer.DefaultMQProducer)13 MessageQueue (org.apache.rocketmq.common.message.MessageQueue)13 Test (org.junit.Test)12 RMQNormalConsumer (org.apache.rocketmq.test.client.rmq.RMQNormalConsumer)4 SendResult (org.apache.rocketmq.client.producer.SendResult)3 MQBrokerException (org.apache.rocketmq.client.exception.MQBrokerException)2 MQClientException (org.apache.rocketmq.client.exception.MQClientException)2 MQProducer (org.apache.rocketmq.client.producer.MQProducer)2 RemotingException (org.apache.rocketmq.remoting.exception.RemotingException)2 RMQNormalListener (org.apache.rocketmq.test.listener.rmq.concurrent.RMQNormalListener)2 RMQNormalListner (org.apache.rocketmq.test.listener.rmq.concurrent.RMQNormalListner)2 SubCommandException (org.apache.rocketmq.tools.command.SubCommandException)2