diff --git a/eventuate-tram-examples-basic-event-common/build.gradle b/eventuate-tram-examples-basic-event-common/build.gradle new file mode 100644 index 0000000..1cf11aa --- /dev/null +++ b/eventuate-tram-examples-basic-event-common/build.gradle @@ -0,0 +1,3 @@ +dependencies { + implementation "org.springframework.boot:spring-boot-starter" +} \ No newline at end of file diff --git a/eventuate-tram-examples-basic-event-common/src/main/java/io/eventuate/tram/examples/basic/events/common/EventConfigurationProperties.java b/eventuate-tram-examples-basic-event-common/src/main/java/io/eventuate/tram/examples/basic/events/common/EventConfigurationProperties.java new file mode 100644 index 0000000..7ca36f5 --- /dev/null +++ b/eventuate-tram-examples-basic-event-common/src/main/java/io/eventuate/tram/examples/basic/events/common/EventConfigurationProperties.java @@ -0,0 +1,17 @@ +package io.eventuate.tram.examples.basic.events.common; + +import org.springframework.boot.context.properties.ConfigurationProperties; + +@ConfigurationProperties(prefix = "event") +public class EventConfigurationProperties { + + private String aggregateSuffix = ""; + + public String getAggregateSuffix() { + return aggregateSuffix; + } + + public void setAggregateSuffix(String aggregateSuffix) { + this.aggregateSuffix = aggregateSuffix; + } +} diff --git a/eventuate-tram-examples-basic-event-cqrs-subscriber/build.gradle b/eventuate-tram-examples-basic-event-cqrs-subscriber/build.gradle index 1f87bf1..10c21ea 100644 --- a/eventuate-tram-examples-basic-event-cqrs-subscriber/build.gradle +++ b/eventuate-tram-examples-basic-event-cqrs-subscriber/build.gradle @@ -2,6 +2,8 @@ apply plugin: 'org.springframework.boot' dependencies { + implementation project(":eventuate-tram-examples-basic-event-common") + implementation "io.eventuate.tram.core:eventuate-tram-spring-events-subscriber-starter" if (messageBroker == "kafka") diff --git a/eventuate-tram-examples-basic-event-cqrs-subscriber/src/main/java/io/eventuate/tram/examples/basic/events/subscriber/AccountEventsConsumer.java b/eventuate-tram-examples-basic-event-cqrs-subscriber/src/main/java/io/eventuate/tram/examples/basic/events/subscriber/AccountEventsConsumer.java index 5141ad3..800b6f4 100644 --- a/eventuate-tram-examples-basic-event-cqrs-subscriber/src/main/java/io/eventuate/tram/examples/basic/events/subscriber/AccountEventsConsumer.java +++ b/eventuate-tram-examples-basic-event-cqrs-subscriber/src/main/java/io/eventuate/tram/examples/basic/events/subscriber/AccountEventsConsumer.java @@ -3,9 +3,11 @@ import io.eventuate.tram.events.subscriber.DomainEventEnvelope; import io.eventuate.tram.events.subscriber.DomainEventHandlers; import io.eventuate.tram.events.subscriber.DomainEventHandlersBuilder; +import io.eventuate.tram.examples.basic.events.common.EventConfigurationProperties; import io.eventuate.tram.examples.basic.events.domain.AccountDebited; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; import java.util.concurrent.BlockingQueue; import java.util.concurrent.LinkedBlockingDeque; @@ -18,9 +20,12 @@ public class AccountEventsConsumer { public AccountEventsConsumer() { } + @Autowired + private EventConfigurationProperties eventConfigurationProperties; + public DomainEventHandlers domainEventHandlers() { return DomainEventHandlersBuilder - .forAggregateType("Account") + .forAggregateType("Account" + eventConfigurationProperties.getAggregateSuffix()) .onEvent(AccountDebited.class, this::handleAccountDebited) .build(); } diff --git a/eventuate-tram-examples-basic-event-cqrs-subscriber/src/main/java/io/eventuate/tram/examples/basic/events/subscriber/CqrsEventSubscriberConfiguration.java b/eventuate-tram-examples-basic-event-cqrs-subscriber/src/main/java/io/eventuate/tram/examples/basic/events/subscriber/CqrsEventSubscriberConfiguration.java index 94955bf..843b075 100644 --- a/eventuate-tram-examples-basic-event-cqrs-subscriber/src/main/java/io/eventuate/tram/examples/basic/events/subscriber/CqrsEventSubscriberConfiguration.java +++ b/eventuate-tram-examples-basic-event-cqrs-subscriber/src/main/java/io/eventuate/tram/examples/basic/events/subscriber/CqrsEventSubscriberConfiguration.java @@ -2,13 +2,16 @@ import io.eventuate.tram.events.subscriber.DomainEventDispatcher; import io.eventuate.tram.events.subscriber.DomainEventDispatcherFactory; +import io.eventuate.tram.examples.basic.events.common.EventConfigurationProperties; import io.eventuate.tram.spring.consumer.common.TramNoopDuplicateMessageDetectorConfiguration; +import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; @Configuration @Import(TramNoopDuplicateMessageDetectorConfiguration.class) +@EnableConfigurationProperties(EventConfigurationProperties.class) public class CqrsEventSubscriberConfiguration { @Bean diff --git a/eventuate-tram-examples-basic-event-cqrs-subscriber/src/test/java/io/eventuate/tram/examples/basic/events/subscriber/broker/CqrsEventSubscriberBrokerTest.java b/eventuate-tram-examples-basic-event-cqrs-subscriber/src/test/java/io/eventuate/tram/examples/basic/events/subscriber/broker/CqrsEventSubscriberBrokerTest.java index 1840a5c..1c458b0 100644 --- a/eventuate-tram-examples-basic-event-cqrs-subscriber/src/test/java/io/eventuate/tram/examples/basic/events/subscriber/broker/CqrsEventSubscriberBrokerTest.java +++ b/eventuate-tram-examples-basic-event-cqrs-subscriber/src/test/java/io/eventuate/tram/examples/basic/events/subscriber/broker/CqrsEventSubscriberBrokerTest.java @@ -21,7 +21,9 @@ import static io.restassured.RestAssured.given; -@SpringBootTest(classes = CqrsEventSubscriberBrokerTest.Config.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +@SpringBootTest(classes = CqrsEventSubscriberBrokerTest.Config.class, + webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, + properties = {"event.aggregate.suffix=-${random.value}"}) public class CqrsEventSubscriberBrokerTest { @Configuration diff --git a/eventuate-tram-examples-basic-event-cqrs-subscriber/src/test/java/io/eventuate/tram/examples/basic/events/subscriber/inmemory/CqrsEventSubscriberInMemoryTest.java b/eventuate-tram-examples-basic-event-cqrs-subscriber/src/test/java/io/eventuate/tram/examples/basic/events/subscriber/inmemory/CqrsEventSubscriberInMemoryTest.java deleted file mode 100644 index 09a85b1..0000000 --- a/eventuate-tram-examples-basic-event-cqrs-subscriber/src/test/java/io/eventuate/tram/examples/basic/events/subscriber/inmemory/CqrsEventSubscriberInMemoryTest.java +++ /dev/null @@ -1,71 +0,0 @@ -package io.eventuate.tram.examples.basic.events.subscriber.inmemory; - -import io.eventuate.tram.examples.basic.events.publisher.EventPublisherConfiguration; -import io.eventuate.tram.examples.basic.events.publisher.PublishRequest; -import io.eventuate.tram.examples.basic.events.subscriber.CqrsEventSubscriberConfiguration; -import io.eventuate.tram.examples.basic.events.subscriber.common.AssertableAccountEventsConsumer; -import io.eventuate.tram.examples.basic.events.subscriber.common.AssertableAccountEventsConsumerConfiguration; -import io.eventuate.tram.spring.inmemory.TramInMemoryConfiguration; -import io.eventuate.util.test.async.Eventually; -import io.restassured.RestAssured; -import io.restassured.http.ContentType; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.autoconfigure.EnableAutoConfiguration; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.boot.web.server.LocalServerPort; -import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.Import; - -import java.util.concurrent.TimeUnit; - -import static io.restassured.RestAssured.given; - - -@SpringBootTest(classes = CqrsEventSubscriberInMemoryTest.Config.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) -public class CqrsEventSubscriberInMemoryTest { - - @Configuration - @EnableAutoConfiguration - @Import({ - EventPublisherConfiguration.class, CqrsEventSubscriberConfiguration.class, - TramInMemoryConfiguration.class, - AssertableAccountEventsConsumerConfiguration.class, - }) - static class Config { - } - - @LocalServerPort - private int port; - - @BeforeEach - public void setup() { - RestAssured.port = port; - } - - - @Autowired - private AssertableAccountEventsConsumer accountEventsConsumer; - - @Test - public void shouldPublishEvent() { - - long accountId = System.currentTimeMillis(); - - given().when() - .log().all() - .body(new PublishRequest(accountId, 102L)) - .contentType(ContentType.JSON) - .post("/publish") - .then() - .statusCode(200); - - Eventually.eventually(100, 500, TimeUnit.MILLISECONDS, () -> { - accountEventsConsumer.assertEventPublished(Long.toString(accountId)); - }); - - } - - -} diff --git a/eventuate-tram-examples-basic-event-cqrs-subscriber/src/test/resources/application.yml b/eventuate-tram-examples-basic-event-cqrs-subscriber/src/test/resources/application.yml new file mode 100644 index 0000000..c0f4704 --- /dev/null +++ b/eventuate-tram-examples-basic-event-cqrs-subscriber/src/test/resources/application.yml @@ -0,0 +1,38 @@ +logging: + level: + io: + eventuate: DEBUG + +server.port: 8081 + +spring: + datasource: + url: jdbc:mysql://${DOCKER_HOST_IP:localhost}/eventuate + username: mysqluser + password: mysqlpw + driver-class-name: com.mysql.cj.jdbc.Driver + +eventuatelocal: + kafka: + bootstrap: + servers: ${DOCKER_HOST_IP:localhost}:9092 + zookeeper: + connection: + string: ${DOCKER_HOST_IP:localhost}:2181 + +activemq: + url: tcp://${DOCKER_HOST_IP:localhost}:61616 + +rabbitmq: + broker: + addresses: ${DOCKER_HOST_IP:localhost} + +--- +spring: + profiles: postgres + datasource: + url: jdbc:postgresql://${DOCKER_HOST_IP:localhost}/eventuate + username: eventuate + password: eventuate + driver-class-name: org.postgresql.Driver + diff --git a/eventuate-tram-examples-basic-event-publisher/build.gradle b/eventuate-tram-examples-basic-event-publisher/build.gradle index 9893e03..997bbfb 100644 --- a/eventuate-tram-examples-basic-event-publisher/build.gradle +++ b/eventuate-tram-examples-basic-event-publisher/build.gradle @@ -1,6 +1,9 @@ apply plugin: 'org.springframework.boot' dependencies { + + implementation project(":eventuate-tram-examples-basic-event-common") + implementation "io.eventuate.tram.core:eventuate-tram-spring-events-publisher-starter" runtimeOnly "io.eventuate.tram.core:eventuate-tram-spring-jdbc-$messageBroker" diff --git a/eventuate-tram-examples-basic-event-publisher/src/main/java/io/eventuate/tram/examples/basic/events/publisher/EventPublisherConfiguration.java b/eventuate-tram-examples-basic-event-publisher/src/main/java/io/eventuate/tram/examples/basic/events/publisher/EventPublisherConfiguration.java index d61eebe..c43fda1 100644 --- a/eventuate-tram-examples-basic-event-publisher/src/main/java/io/eventuate/tram/examples/basic/events/publisher/EventPublisherConfiguration.java +++ b/eventuate-tram-examples-basic-event-publisher/src/main/java/io/eventuate/tram/examples/basic/events/publisher/EventPublisherConfiguration.java @@ -1,10 +1,13 @@ package io.eventuate.tram.examples.basic.events.publisher; +import io.eventuate.tram.examples.basic.events.common.EventConfigurationProperties; +import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; @Configuration @ComponentScan +@EnableConfigurationProperties(EventConfigurationProperties.class) public class EventPublisherConfiguration { diff --git a/eventuate-tram-examples-basic-event-publisher/src/main/java/io/eventuate/tram/examples/basic/events/publisher/EventPublisherController.java b/eventuate-tram-examples-basic-event-publisher/src/main/java/io/eventuate/tram/examples/basic/events/publisher/EventPublisherController.java index 1258660..179a69c 100644 --- a/eventuate-tram-examples-basic-event-publisher/src/main/java/io/eventuate/tram/examples/basic/events/publisher/EventPublisherController.java +++ b/eventuate-tram-examples-basic-event-publisher/src/main/java/io/eventuate/tram/examples/basic/events/publisher/EventPublisherController.java @@ -1,6 +1,7 @@ package io.eventuate.tram.examples.basic.events.publisher; import io.eventuate.tram.events.publisher.DomainEventPublisher; +import io.eventuate.tram.examples.basic.events.common.EventConfigurationProperties; import io.eventuate.tram.examples.basic.events.domain.AccountDebited; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -14,20 +15,23 @@ @RestController public class EventPublisherController { - private Logger logger = LoggerFactory.getLogger(getClass()); + private final Logger logger = LoggerFactory.getLogger(getClass()); - private DomainEventPublisher domainEventPublisher; + private final DomainEventPublisher domainEventPublisher; + private final EventConfigurationProperties eventConfigurationProperties; @Autowired - public EventPublisherController(DomainEventPublisher domainEventPublisher) { + public EventPublisherController(DomainEventPublisher domainEventPublisher, EventConfigurationProperties eventConfigurationProperties) { this.domainEventPublisher = domainEventPublisher; + this.eventConfigurationProperties = eventConfigurationProperties; } @PostMapping("/publish") public void publish(@RequestBody PublishRequest publishRequest) { - logger.info("Publishing {}", publishRequest); - domainEventPublisher.publish("Account", publishRequest.accountId(), Collections.singletonList(new AccountDebited(publishRequest.amount()))); - logger.info("Published {}", publishRequest); + String aggregateType = "Account" + eventConfigurationProperties.getAggregateSuffix(); + logger.info("Publishing {} to {}", publishRequest, aggregateType); + domainEventPublisher.publish(aggregateType, publishRequest.accountId(), Collections.singletonList(new AccountDebited(publishRequest.amount()))); + logger.info("Published {} to {}", publishRequest, aggregateType); } } diff --git a/eventuate-tram-examples-basic-event-subscriber/build.gradle b/eventuate-tram-examples-basic-event-subscriber/build.gradle index b56f8b2..135a921 100644 --- a/eventuate-tram-examples-basic-event-subscriber/build.gradle +++ b/eventuate-tram-examples-basic-event-subscriber/build.gradle @@ -2,6 +2,8 @@ apply plugin: 'org.springframework.boot' dependencies { + implementation project(":eventuate-tram-examples-basic-event-common") + implementation "io.eventuate.tram.core:eventuate-tram-spring-events-subscriber-starter" runtimeOnly "io.eventuate.tram.core:eventuate-tram-spring-jdbc-$messageBroker" diff --git a/eventuate-tram-examples-basic-event-subscriber/src/main/java/io/eventuate/tram/examples/basic/events/subscriber/AccountEventsConsumer.java b/eventuate-tram-examples-basic-event-subscriber/src/main/java/io/eventuate/tram/examples/basic/events/subscriber/AccountEventsConsumer.java index 5141ad3..456102d 100644 --- a/eventuate-tram-examples-basic-event-subscriber/src/main/java/io/eventuate/tram/examples/basic/events/subscriber/AccountEventsConsumer.java +++ b/eventuate-tram-examples-basic-event-subscriber/src/main/java/io/eventuate/tram/examples/basic/events/subscriber/AccountEventsConsumer.java @@ -3,9 +3,11 @@ import io.eventuate.tram.events.subscriber.DomainEventEnvelope; import io.eventuate.tram.events.subscriber.DomainEventHandlers; import io.eventuate.tram.events.subscriber.DomainEventHandlersBuilder; +import io.eventuate.tram.examples.basic.events.common.EventConfigurationProperties; import io.eventuate.tram.examples.basic.events.domain.AccountDebited; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; import java.util.concurrent.BlockingQueue; import java.util.concurrent.LinkedBlockingDeque; @@ -15,12 +17,15 @@ public class AccountEventsConsumer { private final Logger logger = LoggerFactory.getLogger(getClass()); private final BlockingQueue> queue = new LinkedBlockingDeque<>(); + @Autowired + private EventConfigurationProperties eventConfigurationProperties; + public AccountEventsConsumer() { } public DomainEventHandlers domainEventHandlers() { return DomainEventHandlersBuilder - .forAggregateType("Account") + .forAggregateType("Account" + eventConfigurationProperties.getAggregateSuffix()) .onEvent(AccountDebited.class, this::handleAccountDebited) .build(); } diff --git a/eventuate-tram-examples-basic-event-subscriber/src/main/java/io/eventuate/tram/examples/basic/events/subscriber/EventSubscriberConfiguration.java b/eventuate-tram-examples-basic-event-subscriber/src/main/java/io/eventuate/tram/examples/basic/events/subscriber/EventSubscriberConfiguration.java index d9cb2aa..ac347b3 100644 --- a/eventuate-tram-examples-basic-event-subscriber/src/main/java/io/eventuate/tram/examples/basic/events/subscriber/EventSubscriberConfiguration.java +++ b/eventuate-tram-examples-basic-event-subscriber/src/main/java/io/eventuate/tram/examples/basic/events/subscriber/EventSubscriberConfiguration.java @@ -2,10 +2,13 @@ import io.eventuate.tram.events.subscriber.DomainEventDispatcher; import io.eventuate.tram.events.subscriber.DomainEventDispatcherFactory; +import io.eventuate.tram.examples.basic.events.common.EventConfigurationProperties; +import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration +@EnableConfigurationProperties(EventConfigurationProperties.class) public class EventSubscriberConfiguration { @Bean diff --git a/settings.gradle b/settings.gradle index e11073d..04461be 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1,6 +1,7 @@ include 'eventuate-tram-examples-basic-message-common' include 'eventuate-tram-examples-basic-message-producer' include 'eventuate-tram-examples-basic-message-consumer' +include 'eventuate-tram-examples-basic-event-common' include 'eventuate-tram-examples-basic-event-publisher' include 'eventuate-tram-examples-basic-event-subscriber' include 'eventuate-tram-examples-basic-event-cqrs-subscriber'