Search in sources :

Example 1 with TransactionCheckListener

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

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 2 with TransactionCheckListener

use of org.apache.rocketmq.client.producer.TransactionCheckListener in project java-example by 1479005017.

the class AliyunTransactionalListener method main.

public static void main(String[] args) throws Exception {
    /**
     * Alions
     */
    AlionsRPCHook rpcHook = new AlionsRPCHook();
    rpcHook.setAccessKeyId(accessKeyId);
    rpcHook.setAccessKeySecret(accessKeySecret);
    rpcHook.setOnsChannel(onsChannel);
    /**
     * 初始化
     */
    TransactionMQProducer producer = new TransactionMQProducer(producerGroup, rpcHook);
    producer.setNamesrvAddr(HttpTinyClient.fetchNamesrvAddress(nameServer));
    producer.setVipChannelEnabled(vipChannelEnabled);
    /**
     * 回调
     */
    producer.setTransactionCheckListener(new TransactionCheckListener() {

        @Override
        public LocalTransactionState checkLocalTransactionState(MessageExt msg) {
            System.out.printf("%s Receive Callback Message: %s %n", Thread.currentThread().getName(), msg);
            return LocalTransactionState.COMMIT_MESSAGE;
        }
    });
    /**
     * 启动
     */
    producer.start();
}
Also used : MessageExt(org.apache.rocketmq.common.message.MessageExt) LocalTransactionState(org.apache.rocketmq.client.producer.LocalTransactionState) TransactionMQProducer(org.apache.rocketmq.client.producer.TransactionMQProducer) TransactionCheckListener(org.apache.rocketmq.client.producer.TransactionCheckListener)

Example 3 with TransactionCheckListener

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

the class TransactionProducer method main.

