diff --git a/example/example-app/example-app-cmd-domain/pom.xml b/example/example-app/example-app-cmd-domain/pom.xml index 743a37950..b2ed6e26d 100644 --- a/example/example-app/example-app-cmd-domain/pom.xml +++ b/example/example-app/example-app-cmd-domain/pom.xml @@ -6,7 +6,7 @@ com.codingapi.springboot example-app - 3.4.42 + 3.4.43 ../pom.xml diff --git a/example/example-app/example-app-cmd-meta/pom.xml b/example/example-app/example-app-cmd-meta/pom.xml index 5852c80b9..891ead1bc 100644 --- a/example/example-app/example-app-cmd-meta/pom.xml +++ b/example/example-app/example-app-cmd-meta/pom.xml @@ -6,7 +6,7 @@ com.codingapi.springboot example-app - 3.4.42 + 3.4.43 ../pom.xml diff --git a/example/example-app/example-app-query/pom.xml b/example/example-app/example-app-query/pom.xml index fdba18afe..b76513f01 100644 --- a/example/example-app/example-app-query/pom.xml +++ b/example/example-app/example-app-query/pom.xml @@ -6,7 +6,7 @@ com.codingapi.springboot example-app - 3.4.42 + 3.4.43 ../pom.xml diff --git a/example/example-app/pom.xml b/example/example-app/pom.xml index b48165dee..bb03aa715 100644 --- a/example/example-app/pom.xml +++ b/example/example-app/pom.xml @@ -6,7 +6,7 @@ com.codingapi.springboot springboot-example - 3.4.42 + 3.4.43 ../pom.xml pom diff --git a/example/example-domain/example-domain-leave/pom.xml b/example/example-domain/example-domain-leave/pom.xml index 15def6268..9a7eb8912 100644 --- a/example/example-domain/example-domain-leave/pom.xml +++ b/example/example-domain/example-domain-leave/pom.xml @@ -6,7 +6,7 @@ com.codingapi.springboot example-domain - 3.4.42 + 3.4.43 ../pom.xml diff --git a/example/example-domain/example-domain-user/pom.xml b/example/example-domain/example-domain-user/pom.xml index b43d2fb10..47ec402a5 100644 --- a/example/example-domain/example-domain-user/pom.xml +++ b/example/example-domain/example-domain-user/pom.xml @@ -6,7 +6,7 @@ com.codingapi.springboot example-domain - 3.4.42 + 3.4.43 ../pom.xml diff --git a/example/example-domain/pom.xml b/example/example-domain/pom.xml index c0b481cee..7c688f3fe 100644 --- a/example/example-domain/pom.xml +++ b/example/example-domain/pom.xml @@ -5,7 +5,7 @@ com.codingapi.springboot springboot-example - 3.4.42 + 3.4.43 ../pom.xml 4.0.0 diff --git a/example/example-infra/example-infra-flow/pom.xml b/example/example-infra/example-infra-flow/pom.xml index d7e753a1a..884ef1bcc 100644 --- a/example/example-infra/example-infra-flow/pom.xml +++ b/example/example-infra/example-infra-flow/pom.xml @@ -5,7 +5,7 @@ com.codingapi.springboot example-infra - 3.4.42 + 3.4.43 ../pom.xml diff --git a/example/example-infra/example-infra-jpa/pom.xml b/example/example-infra/example-infra-jpa/pom.xml index e94e7b39a..067db0c4a 100644 --- a/example/example-infra/example-infra-jpa/pom.xml +++ b/example/example-infra/example-infra-jpa/pom.xml @@ -5,7 +5,7 @@ com.codingapi.springboot example-infra - 3.4.42 + 3.4.43 ../pom.xml diff --git a/example/example-infra/example-infra-security/pom.xml b/example/example-infra/example-infra-security/pom.xml index 87bf23d76..bfdad5a66 100644 --- a/example/example-infra/example-infra-security/pom.xml +++ b/example/example-infra/example-infra-security/pom.xml @@ -6,7 +6,7 @@ com.codingapi.springboot example-infra - 3.4.42 + 3.4.43 ../pom.xml diff --git a/example/example-infra/pom.xml b/example/example-infra/pom.xml index 3d5944f9b..36678a2c6 100644 --- a/example/example-infra/pom.xml +++ b/example/example-infra/pom.xml @@ -6,7 +6,7 @@ com.codingapi.springboot springboot-example - 3.4.42 + 3.4.43 ../pom.xml pom diff --git a/example/example-interface/pom.xml b/example/example-interface/pom.xml index d2459415f..2d1acfe15 100644 --- a/example/example-interface/pom.xml +++ b/example/example-interface/pom.xml @@ -6,7 +6,7 @@ com.codingapi.springboot springboot-example - 3.4.42 + 3.4.43 example-interface diff --git a/example/example-server/pom.xml b/example/example-server/pom.xml index b86ca8828..1bbbd6c1c 100644 --- a/example/example-server/pom.xml +++ b/example/example-server/pom.xml @@ -5,7 +5,7 @@ springboot-example com.codingapi.springboot - 3.4.42 + 3.4.43 4.0.0 diff --git a/example/pom.xml b/example/pom.xml index cfa5cc3fd..34595dcf6 100644 --- a/example/pom.xml +++ b/example/pom.xml @@ -19,7 +19,7 @@ springboot-example - 3.4.42 + 3.4.43 springboot-example springboot-example project for Spring Boot diff --git a/pom.xml b/pom.xml index b6222bcae..89da7a7ae 100644 --- a/pom.xml +++ b/pom.xml @@ -12,7 +12,7 @@ com.codingapi.springboot springboot-parent - 3.4.42 + 3.4.43 https://github.com/codingapi/springboot-framewrok springboot-parent diff --git a/springboot-starter-data-authorization/pom.xml b/springboot-starter-data-authorization/pom.xml index ce61c113d..863ba9bc6 100644 --- a/springboot-starter-data-authorization/pom.xml +++ b/springboot-starter-data-authorization/pom.xml @@ -6,7 +6,7 @@ com.codingapi.springboot springboot-parent - 3.4.42 + 3.4.43 springboot-starter-data-authorization diff --git a/springboot-starter-data-fast/pom.xml b/springboot-starter-data-fast/pom.xml index 41c33ae5b..9d5268063 100644 --- a/springboot-starter-data-fast/pom.xml +++ b/springboot-starter-data-fast/pom.xml @@ -5,7 +5,7 @@ springboot-parent com.codingapi.springboot - 3.4.42 + 3.4.43 4.0.0 diff --git a/springboot-starter-flow/pom.xml b/springboot-starter-flow/pom.xml index 90c6e710f..0b0c96b32 100644 --- a/springboot-starter-flow/pom.xml +++ b/springboot-starter-flow/pom.xml @@ -6,7 +6,7 @@ springboot-parent com.codingapi.springboot - 3.4.42 + 3.4.43 springboot-starter-flow diff --git a/springboot-starter-security/pom.xml b/springboot-starter-security/pom.xml index 0e51250d2..e96320c36 100644 --- a/springboot-starter-security/pom.xml +++ b/springboot-starter-security/pom.xml @@ -6,7 +6,7 @@ springboot-parent com.codingapi.springboot - 3.4.42 + 3.4.43 springboot-starter-security diff --git a/springboot-starter/pom.xml b/springboot-starter/pom.xml index bc5fdbcb2..b0fe8b6d5 100644 --- a/springboot-starter/pom.xml +++ b/springboot-starter/pom.xml @@ -5,7 +5,7 @@ com.codingapi.springboot springboot-parent - 3.4.42 + 3.4.43 springboot-starter diff --git a/springboot-starter/src/main/java/com/codingapi/springboot/framework/event/SpringDefaultEventHandler.java b/springboot-starter/src/main/java/com/codingapi/springboot/framework/event/SpringDefaultEventHandler.java new file mode 100644 index 000000000..2901f3ae5 --- /dev/null +++ b/springboot-starter/src/main/java/com/codingapi/springboot/framework/event/SpringDefaultEventHandler.java @@ -0,0 +1,53 @@ +package com.codingapi.springboot.framework.event; + +import lombok.extern.slf4j.Slf4j; +import org.springframework.context.event.EventListener; + +import java.util.List; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; + +/** + * handler订阅的Spring触发器,在异步的情况下可配置多线程。 + */ +@Slf4j +public class SpringDefaultEventHandler extends SpringEventHandler{ + + /** + * 异步多线程的KEY + * 可通过 System.setProperty(THREAD_KEY,"20") 调整线程数 + */ + public final static String THREAD_KEY = "Handler.ThreadPools"; + + private final ExecutorService executorService = Executors + .newFixedThreadPool(Integer.parseInt(System.getProperty(THREAD_KEY, "10"))); + + public SpringDefaultEventHandler(List handlers) { + ApplicationHandlerUtils.getInstance().addHandlers(handlers); + log.info("SpringDefaultEventHandler Success Initial."); + } + + @EventListener + public synchronized void dispatch(DomainEvent domainEvent) { + String traceId = domainEvent.getTraceId(); + + if (domainEvent.isSync()) { + try { + EventTraceContext.getInstance().createEventKey(traceId); + ApplicationHandlerUtils.getInstance().handler(domainEvent.getEvent()); + } finally { + EventTraceContext.getInstance().checkEventState(); + } + } else { + executorService.execute(() -> { + try { + EventTraceContext.getInstance().createEventKey(traceId); + ApplicationHandlerUtils.getInstance().handler(domainEvent.getEvent()); + } finally { + EventTraceContext.getInstance().checkEventState(); + } + }); + } + } + +} diff --git a/springboot-starter/src/main/java/com/codingapi/springboot/framework/event/SpringEventHandler.java b/springboot-starter/src/main/java/com/codingapi/springboot/framework/event/SpringEventHandler.java index 141d3c537..a2db53b30 100644 --- a/springboot-starter/src/main/java/com/codingapi/springboot/framework/event/SpringEventHandler.java +++ b/springboot-starter/src/main/java/com/codingapi/springboot/framework/event/SpringEventHandler.java @@ -1,51 +1,4 @@ package com.codingapi.springboot.framework.event; -import lombok.extern.slf4j.Slf4j; -import org.springframework.context.ApplicationListener; - -import java.util.List; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; - -/** - * handler订阅的Spring触发器,在异步的情况下可配置多线程。 - */ -@Slf4j -public class SpringEventHandler implements ApplicationListener { - - /** - * 异步多线程的KEY - * 可通过 System.setProperty(THREAD_KEY,"20") 调整线程数 - */ - public final static String THREAD_KEY = "Handler.ThreadPools"; - - private final ExecutorService executorService = Executors.newFixedThreadPool(Integer.parseInt(System.getProperty(THREAD_KEY, "10"))); - - public SpringEventHandler(List handlers) { - ApplicationHandlerUtils.getInstance().addHandlers(handlers); - } - - @Override - public synchronized void onApplicationEvent(DomainEvent domainEvent) { - String traceId = domainEvent.getTraceId(); - - if (domainEvent.isSync()) { - try { - EventTraceContext.getInstance().createEventKey(traceId); - ApplicationHandlerUtils.getInstance().handler(domainEvent.getEvent()); - } finally { - EventTraceContext.getInstance().checkEventState(); - } - } else { - executorService.execute(() -> { - try { - EventTraceContext.getInstance().createEventKey(traceId); - ApplicationHandlerUtils.getInstance().handler(domainEvent.getEvent()); - } finally { - EventTraceContext.getInstance().checkEventState(); - } - }); - } - } - +public abstract class SpringEventHandler { } diff --git a/springboot-starter/src/main/java/com/codingapi/springboot/framework/event/SpringHandlerConfiguration.java b/springboot-starter/src/main/java/com/codingapi/springboot/framework/event/SpringHandlerConfiguration.java index e3afeb12b..2900e8955 100644 --- a/springboot-starter/src/main/java/com/codingapi/springboot/framework/event/SpringHandlerConfiguration.java +++ b/springboot-starter/src/main/java/com/codingapi/springboot/framework/event/SpringHandlerConfiguration.java @@ -1,6 +1,8 @@ package com.codingapi.springboot.framework.event; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -10,8 +12,17 @@ public class SpringHandlerConfiguration { @Bean - public SpringEventHandler springEventHandler(@Autowired(required = false) List handlers) { - return new SpringEventHandler(handlers); + @ConditionalOnProperty(value = "codingapi.framework.event.transaction.enable",havingValue = "true") + public SpringEventHandler springTransactionEventHandler(@Autowired(required = false) List handlers) { + return new SpringTransactionEventHandler(handlers); } + @Bean + @ConditionalOnMissingBean + public SpringEventHandler springDefaultEventHandler(@Autowired(required = false) List handlers) { + return new SpringDefaultEventHandler(handlers); + } + + + } diff --git a/springboot-starter/src/main/java/com/codingapi/springboot/framework/event/SpringTransactionEventHandler.java b/springboot-starter/src/main/java/com/codingapi/springboot/framework/event/SpringTransactionEventHandler.java new file mode 100644 index 000000000..3d84f07af --- /dev/null +++ b/springboot-starter/src/main/java/com/codingapi/springboot/framework/event/SpringTransactionEventHandler.java @@ -0,0 +1,54 @@ +package com.codingapi.springboot.framework.event; + +import lombok.extern.slf4j.Slf4j; +import org.springframework.transaction.event.TransactionPhase; +import org.springframework.transaction.event.TransactionalEventListener; + +import java.util.List; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; + +/** + * handler订阅的Spring触发器,在异步的情况下可配置多线程。 + */ +@Slf4j +public class SpringTransactionEventHandler extends SpringEventHandler{ + + /** + * 异步多线程的KEY + * 可通过 System.setProperty(THREAD_KEY,"20") 调整线程数 + */ + public final static String THREAD_KEY = "Handler.ThreadPools"; + + private final ExecutorService executorService = Executors + .newFixedThreadPool(Integer.parseInt(System.getProperty(THREAD_KEY, "10"))); + + public SpringTransactionEventHandler(List handlers) { + ApplicationHandlerUtils.getInstance().addHandlers(handlers); + log.info("SpringTransactionEventHandler Success Initial."); + } + + @TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT, fallbackExecution = true) + public synchronized void dispatch(DomainEvent domainEvent) { + String traceId = domainEvent.getTraceId(); + + if (domainEvent.isSync()) { + try { + EventTraceContext.getInstance().createEventKey(traceId); + ApplicationHandlerUtils.getInstance().handler(domainEvent.getEvent()); + } finally { + EventTraceContext.getInstance().checkEventState(); + } + } else { + executorService.execute(() -> { + try { + EventTraceContext.getInstance().createEventKey(traceId); + ApplicationHandlerUtils.getInstance().handler(domainEvent.getEvent()); + } finally { + EventTraceContext.getInstance().checkEventState(); + } + }); + } + } + +} diff --git a/springboot-starter/src/test/java/com/codingapi/springboot/framework/FrameworkApplicationEventTests.java b/springboot-starter/src/test/java/com/codingapi/springboot/framework/FrameworkApplicationEventTests.java new file mode 100644 index 000000000..25c976af5 --- /dev/null +++ b/springboot-starter/src/test/java/com/codingapi/springboot/framework/FrameworkApplicationEventTests.java @@ -0,0 +1,34 @@ +package com.codingapi.springboot.framework; + +import com.codingapi.springboot.framework.repository.MyTestRepository; +import com.codingapi.springboot.framework.service.MyTestService; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import static org.junit.jupiter.api.Assertions.*; + +@SpringBootTest +class FrameworkApplicationEventTests { + + @Autowired + private MyTestService myTestService; + @Autowired + private MyTestRepository repository; + + @Test + void transactionTest1() { + repository.deleteAll(); + assertThrows(ArithmeticException.class,()->{ + myTestService.save1(); + }); + assertEquals(0, repository.findAll().size()); + } + + @Test + void transactionTest2() { + repository.deleteAll(); + myTestService.save2(); + assertEquals(1, repository.findAll().size()); + } + +} diff --git a/springboot-starter/src/test/java/com/codingapi/springboot/framework/entity/MyTest.java b/springboot-starter/src/test/java/com/codingapi/springboot/framework/entity/MyTest.java new file mode 100644 index 000000000..8061cb32f --- /dev/null +++ b/springboot-starter/src/test/java/com/codingapi/springboot/framework/entity/MyTest.java @@ -0,0 +1,17 @@ +package com.codingapi.springboot.framework.entity; + +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; +import lombok.Data; + +@Data +@Entity +public class MyTest { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + private String name; +} diff --git a/springboot-starter/src/test/java/com/codingapi/springboot/framework/event/MyTestAsyncEvent.java b/springboot-starter/src/test/java/com/codingapi/springboot/framework/event/MyTestAsyncEvent.java new file mode 100644 index 000000000..1d2fe477f --- /dev/null +++ b/springboot-starter/src/test/java/com/codingapi/springboot/framework/event/MyTestAsyncEvent.java @@ -0,0 +1,12 @@ +package com.codingapi.springboot.framework.event; + +import com.codingapi.springboot.framework.entity.MyTest; +import lombok.AllArgsConstructor; +import lombok.Getter; + +@Getter +@AllArgsConstructor +public class MyTestAsyncEvent implements IAsyncEvent{ + + private final MyTest myTest; +} diff --git a/springboot-starter/src/test/java/com/codingapi/springboot/framework/event/MyTestSyncEvent.java b/springboot-starter/src/test/java/com/codingapi/springboot/framework/event/MyTestSyncEvent.java new file mode 100644 index 000000000..869b75852 --- /dev/null +++ b/springboot-starter/src/test/java/com/codingapi/springboot/framework/event/MyTestSyncEvent.java @@ -0,0 +1,12 @@ +package com.codingapi.springboot.framework.event; + +import com.codingapi.springboot.framework.entity.MyTest; +import lombok.AllArgsConstructor; +import lombok.Getter; + +@Getter +@AllArgsConstructor +public class MyTestSyncEvent implements ISyncEvent{ + + private final MyTest myTest; +} diff --git a/springboot-starter/src/test/java/com/codingapi/springboot/framework/handler/MyTestAsyncHandler.java b/springboot-starter/src/test/java/com/codingapi/springboot/framework/handler/MyTestAsyncHandler.java new file mode 100644 index 000000000..4ed2b689a --- /dev/null +++ b/springboot-starter/src/test/java/com/codingapi/springboot/framework/handler/MyTestAsyncHandler.java @@ -0,0 +1,16 @@ +package com.codingapi.springboot.framework.handler; + +import com.codingapi.springboot.framework.event.IHandler; +import com.codingapi.springboot.framework.event.MyTestAsyncEvent; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Component; + +@Slf4j +@Component +public class MyTestAsyncHandler implements IHandler { + + @Override + public void handler(MyTestAsyncEvent event) { + log.info("event:{}",event.getMyTest()); + } +} diff --git a/springboot-starter/src/test/java/com/codingapi/springboot/framework/handler/MyTestSyncHandler.java b/springboot-starter/src/test/java/com/codingapi/springboot/framework/handler/MyTestSyncHandler.java new file mode 100644 index 000000000..4a9042c13 --- /dev/null +++ b/springboot-starter/src/test/java/com/codingapi/springboot/framework/handler/MyTestSyncHandler.java @@ -0,0 +1,16 @@ +package com.codingapi.springboot.framework.handler; + +import com.codingapi.springboot.framework.event.IHandler; +import com.codingapi.springboot.framework.event.MyTestSyncEvent; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Component; + +@Slf4j +@Component +public class MyTestSyncHandler implements IHandler { + + @Override + public void handler(MyTestSyncEvent event) { + log.info("event:{}",event.getMyTest()); + } +} diff --git a/springboot-starter/src/test/java/com/codingapi/springboot/framework/repository/MyTestRepository.java b/springboot-starter/src/test/java/com/codingapi/springboot/framework/repository/MyTestRepository.java new file mode 100644 index 000000000..1a6831889 --- /dev/null +++ b/springboot-starter/src/test/java/com/codingapi/springboot/framework/repository/MyTestRepository.java @@ -0,0 +1,10 @@ +package com.codingapi.springboot.framework.repository; + +import com.codingapi.springboot.framework.entity.MyTest; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface MyTestRepository extends JpaRepository { + +} diff --git a/springboot-starter/src/test/java/com/codingapi/springboot/framework/service/MyTestService.java b/springboot-starter/src/test/java/com/codingapi/springboot/framework/service/MyTestService.java new file mode 100644 index 000000000..d05547919 --- /dev/null +++ b/springboot-starter/src/test/java/com/codingapi/springboot/framework/service/MyTestService.java @@ -0,0 +1,37 @@ +package com.codingapi.springboot.framework.service; + +import com.codingapi.springboot.framework.entity.MyTest; +import com.codingapi.springboot.framework.event.EventPusher; +import com.codingapi.springboot.framework.event.MyTestAsyncEvent; +import com.codingapi.springboot.framework.event.MyTestSyncEvent; +import com.codingapi.springboot.framework.repository.MyTestRepository; +import jakarta.transaction.Transactional; +import lombok.AllArgsConstructor; +import org.springframework.stereotype.Service; + +@Service +@AllArgsConstructor +public class MyTestService { + + private final MyTestRepository testRepository; + + @Transactional + public void save1(){ + MyTest myTest = new MyTest(); + myTest.setName("test"); + testRepository.save(myTest); + EventPusher.push(new MyTestSyncEvent(myTest)); + EventPusher.push(new MyTestAsyncEvent(myTest)); + int value = 100 / 0; + System.out.println(value); + } + + @Transactional + public void save2(){ + MyTest myTest = new MyTest(); + myTest.setName("test"); + testRepository.save(myTest); + EventPusher.push(new MyTestSyncEvent(myTest)); + EventPusher.push(new MyTestAsyncEvent(myTest)); + } +} diff --git a/springboot-starter/src/test/resources/application.properties b/springboot-starter/src/test/resources/application.properties index 559cfc80c..68dd11835 100644 --- a/springboot-starter/src/test/resources/application.properties +++ b/springboot-starter/src/test/resources/application.properties @@ -3,4 +3,6 @@ spring.datasource.driver-class-name=org.h2.Driver spring.datasource.url=jdbc:h2:file:./test.db spring.jpa.database-platform=org.hibernate.dialect.H2Dialect spring.jpa.hibernate.ddl-auto=create-drop -spring.jpa.show-sql=true \ No newline at end of file +spring.jpa.show-sql=true + +codingapi.framework.event.transaction.enable=true \ No newline at end of file