// Path: tcc-transaction-core/src/main/java/org/pankai/tcctransaction/recover/TransactionRecovery.java
// public class TransactionRecovery {
// 
//     private final Logger logger = LoggerFactory.getLogger(TransactionRecovery.class);
// 
//     private TransactionConfigurator transactionConfigurator;
// 
//     public void setTransactionConfigurator(TransactionConfigurator transactionConfigurator) {
//         this.transactionConfigurator = transactionConfigurator;
//     }
// 
//     public void startRecover() {
//         logger.debug("TransactionRecovery startRecover.................................");
//         List<Transaction> transactions = loadErrorTransactions();
//         logger.debug("Found " + transactions.size() + " error transactions, will try to recover.");
//         recoverTransactions(transactions);
//     }
// 
//     private List<Transaction> loadErrorTransactions() {
//         TransactionRepository transactionRepository = transactionConfigurator.getTransactionRepository();
//         long currentTimeInMillis = Calendar.getInstance().getTimeInMillis();
//         List<Transaction> transactions = transactionRepository.findAllUnmodifiedSince(new Date(currentTimeInMillis - transactionConfigurator.getRecoverConfig().getRecoverDuration() * 1000));
//         List<Transaction> recoverTransactions = new ArrayList<>();
//         for (Transaction transaction : transactions) {
//             int result = transactionRepository.update(transaction);
//             if (result > 0) {
//                 recoverTransactions.add(transaction);
//             }
//         }
//         return recoverTransactions;
//     }
// 
//     private void recoverTransactions(List<Transaction> transactions) {
//         for (Transaction transaction : transactions) {
//             if (transaction.getRetriedCount() > transactionConfigurator.getRecoverConfig().getMaxRetryCount()) {
//                 logger.error(String.format("recover failed with max retry count, will not try again. txid:%s, status:%s, retried count:%d", transaction.getXid(), transaction.getStatus().getId(), transaction.getRetriedCount()));
//                 continue;
//             }
// 
//             try {
//                 transaction.addRetriedCount();
// 
//                 if (transaction.getStatus().equals(TransactionStatus.CONFIRMING)) {
//                     transaction.changeStatus(TransactionStatus.CONFIRMING);
//                     transactionConfigurator.getTransactionRepository().update(transaction);
//                     transaction.commit();
//                 } else {
//                     transaction.changeStatus(TransactionStatus.CANCELLING);
//                     transactionConfigurator.getTransactionRepository().update(transaction);
//                     transaction.rollback();
//                 }
//                 transactionConfigurator.getTransactionRepository().delete(transaction);
//             } catch (Throwable e) {
//                 logger.warn(String.format("recover failed, txid:%s, status:%s, retried count:%d", transaction.getXid(), transaction.getStatus().getId(), transaction.getRetriedCount()), e);
//             }
//         }
// 
// 
//     }
// }
// 
// Path: tcc-transaction-spring/src/main/java/org/pankai/tcctransaction/spring/support/TccTransactionConfigurator.java
// public class TccTransactionConfigurator implements TransactionConfigurator {
// 
//     @Autowired
//     private TransactionRepository transactionRepository;
// 
//     @Autowired(required = false)
//     private RecoverConfig recoverConfig = DefaultRecoverConfig.INSTANCE;
// 
//     private TransactionManager transactionManager = new TransactionManager(this);
// 
// 
//     @Override
//     public TransactionManager getTransactionManager() {
//         return transactionManager;
//     }
// 
//     @Override
//     public TransactionRepository getTransactionRepository() {
//         return transactionRepository;
//     }
// 
//     @Override
//     public RecoverConfig getRecoverConfig() {
//         return recoverConfig;
//     }
// }


import org.pankai.tcctransaction.recover.TransactionRecovery;
import org.pankai.tcctransaction.spring.support.TccTransactionConfigurator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
package org.pankai.tcctransaction.spring.recover;



/**
 * 原文使用quartz驱动.这里重构为使用spring schedule.
 * Created by pktczwd on 2016/12/16.
 */
@Component
public class RecoverScheduleJob {

