FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

feat: add endpoint to publish raw domain events by rgomezcasas · Pull Request #86 · CodelyTV/java-ddd-example · GitHub

/ java-ddd-example Public template
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .ftl  (1) .http  (2) .java  (30) .properties  (1) .sh  (1) .yml  (2) No extension  (1) dotfile  (1) All 8 file types selected
Deleted files Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
2 changes: 1 addition & 1 deletion Makefile
This file contains hidden or 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
@@ -1,7 +1,7 @@
all: build

start:
@docker-compose -f docker-compose.ci.yml up -d
@docker compose -f docker-compose.ci.yml up -d

build:
@./gradlew build --warning-mode all
Expand Down
1 change: 1 addition & 0 deletions apps/main/resources/application.properties
This file contains hidden or 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 @@
spring.main.allow-bean-definition-overriding=true
This file contains hidden or 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
Expand Up @@ -142,12 +142,12 @@

const urlParts = inputs.map(input => input.name + "=" + input.value);

const url = "http://localhost:8091/courses?" + urlParts.join("&");
const url = "http://localhost:8040/courses?" + urlParts.join("&");

addCoursesList(url);
}
</script>

<script>
addCoursesList("http://localhost:8091/courses");
addCoursesList("http://localhost:8040/courses");
</script>
This file contains hidden or 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
Expand Up @@ -7,6 +7,7 @@
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.FilterType;

import tv.codely.apps.backoffice.backend.command.ConsumeRabbitMqDomainEventsCommand;
import tv.codely.shared.domain.Service;

@SpringBootApplication(exclude = HibernateJpaAutoConfiguration.class)
Expand All @@ -17,8 +18,10 @@
public class BackofficeBackendApplication {

public static HashMap<String, Class<?>> commands() {
return new HashMap<String, Class<?>>() {
{}
return new HashMap<>() {
{
put("domain-events:rabbitmq:consume", ConsumeRabbitMqDomainEventsCommand.class);
}
};
}
}
This file contains hidden or 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 tv.codely.apps.backoffice.backend.command;

import tv.codely.shared.infrastructure.bus.event.rabbitmq.RabbitMqDomainEventsConsumer;
import tv.codely.shared.infrastructure.cli.ConsoleCommand;

public final class ConsumeRabbitMqDomainEventsCommand extends ConsoleCommand {

private final RabbitMqDomainEventsConsumer consumer;

public ConsumeRabbitMqDomainEventsCommand(RabbitMqDomainEventsConsumer consumer) {
this.consumer = consumer;
}

@Override
public void execute(String[] args) {
consumer.consume("backoffice");

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

This is a demo

}
}
This file contains hidden or 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
@@ -1,7 +1,9 @@
package tv.codely.apps.backoffice.frontend.config;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
Expand All @@ -10,6 +12,10 @@
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
import org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver;

import tv.codely.shared.infrastructure.bus.event.mysql.MySqlEventBus;
import tv.codely.shared.infrastructure.bus.event.rabbitmq.RabbitMqEventBus;
import tv.codely.shared.infrastructure.bus.event.rabbitmq.RabbitMqPublisher;

@Configuration
@EnableWebMvc
public class BackofficeFrontendWebConfig implements WebMvcConfigurer {
Expand Down Expand Up @@ -43,4 +49,13 @@ public FreeMarkerConfigurer freeMarkerConfigurer() {

return configurer;
}

@Primary
@Bean
public RabbitMqEventBus rabbitMqEventBus(
RabbitMqPublisher publisher,
@Qualifier("backofficeMysqlEventBus") MySqlEventBus failoverPublisher
) {
return new RabbitMqEventBus(publisher, failoverPublisher);
}
}
This file contains hidden or 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
Expand Up @@ -13,6 +13,6 @@ public ConsumeRabbitMqDomainEventsCommand(RabbitMqDomainEventsConsumer consumer)

@Override
public void execute(String[] args) {
consumer.consume();
consumer.consume("mooc");
Comment thread
rgomezcasas marked this conversation as resolved.
}
}
This file contains hidden or 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
@@ -1,5 +1,7 @@
package tv.codely.apps.mooc.backend.config;

