Skip to content

Commit 1d3d249

Browse files
author
王亮
committed
PersistenceEvent
1 parent c29f732 commit 1d3d249

File tree

13 files changed

+178
-30
lines changed

13 files changed

+178
-30
lines changed

springboot-example/src/main/java/com/codingapi/springboot/example/domain/Demo.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.codingapi.springboot.example.domain;
22

33
import com.codingapi.springboot.example.domain.event.DemoNameChangeEvent;
4+
import com.codingapi.springboot.framework.persistence.IPersistence;
45
import com.codingapi.springboot.framework.event.EventPusher;
56
import lombok.Getter;
67
import lombok.NoArgsConstructor;
@@ -21,7 +22,7 @@
2122
@NoArgsConstructor
2223
@Entity
2324
@ToString
24-
public class Demo{
25+
public class Demo implements IPersistence {
2526

2627
@Id
2728
@GeneratedValue(strategy = GenerationType.IDENTITY)

springboot-example/src/main/java/com/codingapi/springboot/example/domain/event/DemoNameChangeEvent.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.codingapi.springboot.example.domain.event;
22

3-
import com.codingapi.springboot.framework.event.IEvent;
3+
import com.codingapi.springboot.framework.event.IAsyncEvent;
44
import lombok.AllArgsConstructor;
55
import lombok.Getter;
66
import lombok.Setter;
@@ -12,7 +12,7 @@
1212
@Setter
1313
@Getter
1414
@AllArgsConstructor
15-
public class DemoNameChangeEvent implements IEvent {
15+
public class DemoNameChangeEvent implements IAsyncEvent {
1616

1717
private String oldName;
1818

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
package com.codingapi.springboot.example.repository;
22

33
import com.codingapi.springboot.example.domain.Demo;
4+
import com.codingapi.springboot.framework.persistence.IPersistenceRepository;
45
import org.springframework.data.jpa.repository.JpaRepository;
56

67
/**
78
* @author lorne
89
* @since 1.0.0
910
*/
10-
public interface DemoRepository extends JpaRepository<Demo,Integer> {
11+
public interface DemoRepository extends JpaRepository<Demo,Integer>, IPersistenceRepository<Demo> {
1112

13+
@Override
14+
default void persistenceHandler(Demo demo) {
15+
save(demo);
16+
}
1217
}

springboot-example/src/test/java/com/codingapi/springboot/example/ExampleApplicationTests.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,26 @@
22

33
import com.codingapi.springboot.example.domain.Demo;
44
import com.codingapi.springboot.example.domain.service.DemoSwapService;
5-
import com.codingapi.springboot.example.repository.DemoRepository;
65
import lombok.extern.slf4j.Slf4j;
76
import org.junit.jupiter.api.Test;
87
import org.springframework.beans.factory.annotation.Autowired;
98
import org.springframework.boot.test.context.SpringBootTest;
109
import org.springframework.util.Assert;
1110

11+
import javax.transaction.Transactional;
12+
1213
@SpringBootTest
1314
@Slf4j
1415
class ExampleApplicationTests {
1516

16-
@Autowired
17-
private DemoRepository demoRepository;
18-
1917
@Autowired
2018
private DemoSwapService demoSwapService;
2119

2220
@Test
21+
@Transactional
2322
void save() {
2423
Demo demo = new Demo("xiaoming");
25-
demoRepository.save(demo);
24+
demo.persistence();
2625
Assert.isTrue(demo.getId()>0,"demoRepository save error.");
2726
}
2827

springboot-framework/src/main/java/com/codingapi/springboot/framework/event/IPersistenceEvent.java

Lines changed: 0 additions & 8 deletions
This file was deleted.

springboot-framework/src/main/java/com/codingapi/springboot/framework/handler/ApplicationHandlerUtils.java

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class ApplicationHandlerUtils implements IHandler<IEvent>{
1414
private static ApplicationHandlerUtils instance;
1515

1616
private ApplicationHandlerUtils(){
17-
this.handleres = new ArrayList<>();
17+
this.handlers = new ArrayList<>();
1818
}
1919

2020

@@ -30,47 +30,55 @@ public static ApplicationHandlerUtils getInstance(){
3030
}
3131

3232

33-
private List<IHandler<IEvent>> handleres;
33+
private List<IHandler<IEvent>> handlers;
3434

35-
public void addHandlers(List<IHandler> handleres){
36-
if(handleres!=null){
37-
handleres.forEach(this::addHandler);
35+
public void addHandlers(List<IHandler> handlers){
36+
if(handlers!=null){
37+
handlers.forEach(this::addHandler);
3838
}
3939
}
4040

4141

4242
public void addHandler(IHandler handler){
4343
if(handler!=null){
44-
handleres.add(handler);
44+
handlers.add(handler);
4545
}
4646
}
4747

4848

4949
@Override
5050
public void handler(IEvent event) {
51-
for(IHandler<IEvent> handler:handleres){
51+
for(IHandler<IEvent> handler: handlers){
52+
String targetClassName = null;
5253
try{
5354
Class<?> eventClass = event.getClass();
5455
Class<?> targetClass = getHandlerEventClass(handler);
5556
if(eventClass.equals(targetClass)) {
57+
targetClassName = targetClass.getName();
5658
handler.handler(event);
5759
}
5860
}catch(Exception e){
61+
//IPersistenceEvent 抛出异常
62+
if("com.codingapi.springboot.framework.persistence.PersistenceEvent".equals(targetClassName)){
63+
throw e;
64+
}
5965
log.warn("handler exception", e);
6066
handler.error(e);
67+
6168
}
6269
}
6370
}
6471

6572
private Class<?> getHandlerEventClass(IHandler<IEvent> handler){
6673
Type[] types = handler.getClass().getGenericInterfaces();
6774
for(Type type:types){
68-
ParameterizedType parameterizedType = (ParameterizedType) type;
69-
Type[] actualTypeArguments = parameterizedType.getActualTypeArguments();
70-
if(actualTypeArguments!=null){
71-
return (Class<?>) actualTypeArguments[0];
75+
if(type instanceof ParameterizedType) {
76+
ParameterizedType parameterizedType = (ParameterizedType) type;
77+
Type[] actualTypeArguments = parameterizedType.getActualTypeArguments();
78+
if (actualTypeArguments != null) {
79+
return (Class<?>) actualTypeArguments[0];
80+
}
7281
}
73-
7482
}
7583
return null;
7684
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.codingapi.springboot.framework.persistence;
2+
3+
import com.codingapi.springboot.framework.event.EventPusher;
4+
5+
/**
6+
* 可持久化的对象
7+
*/
8+
public interface IPersistence {
9+
10+
/**
11+
* 持久化对象
12+
* @see IPersistenceRepository
13+
*/
14+
default void persistence(){
15+
EventPusher.push(new PersistenceEvent(this));
16+
}
17+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.codingapi.springboot.framework.persistence;
2+
3+
import com.codingapi.springboot.framework.event.ISyncEvent;
4+
5+
/**
6+
*
7+
* 持久化同步事件
8+
* 相比于ISyncEvent在IPersistenceEvent执行出现异常的时候会直接抛出
9+
* @see com.codingapi.springboot.framework.persistence.IPersistence
10+
*/
11+
interface IPersistenceEvent extends ISyncEvent {
12+
13+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.codingapi.springboot.framework.persistence;
2+
3+
/**
4+
* 持久化 存储对象
5+
* @param <T> 对象类型
6+
*/
7+
public interface IPersistenceRepository<T> {
8+
9+
/**
10+
* 持久化回调函数
11+
* @param t 对象
12+
*/
13+
void persistenceHandler(T t);
14+
15+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.codingapi.springboot.framework.persistence;
2+
3+
import lombok.Getter;
4+
5+
/**
6+
* 持久化事件
7+
* @param <T>
8+
*/
9+
class PersistenceEvent<T> implements IPersistenceEvent {
10+
11+
@Getter
12+
private final T val;
13+
14+
public PersistenceEvent(T val) {
15+
this.val = val;
16+
}
17+
}

0 commit comments

Comments
 (0)