Skip to content

Commit

Permalink
BAEL-550 Axon framework
Browse files Browse the repository at this point in the history
* 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
tomekl007 authored and pedja4 committed Mar 8, 2017
1 parent c10d2a3 commit a419237
Show file tree
Hide file tree
Showing 10 changed files with 280 additions and 0 deletions.
52 changes: 52 additions & 0 deletions axon/pom.xml
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>
54 changes: 54 additions & 0 deletions axon/src/main/java/com/baeldung/axon/MessagesRunner.java
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));
}
}
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));
}
}
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;
}
}
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;
}
}
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());
}
}
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 axon/src/main/java/com/baeldung/axon/events/MessageReadEvent.java
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 axon/src/test/java/com/baeldung/axon/MessagesAggregateTest.java
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));
}
}
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
<module>aspectj</module>
<module>assertj</module>
<module>autovalue</module>
<module>axon</module>

<module>cdi</module>
<!-- <module>core-java-9</module> -->
Expand Down

0 comments on commit a419237

Please sign in to comment.