Search in sources :

Example 1 with TransactionMQProducer

use of com.yanghui.elephant.client.producer.TransactionMQProducer in project elephant by yanghuijava.

the class TransactionProducer method main.

public static void main(String[] args) throws MQClientException {
    TransactionMQProducer producer = new TransactionMQProducer("transaction-test");
    producer.setRegisterCenter("120.77.152.143:2181");
    producer.setTransactionCheckListener(new TransactionCheckListenerImpl());
    producer.start();
    LocalTransactionExecuter excuter = new LocalTransactionExecuterImpl();
    /**
     * 目前只支持activemq:
     * 发送queue,message的destination值加上前缀:queue://
     * 发送topic,message的destination值加上前缀:topic://
     */
    for (int i = 0; i < 1; i++) {
        Message msg = new Message("queue://yanghui.queue.test1", ("我是事务消息" + i).getBytes());
        TransactionSendResult transactionSendResult = producer.sendMessageTransaction(msg, excuter, null);
        System.out.println(transactionSendResult);
    }
// producer.shutdown();
}
Also used : Message(com.yanghui.elephant.common.message.Message) LocalTransactionExecuter(com.yanghui.elephant.client.producer.LocalTransactionExecuter) TransactionSendResult(com.yanghui.elephant.client.producer.TransactionSendResult) TransactionMQProducer(com.yanghui.elephant.client.producer.TransactionMQProducer)

Example 2 with TransactionMQProducer

use of com.yanghui.elephant.client.producer.TransactionMQProducer in project elephant by yanghuijava.

the class TransactionProducer1 method main.

public static void main(String[] args) throws MQClientException {
    TransactionMQProducer producer1 = new TransactionMQProducer("transaction-test1");
    producer1.setRegisterCenter("172.16.21.12:2181");
    producer1.setTransactionCheckListener(new TransactionCheckListenerImpl());
    producer1.start();
    TransactionMQProducer producer2 = new TransactionMQProducer("transaction-test2");
    producer2.setRegisterCenter("172.16.21.12:2181");
    producer2.setTransactionCheckListener(new TransactionCheckListenerImpl2());
    producer2.start();
    LocalTransactionExecuter excuter = new LocalTransactionExecuterImpl();
    Message msg1 = new Message("topic://VirtualTopic.Test", ("我是事务消息1").getBytes());
    TransactionSendResult transactionSendResult1 = producer1.sendMessageTransaction(msg1, excuter, null);
    System.out.println("transactionSendResult1=" + transactionSendResult1);
    Message msg2 = new Message("topic://VirtualTopic.Test", ("我是事务消息2").getBytes());
    TransactionSendResult transactionSendResult2 = producer2.sendMessageTransaction(msg2, excuter, null);
    System.out.println("transactionSendResult2=" + transactionSendResult2);
}
Also used : Message(com.yanghui.elephant.common.message.Message) LocalTransactionExecuter(com.yanghui.elephant.client.producer.LocalTransactionExecuter) TransactionSendResult(com.yanghui.elephant.client.producer.TransactionSendResult) TransactionMQProducer(com.yanghui.elephant.client.producer.TransactionMQProducer)

Example 3 with TransactionMQProducer

use of com.yanghui.elephant.client.producer.TransactionMQProducer in project elephant by yanghuijava.

the class DefaultMQProducerImpl method checkTransactionState.

@Override
public void checkTransactionState(final String address, final String producerGroupe, final Message msg) {
    Runnable run = new Runnable() {

        @Override
        public void run() {
            try {
                TransactionMQProducer producer = (TransactionMQProducer) defaultMQProducer;
                LocalTransactionState localState = producer.getTransactionCheckListener().checkLocalTransactionState(msg);
                CheckTransactionStateResponseHeader header = new CheckTransactionStateResponseHeader();
                header.setCommitOrRollback(localState.name());
                header.setMessageId(msg.getMessageId());
                header.setProducerGroup(defaultMQProducer.getProducerGroup());
                RemotingCommand request = RemotingCommand.buildRequestCmd(header, RequestCode.CHECK_TRANSACTION_RESPONSE);
                mqProducerFactory.getRemotingClient().invokeOneway(address, request, 0);
            } catch (Exception e) {
                log.error("check transaction state error:{}", e);
            }
        }
    };
    this.checkExecutor.submit(run);
}
Also used : RemotingCommand(com.yanghui.elephant.remoting.procotol.RemotingCommand) LocalTransactionState(com.yanghui.elephant.common.constant.LocalTransactionState) CheckTransactionStateResponseHeader(com.yanghui.elephant.common.protocol.header.CheckTransactionStateResponseHeader) TransactionMQProducer(com.yanghui.elephant.client.producer.TransactionMQProducer) RemotingSendRequestException(com.yanghui.elephant.remoting.exception.RemotingSendRequestException) MQClientException(com.yanghui.elephant.client.exception.MQClientException) RemotingConnectException(com.yanghui.elephant.remoting.exception.RemotingConnectException) RemotingTimeoutException(com.yanghui.elephant.remoting.exception.RemotingTimeoutException)

Example 4 with TransactionMQProducer

use of com.yanghui.elephant.client.producer.TransactionMQProducer in project elephant by yanghuijava.

the class DefaultMQProducerImpl method initTransaction.

public void initTransaction() {
    TransactionMQProducer producer = (TransactionMQProducer) this.defaultMQProducer;
    this.checkRequestQueue = new LinkedBlockingQueue<Runnable>(producer.getCheckRequestHoldMax());
    this.checkExecutor = new ThreadPoolExecutor(producer.getCheckThreadPoolMinSize(), producer.getCheckThreadPoolMaxSize(), 1000 * 60, TimeUnit.MILLISECONDS, this.checkRequestQueue, new ThreadFactory() {

        @Override
        public Thread newThread(Runnable r) {
            return new Thread(r, "transactioncheckExecutorThread");
        }
    });
    this.mqProducerFactory.registerDefaultRequestProcessor();
}
Also used : ThreadFactory(java.util.concurrent.ThreadFactory) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) TransactionMQProducer(com.yanghui.elephant.client.producer.TransactionMQProducer)

Aggregations

TransactionMQProducer (com.yanghui.elephant.client.producer.TransactionMQProducer)4 LocalTransactionExecuter (com.yanghui.elephant.client.producer.LocalTransactionExecuter)2 TransactionSendResult (com.yanghui.elephant.client.producer.TransactionSendResult)2 Message (com.yanghui.elephant.common.message.Message)2 MQClientException (com.yanghui.elephant.client.exception.MQClientException)1 LocalTransactionState (com.yanghui.elephant.common.constant.LocalTransactionState)1 CheckTransactionStateResponseHeader (com.yanghui.elephant.common.protocol.header.CheckTransactionStateResponseHeader)1 RemotingConnectException (com.yanghui.elephant.remoting.exception.RemotingConnectException)1 RemotingSendRequestException (com.yanghui.elephant.remoting.exception.RemotingSendRequestException)1 RemotingTimeoutException (com.yanghui.elephant.remoting.exception.RemotingTimeoutException)1 RemotingCommand (com.yanghui.elephant.remoting.procotol.RemotingCommand)1 ThreadFactory (java.util.concurrent.ThreadFactory)1 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)1