forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
- Loading branch information
Showing
10 changed files
with
280 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<parent> | ||
<artifactId>parent-modules</artifactId> | ||
<groupId>com.baeldung</groupId> | ||
<version>1.0.0-SNAPSHOT</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>axon</artifactId> | ||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<configuration> | ||
<source>1.8</source> | ||
<target>1.8</target> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.axonframework</groupId> | ||
<artifactId>axon-test</artifactId> | ||
<version>${axon.version}</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.axonframework</groupId> | ||
<artifactId>axon-core</artifactId> | ||
<version>${axon.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>${junit.version}</version> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
<properties> | ||
<axon.version>3.0.2</axon.version> | ||
<junit.version>4.12</junit.version> | ||
</properties> | ||
|
||
|
||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package com.baeldung.axon; | ||
|
||
import com.baeldung.axon.aggregates.MessagesAggregate; | ||
import com.baeldung.axon.commands.CreateMessageCommand; | ||
import com.baeldung.axon.commands.MarkReadMessageCommand; | ||
import com.baeldung.axon.eventhandlers.MessagesEventHandler; | ||
import org.axonframework.commandhandling.AggregateAnnotationCommandHandler; | ||
import org.axonframework.commandhandling.CommandBus; | ||
import org.axonframework.commandhandling.SimpleCommandBus; | ||
import org.axonframework.commandhandling.gateway.CommandGateway; | ||
import org.axonframework.commandhandling.gateway.DefaultCommandGateway; | ||
import org.axonframework.eventhandling.AnnotationEventListenerAdapter; | ||
import org.axonframework.eventsourcing.EventSourcingRepository; | ||
import org.axonframework.eventsourcing.eventstore.EmbeddedEventStore; | ||
import org.axonframework.eventsourcing.eventstore.EventStore; | ||
import org.axonframework.eventsourcing.eventstore.inmemory.InMemoryEventStorageEngine; | ||
|
||
import java.util.UUID; | ||
|
||
public class MessagesRunner { | ||
|
||
public static void main(String[] args) { | ||
CommandBus commandBus = new SimpleCommandBus(); | ||
|
||
CommandGateway commandGateway = new DefaultCommandGateway(commandBus); | ||
|
||
EventStore eventStore = new EmbeddedEventStore(new InMemoryEventStorageEngine()); | ||
|
||
EventSourcingRepository<MessagesAggregate> repository = | ||
new EventSourcingRepository<>(MessagesAggregate.class, eventStore); | ||
|
||
|
||
AggregateAnnotationCommandHandler<MessagesAggregate> messagesAggregateAggregateAnnotationCommandHandler = | ||
new AggregateAnnotationCommandHandler<MessagesAggregate>(MessagesAggregate.class, repository); | ||
messagesAggregateAggregateAnnotationCommandHandler.subscribe(commandBus); | ||
|
||
final AnnotationEventListenerAdapter annotationEventListenerAdapter = | ||
new AnnotationEventListenerAdapter(new MessagesEventHandler()); | ||
eventStore.subscribe(eventMessages -> eventMessages.forEach(e -> { | ||
try { | ||
annotationEventListenerAdapter.handle(e); | ||
} catch (Exception e1) { | ||
throw new RuntimeException(e1); | ||
|
||
} | ||
} | ||
|
||
)); | ||
|
||
final String itemId = UUID.randomUUID().toString(); | ||
commandGateway.send(new CreateMessageCommand(itemId, "Hello, how is your day? :-)")); | ||
commandGateway.send(new MarkReadMessageCommand(itemId)); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
axon/src/main/java/com/baeldung/axon/aggregates/MessagesAggregate.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package com.baeldung.axon.aggregates; | ||
|
||
import com.baeldung.axon.commands.CreateMessageCommand; | ||
import com.baeldung.axon.commands.MarkReadMessageCommand; | ||
import com.baeldung.axon.events.MessageCreatedEvent; | ||
import com.baeldung.axon.events.MessageReadEvent; | ||
import org.axonframework.commandhandling.CommandHandler; | ||
import org.axonframework.commandhandling.model.AggregateIdentifier; | ||
import org.axonframework.eventhandling.EventHandler; | ||
|
||
import static org.axonframework.commandhandling.model.AggregateLifecycle.apply; | ||
|
||
|
||
public class MessagesAggregate { | ||
|
||
@AggregateIdentifier | ||
private String id; | ||
|
||
public MessagesAggregate() { | ||
} | ||
|
||
@CommandHandler | ||
public MessagesAggregate(CreateMessageCommand command) { | ||
apply(new MessageCreatedEvent(command.getId(), command.getText())); | ||
} | ||
|
||
@EventHandler | ||
public void on(MessageCreatedEvent event) { | ||
this.id = event.getId(); | ||
} | ||
|
||
@CommandHandler | ||
public void markRead(MarkReadMessageCommand command) { | ||
apply(new MessageReadEvent(id)); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
axon/src/main/java/com/baeldung/axon/commands/CreateMessageCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.baeldung.axon.commands; | ||
|
||
|
||
import org.axonframework.commandhandling.TargetAggregateIdentifier; | ||
|
||
public class CreateMessageCommand { | ||
|
||
@TargetAggregateIdentifier | ||
private final String id; | ||
private final String text; | ||
|
||
public CreateMessageCommand(String id, String text) { | ||
this.id = id; | ||
this.text = text; | ||
} | ||
|
||
public String getId() { | ||
return id; | ||
} | ||
|
||
public String getText() { | ||
return text; | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
axon/src/main/java/com/baeldung/axon/commands/MarkReadMessageCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.baeldung.axon.commands; | ||
|
||
|
||
import org.axonframework.commandhandling.TargetAggregateIdentifier; | ||
|
||
public class MarkReadMessageCommand { | ||
|
||
@TargetAggregateIdentifier | ||
private final String id; | ||
|
||
public MarkReadMessageCommand(String id) { | ||
this.id = id; | ||
} | ||
|
||
public String getId() { | ||
return id; | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
axon/src/main/java/com/baeldung/axon/eventhandlers/MessagesEventHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.baeldung.axon.eventhandlers; | ||
|
||
import com.baeldung.axon.events.MessageReadEvent; | ||
import com.baeldung.axon.events.MessageCreatedEvent; | ||
import org.axonframework.eventhandling.EventHandler; | ||
|
||
|
||
public class MessagesEventHandler { | ||
|
||
@EventHandler | ||
public void handle(MessageCreatedEvent event) { | ||
System.out.println("Message received: " + event.getText() + " (" + event.getId() + ")"); | ||
} | ||
|
||
@EventHandler | ||
public void handle(MessageReadEvent event) { | ||
System.out.println("Message read: " + event.getId()); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
axon/src/main/java/com/baeldung/axon/events/MessageCreatedEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.baeldung.axon.events; | ||
|
||
public class MessageCreatedEvent { | ||
|
||
private final String id; | ||
private final String text; | ||
|
||
public MessageCreatedEvent(String id, String text) { | ||
this.id = id; | ||
this.text = text; | ||
} | ||
|
||
public String getId() { | ||
return id; | ||
} | ||
|
||
public String getText() { | ||
return text; | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
axon/src/main/java/com/baeldung/axon/events/MessageReadEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.baeldung.axon.events; | ||
|
||
public class MessageReadEvent { | ||
|
||
private final String id; | ||
|
||
public MessageReadEvent(String id) { | ||
this.id = id; | ||
} | ||
|
||
public String getId() { | ||
return id; | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
axon/src/test/java/com/baeldung/axon/MessagesAggregateTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.baeldung.axon; | ||
|
||
import com.baeldung.axon.aggregates.MessagesAggregate; | ||
import com.baeldung.axon.commands.CreateMessageCommand; | ||
import com.baeldung.axon.commands.MarkReadMessageCommand; | ||
import com.baeldung.axon.events.MessageCreatedEvent; | ||
import com.baeldung.axon.events.MessageReadEvent; | ||
import org.axonframework.test.aggregate.AggregateTestFixture; | ||
import org.axonframework.test.aggregate.FixtureConfiguration; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import java.util.UUID; | ||
|
||
public class MessagesAggregateTest { | ||
|
||
private FixtureConfiguration<MessagesAggregate> fixture; | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
fixture = new AggregateTestFixture<MessagesAggregate>(MessagesAggregate.class); | ||
|
||
} | ||
|
||
@Test | ||
public void giveAggregateRoot_whenCreateMessageCommand_thenShouldProduceMessageCreatedEvent() throws Exception { | ||
String eventText = "Hello, how is your day?"; | ||
String id = UUID.randomUUID().toString(); | ||
fixture.given() | ||
.when(new CreateMessageCommand(id, eventText)) | ||
.expectEvents(new MessageCreatedEvent(id, eventText)); | ||
} | ||
|
||
@Test | ||
public void givenMessageCreatedEvent_whenReadMessageCommand_thenShouldProduceMessageReadEvent() throws Exception { | ||
String id = UUID.randomUUID().toString(); | ||
|
||
fixture.given(new MessageCreatedEvent(id, "Hello :-)")) | ||
.when(new MarkReadMessageCommand(id)) | ||
.expectEvents(new MessageReadEvent(id)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters