1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- package org.jeecg.config.transactional;
- import org.apache.commons.lang.StringUtils;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Component;
- import java.util.concurrent.Callable;
- import java.util.stream.Stream;
- /**
- * 功能描述:
- *
- * @Author: chenchuang
- * @Date: 2022/3/31 16:56
- */
- @Component
- public class ComboTransaction {
- @Autowired
- private Db1TxBroker db1TxBroker;
- @Autowired
- private Db2TxBroker db2TxBroker;
- public <V> V inCombinedTx(Callable<V> callable, String[] transactions) {
- if (callable == null) {
- return null;
- }
- Callable<V> combined = Stream.of(transactions)
- .filter(ele -> !StringUtils.isEmpty(ele))
- .distinct()
- .reduce(callable, (r, tx) -> {
- switch (tx) {
- case DbTxConstants.DB1_TX:
- return () -> db1TxBroker.inTransaction(r);
- case DbTxConstants.DB2_TX:
- return () -> db2TxBroker.inTransaction(r);
- default:
- return null;
- }
- }, (r1, r2) -> r2);
- try {
- return combined.call();
- } catch (RuntimeException e) {
- throw e;
- } catch (Exception e) {
- throw new RuntimeException(e);
- }
- }
- }
|