Skip to content

Commit 956c2e7

Browse files
authored
Merge pull request eugenp#6010 from smcvb/BAEL-2435
[BAEL-2435] Update Axon article
2 parents 5a4ac62 + 0cf67f5 commit 956c2e7

24 files changed

Lines changed: 625 additions & 234 deletions

‎axon/pom.xml‎

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,61 @@
44
<modelVersion>4.0.0</modelVersion>
55
<artifactId>axon</artifactId>
66
<name>axon</name>
7-
7+
<description>Basic Axon Framework with Spring Boot configuration tutorial</description>
8+
89
<parent>
9-
<artifactId>parent-modules</artifactId>
1010
<groupId>com.baeldung</groupId>
11-
<version>1.0.0-SNAPSHOT</version>
11+
<artifactId>parent-boot-2</artifactId>
12+
<version>0.0.1-SNAPSHOT</version>
13+
<relativePath>../parent-boot-2</relativePath>
1214
</parent>
1315

1416
<dependencies>
1517
<dependency>
1618
<groupId>org.axonframework</groupId>
17-
<artifactId>axon-test</artifactId>
19+
<artifactId>axon-spring-boot-starter</artifactId>
1820
<version>${axon.version}</version>
19-
<scope>test</scope>
21+
<exclusions>
22+
<exclusion>
23+
<groupId>org.axonframework</groupId>
24+
<artifactId>axon-server-connector</artifactId>
25+
</exclusion>
26+
</exclusions>
2027
</dependency>
28+
2129
<dependency>
2230
<groupId>org.axonframework</groupId>
23-
<artifactId>axon-core</artifactId>
31+
<artifactId>axon-test</artifactId>
2432
<version>${axon.version}</version>
33+
<scope>test</scope>
34+
</dependency>
35+
36+
<dependency>
37+
<groupId>org.springframework.boot</groupId>
38+
<artifactId>spring-boot-autoconfigure</artifactId>
39+
<version>2.1.1.RELEASE</version>
40+
<scope>compile</scope>
41+
</dependency>
42+
43+
<dependency>
44+
<groupId>org.springframework.boot</groupId>
45+
<artifactId>spring-boot-starter-web</artifactId>
46+
</dependency>
47+
48+
<dependency>
49+
<groupId>org.springframework.boot</groupId>
50+
<artifactId>spring-boot-starter-data-jpa</artifactId>
51+
</dependency>
52+
53+
<dependency>
54+
<groupId>com.h2database</groupId>
55+
<artifactId>h2</artifactId>
56+
<scope>runtime</scope>
2557
</dependency>
2658
</dependencies>
2759

2860
<properties>
29-
<axon.version>3.0.2</axon.version>
61+
<axon.version>4.0.3</axon.version>
3062
</properties>
3163

3264
</project>

‎axon/src/main/java/com/baeldung/axon/MessagesRunner.java‎

Lines changed: 0 additions & 54 deletions
This file was deleted.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.baeldung.axon;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class OrderApplication {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(OrderApplication.class, args);
11+
}
12+
13+
}

‎axon/src/main/java/com/baeldung/axon/aggregates/MessagesAggregate.java‎

