jingangwang opened a new issue #2753:
URL: https://github.com/apache/rocketmq/issues/2753


   rocketmq4.8官方事务消息例子不调用事务回查接口(checkLocalTransaction)
   
   public class TransactionListenerImpl implements TransactionListener {
       private AtomicInteger transactionIndex = new AtomicInteger(0);
       private ConcurrentHashMap<String, Integer> localTrans = new 
ConcurrentHashMap<>();
   
       @Override
       public LocalTransactionState executeLocalTransaction(Message msg, Object 
arg) {
           System.out.println("执行本地事务,msg="+ JSON.toJSONString(msg));
           int value = transactionIndex.getAndIncrement();
           int status = value % 3;
           localTrans.put(msg.getTransactionId(), status);
           try {
               Thread.sleep(20000);
           } catch (InterruptedException e) {
               e.printStackTrace();
           }
           return LocalTransactionState.UNKNOW;
       }
   
       @Override
       public LocalTransactionState checkLocalTransaction(MessageExt msg) {
           System.out.println("执行回查本地事务接口,msg="+ JSON.toJSONString(msg));
           Integer status = localTrans.get(msg.getTransactionId());
           if (status != null) {
               switch (status) {
                   case 0:
                       return LocalTransactionState.UNKNOW;
                   case 1:
                       return LocalTransactionState.COMMIT_MESSAGE;
                   case 2:
                       return LocalTransactionState.ROLLBACK_MESSAGE;
               }
           }
           return LocalTransactionState.COMMIT_MESSAGE;
       }
   }
   
   
   public class TransactionProducer {
       public static void main(String[] args) throws Exception{
           TransactionMQProducer producer = new 
TransactionMQProducer("zkt_test_transaction_producer");
           producer.setNamesrvAddr("192.168.4.176:9876");
           producer.setTransactionListener(new TransactionListenerImpl());
           ExecutorService executorService = new ThreadPoolExecutor(2, 5, 100, 
TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(2000), new ThreadFactory() {
               @Override
               public Thread newThread(Runnable r) {
                   Thread thread = new Thread(r);
                   thread.setName("client-transaction-msg-check-thread");
                   return thread;
               }
           });
           producer.setExecutorService(executorService);
           producer.start();
   
           String[] tags = new String[] {"TagA", "TagB", "TagC", "TagD", 
"TagE"};
           for(int i=0;i<10;i++){
               Message msg = new 
Message("TopicTest1",tags[i%tags.length],"Key"+i,("Hello RocketMq 
"+i).getBytes(StandardCharsets.UTF_8));
               TransactionSendResult sendResult = 
producer.sendMessageInTransaction(msg, null);
               System.out.printf("%s%n", sendResult);
               Thread.sleep(10);
           }
   
           for (int i = 0; i < 100000; i++) {
               Thread.sleep(1000);
           }
   
           producer.shutdown();
       }
   }


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to