Skip to content

update version and event support global handler & handler order #78

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion example/example-application/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>springboot-example</artifactId>
<groupId>com.codingapi.springboot</groupId>
<version>3.3.5</version>
<version>3.3.6</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public void handler(CEvent event) {
log.info("c event:{},eventKey:{}", event, EventTraceContext.getInstance().getEventKey());

// EventPusher.push(new AEvent());
throw new RuntimeException("c handler error");
// throw new RuntimeException("c handler error");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.codingapi.example.handler;

import com.codingapi.springboot.framework.event.IEvent;
import com.codingapi.springboot.framework.event.IHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class GlobalEventHandler implements IHandler<IEvent> {

@Override
public void handler(IEvent event) {
log.info("global event:{}", event);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.codingapi.example.handler;

import com.codingapi.example.event.AEvent;
import com.codingapi.example.event.TestEvent;
import com.codingapi.example.infra.entity.TestEntity;
import com.codingapi.example.infra.jpa.TestEntityRepository;
import com.codingapi.springboot.framework.event.EventPusher;
import com.codingapi.springboot.framework.event.IHandler;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Repository;

@Repository
@AllArgsConstructor
public class Test2Handler implements IHandler<TestEvent> {

private TestEntityRepository testEntityRepository;

@Override
public void handler(TestEvent event) {
TestEntity entity = new TestEntity(event.getName()+"123");
testEntityRepository.save(entity);

new Thread(()->{
EventPusher.push(new AEvent());
}).start();

// new Thread(()->{
// EventPusher.push(new AEvent());
// }).start();
}


@Override
public int order() {
return 100;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,13 @@ public void handler(TestEvent event) {
EventPusher.push(new AEvent());
}).start();

new Thread(()->{
EventPusher.push(new AEvent());
}).start();
// new Thread(()->{
// EventPusher.push(new AEvent());
// }).start();
}


@Override
public int order() {
return 10;
}
}
2 changes: 1 addition & 1 deletion example/example-domain/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>springboot-example</artifactId>
<groupId>com.codingapi.springboot</groupId>
<version>3.3.5</version>
<version>3.3.6</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion example/example-infra-flow/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>springboot-example</artifactId>
<groupId>com.codingapi.springboot</groupId>
<version>3.3.5</version>
<version>3.3.6</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion example/example-infra-jpa/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>springboot-example</artifactId>
<groupId>com.codingapi.springboot</groupId>
<version>3.3.5</version>
<version>3.3.6</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion example/example-server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>springboot-example</artifactId>
<groupId>com.codingapi.springboot</groupId>
<version>3.3.5</version>
<version>3.3.6</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion example/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
</parent>

<artifactId>springboot-example</artifactId>
<version>3.3.5</version>
<version>3.3.6</version>

<name>springboot-example</name>
<description>springboot-example project for Spring Boot</description>
Expand Down
40 changes: 24 additions & 16 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.3.4</version>
<version>3.3.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<groupId>com.codingapi.springboot</groupId>
<artifactId>springboot-parent</artifactId>
<version>3.3.5</version>
<version>3.3.6</version>

<url>https://github.com/codingapi/springboot-framewrok</url>
<name>springboot-parent</name>
Expand All @@ -31,18 +31,19 @@
<nexus.staging.maven.plugin>1.6.13</nexus.staging.maven.plugin>
<maven.gpg.plugin>3.1.0</maven.gpg.plugin>
<codingapi.framework.version>${project.version}</codingapi.framework.version>
<fastjson.version>2.0.42</fastjson.version>
<jsonwebtoken.jjwt.version>0.12.5</jsonwebtoken.jjwt.version>
<commons-io.version>2.15.0</commons-io.version>
<fastjson.version>2.0.53</fastjson.version>
<jsonwebtoken.jjwt.version>0.12.6</jsonwebtoken.jjwt.version>
<commons-io.version>2.17.0</commons-io.version>
<commons-lang3.version>3.17.0</commons-lang3.version>
<commons-dbutils.version>1.8.1</commons-dbutils.version>
<commons-text.version>1.11.0</commons-text.version>
<commons-text.version>1.12.0</commons-text.version>
<org.reflections.version>0.10.2</org.reflections.version>
<perf4j.version>0.9.16</perf4j.version>
<bcprov-jdk18on.version>1.77</bcprov-jdk18on.version>
<bcprov-jdk18on.version>1.79</bcprov-jdk18on.version>
<commons-crypto.version>1.2.0</commons-crypto.version>
<snakeyaml.version>2.2</snakeyaml.version>
<apache-groovy.version>4.0.15</apache-groovy.version>
<h2.version>2.2.224</h2.version>
<apache-groovy.version>4.0.24</apache-groovy.version>
<h2.version>2.3.232</h2.version>
<esotericsoftware.kryo.version>5.6.2</esotericsoftware.kryo.version>
</properties>

Expand Down Expand Up @@ -108,6 +109,19 @@
<version>${commons-crypto.version}</version>
</dependency>

<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>${commons-io.version}</version>
</dependency>

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${commons-lang3.version}</version>
</dependency>


<dependency>
<groupId>org.perf4j</groupId>
<artifactId>perf4j</artifactId>
Expand Down Expand Up @@ -168,12 +182,6 @@
<version>${codingapi.framework.version}</version>
</dependency>

<dependency>
<groupId>commons-dbutils</groupId>
<artifactId>commons-dbutils</artifactId>
<version>${commons-dbutils.version}</version>
</dependency>

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-text</artifactId>
Expand Down Expand Up @@ -301,7 +309,7 @@
<plugin>
<groupId>org.openclover</groupId>
<artifactId>clover-maven-plugin</artifactId>
<version>4.4.1</version>
<version>4.5.2</version>
<configuration>
<generateHtml>true</generateHtml>
<generateXml>true</generateXml>
Expand Down
2 changes: 1 addition & 1 deletion springboot-starter-data-fast/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>springboot-parent</artifactId>
<groupId>com.codingapi.springboot</groupId>
<version>3.3.5</version>
<version>3.3.6</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
26 changes: 2 additions & 24 deletions springboot-starter-flow/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<artifactId>springboot-parent</artifactId>
<groupId>com.codingapi.springboot</groupId>
<version>3.3.5</version>
<version>3.3.6</version>
</parent>

<name>springboot-starter-flow</name>
Expand Down Expand Up @@ -45,28 +45,6 @@

</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.12</version>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>


</project>
2 changes: 1 addition & 1 deletion springboot-starter-security/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<artifactId>springboot-parent</artifactId>
<groupId>com.codingapi.springboot</groupId>
<version>3.3.5</version>
<version>3.3.6</version>
</parent>

<artifactId>springboot-starter-security</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion springboot-starter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.codingapi.springboot</groupId>
<artifactId>springboot-parent</artifactId>
<version>3.3.5</version>
<version>3.3.6</version>
</parent>
<artifactId>springboot-starter</artifactId>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import com.codingapi.springboot.framework.exception.EventException;
import com.codingapi.springboot.framework.exception.EventLoopException;
import org.springframework.core.ResolvableType;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

class ApplicationHandlerUtils implements IHandler<IEvent> {
Expand Down Expand Up @@ -40,18 +42,37 @@ public void addHandler(IHandler handler) {
}
}

/**
* 获取订阅的事件类型
*/
private Class<?> getHandlerEventClass(IHandler<?> handler) {
ResolvableType resolvableType = ResolvableType.forClass(handler.getClass()).as(IHandler.class);
return resolvableType.getGeneric(0).resolve();
}


@Override
public void handler(IEvent event) {
Class<?> eventClass = event.getClass();

List<IHandler<IEvent>> matchHandlers = handlers
.stream()
.filter(handler -> {
Class<?> targetClass = getHandlerEventClass(handler);
return targetClass.isAssignableFrom(eventClass);
})
.sorted(Comparator.comparingInt(IHandler::order))
.toList();

if (matchHandlers.isEmpty()) {
return;
}

List<Exception> errorStack = new ArrayList<>();
boolean throwException = false;
for (IHandler<IEvent> handler : handlers) {
boolean hasThrowException = false;
for (IHandler<IEvent> handler : matchHandlers) {
try {
Class<?> targetClass = handler.getHandlerEventClass();
if (eventClass.equals(targetClass)) {
handler.handler(event);
}
handler.handler(event);
} catch (Exception e) {
if (e instanceof EventLoopException) {
throw e;
Expand All @@ -60,12 +81,12 @@ public void handler(IEvent event) {
handler.error(e);
errorStack.add(e);
} catch (Exception err) {
throwException = true;
hasThrowException = true;
errorStack.add(err);
}
}
}
if(throwException){
if (hasThrowException) {
throw new EventException(errorStack);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,4 @@
*/
public interface IEvent extends Serializable {


}
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
package com.codingapi.springboot.framework.event;

import org.springframework.core.ResolvableType;

/**
* handler 订阅
*
* @param <T> Event 类型
*/
public interface IHandler<T extends IEvent> {

/**
* 事件订阅排序
* 在同样的事件中,可以通过order来控制订阅的顺序
*/
default int order() {
return 0;
}

/**
* 订阅触发
*
Expand All @@ -27,13 +33,6 @@ default void error(Exception exception) throws Exception {
}


/**
* 获取订阅的事件类型
*/
default Class<?> getHandlerEventClass() {
ResolvableType resolvableType = ResolvableType.forClass(getClass()).as(IHandler.class);
return resolvableType.getGeneric(0).resolve();
}


}