Search in sources :

Example 41 with DefaultMQProducer

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

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 42 with DefaultMQProducer

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

the class AsyncSendExceptionIT method testSendMQNull.

@Test(expected = java.lang.NullPointerException.class)
public void testSendMQNull() throws Exception {
    Message msg = new Message(topic, RandomUtils.getStringByUUID().getBytes());
    DefaultMQProducer producer = ProducerFactory.getRMQProducer(nsAddr);
    MessageQueue messageQueue = null;
    producer.send(msg, messageQueue, SendCallBackFactory.getSendCallBack());
}
Also used : 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 43 with DefaultMQProducer

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

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 44 with DefaultMQProducer

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

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 45 with DefaultMQProducer

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

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)

Aggregations

DefaultMQProducer (org.apache.rocketmq.client.producer.DefaultMQProducer)90 Message (org.apache.rocketmq.common.message.Message)63 SendResult (org.apache.rocketmq.client.producer.SendResult)36 MQClientException (org.apache.rocketmq.client.exception.MQClientException)31 Test (org.junit.Test)27 MessageQueue (org.apache.rocketmq.common.message.MessageQueue)17 MessageQueueSelector (org.apache.rocketmq.client.producer.MessageQueueSelector)13 SendCallback (org.apache.rocketmq.client.producer.SendCallback)11 ArrayList (java.util.ArrayList)10 MQBrokerException (org.apache.rocketmq.client.exception.MQBrokerException)8 RemotingException (org.apache.rocketmq.remoting.exception.RemotingException)8 SubCommandException (org.apache.rocketmq.tools.command.SubCommandException)8 UnsupportedEncodingException (java.io.UnsupportedEncodingException)7 MQProducer (org.apache.rocketmq.client.producer.MQProducer)5 DefaultMQAdminExt (org.apache.rocketmq.tools.admin.DefaultMQAdminExt)4 CommandLine (org.apache.commons.cli.CommandLine)3 IOException (java.io.IOException)2 BigDecimal (java.math.BigDecimal)2 Date (java.util.Date)2 LinkedList (java.util.LinkedList)2