Skip to content

Commit

Permalink
1.更新使用示例
Browse files Browse the repository at this point in the history
  • Loading branch information
654894017 committed Feb 19, 2024
1 parent b5f3059 commit 56a8ee8
Show file tree
Hide file tree
Showing 31 changed files with 394 additions and 177 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# tcc

# 示例
# 使用示例

https://github.com/654894017/ddd-quick
https://github.com/654894017/tcc/tree/master/src/test/java/com/damon/sample

### 步骤1:运行 com.damon.sample.points.PointsApplication
### 步骤2:运行 com.damon.sample.order.TestRun
20 changes: 19 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<name>tcc</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.version>5.3.27</spring.version>
<spring.version>5.3.28</spring.version>
<slf4japi.version>1.7.32</slf4japi.version>
<hutool.version>5.8.20</hutool.version>
<java.version>1.8</java.version>
Expand Down Expand Up @@ -48,6 +48,12 @@
<version>2.7.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.7.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down Expand Up @@ -96,6 +102,18 @@
<version>1.2.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<version>2.7.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
27 changes: 14 additions & 13 deletions src/main/java/com/damon/tcc/TccMainService.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.damon.tcc;

import cn.hutool.core.thread.NamedThreadFactory;
import com.damon.tcc.exception.TccLocalTransactionException;
import com.damon.tcc.exception.TccTryException;
import com.damon.tcc.main_log.ITccMainLogService;
import com.damon.tcc.main_log.TccMainLog;
Expand Down Expand Up @@ -79,8 +80,8 @@ protected void check(TccFailedLogIterator iterator) {
List<TccMainLog> tccMainLogs = iterator.next();
tccMainLogs.forEach(tccLog -> {
asyncCheckExecutorService.execute(
new TccMasterLogAsyncCheckRunnable<>(tccLogService, bizType, this::commitPhase,
this::cancelPhase, this::callbackParameter, tccLog
new TccMasterLogAsyncCheckRunnable<>(tccLogService, bizType, this::commit,
this::cancel, this::callbackParameter, tccLog
)
);
});
Expand All @@ -105,33 +106,33 @@ protected R process(O parameter) throws TccTryException {

private void executeTry(O parameter) throws TccTryException {
try {
tryPhase(parameter);
attempt(parameter);
} catch (Exception exception) {
log.error("业务类型: {}, 业务id : {}, 预执行失败", bizType, parameter.getBizId(), exception);
asyncCommitExecutorService.execute(
new TccMasterLogAsyncCheckRunnable<>(tccLogService, bizType, this::commitPhase, this::cancelPhase, parameter)
new TccMasterLogAsyncCheckRunnable<>(tccLogService, bizType, this::commit, this::cancel, parameter)
);
throw exception;
throw new TccTryException(exception);
}
}

private void executeCommit(O parameter, TccMainLog tccMainLog) {
asyncCommitExecutorService.execute(
new TccMasterLogAsyncCommitRunnable<>(tccLogService, tccMainLog, bizType, this::commitPhase, parameter)
new TccMasterLogAsyncCommitRunnable<>(tccLogService, tccMainLog, bizType, this::commit, parameter)
);
}

private R executeLocalTransaction(O parameter, TccMainLog tccMainLog) {
try {
return localTransactionService.execute(
new TccLocalTransactionSupplier<>(tccLogService, tccMainLog, this::executeLocalTransactionPhase, parameter)
new TccLocalTransactionSupplier<>(tccLogService, tccMainLog, this::executeLocalTransaction, parameter)
);
} catch (Exception exception) {
log.error("业务类型: {}, 业务id : {}, 本地事务执行失败", bizType, parameter.getBizId(), exception);
asyncCommitExecutorService.execute(
new TccMasterLogAsyncCheckRunnable<>(tccLogService, bizType, this::commitPhase, this::cancelPhase, parameter)
new TccMasterLogAsyncCheckRunnable<>(tccLogService, bizType, this::commit, this::cancel, parameter)
);
throw exception;
throw new TccLocalTransactionException(exception);
}
}

Expand All @@ -150,18 +151,18 @@ private R executeLocalTransaction(O parameter, TccMainLog tccMainLog) {
*
* @param object
*/
protected abstract void tryPhase(O object);
protected abstract void attempt(O object);

/**
* 执行本地事务方法和tcc事务日志在一个事务域内处理
*
* @param object
* @return
*/
protected abstract R executeLocalTransactionPhase(O object);
protected abstract R executeLocalTransaction(O object);

protected abstract void commitPhase(O object);
protected abstract void commit(O object);

protected abstract void cancelPhase(O object);
protected abstract void cancel(O object);

}
2 changes: 1 addition & 1 deletion src/main/java/com/damon/tcc/TccSubConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@

public class TccSubConfig {

private final DataSource dataSource;
private ITccSubLogService tccSubLogService;
private ILocalTransactionService localTransactionService;
private String bizType;
private final DataSource dataSource;

public TccSubConfig(ITccSubLogService tccSubLogService, ILocalTransactionService localTransactionService, DataSource dataSource, String bizType) {
this.tccSubLogService = tccSubLogService;
Expand Down
43 changes: 31 additions & 12 deletions src/main/java/com/damon/tcc/TccSubService.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.function.Consumer;
import java.util.function.Function;

/**
* 使用示例
*
Expand All @@ -29,34 +32,50 @@ protected TccSubService(TccSubConfig tccSubConfig) {
this.bizType = tccSubConfig.getBizType();
}

protected R processTry(P parameter) throws TccTryException {
/**
* try执行业务,进行资源预留
*
* @param parameter
* @param attempt
* @return
* @throws TccTryException
*/
protected R attempt(P parameter, Function<P, R> attempt) throws TccTryException {
R result = localTransactionService.execute(() ->
new TccSubLogTryHandler<>(tccSubLogService, this::tryPhase, bizType).execute(parameter)
new TccSubLogTryHandler<>(tccSubLogService, attempt::apply, bizType).execute(parameter)
);
log.info("子事务业务类型: {}, 业务id : {}, try 成功", bizType, parameter.getBizId());
return result;
}

protected void processCommit(P parameter) throws TccCommitException {
/**
* commit预留的资源
*
* @param parameter
* @param commit
* @throws TccCommitException
*/
protected void commit(P parameter, Consumer<P> commit) throws TccCommitException {
localTransactionService.execute(() -> {
new TccSubLogCommitHandler<>(tccSubLogService, this::commitPhase, bizType).execute(parameter);
new TccSubLogCommitHandler<>(tccSubLogService, commit::accept, bizType).execute(parameter);
return null;
});
log.info("子事务业务类型: {}, 业务id : {}, 异步commit成功", bizType, parameter.getBizId());
}

protected void processCancel(P parameter) throws TccCancelException {
/**
* cancel预留的资源
*
* @param parameter
* @param cancel
* @throws TccCancelException
*/
protected void cancel(P parameter, Consumer<P> cancel) throws TccCancelException {
localTransactionService.execute(() -> {
new TccSubLogCancelHandler<>(tccSubLogService, this::cancelPhase, bizType).execute(parameter);
new TccSubLogCancelHandler<>(tccSubLogService, cancel::accept, bizType).execute(parameter);
return null;
});
log.info("子事务业务类型: {}, 业务id : {}, 异步cancel成功", bizType, parameter.getBizId());
}

protected abstract R tryPhase(P parameter);

protected abstract void commitPhase(P parameter);

protected abstract void cancelPhase(P parameter);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.damon.tcc.exception;

public class TccLocalTransactionException extends RuntimeException {
public TccLocalTransactionException(Throwable cause) {
super(cause);
}

}
1 change: 1 addition & 0 deletions src/main/java/com/damon/tcc/main_log/TccMainLog.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public class TccMainLog {
private int checkedTimes;
private Long lastUpdateTime;
private Long createTime;

public TccMainLog(Long bizId) {
Long createTime = System.currentTimeMillis();
this.checkedTimes = 0;
Expand Down
27 changes: 0 additions & 27 deletions src/test/java/com/damon/Application.java

This file was deleted.

14 changes: 14 additions & 0 deletions src/test/java/com/damon/sample/order/OrderApplication.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.damon.sample.order;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;


@SpringBootApplication(scanBasePackages = "com.damon.sample.order")
public class OrderApplication {

public static void main(String[] args) {
SpringApplication.run(OrderApplication.class, args);
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.damon.sample;
package com.damon.sample.order;

import com.damon.Application;
import com.damon.sample.order.OrderSubmitService;
import com.damon.sample.order.app.OrderSubmitAppService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -12,18 +11,18 @@
import java.util.concurrent.Executors;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
@SpringBootTest(classes = OrderApplication.class)
public class TestPerformanceRun {
@Autowired
private OrderSubmitService orderSubmitService;
private OrderSubmitAppService orderSubmitAppService;

@Test
public void test() throws InterruptedException {
ExecutorService executorService = Executors.newFixedThreadPool(100);
for (int i = 0; i < 100; i++) {
for (int i = 0; i < 20; i++) {
executorService.submit(() -> {
for (int j = 0; j < 10000; j++) {
orderSubmitService.execute(12345678L, 100L);
for (int j = 0; j < 100000; j++) {
orderSubmitAppService.submitOrder(12345678L, 100L);
}
});
}
Expand All @@ -38,7 +37,7 @@ public void testTryFailed() throws InterruptedException {
for (int i = 0; i < 100; i++) {
executorService.submit(() -> {
for (int j = 0; j < 10000; j++) {
orderSubmitService.execute(12345679L, 100L);
orderSubmitAppService.submitOrder(12345679L, 100L);
}
});
}
Expand All @@ -49,7 +48,7 @@ public void testTryFailed() throws InterruptedException {

@Test
public void testFailedLog() throws InterruptedException {
orderSubmitService.executeFailedLogCheck();
orderSubmitAppService.executeFailedLogCheck();
Thread.sleep(222222);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,35 +1,34 @@
package com.damon.sample;
package com.damon.sample.order;

import com.damon.Application;
import com.damon.sample.order.OrderSubmitService;
import com.damon.sample.order.app.OrderSubmitAppService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
@SpringBootTest(classes = OrderApplication.class)
public class TestRun {
@Autowired
private OrderSubmitService orderSubmitService;
private OrderSubmitAppService orderSubmitAppService;

@Test
public void test() throws InterruptedException {
orderSubmitService.execute(12345678L, 100L);
orderSubmitAppService.submitOrder(12345678L, 100L);
Thread.sleep(2000);
}

@Test
public void testTryFailed() throws InterruptedException {
//不存在的用户id
orderSubmitService.execute(12345679L, 100L);
orderSubmitAppService.submitOrder(12345679L, 100L);
Thread.sleep(2000);
}

@Test
public void testFailedLog() throws InterruptedException {
orderSubmitService.executeFailedLogCheck();
orderSubmitAppService.executeFailedLogCheck();
Thread.sleep(2000);
}

Expand Down
Loading

0 comments on commit 56a8ee8

Please sign in to comment.