Skip to content

Commit f62f973

Browse files
authored
Merge pull request #78 from codingapi/dev
update version and event support global handler & handler order
2 parents b2846ca + bfa2715 commit f62f973

File tree

18 files changed

+133
-72
lines changed

18 files changed

+133
-72
lines changed

example/example-application/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<artifactId>springboot-example</artifactId>
77
<groupId>com.codingapi.springboot</groupId>
8-
<version>3.3.5</version>
8+
<version>3.3.6</version>
99
</parent>
1010
<modelVersion>4.0.0</modelVersion>
1111

example/example-application/src/main/java/com/codingapi/example/handler/CHandler.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public void handler(CEvent event) {
1515
log.info("c event:{},eventKey:{}", event, EventTraceContext.getInstance().getEventKey());
1616

1717
// EventPusher.push(new AEvent());
18-
throw new RuntimeException("c handler error");
18+
// throw new RuntimeException("c handler error");
1919
}
2020

2121
@Override
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.codingapi.example.handler;
2+
3+
import com.codingapi.springboot.framework.event.IEvent;
4+
import com.codingapi.springboot.framework.event.IHandler;
5+
import lombok.extern.slf4j.Slf4j;
6+
import org.springframework.stereotype.Service;
7+
8+
@Slf4j
9+
@Service
10+
public class GlobalEventHandler implements IHandler<IEvent> {
11+
12+
@Override
13+
public void handler(IEvent event) {
14+
log.info("global event:{}", event);
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.codingapi.example.handler;
2+
3+
import com.codingapi.example.event.AEvent;
4+
import com.codingapi.example.event.TestEvent;
5+
import com.codingapi.example.infra.entity.TestEntity;
6+
import com.codingapi.example.infra.jpa.TestEntityRepository;
7+
import com.codingapi.springboot.framework.event.EventPusher;
8+
import com.codingapi.springboot.framework.event.IHandler;
9+
import lombok.AllArgsConstructor;
10+
import org.springframework.stereotype.Repository;
11+
12+
@Repository
13+
@AllArgsConstructor
14+
public class Test2Handler implements IHandler<TestEvent> {
15+
16+
private TestEntityRepository testEntityRepository;
17+
18+
@Override
19+
public void handler(TestEvent event) {
20+
TestEntity entity = new TestEntity(event.getName()+"123");
21+
testEntityRepository.save(entity);
22+
23+
new Thread(()->{
24+
EventPusher.push(new AEvent());
25+
}).start();
26+
27+
// new Thread(()->{
28+
// EventPusher.push(new AEvent());
29+
// }).start();
30+
}
31+
32+
33+
@Override
34+
public int order() {
35+
return 100;
36+
}
37+
}

example/example-application/src/main/java/com/codingapi/example/handler/TestHandler.java

+7-4
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,13 @@ public void handler(TestEvent event) {
2424
EventPusher.push(new AEvent());
2525
}).start();
2626

27-
new Thread(()->{
28-
EventPusher.push(new AEvent());
29-
}).start();
27+
// new Thread(()->{
28+
// EventPusher.push(new AEvent());
29+
// }).start();
3030
}
3131

32-
32+
@Override
33+
public int order() {
34+
return 10;
35+
}
3336
}

example/example-domain/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<artifactId>springboot-example</artifactId>
77
<groupId>com.codingapi.springboot</groupId>
8-
<version>3.3.5</version>
8+
<version>3.3.6</version>
99
</parent>
1010
<modelVersion>4.0.0</modelVersion>
1111

example/example-infra-flow/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<artifactId>springboot-example</artifactId>
77
<groupId>com.codingapi.springboot</groupId>
8-
<version>3.3.5</version>
8+
<version>3.3.6</version>
99
</parent>
1010
<modelVersion>4.0.0</modelVersion>
1111

example/example-infra-jpa/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<artifactId>springboot-example</artifactId>
77
<groupId>com.codingapi.springboot</groupId>
8-
<version>3.3.5</version>
8+
<version>3.3.6</version>
99
</parent>
1010
<modelVersion>4.0.0</modelVersion>
1111

example/example-server/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<artifactId>springboot-example</artifactId>
77
<groupId>com.codingapi.springboot</groupId>
8-
<version>3.3.5</version>
8+
<version>3.3.6</version>
99
</parent>
1010
<modelVersion>4.0.0</modelVersion>
1111

example/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
</parent>
1818

1919
<artifactId>springboot-example</artifactId>
20-
<version>3.3.5</version>
20+
<version>3.3.6</version>
2121

2222
<name>springboot-example</name>
2323
<description>springboot-example project for Spring Boot</description>

pom.xml

