Search in sources :

Example 6 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 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.sendOneway(msg, new MessageQueueSelector() {

        @Override
        public MessageQueue select(List<MessageQueue> list, Message message, Object o) {
            String str = null;
            return list.get(str.length());
        }
    }, null);
}
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 7 with MessageQueueSelector

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

the class CheckMsgSendRTCommand method execute.

@Override
public void execute(CommandLine commandLine, Options options, RPCHook rpcHook) throws SubCommandException {
    DefaultMQProducer producer = new DefaultMQProducer(rpcHook);
    producer.setProducerGroup(Long.toString(System.currentTimeMillis()));
    try {
        producer.start();
        long start = 0;
        long end = 0;
        long timeElapsed = 0;
        boolean sendSuccess = false;
        String topic = commandLine.getOptionValue('t').trim();
        long amount = !commandLine.hasOption('a') ? 100 : Long.parseLong(commandLine.getOptionValue('a').trim());
        long msgSize = !commandLine.hasOption('s') ? 128 : Long.parseLong(commandLine.getOptionValue('s').trim());
        Message msg = new Message(topic, getStringBySize(msgSize).getBytes(MixAll.DEFAULT_CHARSET));
        System.out.printf("%-32s  %-4s  %-20s    %s%n", "#Broker Name", "#QID", "#Send Result", "#RT");
        for (int i = 0; i < amount; i++) {
            start = System.currentTimeMillis();
            try {
                producer.send(msg, new MessageQueueSelector() {

                    @Override
                    public MessageQueue select(List<MessageQueue> mqs, Message msg, Object arg) {
                        int queueIndex = (Integer) arg % mqs.size();
                        MessageQueue queue = mqs.get(queueIndex);
                        brokerName = queue.getBrokerName();
                        queueId = queue.getQueueId();
                        return queue;
                    }
                }, i);
                sendSuccess = true;
                end = System.currentTimeMillis();
            } catch (Exception e) {
                sendSuccess = false;
                end = System.currentTimeMillis();
            }
            if (i != 0) {
                timeElapsed += end - start;
            }
            System.out.printf("%-32s  %-4s  %-20s    %s%n", brokerName, queueId, sendSuccess, end - start);
        }
        double rt = (double) timeElapsed / (amount - 1);
        System.out.printf("Avg RT: %s%n", String.format("%.2f", rt));
    } catch (Exception e) {
        throw new SubCommandException(this.getClass().getSimpleName() + " command failed", e);
    } finally {
        producer.shutdown();
    }
}
Also used : Message(org.apache.rocketmq.common.message.Message) SubCommandException(org.apache.rocketmq.tools.command.SubCommandException) DefaultMQProducer(org.apache.rocketmq.client.producer.DefaultMQProducer) SubCommandException(org.apache.rocketmq.tools.command.SubCommandException) MessageQueueSelector(org.apache.rocketmq.client.producer.MessageQueueSelector) MessageQueue(org.apache.rocketmq.common.message.MessageQueue)

Example 8 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 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 9 with MessageQueueSelector

use of org.apache.rocketmq.client.producer.MessageQueueSelector in project spring-boot-starter-samples by vindell.

the class OrderedProducer method main.

public static void main(String[] args) throws Exception {
    // Instantiate with a producer group name.
    MQProducer producer = new DefaultMQProducer("example_group_name");
    // Launch the instance.
    producer.start();
    String[] tags = new String[] { "TagA", "TagB", "TagC", "TagD", "TagE" };
    for (int i = 0; i < 100; i++) {
        int orderId = i % 10;
        // Create a message instance, specifying topic, tag and message body.
        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);
    }
    // server shutdown
    producer.shutdown();
}
Also used : Message(org.apache.rocketmq.common.message.Message) DefaultMQProducer(org.apache.rocketmq.client.producer.DefaultMQProducer) MQProducer(org.apache.rocketmq.client.producer.MQProducer) 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)

Example 10 with MessageQueueSelector

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

the class Producer method main.

public static void main(String[] args) throws UnsupportedEncodingException {
    try {
        MQProducer producer = new DefaultMQProducer("please_rename_unique_group_name");
        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) MQProducer(org.apache.rocketmq.client.producer.MQProducer) 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