    @Autowired
    private TransactionRecovery transactionRecovery;
    @Autowired
// Path: tcc-transaction-core/src/main/java/org/pankai/tcctransaction/recover/TransactionRecovery.java
// public class TransactionRecovery {
// 
//     private final Logger logger = LoggerFactory.getLogger(TransactionRecovery.class);
// 
//     private TransactionConfigurator transactionConfigurator;
// 
//     public void setTransactionConfigurator(TransactionConfigurator transactionConfigurator) {
//         this.transactionConfigurator = transactionConfigurator;
//     }
// 
//     public void startRecover() {
//         logger.debug("TransactionRecovery startRecover.................................");
//         List<Transaction> transactions = loadErrorTransactions();
//         logger.debug("Found " + transactions.size() + " error transactions, will try to recover.");
//         recoverTransactions(transactions);
//     }
// 
//     private List<Transaction> loadErrorTransactions() {
//         TransactionRepository transactionRepository = transactionConfigurator.getTransactionRepository();
//         long currentTimeInMillis = Calendar.getInstance().getTimeInMillis();
//         List<Transaction> transactions = transactionRepository.findAllUnmodifiedSince(new Date(currentTimeInMillis - transactionConfigurator.getRecoverConfig().getRecoverDuration() * 1000));
//         List<Transaction> recoverTransactions = new ArrayList<>();
//         for (Transaction transaction : transactions) {
//             int result = transactionRepository.update(transaction);
//             if (result > 0) {
//                 recoverTransactions.add(transaction);
//             }
//         }
//         return recoverTransactions;
//     }
// 
//     private void recoverTransactions(List<Transaction> transactions) {
//         for (Transaction transaction : transactions) {
//             if (transaction.getRetriedCount() > transactionConfigurator.getRecoverConfig().getMaxRetryCount()) {
//                 logger.error(String.format("recover failed with max retry count, will not try again. txid:%s, status:%s, retried count:%d", transaction.getXid(), transaction.getStatus().getId(), transaction.getRetriedCount()));
//                 continue;
//             }
// 
//             try {
//                 transaction.addRetriedCount();
// 
//                 if (transaction.getStatus().equals(TransactionStatus.CONFIRMING)) {
//                     transaction.changeStatus(TransactionStatus.CONFIRMING);
//                     transactionConfigurator.getTransactionRepository().update(transaction);
//                     transaction.commit();
//                 } else {
//                     transaction.changeStatus(TransactionStatus.CANCELLING);
//                     transactionConfigurator.getTransactionRepository().update(transaction);
//                     transaction.rollback();
//                 }
//                 transactionConfigurator.getTransactionRepository().delete(transaction);
//             } catch (Throwable e) {
//                 logger.warn(String.format("recover failed, txid:%s, status:%s, retried count:%d", transaction.getXid(), transaction.getStatus().getId(), transaction.getRetriedCount()), e);
//             }
//         }
// 
// 
//     }
// }
// 
// Path: tcc-transaction-spring/src/main/java/org/pankai/tcctransaction/spring/support/TccTransactionConfigurator.java
// public class TccTransactionConfigurator implements TransactionConfigurator {
// 
//     @Autowired
//     private TransactionRepository transactionRepository;
// 
//     @Autowired(required = false)
//     private RecoverConfig recoverConfig = DefaultRecoverConfig.INSTANCE;
// 
//     private TransactionManager transactionManager = new TransactionManager(this);
// 
// 
//     @Override
//     public TransactionManager getTransactionManager() {
//         return transactionManager;
//     }
// 
//     @Override
//     public TransactionRepository getTransactionRepository() {
//         return transactionRepository;
//     }
// 
//     @Override
//     public RecoverConfig getRecoverConfig() {
//         return recoverConfig;
//     }
// }


// Path: tcc-transaction-spring/src/main/java/org/pankai/tcctransaction/spring/recover/RecoverScheduleJob.java
import org.pankai.tcctransaction.recover.TransactionRecovery;
import org.pankai.tcctransaction.spring.support.TccTransactionConfigurator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;

package org.pankai.tcctransaction.spring.recover;



/**
 * 原文使用quartz驱动.这里重构为使用spring schedule.
 * Created by pktczwd on 2016/12/16.
 */
@Component
public class RecoverScheduleJob {

    @Autowired
    private TransactionRecovery transactionRecovery;
    @Autowired
    private TccTransactionConfigurator tccTransactionConfigurator;