+24-16
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66
<parent>
77
<groupId>org.springframework.boot</groupId>
88
<artifactId>spring-boot-starter-parent</artifactId>
9-
<version>3.3.4</version>
9+
<version>3.3.5</version>
1010
<relativePath/> <!-- lookup parent from repository -->
1111
</parent>
1212

1313
<groupId>com.codingapi.springboot</groupId>
1414
<artifactId>springboot-parent</artifactId>
15-
<version>3.3.5</version>
15+
<version>3.3.6</version>
1616

1717
<url>https://github.com/codingapi/springboot-framewrok</url>
1818
<name>springboot-parent</name>
@@ -31,18 +31,19 @@
3131
<nexus.staging.maven.plugin>1.6.13</nexus.staging.maven.plugin>
3232
<maven.gpg.plugin>3.1.0</maven.gpg.plugin>
3333
<codingapi.framework.version>${project.version}</codingapi.framework.version>
34-
<fastjson.version>2.0.42</fastjson.version>
35-
<jsonwebtoken.jjwt.version>0.12.5</jsonwebtoken.jjwt.version>
36-
<commons-io.version>2.15.0</commons-io.version>
34+
<fastjson.version>2.0.53</fastjson.version>
35+
<jsonwebtoken.jjwt.version>0.12.6</jsonwebtoken.jjwt.version>
36+
<commons-io.version>2.17.0</commons-io.version>
37+
<commons-lang3.version>3.17.0</commons-lang3.version>
3738
<commons-dbutils.version>1.8.1</commons-dbutils.version>
38-
<commons-text.version>1.11.0</commons-text.version>
39+
<commons-text.version>1.12.0</commons-text.version>
3940
<org.reflections.version>0.10.2</org.reflections.version>
4041
<perf4j.version>0.9.16</perf4j.version>
41-
<bcprov-jdk18on.version>1.77</bcprov-jdk18on.version>
42+
<bcprov-jdk18on.version>1.79</bcprov-jdk18on.version>
4243
<commons-crypto.version>1.2.0</commons-crypto.version>
4344
<snakeyaml.version>2.2</snakeyaml.version>
44-
<apache-groovy.version>4.0.15</apache-groovy.version>
45-
<h2.version>2.2.224</h2.version>
45+
<apache-groovy.version>4.0.24</apache-groovy.version>
46+
<h2.version>2.3.232</h2.version>
4647
<esotericsoftware.kryo.version>5.6.2</esotericsoftware.kryo.version>
4748
</properties>
4849

@@ -108,6 +109,19 @@
108109
<version>${commons-crypto.version}</version>
109110
</dependency>
110111

112+
<dependency>
113+
<groupId>commons-io</groupId>
114+
<artifactId>commons-io</artifactId>
115+
<version>${commons-io.version}</version>
116+
</dependency>
117+
118+
<dependency>
119+
<groupId>org.apache.commons</groupId>
120+
<artifactId>commons-lang3</artifactId>
121+
<version>${commons-lang3.version}</version>
122+
</dependency>
123+
124+
111125
<dependency>
112126
<groupId>org.perf4j</groupId>
113127
<artifactId>perf4j</artifactId>
@@ -168,12 +182,6 @@
168182
<version>${codingapi.framework.version}</version>
169183
</dependency>
170184

171-
<dependency>
172-
<groupId>commons-dbutils</groupId>
173-
<artifactId>commons-dbutils</artifactId>
174-
<version>${commons-dbutils.version}</version>
175-
</dependency>
176-
177185
<dependency>
178186
<groupId>org.apache.commons</groupId>
179187
<artifactId>commons-text</artifactId>
@@ -301,7 +309,7 @@
301309
<plugin>
302310
<groupId>org.openclover</groupId>
303311
<artifactId>clover-maven-plugin</artifactId>
304-
<version>4.4.1</version>
312+
<version>4.5.2</version>
305313
<configuration>
306314
<generateHtml>true</generateHtml>
307315
<generateXml>true</generateXml>

springboot-starter-data-fast/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<artifactId>springboot-parent</artifactId>
77
<groupId>com.codingapi.springboot</groupId>
8-
<version>3.3.5</version>
8+
<version>3.3.6</version>
99
</parent>
1010
<modelVersion>4.0.0</modelVersion>
1111

springboot-starter-flow/pom.xml

+2-24
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<parent>
77
<artifactId>springboot-parent</artifactId>
88
<groupId>com.codingapi.springboot</groupId>
9-
<version>3.3.5</version>
9+
<version>3.3.6</version>
1010
</parent>
1111

1212
<name>springboot-starter-flow</name>
@@ -45,28 +45,6 @@
4545

4646
</dependencies>
4747

