Search in sources :

Example 96 with MQBrokerException

use of org.apache.rocketmq.client.exception.MQBrokerException 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)

Example 97 with MQBrokerException

use of org.apache.rocketmq.client.exception.MQBrokerException in project rocketmq by apache.

the class StatsBenchmarkProducer method main.

public static void main(String[] args) throws MQClientException, UnsupportedEncodingException {
    Options options = ServerUtil.buildCommandlineOptions(new Options());
    CommandLine commandLine = ServerUtil.parseCmdLine("benchmarkProducer", args, buildCommandlineOptions(options), new PosixParser());
    if (null == commandLine) {
        System.exit(-1);
    }
    final String topic = commandLine.hasOption('t') ? commandLine.getOptionValue('t').trim() : "BenchmarkTest";
    final int threadCount = commandLine.hasOption('w') ? Integer.parseInt(commandLine.getOptionValue('w')) : 64;
    final int messageSize = commandLine.hasOption('s') ? Integer.parseInt(commandLine.getOptionValue('s')) : 128;
    final boolean keyEnable = commandLine.hasOption('k') && Boolean.parseBoolean(commandLine.getOptionValue('k'));
    final int propertySize = commandLine.hasOption('p') ? Integer.parseInt(commandLine.getOptionValue('p')) : 0;
    System.out.printf("topic %s threadCount %d messageSize %d keyEnable %s%n", topic, threadCount, messageSize, keyEnable);
    final Logger log = ClientLogger.getLog();
    final ExecutorService sendThreadPool = Executors.newFixedThreadPool(threadCount);
    final StatsBenchmarkProducer statsBenchmark = new StatsBenchmarkProducer();
    final Timer timer = new Timer("BenchmarkTimerThread", true);
    final LinkedList<Long[]> snapshotList = new LinkedList<Long[]>();
    timer.scheduleAtFixedRate(new TimerTask() {

        @Override
        public void run() {
            snapshotList.addLast(statsBenchmark.createSnapshot());
            if (snapshotList.size() > 10) {
                snapshotList.removeFirst();
            }
        }
    }, 1000, 1000);
    timer.scheduleAtFixedRate(new TimerTask() {

        private void printStats() {
            if (snapshotList.size() >= 10) {
                Long[] begin = snapshotList.getFirst();
                Long[] end = snapshotList.getLast();
                final long sendTps = (long) (((end[3] - begin[3]) / (double) (end[0] - begin[0])) * 1000L);
                final double averageRT = (end[5] - begin[5]) / (double) (end[3] - begin[3]);
                System.out.printf("Send TPS: %d Max RT: %d Average RT: %7.3f Send Failed: %d Response Failed: %d%n", sendTps, statsBenchmark.getSendMessageMaxRT().get(), averageRT, end[2], end[4]);
            }
        }

        @Override
        public void run() {
            try {
                this.printStats();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }, 10000, 10000);
    final DefaultMQProducer producer = new DefaultMQProducer("benchmark_producer");
    producer.setInstanceName(Long.toString(System.currentTimeMillis()));
    if (commandLine.hasOption('n')) {
        String ns = commandLine.getOptionValue('n');
        producer.setNamesrvAddr(ns);
    }
    producer.setCompressMsgBodyOverHowmuch(Integer.MAX_VALUE);
    producer.start();
    for (int i = 0; i < threadCount; i++) {
        sendThreadPool.execute(new Runnable() {

            @Override
            public void run() {
                while (true) {
                    try {
                        final Message msg;
                        try {
                            msg = buildMessage(messageSize, topic);
                        } catch (UnsupportedEncodingException e) {
                            e.printStackTrace();
                            return;
                        }
                        final long beginTimestamp = System.currentTimeMillis();
                        if (keyEnable) {
                            msg.setKeys(String.valueOf(beginTimestamp / 1000));
                        }
                        if (propertySize > 0) {
                            if (msg.getProperties() != null) {
                                msg.getProperties().clear();
                            }
                            int i = 0;
                            int startValue = (new Random(System.currentTimeMillis())).nextInt(100);
                            int size = 0;
                            while (true) {
                                String prop1 = "prop" + i, prop1V = "hello" + startValue;
                                String prop2 = "prop" + (i + 1), prop2V = String.valueOf(startValue);
                                msg.putUserProperty(prop1, prop1V);
                                msg.putUserProperty(prop2, prop2V);
                                size += prop1.length() + prop2.length() + prop1V.length() + prop2V.length();
                                if (size > propertySize) {
                                    break;
                                }
                                i += 2;
                                startValue += 2;
                            }
                        }
                        producer.send(msg);
                        statsBenchmark.getSendRequestSuccessCount().incrementAndGet();
                        statsBenchmark.getReceiveResponseSuccessCount().incrementAndGet();
                        final long currentRT = System.currentTimeMillis() - beginTimestamp;
                        statsBenchmark.getSendMessageSuccessTimeTotal().addAndGet(currentRT);
                        long prevMaxRT = statsBenchmark.getSendMessageMaxRT().get();
                        while (currentRT > prevMaxRT) {
                            boolean updated = statsBenchmark.getSendMessageMaxRT().compareAndSet(prevMaxRT, currentRT);
                            if (updated)
                                break;
                            prevMaxRT = statsBenchmark.getSendMessageMaxRT().get();
                        }
                    } catch (RemotingException e) {
                        statsBenchmark.getSendRequestFailedCount().incrementAndGet();
                        log.error("[BENCHMARK_PRODUCER] Send Exception", e);
                        try {
                            Thread.sleep(3000);
                        } catch (InterruptedException ignored) {
                        }
                    } catch (InterruptedException e) {
                        statsBenchmark.getSendRequestFailedCount().incrementAndGet();
                        try {
                            Thread.sleep(3000);
                        } catch (InterruptedException e1) {
                        }
                    } catch (MQClientException e) {
                        statsBenchmark.getSendRequestFailedCount().incrementAndGet();
                        log.error("[BENCHMARK_PRODUCER] Send Exception", e);
                    } catch (MQBrokerException e) {
                        statsBenchmark.getReceiveResponseFailedCount().incrementAndGet();
                        log.error("[BENCHMARK_PRODUCER] Send Exception", e);
                        try {
                            Thread.sleep(3000);
                        } catch (InterruptedException ignored) {
                        }
                    }
                }
            }
        });
    }
}
Also used : Options(org.apache.commons.cli.Options) Message(org.apache.rocketmq.common.message.Message) PosixParser(org.apache.commons.cli.PosixParser) Logger(org.slf4j.Logger) ClientLogger(org.apache.rocketmq.client.log.ClientLogger) TimerTask(java.util.TimerTask) Random(java.util.Random) MQClientException(org.apache.rocketmq.client.exception.MQClientException) MQBrokerException(org.apache.rocketmq.client.exception.MQBrokerException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) DefaultMQProducer(org.apache.rocketmq.client.producer.DefaultMQProducer) LinkedList(java.util.LinkedList) MQClientException(org.apache.rocketmq.client.exception.MQClientException) MQBrokerException(org.apache.rocketmq.client.exception.MQBrokerException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) RemotingException(org.apache.rocketmq.remoting.exception.RemotingException) CommandLine(org.apache.commons.cli.CommandLine) Timer(java.util.Timer) RemotingException(org.apache.rocketmq.remoting.exception.RemotingException) ExecutorService(java.util.concurrent.ExecutorService) AtomicLong(java.util.concurrent.atomic.AtomicLong)

Example 98 with MQBrokerException

use of org.apache.rocketmq.client.exception.MQBrokerException in project rocketmq by apache.

the class MQClientAPIImpl method searchOffset.

public long searchOffset(final String addr, final String topic, final int queueId, final long timestamp, final long timeoutMillis) throws RemotingException, MQBrokerException, InterruptedException {
    SearchOffsetRequestHeader requestHeader = new SearchOffsetRequestHeader();
    requestHeader.setTopic(topic);
    requestHeader.setQueueId(queueId);
    requestHeader.setTimestamp(timestamp);
    RemotingCommand request = RemotingCommand.createRequestCommand(RequestCode.SEARCH_OFFSET_BY_TIMESTAMP, requestHeader);
    RemotingCommand response = this.remotingClient.invokeSync(MixAll.brokerVIPChannel(this.clientConfig.isVipChannelEnabled(), addr), request, timeoutMillis);
    assert response != null;
    switch(response.getCode()) {
        case ResponseCode.SUCCESS:
            {
                SearchOffsetResponseHeader responseHeader = (SearchOffsetResponseHeader) response.decodeCommandCustomHeader(SearchOffsetResponseHeader.class);
                return responseHeader.getOffset();
            }
        default:
            break;
    }
    throw new MQBrokerException(response.getCode(), response.getRemark());
}
Also used : RemotingCommand(org.apache.rocketmq.remoting.protocol.RemotingCommand) SearchOffsetRequestHeader(org.apache.rocketmq.common.protocol.header.SearchOffsetRequestHeader) MQBrokerException(org.apache.rocketmq.client.exception.MQBrokerException) SearchOffsetResponseHeader(org.apache.rocketmq.common.protocol.header.SearchOffsetResponseHeader)

Example 99 with MQBrokerException

use of org.apache.rocketmq.client.exception.MQBrokerException in project rocketmq by apache.

the class MQClientAPIImpl method viewMessage.

public MessageExt viewMessage(final String addr, final long phyoffset, final long timeoutMillis) throws RemotingException, MQBrokerException, InterruptedException {
    ViewMessageRequestHeader requestHeader = new ViewMessageRequestHeader();
    requestHeader.setOffset(phyoffset);
    RemotingCommand request = RemotingCommand.createRequestCommand(RequestCode.VIEW_MESSAGE_BY_ID, requestHeader);
    RemotingCommand response = this.remotingClient.invokeSync(MixAll.brokerVIPChannel(this.clientConfig.isVipChannelEnabled(), addr), request, timeoutMillis);
    assert response != null;
    switch(response.getCode()) {
        case ResponseCode.SUCCESS:
            {
                ByteBuffer byteBuffer = ByteBuffer.wrap(response.getBody());
                MessageExt messageExt = MessageDecoder.clientDecode(byteBuffer, true);
                return messageExt;
            }
        default:
            break;
    }
    throw new MQBrokerException(response.getCode(), response.getRemark());
}
Also used : ViewMessageRequestHeader(org.apache.rocketmq.common.protocol.header.ViewMessageRequestHeader) RemotingCommand(org.apache.rocketmq.remoting.protocol.RemotingCommand) MessageExt(org.apache.rocketmq.common.message.MessageExt) MQBrokerException(org.apache.rocketmq.client.exception.MQBrokerException) ByteBuffer(java.nio.ByteBuffer)

Example 100 with MQBrokerException

use of org.apache.rocketmq.client.exception.MQBrokerException in project rocketmq by apache.

the class MQClientAPIImpl method getAllTopicConfig.

public TopicConfigSerializeWrapper getAllTopicConfig(final String addr, long timeoutMillis) throws RemotingConnectException, RemotingSendRequestException, RemotingTimeoutException, InterruptedException, MQBrokerException {
    RemotingCommand request = RemotingCommand.createRequestCommand(RequestCode.GET_ALL_TOPIC_CONFIG, null);
    RemotingCommand response = this.remotingClient.invokeSync(MixAll.brokerVIPChannel(this.clientConfig.isVipChannelEnabled(), addr), request, timeoutMillis);
    assert response != null;
    switch(response.getCode()) {
        case ResponseCode.SUCCESS:
            {
                return TopicConfigSerializeWrapper.decode(response.getBody(), TopicConfigSerializeWrapper.class);
            }
        default:
            break;
    }
    throw new MQBrokerException(response.getCode(), response.getRemark());
}
Also used : RemotingCommand(org.apache.rocketmq.remoting.protocol.RemotingCommand) MQBrokerException(org.apache.rocketmq.client.exception.MQBrokerException) TopicConfigSerializeWrapper(org.apache.rocketmq.common.protocol.body.TopicConfigSerializeWrapper)

Aggregations

MQBrokerException (org.apache.rocketmq.client.exception.MQBrokerException)116 RemotingCommand (org.apache.rocketmq.remoting.protocol.RemotingCommand)70 MQClientException (org.apache.rocketmq.client.exception.MQClientException)40 RemotingException (org.apache.rocketmq.remoting.exception.RemotingException)35 Message (org.apache.rocketmq.common.message.Message)12 SendResult (org.apache.rocketmq.client.producer.SendResult)11 RemotingConnectException (org.apache.rocketmq.remoting.exception.RemotingConnectException)11 RemotingTimeoutException (org.apache.rocketmq.remoting.exception.RemotingTimeoutException)11 UnsupportedEncodingException (java.io.UnsupportedEncodingException)10 SubCommandException (org.apache.rocketmq.tools.command.SubCommandException)10 RemotingSendRequestException (org.apache.rocketmq.remoting.exception.RemotingSendRequestException)9 Test (org.junit.Test)9 MessageExt (org.apache.rocketmq.common.message.MessageExt)8 DefaultMQProducer (org.apache.rocketmq.client.producer.DefaultMQProducer)6 MessageQueue (org.apache.rocketmq.common.message.MessageQueue)6 ConsumerConnection (org.apache.rocketmq.common.protocol.body.ConsumerConnection)6 GroupList (org.apache.rocketmq.common.protocol.body.GroupList)6 SubscriptionData (org.apache.rocketmq.common.protocol.heartbeat.SubscriptionData)6 BrokerData (org.apache.rocketmq.common.protocol.route.BrokerData)6 IOException (java.io.IOException)5