Skip to content

Commit a419237

Browse files
tomekl007pedja4
authored andcommitted
BAEL-550 Axon framework
* BEEL-550 create axon module * BEEL-550 proper naming * BEEL-550 better example of message service * BEEL-550 proper name of method * BEEL-550 remove not needed comments * BEEL-550 proper message * BEEL-550 axon test scope test * BEEL-550 tries to migrate to axon 3 * BEEL-550 migrate to vesrion 3 successfull
1 parent c10d2a3 commit a419237

10 files changed

Lines changed: 280 additions & 0 deletions

File tree

‎axon/pom.xml‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>parent-modules</artifactId>
7+
<groupId>com.baeldung</groupId>
8+
<version>1.0.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>axon</artifactId>
13+
<build>
14+
<plugins>
15+
<plugin>
16+
<groupId>org.apache.maven.plugins</groupId>
17+
<artifactId>maven-compiler-plugin</artifactId>
18+
<configuration>
19+
<source>1.8</source>
20+
<target>1.8</target>
21+
</configuration>
22+
</plugin>
23+
</plugins>
24+
</build>
25+
26+
<dependencies>
27+
<dependency>
28+
<groupId>org.axonframework</groupId>
29+
<artifactId>axon-test</artifactId>
30+
<version>${axon.version}</version>
31+
<scope>test</scope>
32+
</dependency>
33+
<dependency>
34+
<groupId>org.axonframework</groupId>
35+
<artifactId>axon-core</artifactId>
36+
<version>${axon.version}</version>
37+
</dependency>
38+
<dependency>
39+
<groupId>junit</groupId>
40+
<artifactId>junit</artifactId>
41+
<version>${junit.version}</version>
42+
<scope>test</scope>
43+
</dependency>
44+
</dependencies>
45+
46+
<properties>
47+
<axon.version>3.0.2</axon.version>
48+
<junit.version>4.12</junit.version>
49+
</properties>
50+
51+
52+
</project>
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.baeldung.axon;
2+
3+
import com.baeldung.axon.aggregates.MessagesAggregate;
4+
import com.baeldung.axon.commands.CreateMessageCommand;
5+
import com.baeldung.axon.commands.MarkReadMessageCommand;
6+
import com.baeldung.axon.eventhandlers.MessagesEventHandler;
7+
import org.axonframework.commandhandling.AggregateAnnotationCommandHandler;
8+
import org.axonframework.commandhandling.CommandBus;
9+
import org.axonframework.commandhandling.SimpleCommandBus;
10+
import org.axonframework.commandhandling.gateway.CommandGateway;
11+
import org.axonframework.commandhandling.gateway.DefaultCommandGateway;
12+
import org.axonframework.eventhandling.AnnotationEventListenerAdapter;
13+
import org.axonframework.eventsourcing.EventSourcingRepository;
14+
import org.axonframework.eventsourcing.eventstore.EmbeddedEventStore;
15+
import org.axonframework.eventsourcing.eventstore.EventStore;
16+
import org.axonframework.eventsourcing.eventstore.inmemory.InMemoryEventStorageEngine;
17+
18+
import java.util.UUID;
19+
20+
public class MessagesRunner {
21+
22+
public static void main(String[] args) {
23+
CommandBus commandBus = new SimpleCommandBus();
24+
25+
CommandGateway commandGateway = new DefaultCommandGateway(commandBus);
26+
27+
EventStore eventStore = new EmbeddedEventStore(new InMemoryEventStorageEngine());
28+
29+
EventSourcingRepository<MessagesAggregate> repository =
30+
new EventSourcingRepository<>(MessagesAggregate.class, eventStore);
31+
32+
33+
AggregateAnnotationCommandHandler<MessagesAggregate> messagesAggregateAggregateAnnotationCommandHandler =
34+
new AggregateAnnotationCommandHandler<MessagesAggregate>(MessagesAggregate.class, repository);
35+
messagesAggregateAggregateAnnotationCommandHandler.subscribe(commandBus);
36+
37+
final AnnotationEventListenerAdapter annotationEventListenerAdapter =
38+
new AnnotationEventListenerAdapter(new MessagesEventHandler());
39+
eventStore.subscribe(eventMessages -> eventMessages.forEach(e -> {
40+
try {
41+
annotationEventListenerAdapter.handle(e);
42+
} catch (Exception e1) {
43+
throw new RuntimeException(e1);
44+
45+
}
46+
}
47+
48+
));
49+
50+
final String itemId = UUID.randomUUID().toString();
51+
commandGateway.send(new CreateMessageCommand(itemId, "Hello, how is your day? :-)"));
52+
commandGateway.send(new MarkReadMessageCommand(itemId));
53+
}
54+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.baeldung.axon.aggregates;
2+
3+
import com.baeldung.axon.commands.CreateMessageCommand;
4+
import com.baeldung.axon.commands.MarkReadMessageCommand;
5+
import com.baeldung.axon.events.MessageCreatedEvent;
6+
import com.baeldung.axon.events.MessageReadEvent;
7+
import org.axonframework.commandhandling.CommandHandler;
8+
import org.axonframework.commandhandling.model.AggregateIdentifier;
9+
import org.axonframework.eventhandling.EventHandler;
10+
11+
import static org.axonframework.commandhandling.model.AggregateLifecycle.apply;
12+
13+
14+
public class MessagesAggregate {
15+
16+
@AggregateIdentifier
17+
private String id;
18+
19+
public MessagesAggregate() {
20+
}
21+
22+
@CommandHandler
23+
public MessagesAggregate(CreateMessageCommand command) {
24+
apply(new MessageCreatedEvent(command.getId(), command.getText()));
25+
}
26+
27+
@EventHandler
28+
public void on(MessageCreatedEvent event) {
29+
this.id = event.getId();
30+
}
31+
32+
@CommandHandler
33+
public void markRead(MarkReadMessageCommand command) {
34+
apply(new MessageReadEvent(id));
35+
}
36+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.baeldung.axon.commands;
2+
3+
4+
import org.axonframework.commandhandling.TargetAggregateIdentifier;
5+
6+
public class CreateMessageCommand {
7+
8+
@TargetAggregateIdentifier
9+
private final String id;
10+
private final String text;
11+
12+
public CreateMessageCommand(String id, String text) {
13+
this.id = id;
14+
this.text = text;
15+
}
16+
17+
public String getId() {
18+
return id;
19+
}
20+
21+
public String getText() {
22+
return text;
23+
}
24+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.baeldung.axon.commands;
2+
3+
4+
import org.axonframework.commandhandling.TargetAggregateIdentifier;
5+
6+
public class MarkReadMessageCommand {
7+
8+
@TargetAggregateIdentifier
9+
private final String id;
10+
11+
public MarkReadMessageCommand(String id) {
12+
this.id = id;
13+
}
14+
15+
public String getId() {
16+
return id;
17+
}
18+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.baeldung.axon.eventhandlers;
2+
3+
import com.baeldung.axon.events.MessageReadEvent;
4+
import com.baeldung.axon.events.MessageCreatedEvent;
5+
import org.axonframework.eventhandling.EventHandler;
6+
7+
8+
public class MessagesEventHandler {
9+
10+
@EventHandler
11+
public void handle(MessageCreatedEvent event) {
12+
System.out.println("Message received: " + event.getText() + " (" + event.getId() + ")");
13+
}
14+
15+
@EventHandler
16+
public void handle(MessageReadEvent event) {
17+
System.out.println("Message read: " + event.getId());
18+
}
19+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.baeldung.axon.events;
2+
3+
public class MessageCreatedEvent {
4+
5+
private final String id;
6+
private final String text;
7+
8+
public MessageCreatedEvent(String id, String text) {
9+
this.id = id;
10+
this.text = text;
11+
}
12+
13+
public String getId() {
14+
return id;
15+
}
16+
17+
public String getText() {
18+
return text;
19+
}
20+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.baeldung.axon.events;
2+
3+
public class MessageReadEvent {
4+
5+
private final String id;
6+
7+
public MessageReadEvent(String id) {
8+
this.id = id;
9+
}
10+
11+
public String getId() {
12+
return id;
13+
}
14+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.baeldung.axon;
2+
3+
import com.baeldung.axon.aggregates.MessagesAggregate;
4+
import com.baeldung.axon.commands.CreateMessageCommand;
5+
import com.baeldung.axon.commands.MarkReadMessageCommand;
6+
import com.baeldung.axon.events.MessageCreatedEvent;
7+
import com.baeldung.axon.events.MessageReadEvent;
8+
import org.axonframework.test.aggregate.AggregateTestFixture;
9+
import org.axonframework.test.aggregate.FixtureConfiguration;
10+
import org.junit.Before;
11+
import org.junit.Test;
12+
13+
import java.util.UUID;
14+
15+
public class MessagesAggregateTest {
16+
17+
private FixtureConfiguration<MessagesAggregate> fixture;
18+
19+
@Before
20+
public void setUp() throws Exception {
21+
fixture = new AggregateTestFixture<MessagesAggregate>(MessagesAggregate.class);
22+
23+
}
24+
25+
@Test
26+
public void giveAggregateRoot_whenCreateMessageCommand_thenShouldProduceMessageCreatedEvent() throws Exception {
27+
String eventText = "Hello, how is your day?";
28+
String id = UUID.randomUUID().toString();
29+
fixture.given()
30+
.when(new CreateMessageCommand(id, eventText))
31+
.expectEvents(new MessageCreatedEvent(id, eventText));
32+
}
33+
34+
@Test
35+
public void givenMessageCreatedEvent_whenReadMessageCommand_thenShouldProduceMessageReadEvent() throws Exception {
36+
String id = UUID.randomUUID().toString();
37+
38+
fixture.given(new MessageCreatedEvent(id, "Hello :-)"))
39+
.when(new MarkReadMessageCommand(id))
40+
.expectEvents(new MessageReadEvent(id));
41+
}
42+
}

‎pom.xml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
<module>aspectj</module>
2626
<module>assertj</module>
2727
<module>autovalue</module>
28+
<module>axon</module>
2829

2930
<module>cdi</module>
3031
<!-- <module>core-java-9</module> -->

0 commit comments

Comments
 (0)