-
Notifications
You must be signed in to change notification settings - Fork 789
/
DisruptorUnitOfWork.java
191 lines (167 loc) · 6.49 KB
/
DisruptorUnitOfWork.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
* Copyright (c) 2010-2011. Axon Framework
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.axonframework.commandhandling.disruptor;
import org.axonframework.domain.AggregateRoot;
import org.axonframework.domain.DomainEventMessage;
import org.axonframework.domain.DomainEventStream;
import org.axonframework.domain.EventMessage;
import org.axonframework.domain.EventRegistrationCallback;
import org.axonframework.domain.SimpleDomainEventStream;
import org.axonframework.eventhandling.EventBus;
import org.axonframework.eventsourcing.EventSourcedAggregateRoot;
import org.axonframework.unitofwork.CurrentUnitOfWork;
import org.axonframework.unitofwork.SaveAggregateCallback;
import org.axonframework.unitofwork.UnitOfWork;
import org.axonframework.unitofwork.UnitOfWorkListener;
import org.axonframework.unitofwork.UnitOfWorkListenerCollection;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* Specialized UnitOfWork instance for the DisruptorCommandBus. It expects the executing command to target a single
* aggregate instance.
*
* @author Allard Buijze
* @since 2.0
*/
public class DisruptorUnitOfWork implements UnitOfWork, EventRegistrationCallback {
private static final DomainEventStream EMPTY_DOMAIN_EVENT_STREAM = new SimpleDomainEventStream();
private boolean committed;
private Throwable rollbackReason;
private DomainEventStream eventsToStore = EMPTY_DOMAIN_EVENT_STREAM;
private final List<EventMessage> eventsToPublish = new ArrayList<EventMessage>();
private final UnitOfWorkListenerCollection listeners = new UnitOfWorkListenerCollection();
private EventSourcedAggregateRoot aggregate;
/**
* Initializes a UnitOfWork for execution of a command on the given <code>aggregate</code>.
*
* @param aggregate The aggregate on which a command is to be executed.
*/
public DisruptorUnitOfWork(EventSourcedAggregateRoot aggregate) {
this.aggregate = aggregate;
}
@Override
public void commit() {
committed = true;
eventsToStore = aggregate.getUncommittedEvents();
aggregate.commitEvents();
CurrentUnitOfWork.clear(this);
}
/**
* Invokes this UnitOfWork's on-prepare-commit cycle. Typically, this is run after the actual aggregates have been
* committed, but before any of the changes are made public.
*/
public void onPrepareCommit() {
listeners.onPrepareCommit(Collections.<AggregateRoot>singleton(aggregate), eventsToPublish);
}
/**
* Invokes this UnitOfWork's on-after-commit cycle. Typically, this is run after all the events have been stored
* and published.
*/
public void onAfterCommit() {
listeners.afterCommit();
}
/**
* Invokes this UnitOfWork's on-cleanup cycle. Typically, this is run after all the events have been stored and
* published and the after-commit cycle has been executed.
*/
public void onCleanup() {
listeners.onCleanup();
}
/**
* Invokes this UnitOfWork's on-rollback cycle. Typically, this is run after all the events have been stored and
* published and the after-commit cycle has been executed.
*
* @param cause The cause of the rollback
*/
public void onRollback(Throwable cause) {
listeners.onRollback(cause);
}
@Override
public void rollback() {
rollback(null);
}
@Override
public void rollback(Throwable cause) {
rollbackReason = cause;
if (aggregate != null) {
aggregate.commitEvents();
}
CurrentUnitOfWork.clear(this);
}
@Override
public void start() {
CurrentUnitOfWork.set(this);
}
@Override
public boolean isStarted() {
return !committed && rollbackReason == null;
}
@Override
public void registerListener(UnitOfWorkListener listener) {
listeners.add(listener);
}
@SuppressWarnings("unchecked")
@Override
public <T extends AggregateRoot> T registerAggregate(T aggregateRoot, EventBus eventBus,
SaveAggregateCallback<T> saveAggregateCallback) {
if (aggregate == null) {
aggregate = (EventSourcedAggregateRoot) aggregateRoot;
}
if (!aggregateRoot.getClass().isInstance(aggregate)
|| !aggregate.getIdentifier().equals(aggregateRoot.getIdentifier())) {
throw new IllegalArgumentException("Cannot register an aggregate other than the preloaded aggregate. "
+ "This error typically occurs when an aggregate is loaded "
+ "which is not the aggregate targeted by the command");
}
aggregate.addEventRegistrationCallback(this);
return (T) aggregate;
}
@Override
public void publishEvent(EventMessage event, EventBus eventBus) {
eventsToPublish.add(event);
}
/**
* Returns the events that need to be stored as part of this Unit of Work.
*
* @return the events that need to be stored as part of this Unit of Work
*/
public DomainEventStream getEventsToStore() {
return eventsToStore;
}
/**
* Returns the events that need to be published as part of this Unit of Work.
*
* @return the events that need to be published as part of this Unit of Work
*/
public List<EventMessage> getEventsToPublish() {
return eventsToPublish;
}
/**
* Returns the identifier of the aggregate modified in this UnitOfWork.
*
* @return the identifier of the aggregate modified in this UnitOfWork
*/
public EventSourcedAggregateRoot getAggregate() {
return aggregate;
}
@Override
public <T> DomainEventMessage<T> onRegisteredEvent(DomainEventMessage<T> event) {
DomainEventMessage<T> message = (DomainEventMessage<T>) listeners.onEventRegistered(event);
eventsToPublish.add(message);
return message;
}
}