Search in sources :

Example 81 with SendResult

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

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

the class StatsBenchmarkTProducer method main.

public static void main(String[] args) throws MQClientException, UnsupportedEncodingException {
    threadCount = args.length >= 1 ? Integer.parseInt(args[0]) : 32;
    messageSize = args.length >= 2 ? Integer.parseInt(args[1]) : 1024 * 2;
    ischeck = args.length >= 3 && Boolean.parseBoolean(args[2]);
    ischeckffalse = args.length >= 4 && Boolean.parseBoolean(args[3]);
    final Message msg = buildMessage(messageSize);
    final ExecutorService sendThreadPool = Executors.newFixedThreadPool(threadCount);
    final StatsBenchmarkTProducer statsBenchmark = new StatsBenchmarkTProducer();
    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());
            while (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 transaction checkCount: %d %n", sendTps, statsBenchmark.getSendMessageMaxRT().get(), averageRT, end[2], end[4], end[6]);
            }
        }

        @Override
        public void run() {
            try {
                this.printStats();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }, 10000, 10000);
    final TransactionCheckListener transactionCheckListener = new TransactionCheckListenerBImpl(ischeckffalse, statsBenchmark);
    final TransactionMQProducer producer = new TransactionMQProducer("benchmark_transaction_producer");
    producer.setInstanceName(Long.toString(System.currentTimeMillis()));
    producer.setTransactionCheckListener(transactionCheckListener);
    producer.setDefaultTopicQueueNums(1000);
    producer.start();
    final TransactionExecuterBImpl tranExecuter = new TransactionExecuterBImpl(ischeck);
    for (int i = 0; i < threadCount; i++) {
        sendThreadPool.execute(new Runnable() {

            @Override
            public void run() {
                while (true) {
                    try {
                        // Thread.sleep(1000);
                        final long beginTimestamp = System.currentTimeMillis();
                        SendResult sendResult = producer.sendMessageInTransaction(msg, tranExecuter, null);
                        if (sendResult != null) {
                            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 (MQClientException e) {
                        statsBenchmark.getSendRequestFailedCount().incrementAndGet();
                    }
                }
            }
        });
    }
}
Also used : Message(org.apache.rocketmq.common.message.Message) LinkedList(java.util.LinkedList) MQClientException(org.apache.rocketmq.client.exception.MQClientException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) TransactionCheckListener(org.apache.rocketmq.client.producer.TransactionCheckListener) TransactionMQProducer(org.apache.rocketmq.client.producer.TransactionMQProducer) Timer(java.util.Timer) TimerTask(java.util.TimerTask) SendResult(org.apache.rocketmq.client.producer.SendResult) ExecutorService(java.util.concurrent.ExecutorService) AtomicLong(java.util.concurrent.atomic.AtomicLong) MQClientException(org.apache.rocketmq.client.exception.MQClientException)

Example 83 with SendResult

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

the class TestProducer method main.

public static void main(String[] args) throws MQClientException, InterruptedException {
    DefaultMQProducer producer = new DefaultMQProducer("ProducerGroupName");
    producer.start();
    for (int i = 0; i < 1; i++) try {
        {
            Message msg = new Message("TopicTest1", "TagA", "key113", "Hello world".getBytes(RemotingHelper.DEFAULT_CHARSET));
            SendResult sendResult = producer.send(msg);
            System.out.printf("%s%n", sendResult);
            QueryResult queryMessage = producer.queryMessage("TopicTest1", "key113", 10, 0, System.currentTimeMillis());
            for (MessageExt m : queryMessage.getMessageList()) {
                System.out.printf("%s%n", m);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    producer.shutdown();
}
Also used : MessageExt(org.apache.rocketmq.common.message.MessageExt) QueryResult(org.apache.rocketmq.client.QueryResult) Message(org.apache.rocketmq.common.message.Message) SendResult(org.apache.rocketmq.client.producer.SendResult) DefaultMQProducer(org.apache.rocketmq.client.producer.DefaultMQProducer) MQClientException(org.apache.rocketmq.client.exception.MQClientException)

Example 84 with SendResult

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

the class MQClientAPIImpl method sendMessageAsync.

private void sendMessageAsync(final String addr, final String brokerName, final Message msg, final long timeoutMillis, final RemotingCommand request, final SendCallback sendCallback, final TopicPublishInfo topicPublishInfo, final MQClientInstance instance, final int retryTimesWhenSendFailed, final AtomicInteger times, final SendMessageContext context, final DefaultMQProducerImpl producer) throws InterruptedException, RemotingException {
    this.remotingClient.invokeAsync(addr, request, timeoutMillis, new InvokeCallback() {

        @Override
        public void operationComplete(ResponseFuture responseFuture) {
            RemotingCommand response = responseFuture.getResponseCommand();
            if (null == sendCallback && response != null) {
                try {
                    SendResult sendResult = MQClientAPIImpl.this.processSendResponse(brokerName, msg, response);
                    if (context != null && sendResult != null) {
                        context.setSendResult(sendResult);
                        context.getProducer().executeSendMessageHookAfter(context);
                    }
                } catch (Throwable e) {
                }
                producer.updateFaultItem(brokerName, System.currentTimeMillis() - responseFuture.getBeginTimestamp(), false);
                return;
            }
            if (response != null) {
                try {
                    SendResult sendResult = MQClientAPIImpl.this.processSendResponse(brokerName, msg, response);
                    assert sendResult != null;
                    if (context != null) {
                        context.setSendResult(sendResult);
                        context.getProducer().executeSendMessageHookAfter(context);
                    }
                    try {
                        sendCallback.onSuccess(sendResult);
                    } catch (Throwable e) {
                    }
                    producer.updateFaultItem(brokerName, System.currentTimeMillis() - responseFuture.getBeginTimestamp(), false);
                } catch (Exception e) {
                    producer.updateFaultItem(brokerName, System.currentTimeMillis() - responseFuture.getBeginTimestamp(), true);
                    onExceptionImpl(brokerName, msg, 0L, request, sendCallback, topicPublishInfo, instance, retryTimesWhenSendFailed, times, e, context, false, producer);
                }
            } else {
                producer.updateFaultItem(brokerName, System.currentTimeMillis() - responseFuture.getBeginTimestamp(), true);
                if (!responseFuture.isSendRequestOK()) {
                    MQClientException ex = new MQClientException("send request failed", responseFuture.getCause());
                    onExceptionImpl(brokerName, msg, 0L, request, sendCallback, topicPublishInfo, instance, retryTimesWhenSendFailed, times, ex, context, true, producer);
                } else if (responseFuture.isTimeout()) {
                    MQClientException ex = new MQClientException("wait response timeout " + responseFuture.getTimeoutMillis() + "ms", responseFuture.getCause());
                    onExceptionImpl(brokerName, msg, 0L, request, sendCallback, topicPublishInfo, instance, retryTimesWhenSendFailed, times, ex, context, true, producer);
                } else {
                    MQClientException ex = new MQClientException("unknow reseaon", responseFuture.getCause());
                    onExceptionImpl(brokerName, msg, 0L, request, sendCallback, topicPublishInfo, instance, retryTimesWhenSendFailed, times, ex, context, true, producer);
                }
            }
        }
    });
}
Also used : RemotingCommand(org.apache.rocketmq.remoting.protocol.RemotingCommand) InvokeCallback(org.apache.rocketmq.remoting.InvokeCallback) SendResult(org.apache.rocketmq.client.producer.SendResult) ResponseFuture(org.apache.rocketmq.remoting.netty.ResponseFuture) MQClientException(org.apache.rocketmq.client.exception.MQClientException) RemotingTimeoutException(org.apache.rocketmq.remoting.exception.RemotingTimeoutException) RemotingTooMuchRequestException(org.apache.rocketmq.remoting.exception.RemotingTooMuchRequestException) MQBrokerException(org.apache.rocketmq.client.exception.MQBrokerException) RemotingConnectException(org.apache.rocketmq.remoting.exception.RemotingConnectException) RemotingSendRequestException(org.apache.rocketmq.remoting.exception.RemotingSendRequestException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) RemotingException(org.apache.rocketmq.remoting.exception.RemotingException) RemotingCommandException(org.apache.rocketmq.remoting.exception.RemotingCommandException) MQClientException(org.apache.rocketmq.client.exception.MQClientException)

Example 85 with SendResult

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

the class SequenceProducerImpl method commit.

@Override
public synchronized void commit() {
    List<Message> messages = new ArrayList<>();
    msgCacheQueue.drainTo(messages);
    List<org.apache.rocketmq.common.message.Message> rmqMessages = new ArrayList<>();
    for (Message message : messages) {
        rmqMessages.add(OMSUtil.msgConvert((BytesMessage) message));
    }
    if (rmqMessages.size() == 0) {
        return;
    }
    try {
        SendResult sendResult = this.rocketmqProducer.send(rmqMessages);
        String[] msgIdArray = sendResult.getMsgId().split(",");
        for (int i = 0; i < messages.size(); i++) {
            Message message = messages.get(i);
            message.headers().put(MessageHeader.MESSAGE_ID, msgIdArray[i]);
        }
    } catch (Exception e) {
        throw checkProducerException("", "", e);
    }
}
Also used : Message(io.openmessaging.Message) BytesMessage(io.openmessaging.BytesMessage) SendResult(org.apache.rocketmq.client.producer.SendResult) ArrayList(java.util.ArrayList) BytesMessage(io.openmessaging.BytesMessage) MQClientException(org.apache.rocketmq.client.exception.MQClientException)

Aggregations

SendResult (org.apache.rocketmq.client.producer.SendResult)95 Message (org.apache.rocketmq.common.message.Message)64 DefaultMQProducer (org.apache.rocketmq.client.producer.DefaultMQProducer)42 MQClientException (org.apache.rocketmq.client.exception.MQClientException)39 Test (org.junit.Test)32 MQBrokerException (org.apache.rocketmq.client.exception.MQBrokerException)15 RemotingException (org.apache.rocketmq.remoting.exception.RemotingException)15 SendCallback (org.apache.rocketmq.client.producer.SendCallback)13 UnsupportedEncodingException (java.io.UnsupportedEncodingException)11 MessageQueue (org.apache.rocketmq.common.message.MessageQueue)10 ArrayList (java.util.ArrayList)8 SendMessageContext (org.apache.rocketmq.client.hook.SendMessageContext)8 SendMessageRequestHeader (org.apache.rocketmq.common.protocol.header.SendMessageRequestHeader)8 RemotingCommand (org.apache.rocketmq.remoting.protocol.RemotingCommand)8 IOException (java.io.IOException)6 TransactionSendResult (org.apache.rocketmq.client.producer.TransactionSendResult)6 RemotingConnectException (org.apache.rocketmq.remoting.exception.RemotingConnectException)6 RemotingTimeoutException (org.apache.rocketmq.remoting.exception.RemotingTimeoutException)6 MessageExt (org.apache.rocketmq.common.message.MessageExt)5 BytesMessage (io.openmessaging.BytesMessage)4