import java.util.Optional;

import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
Expand All @@ -10,17 +12,17 @@
@Configuration
public class MoocBackendServerConfiguration {

private final RequestMappingHandlerMapping mapping;
private final Optional<RequestMappingHandlerMapping> mapping;

public MoocBackendServerConfiguration(RequestMappingHandlerMapping mapping) {
public MoocBackendServerConfiguration(Optional<RequestMappingHandlerMapping> mapping) {
this.mapping = mapping;
}

@Bean
public FilterRegistrationBean<ApiExceptionMiddleware> apiExceptionMiddleware() {
FilterRegistrationBean<ApiExceptionMiddleware> registrationBean = new FilterRegistrationBean<>();

registrationBean.setFilter(new ApiExceptionMiddleware(mapping));
mapping.ifPresent(map -> registrationBean.setFilter(new ApiExceptionMiddleware(map)));

return registrationBean;
}
Expand Down
This file contains hidden or 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,53 @@
package tv.codely.apps.mooc.backend.controller.playground;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePropertiesBuilder;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import tv.codely.shared.domain.Utils;

@RestController
record DomainEventPostController(RabbitTemplate rabbitTemplate) {
@PostMapping(value = "/domain-events")
public ResponseEntity<String> index(@RequestBody Request request) {
System.out.println(request.eventName());

var serializedEvent = Utils.jsonEncode(request.eventRaw());

Message message = new Message(
serializedEvent.getBytes(),
MessagePropertiesBuilder.newInstance().setContentEncoding("utf-8").setContentType("application/json").build()
);

rabbitTemplate.send("domain_events", request.eventName(), message);

return new ResponseEntity<>(HttpStatus.CREATED);
}
}

final class Request {

private String eventName;
private Object eventRaw;

public void setEventName(String eventName) {
this.eventName = eventName;
}

public void setEventRaw(Object eventRaw) {
this.eventRaw = eventRaw;
}

String eventName() {
return eventName;
}

Object eventRaw() {
return eventRaw;
}
}
2 changes: 1 addition & 1 deletion docker-compose.ci.yml
This file contains hidden or 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
Expand Up @@ -23,7 +23,7 @@ services:
platform: linux/amd64
restart: unless-stopped
ports:
- "5630:5672"
- "5672:5672"
- "8090:15672"
environment:
- RABBITMQ_DEFAULT_USER=codely
Expand Down
38 changes: 32 additions & 6 deletions docker-compose.yml
This file contains hidden or 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
Expand Up @@ -23,7 +23,7 @@ services:
platform: linux/amd64
restart: unless-stopped
ports:
- "5630:5672"
- "5672:5672"
- "8090:15672"
environment:
- RABBITMQ_DEFAULT_USER=codely
Expand Down Expand Up @@ -51,12 +51,29 @@ services:
volumes:
- .:/app:delegated
- backoffice_backend_gradle_cache:/app/.gradle
- backoffice_backend_build:/app/build
depends_on:
- shared_mysql
- shared_rabbitmq
- backoffice_elasticsearch
command: ["./gradlew", "bootRun", "--args", "backoffice_backend server"]

backoffice_backend_consumers_java:
container_name: codely-java_ddd_example-backoffice_backend_consumers
build:
context: .
dockerfile: Dockerfile
restart: unless-stopped
volumes:
- .:/app:delegated
- backoffice_consumers_gradle_cache:/app/.gradle
- backoffice_consumers_build:/app/build
depends_on:
- shared_mysql
- shared_rabbitmq
- backoffice_elasticsearch
command: ["./gradlew", "bootRun", "--args", "backoffice_backend domain-events:rabbitmq:consume"]

backoffice_frontend_server_java:
container_name: codely-java_ddd_example-backoffice_frontend_server
build:
Expand All @@ -68,6 +85,7 @@ services:
volumes:
- .:/app:delegated
- backoffice_frontend_gradle_cache:/app/.gradle
- backoffice_frontend_build:/app/build
depends_on:
- shared_mysql
- shared_rabbitmq
Expand All @@ -85,29 +103,37 @@ services:
volumes:
- .:/app:delegated
- mooc_backend_gradle_cache:/app/.gradle
- mooc_backend_build:/app/build
depends_on:
- shared_mysql
- shared_rabbitmq
- backoffice_elasticsearch
command: ["./gradlew", "bootRun", "--args", "mooc_backend server"]

test_server_java:
container_name: codely-java_ddd_example-test_server
mooc_backend_consumers_java:
container_name: codely-java_ddd_example-mooc_backend_consumers
build:
context: .
dockerfile: Dockerfile
restart: unless-stopped
volumes:
- .:/app:delegated
- test_gradle_cache:/app/.gradle
- mooc_consumers_gradle_cache:/app/.gradle
- mooc_consumers_build:/app/build
depends_on:
- shared_mysql
- shared_rabbitmq
- backoffice_elasticsearch
tty: true
command: ["./gradlew", "bootRun", "--args", "mooc_backend domain-events:rabbitmq:consume"]

volumes:
backoffice_backend_gradle_cache:
backoffice_backend_build:
backoffice_consumers_gradle_cache:
backoffice_consumers_build:
backoffice_frontend_gradle_cache:
backoffice_frontend_build:
mooc_backend_gradle_cache:
test_gradle_cache:
mooc_backend_build:
mooc_consumers_gradle_cache:
mooc_consumers_build:
62 changes: 62 additions & 0 deletions etc/http/publish_domain_events.http
This file contains hidden or 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,62 @@
POST http://localhost:8030/domain-events
Content-Type: application/json

{
"eventName": "course.created",
"eventRaw": {
"data": {
"id": "{{$random.uuid}}",
"type": "course.created",
"occurred_on": "2023-11-14 10:00:00",
"attributes": {
"id": "c3a11f1d-512e-420b-aeae-e687a3c449aa",
"name": "Demo course",
"duration": "2 days"
}
},
"meta": {
}
}
}

###
POST http://localhost:8030/domain-events
Content-Type: application/json

{
"eventName": "course.renamed",
"eventRaw": {
"data": {
"id": "{{$random.uuid}}",
"type": "course.renamed",
"occurred_on": "2023-11-14 10:00:00",
"attributes": {
"id": "7b081a3e-f90e-4efe-a3a5-81e853e89c8b",
"name": "Este es el nombre bueno"
}
},
"meta": {
}
}
}

###
POST http://localhost:8030/domain-events
Content-Type: application/json

{
"eventName": "course.renamed",
"eventRaw": {
"data": {
"id": "{{$random.uuid}}",
"type": "course.renamed",
"occurred_on": "2022-11-14 10:00:00",
"attributes": {
"id": "7b081a3e-f90e-4efe-a3a5-81e853e89c8b",
"name": "Este es el nombre malo"
}
},
"meta": {
}
}
}
33 changes: 33 additions & 0 deletions etc/scripts/create_infinite_courses.sh
This file contains hidden or 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,33 @@
function create_course() {
eventId=$(uuidgen)
courseId=$(uuidgen)
courseName=$(shuf -n3 /usr/share/dict/words | xargs)
courseDuration=$((1 + RANDOM % 1000))

curl -X POST --location "http://localhost:8030/domain-events" \
-H "Content-Type: application/json" \
-d '{
"eventName": "course.created",
"eventRaw": {
"data": {
"id": "'"$eventId"'",
"type": "course.created",
"occurred_on": "2023-11-14 10:00:00",
"attributes": {
"id": "'"$courseId"'",
"name": "'"$courseName"'",
"duration": "'"$courseDuration"' days"
}
},
"meta": {
}
}
}'

echo "Created: $courseName"
}

while true; do
create_course &
sleep 0.001
done
This file contains hidden or 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
Expand Up @@ -13,6 +13,8 @@ public BackofficeCourseCreator(BackofficeCourseRepository repository) {
}

public void create(String id, String name, String duration) {
this.repository.save(new BackofficeCourse(id, name, duration));
if (this.repository.search(id).isEmpty()) {
this.repository.save(new BackofficeCourse(id, name, duration));
}
}
}
Loading

Back | FazBrowse Home | New Git URL