Lines changed: 0 additions & 36 deletions
This file was deleted.
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.baeldung.axon.commandmodel;
2+
3+
import static org.axonframework.modelling.command.AggregateLifecycle.apply;
4+
5+
import org.axonframework.commandhandling.CommandHandler;
6+
import org.axonframework.eventsourcing.EventSourcingHandler;
7+
import org.axonframework.modelling.command.AggregateIdentifier;
8+
import org.axonframework.spring.stereotype.Aggregate;
9+
10+
import com.baeldung.axon.coreapi.commands.ConfirmOrderCommand;
11+
import com.baeldung.axon.coreapi.commands.PlaceOrderCommand;
12+
import com.baeldung.axon.coreapi.commands.ShipOrderCommand;
13+
import com.baeldung.axon.coreapi.events.OrderConfirmedEvent;
14+
import com.baeldung.axon.coreapi.events.OrderPlacedEvent;
15+
import com.baeldung.axon.coreapi.events.OrderShippedEvent;
16+
17+
@Aggregate
18+
public class OrderAggregate {
19+
20+
@AggregateIdentifier
21+
private String orderId;
22+
private boolean orderConfirmed;
23+
24+
@CommandHandler
25+
public OrderAggregate(PlaceOrderCommand command) {
26+
apply(new OrderPlacedEvent(command.getOrderId(), command.getProduct()));
27+
}
28+
29+
@CommandHandler
30+
public void handle(ConfirmOrderCommand command) {
31+
apply(new OrderConfirmedEvent(orderId));
32+
}
33+
34+
@CommandHandler
35+
public void handle(ShipOrderCommand command) {
36+
if (!orderConfirmed) {
37+
throw new IllegalStateException("Cannot ship an order which has not been confirmed yet.");
38+
}
39+
40+
apply(new OrderShippedEvent(orderId));
41+
}
42+
43+
@EventSourcingHandler
44+
public void on(OrderPlacedEvent event) {
45+
this.orderId = event.getOrderId();
46+
orderConfirmed = false;
47+
}
48+
49+
@EventSourcingHandler
50+
public void on(OrderConfirmedEvent event) {
51+
orderConfirmed = true;
52+
}
53+
54+
protected OrderAggregate() {
55+
// Required by Axon to build a default Aggregate prior to Event Sourcing
56+
}
57+
58+
}

‎axon/src/main/java/com/baeldung/axon/commands/CreateMessageCommand.java‎

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

‎axon/src/main/java/com/baeldung/axon/commands/MarkReadMessageCommand.java‎

Lines changed: 0 additions & 18 deletions
This file was deleted.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.baeldung.axon.coreapi.commands;
2+
3+
import org.axonframework.modelling.command.TargetAggregateIdentifier;
4+
5+
import java.util.Objects;
6+
7+
public class ConfirmOrderCommand {
8+
9+
@TargetAggregateIdentifier
10+
private final String orderId;
11+
12+
public ConfirmOrderCommand(String orderId) {
13+
this.orderId = orderId;
14+
}
15+
16+
public String getOrderId() {
17+
return orderId;
18+
}
19+
20+
@Override
21+
public int hashCode() {
22+
return Objects.hash(orderId);
23+
}
24+
25+
@Override
26+
public boolean equals(Object obj) {
27+
if (this == obj) {
28+
return true;
29+
}
30+
if (obj == null || getClass() != obj.getClass()) {
31+
return false;
32+
}
33+
final ConfirmOrderCommand other = (ConfirmOrderCommand) obj;
34+
return Objects.equals(this.orderId, other.orderId);
35+
}
36+
37+
@Override
38+
public String toString() {
39+
return "ConfirmOrderCommand{" +
40+
"orderId='" + orderId + '\'' +
41+
'}';
42+
}
43+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.baeldung.axon.coreapi.commands;
2+
3+
import java.util.Objects;
4+
5+
import org.axonframework.modelling.command.TargetAggregateIdentifier;
6+
7+
public class PlaceOrderCommand {
8+
9+
@TargetAggregateIdentifier
10+
private final String orderId;
11+
private final String product;
12+
13+
public PlaceOrderCommand(String orderId, String product) {
14+
this.orderId = orderId;
15+
this.product = product;
16+
}
17+
18+
public String getOrderId() {
19+
return orderId;
20+
}
21+
22+
public String getProduct() {
23+
return product;
24+
}
25+
26+
@Override
27+
public int hashCode() {
28+
return Objects.hash(orderId, product);
29+
}
30+
31+
@Override
32+
public boolean equals(Object obj) {
33+
if (this == obj) {
34+
return true;
35+
}
36+
if (obj == null || getClass() != obj.getClass()) {
37+
return false;
38+
}
39+
final PlaceOrderCommand other = (PlaceOrderCommand) obj;
40+
return Objects.equals(this.orderId, other.orderId)
41+
&& Objects.equals(this.product, other.product);
42+
}
43+
44+
@Override
45+
public String toString() {
46+
return "PlaceOrderCommand{" +
47+
"orderId='" + orderId + '\'' +
48+
", product='" + product + '\'' +
49+
'}';
50+
}
51+
}

0 commit comments

Comments
 (0)