Skip to content

Commit

Permalink
fix: rabbitmq event bus
Browse files Browse the repository at this point in the history
  • Loading branch information
rgomezcasas committed Nov 13, 2023
1 parent ec05c6c commit 4f0ff47
Show file tree
Hide file tree
Showing 23 changed files with 219 additions and 103 deletions.
2 changes: 1 addition & 1 deletion Makefile
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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
spring.main.allow-bean-definition-overriding=true
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>
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);
}
};
}
}
Empty file.
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");
}
}
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);
}
}
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");
}
}
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
42 changes: 42 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
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,13 +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"]

mooc_backend_consumers_java:
container_name: codely-java_ddd_example-mooc_backend_consumers
build:
context: .
dockerfile: Dockerfile
restart: unless-stopped
volumes:
- .:/app:delegated
- mooc_consumers_gradle_cache:/app/.gradle
- mooc_consumers_build:/app/build
depends_on:
- shared_mysql
- shared_rabbitmq
- backoffice_elasticsearch
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:
mooc_backend_build:
mooc_consumers_gradle_cache:
mooc_consumers_build:
23 changes: 22 additions & 1 deletion etc/http/publish_domain_events.http
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Content-Type: application/json
"type": "course.created",
"occurred_on": "2023-11-14 10:00:00",
"attributes": {
"id": "{{$random.uuid}}",
"id": "9bd0c98a-92cc-4a56-a5a1-7d40839ddc83",
"name": "Demo course",
"duration": "2 days"
}
Expand All @@ -18,3 +18,24 @@ Content-Type: application/json
}
}
}

###
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": "9bd0c98a-92cc-4a56-a5a1-7d40839ddc83",
"name": "heeey 22222"
}
},
"meta": {
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package tv.codely.backoffice.courses.application.rename;

import tv.codely.backoffice.courses.domain.BackofficeCourseNotFound;
import tv.codely.backoffice.courses.domain.BackofficeCourseRepository;
import tv.codely.shared.domain.Service;

Expand All @@ -13,10 +14,13 @@ public BackofficeCourseRenamer(BackofficeCourseRepository repository) {

public void rename(String id, String name) {
this.repository.search(id)
.ifPresent(course -> {
course.rename(name);
.ifPresentOrElse(course -> {
course.rename(name);

this.repository.save(course);
});
this.repository.save(course);
},
() -> {
throw new BackofficeCourseNotFound(id);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
import org.springframework.context.event.EventListener;
import tv.codely.shared.domain.Service;
import tv.codely.shared.domain.bus.event.DomainEventSubscriber;
import tv.codely.shared.domain.course.CourseCreatedDomainEvent;
import tv.codely.shared.domain.course.CourseRenamedDomainEvent;

@Service
@DomainEventSubscriber({CourseCreatedDomainEvent.class})
@DomainEventSubscriber({CourseRenamedDomainEvent.class})
public final class RenameBackofficeCourseOnCourseRenamed {
private final BackofficeCourseRenamer renamer;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package tv.codely.backoffice.courses.domain;

public class BackofficeCourseNotFound extends RuntimeException {
public BackofficeCourseNotFound(String id) {
super(String.format("The course <%s> doesn't exist", id));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
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 tv.codely.shared.infrastructure.bus.event.DomainEventsInformation;
import tv.codely.shared.infrastructure.bus.event.mysql.MySqlDomainEventsConsumer;
import tv.codely.shared.infrastructure.bus.event.mysql.MySqlEventBus;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ void publish_and_consume_domain_events_from_rabbitmq() throws Exception {

eventBus.publish(Collections.singletonList(domainEvent));

consumer.consume();
consumer.consume("mooc");

eventually(() -> assertTrue(subscriber.hasBeenExecuted));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public CourseRenamedDomainEvent(

@Override
public String eventName() {
return "course.created";
return "course.renamed";
}

@Override
Expand Down
Loading

0 comments on commit 4f0ff47

Please sign in to comment.