public static void main(String[] args) throws MQClientException, InterruptedException {
    TransactionCheckListener transactionCheckListener = new TransactionCheckListenerImpl();
    TransactionMQProducer producer = new TransactionMQProducer("please_rename_unique_group_name");
    producer.setCheckThreadPoolMinSize(2);
    producer.setCheckThreadPoolMaxSize(2);
    producer.setCheckRequestHoldMax(2000);
    producer.setTransactionCheckListener(transactionCheckListener);
    producer.start();
    String[] tags = new String[] { "TagA", "TagB", "TagC", "TagD", "TagE" };
    TransactionExecuterImpl tranExecuter = new TransactionExecuterImpl();
    for (int i = 0; i < 100; i++) {
        try {
            Message msg = new Message("TopicTest", tags[i % tags.length], "KEY" + i, ("Hello RocketMQ " + i).getBytes(RemotingHelper.DEFAULT_CHARSET));
            SendResult sendResult = producer.sendMessageInTransaction(msg, tranExecuter, null);
            System.out.printf("%s%n", sendResult);
            Thread.sleep(10);
        } catch (MQClientException | UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }
    for (int i = 0; i < 100000; i++) {
        Thread.sleep(1000);
    }
    producer.shutdown();
}
Also used : Message(org.apache.rocketmq.common.message.Message) SendResult(org.apache.rocketmq.client.producer.SendResult) UnsupportedEncodingException(java.io.UnsupportedEncodingException) TransactionCheckListener(org.apache.rocketmq.client.producer.TransactionCheckListener) TransactionMQProducer(org.apache.rocketmq.client.producer.TransactionMQProducer) MQClientException(org.apache.rocketmq.client.exception.MQClientException)

Example 4 with TransactionCheckListener

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

the class TransactionProducer method main.

public static void main(String[] args) throws MQClientException, InterruptedException {
    TransactionCheckListener transactionCheckListener = new TransactionCheckListenerImpl();
    TransactionMQProducer producer = new TransactionMQProducer("please_rename_unique_group_name");
    producer.setCheckThreadPoolMinSize(2);
    producer.setCheckThreadPoolMaxSize(2);
    producer.setCheckRequestHoldMax(2000);
    producer.setTransactionCheckListener(transactionCheckListener);
    producer.start();
    String[] tags = new String[] { "TagA", "TagB", "TagC", "TagD", "TagE" };
    TransactionExecuterImpl tranExecuter = new TransactionExecuterImpl();
    for (int i = 0; i < 100; i++) {
        try {
            Message msg = new Message("TopicTest", tags[i % tags.length], "KEY" + i, ("Hello RocketMQ " + i).getBytes(RemotingHelper.DEFAULT_CHARSET));
            SendResult sendResult = producer.sendMessageInTransaction(msg, tranExecuter, null);
            System.out.printf("%s%n", sendResult);
            Thread.sleep(10);
        } catch (MQClientException | UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }
    for (int i = 0; i < 100000; i++) {
        Thread.sleep(1000);
    }
    producer.shutdown();
}
Also used : Message(org.apache.rocketmq.common.message.Message) SendResult(org.apache.rocketmq.client.producer.SendResult) UnsupportedEncodingException(java.io.UnsupportedEncodingException) TransactionCheckListener(org.apache.rocketmq.client.producer.TransactionCheckListener) TransactionMQProducer(org.apache.rocketmq.client.producer.TransactionMQProducer) MQClientException(org.apache.rocketmq.client.exception.MQClientException)

Example 5 with TransactionCheckListener

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

the class DefaultMQProducerImpl method checkTransactionState.

@Override
public void checkTransactionState(final String addr, final MessageExt msg, final CheckTransactionStateRequestHeader header) {
    Runnable request = new Runnable() {

        private final String brokerAddr = addr;

        private final MessageExt message = msg;

        private final CheckTransactionStateRequestHeader checkRequestHeader = header;

        private final String group = DefaultMQProducerImpl.this.defaultMQProducer.getProducerGroup();

        @Override
        public void run() {
            TransactionCheckListener transactionCheckListener = DefaultMQProducerImpl.this.checkListener();
            if (transactionCheckListener != null) {
                LocalTransactionState localTransactionState = LocalTransactionState.UNKNOW;
                Throwable exception = null;
                try {
                    localTransactionState = transactionCheckListener.checkLocalTransactionState(message);
                } catch (Throwable e) {
                    log.error("Broker call checkTransactionState, but checkLocalTransactionState exception", e);
                    exception = e;
                }
                // 
                this.processTransactionState(// 
                localTransactionState, // 
                group, exception);
            } else {
                log.warn("checkTransactionState, pick transactionCheckListener by group[{}] failed", group);
            }
        }

        private // 
        void processTransactionState(// 
        final LocalTransactionState localTransactionState, // 
        final String producerGroup, final Throwable exception) {
            final EndTransactionRequestHeader thisHeader = new EndTransactionRequestHeader();
            thisHeader.setCommitLogOffset(checkRequestHeader.getCommitLogOffset());
            thisHeader.setProducerGroup(producerGroup);
            thisHeader.setTranStateTableOffset(checkRequestHeader.getTranStateTableOffset());
            thisHeader.setFromTransactionCheck(true);
            String uniqueKey = message.getProperties().get(MessageConst.PROPERTY_UNIQ_CLIENT_MESSAGE_ID_KEYIDX);
            if (uniqueKey == null) {
                uniqueKey = message.getMsgId();
            }
            thisHeader.setMsgId(uniqueKey);
            thisHeader.setTransactionId(checkRequestHeader.getTransactionId());
            switch(localTransactionState) {
                case COMMIT_MESSAGE:
                    thisHeader.setCommitOrRollback(MessageSysFlag.TRANSACTION_COMMIT_TYPE);
                    break;
                case ROLLBACK_MESSAGE:
                    thisHeader.setCommitOrRollback(MessageSysFlag.TRANSACTION_ROLLBACK_TYPE);
                    log.warn("when broker check, client rollback this transaction, {}", thisHeader);
                    break;
                case UNKNOW:
                    thisHeader.setCommitOrRollback(MessageSysFlag.TRANSACTION_NOT_TYPE);
                    log.warn("when broker check, client does not know this transaction state, {}", thisHeader);
                    break;
                default:
                    break;
            }
            String remark = null;
            if (exception != null) {
                remark = "checkLocalTransactionState Exception: " + RemotingHelper.exceptionSimpleDesc(exception);
            }
            try {
                DefaultMQProducerImpl.this.mQClientFactory.getMQClientAPIImpl().endTransactionOneway(brokerAddr, thisHeader, remark, 3000);
            } catch (Exception e) {
                log.error("endTransactionOneway exception", e);
            }
        }
    };
    this.checkExecutor.submit(request);
}
Also used : MessageExt(org.apache.rocketmq.common.message.MessageExt) LocalTransactionState(org.apache.rocketmq.client.producer.LocalTransactionState) CheckTransactionStateRequestHeader(org.apache.rocketmq.common.protocol.header.CheckTransactionStateRequestHeader) TransactionCheckListener(org.apache.rocketmq.client.producer.TransactionCheckListener) MQClientException(org.apache.rocketmq.client.exception.MQClientException) RemotingConnectException(org.apache.rocketmq.remoting.exception.RemotingConnectException) RemotingTimeoutException(org.apache.rocketmq.remoting.exception.RemotingTimeoutException) RemotingException(org.apache.rocketmq.remoting.exception.RemotingException) MQBrokerException(org.apache.rocketmq.client.exception.MQBrokerException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) EndTransactionRequestHeader(org.apache.rocketmq.common.protocol.header.EndTransactionRequestHeader)

Aggregations

TransactionCheckListener (org.apache.rocketmq.client.producer.TransactionCheckListener)7 MQClientException (org.apache.rocketmq.client.exception.MQClientException)6 TransactionMQProducer (org.apache.rocketmq.client.producer.TransactionMQProducer)5 UnsupportedEncodingException (java.io.UnsupportedEncodingException)4 SendResult (org.apache.rocketmq.client.producer.SendResult)4 Message (org.apache.rocketmq.common.message.Message)4 LocalTransactionState (org.apache.rocketmq.client.producer.LocalTransactionState)3 MessageExt (org.apache.rocketmq.common.message.MessageExt)3 IOException (java.io.IOException)2 UnknownHostException (java.net.UnknownHostException)2 LinkedList (java.util.LinkedList)2 Timer (java.util.Timer)2 TimerTask (java.util.TimerTask)2 ExecutorService (java.util.concurrent.ExecutorService)2 AtomicLong (java.util.concurrent.atomic.AtomicLong)2 MQBrokerException (org.apache.rocketmq.client.exception.MQBrokerException)2 CheckTransactionStateRequestHeader (org.apache.rocketmq.common.protocol.header.CheckTransactionStateRequestHeader)2 EndTransactionRequestHeader (org.apache.rocketmq.common.protocol.header.EndTransactionRequestHeader)2 RemotingConnectException (org.apache.rocketmq.remoting.exception.RemotingConnectException)2 RemotingException (org.apache.rocketmq.remoting.exception.RemotingException)2