-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9bbe84b
commit 0c5b9dd
Showing
14 changed files
with
381 additions
and
122 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
38 changes: 38 additions & 0 deletions
38
shipping/src/main/java/org/shipping/config/EventMappingConfig.java
This file contains 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,38 @@ | ||
package org.shipping.config; | ||
|
||
import jakarta.enterprise.context.ApplicationScoped; | ||
import org.eclipse.microprofile.config.Config; | ||
import org.eclipse.microprofile.config.ConfigProvider; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.UUID; | ||
|
||
@ApplicationScoped | ||
public class EventMappingConfig { | ||
|
||
private final Map<UUID, String> eventMappings = new HashMap<>(); | ||
|
||
public EventMappingConfig() { | ||
Config config = ConfigProvider.getConfig(); | ||
config.getPropertyNames() | ||
.forEach(property -> { | ||
if (property.startsWith("event.mapping.")) { | ||
String uuidString = property.replace("event.mapping.", ""); | ||
String status = config.getValue(property, String.class); | ||
eventMappings.put(UUID.fromString(uuidString), status); | ||
} | ||
}); | ||
} | ||
|
||
public String getDeliveryStatus(String eventType) { | ||
try { | ||
// Convert the eventType string to UUID | ||
UUID eventTypeUUID = UUID.fromString(eventType); | ||
return eventMappings.get(eventTypeUUID); // Return status if found | ||
} catch (IllegalArgumentException e) { | ||
// Handle invalid UUID format, log or return a default value | ||
return null; | ||
} | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
shipping/src/main/java/org/shipping/exception/AddressNotFoundException.java
This file contains 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,7 @@ | ||
package org.shipping.exception; | ||
|
||
public class AddressNotFoundException extends RuntimeException { | ||
public AddressNotFoundException(String message) { | ||
super(message); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
shipping/src/main/java/org/shipping/exception/OrderAlreadyAssociatedException.java
This file contains 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,7 @@ | ||
package org.shipping.exception; | ||
|
||
public class OrderAlreadyAssociatedException extends RuntimeException { | ||
public OrderAlreadyAssociatedException(String message) { | ||
super(message); | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
shipping/src/main/java/org/shipping/messaging/OutboxEventPublisher.java
This file contains 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,66 @@ | ||
package org.shipping.messaging; | ||
|
||
import jakarta.enterprise.context.ApplicationScoped; | ||
import jakarta.inject.Inject; | ||
|
||
import org.shipping.config.EventMappingConfig; | ||
import org.shipping.model.OutboxEvent; | ||
import org.shipping.model.DeliveryStatus; | ||
import org.shipping.repository.OutboxRepository; | ||
|
||
import io.quarkus.scheduler.Scheduled; | ||
import org.jboss.logging.Logger; | ||
|
||
import java.util.List; | ||
import java.util.UUID; | ||
|
||
@ApplicationScoped | ||
public class OutboxEventPublisher { | ||
|
||
private static final Logger logger = Logger.getLogger(OutboxEventPublisher.class); | ||
|
||
@Inject | ||
OutboxRepository outboxRepository; | ||
|
||
@Inject | ||
DeliveryStatusPublisher deliveryStatusPublisher; | ||
|
||
@Inject | ||
EventMappingConfig eventMappingConfig; | ||
|
||
@Scheduled(every = "60s") | ||
public void processOutbox() { | ||
// Récupération des événements non traités | ||
List<OutboxEvent> events = outboxRepository.find("processed = 'false'").list(); | ||
|
||
for (OutboxEvent event : events) { | ||
try { | ||
// Extraction du type d'événement | ||
String eventType = event.getEventType(); | ||
String statusName = eventMappingConfig.getDeliveryStatus(eventType); | ||
|
||
if (statusName != null) { | ||
// Conversion du statut et publication | ||
DeliveryStatus status = DeliveryStatus.valueOf(statusName); | ||
deliveryStatusPublisher.publishStatus(UUID.fromString(event.getPayload()), status); | ||
|
||
// Journaux pour le débogage | ||
logger.infof("Traitement de l'événement %s avec le type %s", event.getId(), event.getEventType()); | ||
logger.infof("Statut publié : %s", statusName); | ||
|
||
// Marquage de l'événement comme traité | ||
event.setProcessed("true"); | ||
outboxRepository.persist(event); | ||
logger.info("L'événement a été marqué comme traité."); | ||
} else { | ||
// Aucun mappage trouvé | ||
logger.warnf("Aucun mappage trouvé pour le type d'événement : %s", eventType); | ||
} | ||
} catch (Exception e) { | ||
// Gestion des erreurs | ||
logger.errorf("Erreur lors du traitement de l'événement %s : %s", event.getId(), e.getMessage()); | ||
e.printStackTrace(); | ||
} | ||
} | ||
} | ||
} |
This file contains 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,5 @@ | ||
package org.shipping.messaging; | ||
|
||
public @interface Scheduled { | ||
|
||
} |
22 changes: 22 additions & 0 deletions
22
shipping/src/main/java/org/shipping/model/EventTypeMapper.java
This file contains 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,22 @@ | ||
package org.shipping.model; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class EventTypeMapper { | ||
// Dictionnaire pour mappage d'événements | ||
private static final Map<String, String> eventTypes = new HashMap<>(); | ||
|
||
static { | ||
// Vous pouvez ajouter autant d'événements que vous le souhaitez | ||
eventTypes.put("SHIPPING_CREATED", "shipping.created"); | ||
eventTypes.put("SHIPPING_UPDATED", "shipping.updated"); | ||
eventTypes.put("SHIPPING_DELETED", "shipping.deleted"); | ||
// Vous pouvez ajouter d'autres événements ici | ||
} | ||
|
||
// Récupère le type d'événement en fonction du nom | ||
public static String getEventType(String eventType) { | ||
return eventTypes.getOrDefault(eventType, "unknown.event"); | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
shipping/src/main/java/org/shipping/model/OutboxEvent.java
This file contains 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,68 @@ | ||
package org.shipping.model; | ||
|
||
|
||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.Id; | ||
import java.util.UUID; | ||
|
||
@Entity | ||
public class OutboxEvent { | ||
|
||
@Id | ||
private UUID id; | ||
private UUID aggregateId; | ||
private String eventType; | ||
private String payload; | ||
private String processed ; | ||
|
||
public OutboxEvent() { | ||
} | ||
|
||
public OutboxEvent(UUID id, UUID aggregateId, String eventType, String payload, String processed) { | ||
this.id = id; | ||
this.aggregateId = aggregateId; | ||
this.eventType = eventType; | ||
this.payload = payload; | ||
this.processed = processed; | ||
} | ||
|
||
public OutboxEvent(UUID aggregateId, String eventType, String payload, String processed) { | ||
this.aggregateId = aggregateId; | ||
this.eventType = eventType; | ||
this.payload = payload; | ||
this.processed = processed; | ||
} | ||
|
||
public UUID getId() { | ||
return id; | ||
} | ||
public void setId(UUID id) { | ||
this.id = id; | ||
} | ||
public UUID getAggregateId() { | ||
return aggregateId; | ||
} | ||
public void setAggregateId(UUID aggregateId) { | ||
this.aggregateId = aggregateId; | ||
} | ||
public String getEventType() { | ||
return eventType; | ||
} | ||
public void setEventType(String eventType) { | ||
this.eventType = eventType; | ||
} | ||
public String getPayload() { | ||
return payload; | ||
} | ||
public void setPayload(String payload) { | ||
this.payload = payload; | ||
} | ||
public String isProcessed() { | ||
return processed; | ||
} | ||
public void setProcessed(String processed) { | ||
this.processed = processed; | ||
} | ||
|
||
|
||
} |
This file contains 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
10 changes: 10 additions & 0 deletions
10
shipping/src/main/java/org/shipping/repository/OutboxRepository.java
This file contains 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,10 @@ | ||
package org.shipping.repository; | ||
|
||
import io.quarkus.hibernate.orm.panache.PanacheRepository; | ||
import jakarta.enterprise.context.ApplicationScoped; | ||
|
||
import org.shipping.model.OutboxEvent; | ||
|
||
@ApplicationScoped | ||
public class OutboxRepository implements PanacheRepository<OutboxEvent> { | ||
} |
Oops, something went wrong.