48-
<build>
49-
<plugins>
50-
<plugin>
51-
<groupId>org.jacoco</groupId>
52-
<artifactId>jacoco-maven-plugin</artifactId>
53-
<version>0.8.12</version>
54-
<executions>
55-
<execution>
56-
<goals>
57-
<goal>prepare-agent</goal>
58-
</goals>
59-
</execution>
60-
<execution>
61-
<id>report</id>
62-
<phase>test</phase>
63-
<goals>
64-
<goal>report</goal>
65-
</goals>
66-
</execution>
67-
</executions>
68-
</plugin>
69-
</plugins>
70-
</build>
48+
7149

7250
</project>

springboot-starter-security/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<parent>
77
<artifactId>springboot-parent</artifactId>
88
<groupId>com.codingapi.springboot</groupId>
9-
<version>3.3.5</version>
9+
<version>3.3.6</version>
1010
</parent>
1111

1212
<artifactId>springboot-starter-security</artifactId>

springboot-starter/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<groupId>com.codingapi.springboot</groupId>
77
<artifactId>springboot-parent</artifactId>
8-
<version>3.3.5</version>
8+
<version>3.3.6</version>
99
</parent>
1010
<artifactId>springboot-starter</artifactId>
1111

springboot-starter/src/main/java/com/codingapi/springboot/framework/event/ApplicationHandlerUtils.java

+29-8
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
import com.codingapi.springboot.framework.exception.EventException;
44
import com.codingapi.springboot.framework.exception.EventLoopException;
5+
import org.springframework.core.ResolvableType;
56

67
import java.util.ArrayList;
8+
import java.util.Comparator;
79
import java.util.List;
810

911
class ApplicationHandlerUtils implements IHandler<IEvent> {
@@ -40,18 +42,37 @@ public void addHandler(IHandler handler) {
4042
}
4143
}
4244

45+
/**
46+
* 获取订阅的事件类型
47+
*/
48+
private Class<?> getHandlerEventClass(IHandler<?> handler) {
49+
ResolvableType resolvableType = ResolvableType.forClass(handler.getClass()).as(IHandler.class);
50+
return resolvableType.getGeneric(0).resolve();
51+
}
52+
4353

4454
@Override
4555
public void handler(IEvent event) {
4656
Class<?> eventClass = event.getClass();
57+
58+
List<IHandler<IEvent>> matchHandlers = handlers
59+
.stream()
60+
.filter(handler -> {
61+
Class<?> targetClass = getHandlerEventClass(handler);
62+
return targetClass.isAssignableFrom(eventClass);
63+
})
64+
.sorted(Comparator.comparingInt(IHandler::order))
65+
.toList();
66+
67+
if (matchHandlers.isEmpty()) {
68+
return;
69+
}
70+
4771
List<Exception> errorStack = new ArrayList<>();
48-
boolean throwException = false;
49-
for (IHandler<IEvent> handler : handlers) {
72+
boolean hasThrowException = false;
73+
for (IHandler<IEvent> handler : matchHandlers) {
5074
try {
51-
Class<?> targetClass = handler.getHandlerEventClass();
52-
if (eventClass.equals(targetClass)) {
53-
handler.handler(event);
54-
}
75+
handler.handler(event);
5576
} catch (Exception e) {
5677
if (e instanceof EventLoopException) {
5778
throw e;
@@ -60,12 +81,12 @@ public void handler(IEvent event) {
6081
handler.error(e);
6182
errorStack.add(e);
6283
} catch (Exception err) {
63-
throwException = true;
84+
hasThrowException = true;
6485
errorStack.add(err);
6586
}
6687
}
6788
}
68-
if(throwException){
89+
if (hasThrowException) {
6990
throw new EventException(errorStack);
7091
}
7192
}

springboot-starter/src/main/java/com/codingapi/springboot/framework/event/IEvent.java

-1
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,4 @@
1212
*/
1313
public interface IEvent extends Serializable {
1414

15-
1615
}

springboot-starter/src/main/java/com/codingapi/springboot/framework/event/IHandler.java

+8-9
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
package com.codingapi.springboot.framework.event;
22

3-
import org.springframework.core.ResolvableType;
4-
53
/**
64
* handler 订阅
75
*
86
* @param <T> Event 类型
97
*/
108
public interface IHandler<T extends IEvent> {
119

10+
/**
11+
* 事件订阅排序
12+
* 在同样的事件中,可以通过order来控制订阅的顺序
13+
*/
14+
default int order() {
15+
return 0;
16+
}
17+
1218
/**
1319
* 订阅触发
1420
*
@@ -27,13 +33,6 @@ default void error(Exception exception) throws Exception {
2733
}
2834

2935

30-
/**
31-
* 获取订阅的事件类型
32-
*/
33-
default Class<?> getHandlerEventClass() {
34-
ResolvableType resolvableType = ResolvableType.forClass(getClass()).as(IHandler.class);
35-
return resolvableType.getGeneric(0).resolve();
36-
}
3736

3837

3938
}

0 commit comments

Comments
 (0)