|
6 | 6 | import org.springframework.beans.factory.annotation.Qualifier;
|
7 | 7 | import tv.codely.shared.domain.Utils;
|
8 | 8 | import tv.codely.shared.domain.bus.event.DomainEvent;
|
| 9 | +import tv.codely.shared.domain.bus.event.EventBus; |
9 | 10 | import tv.codely.shared.infrastructure.bus.event.DomainEventsInformation;
|
10 |
| -import tv.codely.shared.infrastructure.bus.event.spring.SpringApplicationEventBus; |
11 | 11 |
|
12 | 12 | import java.lang.reflect.InvocationTargetException;
|
13 | 13 | import java.lang.reflect.Method;
|
|
17 | 17 | import java.util.List;
|
18 | 18 |
|
19 | 19 | public class MySqlDomainEventsConsumer {
|
20 |
| - private final SessionFactory sessionFactory; |
21 |
| - private final DomainEventsInformation domainEventsInformation; |
22 |
| - private final SpringApplicationEventBus bus; |
23 |
| - private final Integer CHUNKS = 200; |
24 |
| - private Boolean shouldStop = false; |
25 |
| - |
26 |
| - public MySqlDomainEventsConsumer( |
27 |
| - @Qualifier("mooc-session_factory") SessionFactory sessionFactory, |
28 |
| - DomainEventsInformation domainEventsInformation, |
29 |
| - SpringApplicationEventBus bus |
30 |
| - ) { |
31 |
| - this.sessionFactory = sessionFactory; |
32 |
| - this.domainEventsInformation = domainEventsInformation; |
33 |
| - this.bus = bus; |
34 |
| - } |
35 |
| - |
36 |
| - @Transactional |
37 |
| - public void consume() { |
38 |
| - while (!shouldStop) { |
39 |
| - NativeQuery query = sessionFactory.getCurrentSession().createNativeQuery( |
40 |
| - "SELECT * FROM domain_events ORDER BY occurred_on ASC LIMIT :chunk" |
41 |
| - ); |
42 |
| - |
43 |
| - query.setParameter("chunk", CHUNKS); |
44 |
| - |
45 |
| - List<Object[]> events = query.list(); |
46 |
| - |
47 |
| - try { |
48 |
| - for (Object[] event : events) { |
49 |
| - executeSubscribers( |
50 |
| - (String) event[0], |
51 |
| - (String) event[1], |
52 |
| - (String) event[2], |
53 |
| - (String) event[3], |
54 |
| - (Timestamp) event[4] |
55 |
| - ); |
56 |
| - } |
57 |
| - } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException | InstantiationException e) { |
58 |
| - e.printStackTrace(); |
59 |
| - } |
60 |
| - |
61 |
| - sessionFactory.getCurrentSession().clear(); |
62 |
| - } |
63 |
| - } |
64 |
| - |
65 |
| - public void stop() { |
66 |
| - shouldStop = true; |
67 |
| - } |
68 |
| - |
69 |
| - private void executeSubscribers( |
70 |
| - String id, String aggregateId, String eventName, String body, Timestamp occurredOn |
71 |
| - ) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException { |
72 |
| - |
73 |
| - Class<? extends DomainEvent> domainEventClass = domainEventsInformation.forName(eventName); |
74 |
| - |
75 |
| - DomainEvent nullInstance = domainEventClass.getConstructor().newInstance(); |
76 |
| - |
77 |
| - Method fromPrimitivesMethod = domainEventClass.getMethod( |
78 |
| - "fromPrimitives", |
79 |
| - String.class, |
80 |
| - HashMap.class, |
81 |
| - String.class, |
82 |
| - String.class |
83 |
| - ); |
84 |
| - |
85 |
| - Object domainEvent = fromPrimitivesMethod.invoke( |
86 |
| - nullInstance, |
87 |
| - aggregateId, |
88 |
| - Utils.jsonDecode(body), |
89 |
| - id, |
90 |
| - Utils.dateToString(occurredOn) |
91 |
| - ); |
92 |
| - |
93 |
| - bus.publish(Collections.singletonList((DomainEvent) domainEvent)); |
94 |
| - } |
| 20 | + private final SessionFactory sessionFactory; |
| 21 | + private final DomainEventsInformation domainEventsInformation; |
| 22 | + private final EventBus bus; |
| 23 | + private final Integer CHUNKS = 200; |
| 24 | + private Boolean shouldStop = false; |
| 25 | + |
| 26 | + public MySqlDomainEventsConsumer( |
| 27 | + @Qualifier("mooc-session_factory") SessionFactory sessionFactory, |
| 28 | + DomainEventsInformation domainEventsInformation, |
| 29 | + EventBus bus |
| 30 | + ) { |
| 31 | + this.sessionFactory = sessionFactory; |
| 32 | + this.domainEventsInformation = domainEventsInformation; |
| 33 | + this.bus = bus; |
| 34 | + } |
| 35 | + |
| 36 | + @Transactional |
| 37 | + public void consume() { |
| 38 | + while (!shouldStop) { |
| 39 | + NativeQuery query = sessionFactory.getCurrentSession().createNativeQuery( |
| 40 | + "SELECT * FROM domain_events ORDER BY occurred_on ASC LIMIT :chunk" |
| 41 | + ); |
| 42 | + |
| 43 | + query.setParameter("chunk", CHUNKS); |
| 44 | + |
| 45 | + List<Object[]> events = query.list(); |
| 46 | + |
| 47 | + try { |
| 48 | + for (Object[] event : events) { |
| 49 | + executeSubscribers( |
| 50 | + (String) event[0], |
| 51 | + (String) event[1], |
| 52 | + (String) event[2], |
| 53 | + (String) event[3], |
| 54 | + (Timestamp) event[4] |
| 55 | + ); |
| 56 | + } |
| 57 | + } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException | |
| 58 | + InstantiationException e) { |
| 59 | + e.printStackTrace(); |
| 60 | + } |
| 61 | + |
| 62 | + sessionFactory.getCurrentSession().clear(); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + public void stop() { |
| 67 | + shouldStop = true; |
| 68 | + } |
| 69 | + |
| 70 | + private void executeSubscribers( |
| 71 | + String id, String aggregateId, String eventName, String body, Timestamp occurredOn |
| 72 | + ) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException { |
| 73 | + |
| 74 | + Class<? extends DomainEvent> domainEventClass = domainEventsInformation.forName(eventName); |
| 75 | + |
| 76 | + DomainEvent nullInstance = domainEventClass.getConstructor().newInstance(); |
| 77 | + |
| 78 | + Method fromPrimitivesMethod = domainEventClass.getMethod( |
| 79 | + "fromPrimitives", |
| 80 | + String.class, |
| 81 | + HashMap.class, |
| 82 | + String.class, |
| 83 | + String.class |
| 84 | + ); |
| 85 | + |
| 86 | + Object domainEvent = fromPrimitivesMethod.invoke( |
| 87 | + nullInstance, |
| 88 | + aggregateId, |
| 89 | + Utils.jsonDecode(body), |
| 90 | + id, |
| 91 | + Utils.dateToString(occurredOn) |
| 92 | + ); |
| 93 | + |
| 94 | + bus.publish(Collections.singletonList((DomainEvent) domainEvent)); |
| 95 | + } |
95 | 96 | }
|
0 commit comments