Search in sources :

Example 16 with MessageQueueSelector

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

the class OneWaySendExceptionIT 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.sendOneway(msg, selector, 100);
}
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 17 with MessageQueueSelector

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

the class Producer method main.

public static void main(String[] args) throws UnsupportedEncodingException {
    try {
        DefaultMQProducer producer = new DefaultMQProducer("please_rename_unique_group_name");
        // 指定NameServer地址
        producer.setNamesrvAddr("127.0.0.1:9876");
        producer.start();
        String[] tags = new String[] { "TagA", "TagB", "TagC", "TagD", "TagE" };
        for (int i = 0; i < 100; i++) {
            int orderId = i % 10;
            Message msg = new Message("TopicTestjjj", tags[i % tags.length], "KEY" + i, ("Hello RocketMQ " + i).getBytes(RemotingHelper.DEFAULT_CHARSET));
            SendResult sendResult = producer.send(msg, new MessageQueueSelector() {

                @Override
                public MessageQueue select(List<MessageQueue> mqs, Message msg, Object arg) {
                    Integer id = (Integer) arg;
                    int index = id % mqs.size();
                    return mqs.get(index);
                }
            }, orderId);
            System.out.printf("%s%n", sendResult);
        }
        producer.shutdown();
    } catch (MQClientException | RemotingException | MQBrokerException | InterruptedException e) {
        e.printStackTrace();
    }
}
Also used : Message(org.apache.rocketmq.common.message.Message) MQBrokerException(org.apache.rocketmq.client.exception.MQBrokerException) DefaultMQProducer(org.apache.rocketmq.client.producer.DefaultMQProducer) MessageQueueSelector(org.apache.rocketmq.client.producer.MessageQueueSelector) MessageQueue(org.apache.rocketmq.common.message.MessageQueue) SendResult(org.apache.rocketmq.client.producer.SendResult) RemotingException(org.apache.rocketmq.remoting.exception.RemotingException) MQClientException(org.apache.rocketmq.client.exception.MQClientException)

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