Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added update product api changes with price change event #2

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -30,85 +30,79 @@
@Component
public class EventBusRabbitMQ implements EventBus {

private static final String EXCHANGE_TYPE_DIRECT = "direct";
private static final String BROKER_NAME = "eshop_event_bus_exchange";
private static final String QUEUE_NAME = "Catalog";

private final Sender sender;
private final InMemoryEventBusSubscriptionManager subscriptionManager;
private final Receiver receiver;

@Override
public Mono<Void> publish(IntegrationEvent event) {

ObjectMapper mapper = new ObjectMapper();
String eventName = event.getClass().getSimpleName();
BasicProperties basicProperties = new BasicProperties().builder().deliveryMode(2).build();

try {

byte[] body = mapper.writeValueAsBytes(event);
OutboundMessage message = new OutboundMessage(BROKER_NAME, eventName, basicProperties, body);

return sender.declare(ExchangeSpecification.exchange(BROKER_NAME).type(EXCHANGE_TYPE_DIRECT))
.then(sender.send(Mono.just(message)));

} catch (JsonProcessingException e) {
return Mono.error(e);
}
}

@Override
public <T extends IntegrationEvent, TH extends IntegrationEventHandler<T>> Mono<Void> subscribe(Class<T> eventType,
Class<TH> eventHandler) {

subscriptionManager.addSubscription(eventType, eventHandler);
log.info("Entered subscribe method");

Mono<Void> mono = Mono.fromRunnable(() -> {
receiver.consumeAutoAck(QUEUE_NAME).subscribe(message -> {

String routingKey = message.getEnvelope().getRoutingKey();
// Class eventType = subscriptionManager.getEventTypeByName(routingKey);
byte[] messageBody = message.getBody();
try {
Object event = new ObjectMapper().readValue(messageBody, eventType);
log.info(event.toString());
List<SubscriptionInfo> subscriptions = subscriptionManager.getHandlersForEvent(eventType);
if (subscriptions != null && !subscriptions.isEmpty()) {
for (SubscriptionInfo subscription : subscriptions) {
Class handler = subscription.getHandler();
Method methodInfo = null;
try {
methodInfo = handler.getDeclaredMethod("handle", IntegrationEvent.class);
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

if(methodInfo != null)
try {
methodInfo.invoke(handler.newInstance(), event);
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}
} catch (IOException e) {
e.printStackTrace();
}
});
});
return sender.declare(ExchangeSpecification.exchange(BROKER_NAME))
.then(sender.declareQueue(QueueSpecification.queue(QUEUE_NAME)))
.then(sender.bind(BindingSpecification.binding(BROKER_NAME, eventType.getSimpleName(), QUEUE_NAME)))
.then(mono);

}
private static final String EXCHANGE_TYPE_DIRECT = "direct";
private static final String BROKER_NAME = "eshop_event_bus_exchange";
private static final String QUEUE_NAME = "Catalog";

private final Sender sender;
private final InMemoryEventBusSubscriptionManager subscriptionManager;
private final Receiver receiver;

@Override
public Mono<Void> publish(IntegrationEvent event) {
ObjectMapper mapper = new ObjectMapper();
String eventName = event.getClass().getSimpleName();
System.out.println("Event Name : " + eventName);
BasicProperties basicProperties = new BasicProperties().builder().deliveryMode(2).build();
try {
byte[] body = mapper.writeValueAsBytes(event);
OutboundMessage message = new OutboundMessage(BROKER_NAME, eventName, basicProperties, body);
return sender.declare(ExchangeSpecification.exchange(BROKER_NAME).type(EXCHANGE_TYPE_DIRECT))
.then(sender.send(Mono.just(message)));
} catch (JsonProcessingException e) {
return Mono.error(e);
}
}

@Override
public <T extends IntegrationEvent, TH extends IntegrationEventHandler<T>> Mono<Void> subscribe(Class<T> eventType,
Class<TH> eventHandler) {
subscriptionManager.addSubscription(eventType, eventHandler);
log.info("Entered subscribe method");
Mono<Void> mono = Mono.fromRunnable(() -> {
receiver.consumeAutoAck(QUEUE_NAME).subscribe(message -> {
String routingKey = message.getEnvelope().getRoutingKey();
// Class eventType = subscriptionManager.getEventTypeByName(routingKey);
byte[] messageBody = message.getBody();
try {
Object event = new ObjectMapper().readValue(messageBody, eventType);
log.info(event.toString());
List<SubscriptionInfo> subscriptions = subscriptionManager.getHandlersForEvent(eventType);
if (subscriptions != null && !subscriptions.isEmpty()) {
for (SubscriptionInfo subscription : subscriptions) {
Class handler = subscription.getHandler();
Method methodInfo = null;
try {
methodInfo = handler.getDeclaredMethod("handle", IntegrationEvent.class);
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

if (methodInfo != null)
try {
methodInfo.invoke(handler.newInstance(), event);
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException
| InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}
} catch (IOException e) {
e.printStackTrace();
}
});
});
return sender.declare(ExchangeSpecification.exchange(BROKER_NAME))
.then(sender.declareQueue(QueueSpecification.queue(QUEUE_NAME)))
.then(sender.bind(BindingSpecification.binding(BROKER_NAME, eventType.getSimpleName(), QUEUE_NAME)))
.then(mono);

}

}
101 changes: 49 additions & 52 deletions src/BuildingBlocks/EventBus/eventbus/pom.xml
Original file line number Diff line number Diff line change
@@ -1,55 +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">
<modelVersion>4.0.0</modelVersion>

<groupId>com.eshoponcontainers</groupId>
<artifactId>eventbus</artifactId>
<version>1.0-SNAPSHOT</version>

<name>eventbus</name>

<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.26</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.3.13</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.13</version>
</dependency>


<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-core</artifactId>
<version>3.5.4</version>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.9.2</version>
<scope>test</scope>
</dependency>

</dependencies>


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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.eshoponcontainers</groupId>
<artifactId>eventbus</artifactId>
<version>1.0-SNAPSHOT</version>
<name>eventbus</name>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.26</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.3.13</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.13</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.7.4</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-r2dbc</artifactId>
<version>2.7.10</version>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-core</artifactId>
<version>3.5.4</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.9.2</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.eshoponcontainers;

public enum EventStateEnum {
NOT_PUBLISHED(0),
IN_PROGRESS(1),
PUBLISHED(2),
PUBLISH_FAILED(3);

private int value;
private EventStateEnum(int value) {
this.value = value;
}
public int getValue() {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Added getValue for Enum as navchar2(Ex : EventStateEnum.NOT_PUBLISHED) cannot be persisted to int in db.
Need to revisit

return value;
}
}
Loading