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
This commit is contained in:
parent
c10d2a33fa
commit
a419237226
52
axon/pom.xml
Normal file
52
axon/pom.xml
Normal file
@ -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>
|
54
axon/src/main/java/com/baeldung/axon/MessagesRunner.java
Normal file
54
axon/src/main/java/com/baeldung/axon/MessagesRunner.java
Normal file
@ -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));
|
||||||
|
}
|
||||||
|
}
|
@ -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));
|
||||||
|
}
|
||||||
|
}
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
@ -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());
|
||||||
|
}
|
||||||
|
}
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
@ -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));
|
||||||
|
}
|
||||||
|
}
|
1
pom.xml
1
pom.xml
@ -25,6 +25,7 @@
|
|||||||
<module>aspectj</module>
|
<module>aspectj</module>
|
||||||
<module>assertj</module>
|
<module>assertj</module>
|
||||||
<module>autovalue</module>
|
<module>autovalue</module>
|
||||||
|
<module>axon</module>
|
||||||
|
|
||||||
<module>cdi</module>
|
<module>cdi</module>
|
||||||
<!-- <module>core-java-9</module> -->
|
<!-- <module>core-java-9</module> -->
|
||||||
|
Loading…
x
Reference in New Issue
Block a user