Search in sources :

Example 81 with DefaultMQProducer

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

the class AsyncSendExceptionIT method testQueueIdBigThanQueueNum.

@Test
public void testQueueIdBigThanQueueNum() throws Exception {
    int queueId = 100;
    sendFail = false;
    MessageQueue mq = new MessageQueue(topic, broker1Name, queueId);
    Message msg = new Message(topic, RandomUtils.getStringByUUID().getBytes());
    DefaultMQProducer producer = ProducerFactory.getRMQProducer(nsAddr);
    producer.send(msg, mq, new SendCallback() {

        @Override
        public void onSuccess(SendResult sendResult) {
        }

        @Override
        public void onException(Throwable throwable) {
            sendFail = true;
        }
    });
    int checkNum = 50;
    while (!sendFail && checkNum > 0) {
        checkNum--;
        TestUtils.waitForMoment(100);
    }
    producer.shutdown();
    assertThat(sendFail).isEqualTo(true);
}
Also used : MessageQueue(org.apache.rocketmq.common.message.MessageQueue) Message(org.apache.rocketmq.common.message.Message) SendResult(org.apache.rocketmq.client.producer.SendResult) DefaultMQProducer(org.apache.rocketmq.client.producer.DefaultMQProducer) SendCallback(org.apache.rocketmq.client.producer.SendCallback) Test(org.junit.Test)

Example 82 with DefaultMQProducer

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

the class SendMsgStatusCommand method execute.

@Override
public void execute(CommandLine commandLine, Options options, RPCHook rpcHook) throws SubCommandException {
    final DefaultMQProducer producer = new DefaultMQProducer("PID_SMSC", rpcHook);
    producer.setInstanceName("PID_SMSC_" + System.currentTimeMillis());
    try {
        producer.start();
        String brokerName = commandLine.getOptionValue('b').trim();
        int messageSize = commandLine.hasOption('s') ? Integer.parseInt(commandLine.getOptionValue('s')) : 128;
        int count = commandLine.hasOption('c') ? Integer.parseInt(commandLine.getOptionValue('c')) : 50;
        producer.send(buildMessage(brokerName, 16));
        for (int i = 0; i < count; i++) {
            long begin = System.currentTimeMillis();
            SendResult result = producer.send(buildMessage(brokerName, messageSize));
            System.out.printf("rt:" + (System.currentTimeMillis() - begin) + "ms, SendResult=" + result);
        }
    } catch (Exception e) {
        throw new SubCommandException(this.getClass().getSimpleName() + " command failed", e);
    } finally {
        producer.shutdown();
    }
}
Also used : SubCommandException(org.apache.rocketmq.tools.command.SubCommandException) SendResult(org.apache.rocketmq.client.producer.SendResult) DefaultMQProducer(org.apache.rocketmq.client.producer.DefaultMQProducer) SubCommandException(org.apache.rocketmq.tools.command.SubCommandException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 83 with DefaultMQProducer

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

the class CLusterSendMsgRTCommand method execute.

@Override
public void execute(CommandLine commandLine, Options options, RPCHook rpcHook) throws SubCommandException {
    DefaultMQAdminExt defaultMQAdminExt = new DefaultMQAdminExt(rpcHook);
    defaultMQAdminExt.setInstanceName(Long.toString(System.currentTimeMillis()));
    DefaultMQProducer producer = new DefaultMQProducer(rpcHook);
    producer.setProducerGroup(Long.toString(System.currentTimeMillis()));
    try {
        defaultMQAdminExt.start();
        producer.start();
        ClusterInfo clusterInfoSerializeWrapper = defaultMQAdminExt.examineBrokerClusterInfo();
        HashMap<String, Set<String>> clusterAddr = clusterInfoSerializeWrapper.getClusterAddrTable();
        Set<String> clusterNames = null;
        long amount = !commandLine.hasOption('a') ? 50 : Long.parseLong(commandLine.getOptionValue('a').trim());
        long size = !commandLine.hasOption('s') ? 128 : Long.parseLong(commandLine.getOptionValue('s').trim());
        long interval = !commandLine.hasOption('i') ? 10 : Long.parseLong(commandLine.getOptionValue('i').trim());
        boolean printAsTlog = commandLine.hasOption('p') && Boolean.parseBoolean(commandLine.getOptionValue('p').trim());
        String machineRoom = !commandLine.hasOption('m') ? "noname" : commandLine.getOptionValue('m').trim();
        if (commandLine.hasOption('c')) {
            clusterNames = new TreeSet<String>();
            clusterNames.add(commandLine.getOptionValue('c').trim());
        } else {
            clusterNames = clusterAddr.keySet();
        }
        if (!printAsTlog) {
            System.out.printf("%-24s  %-24s  %-4s  %-8s  %-8s%n", "#Cluster Name", "#Broker Name", "#RT", "#successCount", "#failCount");
        }
        while (true) {
            for (String clusterName : clusterNames) {
                Set<String> brokerNames = clusterAddr.get(clusterName);
                if (brokerNames == null) {
                    System.out.printf("cluster [%s] not exist", clusterName);
                    break;
                }
                for (String brokerName : brokerNames) {
                    Message msg = new Message(brokerName, getStringBySize(size).getBytes(MixAll.DEFAULT_CHARSET));
                    long start = 0;
                    long end = 0;
                    long elapsed = 0;
                    int successCount = 0;
                    int failCount = 0;
                    for (int i = 0; i < amount; i++) {
                        start = System.currentTimeMillis();
                        try {
                            producer.send(msg);
                            successCount++;
                            end = System.currentTimeMillis();
                        } catch (Exception e) {
                            failCount++;
                            end = System.currentTimeMillis();
                        }
                        if (i != 0) {
                            elapsed += end - start;
                        }
                    }
                    double rt = (double) elapsed / (amount - 1);
                    if (!printAsTlog) {
                        System.out.printf("%-24s  %-24s  %-8s  %-16s  %-16s%n", clusterName, brokerName, String.format("%.2f", rt), successCount, failCount);
                    } else {
                        System.out.printf(String.format("%s|%s|%s|%s|%s%n", getCurTime(), machineRoom, clusterName, brokerName, new BigDecimal(rt).setScale(0, BigDecimal.ROUND_HALF_UP)));
                    }
                }
            }
            Thread.sleep(interval * 1000);
        }
    } catch (Exception e) {
        throw new SubCommandException(this.getClass().getSimpleName() + " command failed", e);
    } finally {
        defaultMQAdminExt.shutdown();
        producer.shutdown();
    }
}
Also used : Set(java.util.Set) TreeSet(java.util.TreeSet) 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) BigDecimal(java.math.BigDecimal) ClusterInfo(org.apache.rocketmq.common.protocol.body.ClusterInfo) DefaultMQAdminExt(org.apache.rocketmq.tools.admin.DefaultMQAdminExt)

Example 84 with DefaultMQProducer

use of org.apache.rocketmq.client.producer.DefaultMQProducer 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 85 with DefaultMQProducer

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

the class QueryMsgByIdSubCommand method execute.

@Override
public void execute(CommandLine commandLine, Options options, RPCHook rpcHook) throws SubCommandException {
    DefaultMQAdminExt defaultMQAdminExt = new DefaultMQAdminExt(rpcHook);
    defaultMQAdminExt.setInstanceName(Long.toString(System.currentTimeMillis()));
    DefaultMQProducer defaultMQProducer = new DefaultMQProducer("ReSendMsgById");
    defaultMQProducer.setInstanceName(Long.toString(System.currentTimeMillis()));
    try {
        defaultMQAdminExt.start();
        if (commandLine.hasOption('s')) {
            if (commandLine.hasOption('u')) {
                String unitName = commandLine.getOptionValue('u').trim();
                defaultMQProducer.setUnitName(unitName);
            }
            defaultMQProducer.start();
        }
        final String msgIds = commandLine.getOptionValue('i').trim();
        final String[] msgIdArr = StringUtils.split(msgIds, ",");
        if (commandLine.hasOption('g') && commandLine.hasOption('d')) {
            final String consumerGroup = commandLine.getOptionValue('g').trim();
            final String clientId = commandLine.getOptionValue('d').trim();
            for (String msgId : msgIdArr) {
                if (StringUtils.isNotBlank(msgId)) {
                    pushMsg(defaultMQAdminExt, consumerGroup, clientId, msgId.trim());
                }
            }
        } else if (commandLine.hasOption('s')) {
            boolean resend = Boolean.parseBoolean(commandLine.getOptionValue('s', "false").trim());
            if (resend) {
                for (String msgId : msgIdArr) {
                    if (StringUtils.isNotBlank(msgId)) {
                        sendMsg(defaultMQAdminExt, defaultMQProducer, msgId.trim());
                    }
                }
            }
        } else {
            for (String msgId : msgIdArr) {
                if (StringUtils.isNotBlank(msgId)) {
                    queryById(defaultMQAdminExt, msgId.trim());
                }
            }
        }
    } catch (Exception e) {
        throw new SubCommandException(this.getClass().getSimpleName() + " command failed", e);
    } finally {
        defaultMQProducer.shutdown();
        defaultMQAdminExt.shutdown();
    }
}
Also used : SubCommandException(org.apache.rocketmq.tools.command.SubCommandException) DefaultMQAdminExt(org.apache.rocketmq.tools.admin.DefaultMQAdminExt) DefaultMQProducer(org.apache.rocketmq.client.producer.DefaultMQProducer) IOException(java.io.IOException) SubCommandException(org.apache.rocketmq.tools.command.SubCommandException) MQClientException(org.apache.rocketmq.client.exception.MQClientException) MQBrokerException(org.apache.rocketmq.client.exception.MQBrokerException) RemotingException(org.apache.rocketmq.remoting.exception.RemotingException)

Aggregations

DefaultMQProducer (org.apache.rocketmq.client.producer.DefaultMQProducer)100 Message (org.apache.rocketmq.common.message.Message)68 SendResult (org.apache.rocketmq.client.producer.SendResult)41 MQClientException (org.apache.rocketmq.client.exception.MQClientException)31 Test (org.junit.Test)28 MessageQueue (org.apache.rocketmq.common.message.MessageQueue)17 MessageQueueSelector (org.apache.rocketmq.client.producer.MessageQueueSelector)13 ArrayList (java.util.ArrayList)12 SendCallback (org.apache.rocketmq.client.producer.SendCallback)11 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 InvocationOnMock (org.mockito.invocation.InvocationOnMock)4 Date (java.util.Date)3 CommandLine (org.apache.commons.cli.CommandLine)3 IOException (java.io.IOException)2 Field (java.lang.reflect.Field)2