-
Notifications
You must be signed in to change notification settings - Fork 0
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
c2c61b1
commit f35bded
Showing
12 changed files
with
407 additions
and
43 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
9 changes: 2 additions & 7 deletions
9
api/src/main/java/ca/bc/gov/educ/eas/api/constants/EventType.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 |
---|---|---|
@@ -1,11 +1,6 @@ | ||
package ca.bc.gov.educ.eas.api.constants; | ||
|
||
/** | ||
* The enum Event type. | ||
*/ | ||
public enum EventType { | ||
READ_FROM_TOPIC, | ||
INITIATED, | ||
MARK_SAGA_COMPLETE, | ||
GET_PAGINATED_SCHOOLS | ||
CREATE_STUDENT_REGISTRATION, | ||
GET_STUDENT_REGISTRATION | ||
} |
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
75 changes: 75 additions & 0 deletions
75
api/src/main/java/ca/bc/gov/educ/eas/api/model/v1/EasEventEntity.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,75 @@ | ||
package ca.bc.gov.educ.eas.api.model.v1; | ||
|
||
import jakarta.persistence.*; | ||
import jakarta.validation.constraints.NotNull; | ||
import jakarta.validation.constraints.PastOrPresent; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
import org.hibernate.annotations.DynamicUpdate; | ||
import org.hibernate.annotations.UuidGenerator; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
import java.time.LocalDateTime; | ||
import java.util.UUID; | ||
|
||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
@Builder | ||
@Entity | ||
@Table(name = "EAS_EVENT") | ||
@Data | ||
@DynamicUpdate | ||
public class EasEventEntity { | ||
|
||
@Id | ||
@UuidGenerator | ||
@Column(name = "EVENT_ID", unique = true, updatable = false, columnDefinition = "BINARY(16)") | ||
private UUID eventId; | ||
@NotNull(message = "eventPayload cannot be null") | ||
@Lob | ||
@Column(name = "EVENT_PAYLOAD") | ||
private byte[] eventPayloadBytes; | ||
@NotNull(message = "eventStatus cannot be null") | ||
@Column(name = "EVENT_STATUS") | ||
private String eventStatus; | ||
@NotNull(message = "eventType cannot be null") | ||
@Column(name = "EVENT_TYPE") | ||
private String eventType; | ||
@Column(name = "CREATE_USER", updatable = false) | ||
String createUser; | ||
@Column(name = "CREATE_DATE", updatable = false) | ||
@PastOrPresent | ||
LocalDateTime createDate; | ||
@Column(name = "UPDATE_USER") | ||
String updateUser; | ||
@Column(name = "UPDATE_DATE") | ||
@PastOrPresent | ||
LocalDateTime updateDate; | ||
@Column(name = "SAGA_ID", updatable = false) | ||
private UUID sagaId; | ||
@NotNull(message = "eventOutcome cannot be null.") | ||
@Column(name = "EVENT_OUTCOME") | ||
private String eventOutcome; | ||
@Column(name = "REPLY_CHANNEL") | ||
private String replyChannel; | ||
|
||
public String getEventPayload() { | ||
return new String(getEventPayloadBytes(), StandardCharsets.UTF_8); | ||
} | ||
|
||
public void setEventPayload(String eventPayload) { | ||
setEventPayloadBytes(eventPayload.getBytes(StandardCharsets.UTF_8)); | ||
} | ||
|
||
public static class EasEventBuilder { | ||
|
||
byte[] eventPayloadBytes; | ||
|
||
public EasEventEntity.EasEventBuilder eventPayload(String eventPayload) { | ||
this.eventPayloadBytes = eventPayload.getBytes(StandardCharsets.UTF_8); | ||
return this; | ||
} | ||
} | ||
} |
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
17 changes: 17 additions & 0 deletions
17
api/src/main/java/ca/bc/gov/educ/eas/api/repository/v1/EasEventRepository.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,17 @@ | ||
package ca.bc.gov.educ.eas.api.repository.v1; | ||
|
||
|
||
import ca.bc.gov.educ.eas.api.model.v1.EasEventEntity; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import java.util.Optional; | ||
import java.util.UUID; | ||
|
||
|
||
@Repository | ||
public interface EasEventRepository extends JpaRepository<EasEventEntity, UUID> { | ||
|
||
Optional<EasEventEntity> findBySagaIdAndEventType(UUID sagaId, String eventType); | ||
|
||
} |
1 change: 1 addition & 0 deletions
1
api/src/main/java/ca/bc/gov/educ/eas/api/repository/v1/SagaEventRepository.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
94 changes: 94 additions & 0 deletions
94
api/src/main/java/ca/bc/gov/educ/eas/api/service/v1/events/EventHandlerDelegatorService.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,94 @@ | ||
package ca.bc.gov.educ.eas.api.service.v1.events; | ||
|
||
|
||
import ca.bc.gov.educ.eas.api.messaging.MessagePublisher; | ||
import ca.bc.gov.educ.eas.api.messaging.jetstream.Publisher; | ||
import ca.bc.gov.educ.eas.api.model.v1.EasEventEntity; | ||
import ca.bc.gov.educ.eas.api.struct.Event; | ||
import io.nats.client.Message; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.apache.commons.lang3.tuple.Pair; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Service; | ||
|
||
import static ca.bc.gov.educ.eas.api.service.v1.events.EventHandlerService.PAYLOAD_LOG; | ||
|
||
|
||
/** | ||
* The type Event handler service. | ||
*/ | ||
@Service | ||
@Slf4j | ||
@SuppressWarnings({"java:S3864", "java:S3776"}) | ||
public class EventHandlerDelegatorService { | ||
|
||
/** | ||
* The constant RESPONDING_BACK_TO_NATS_ON_CHANNEL. | ||
*/ | ||
public static final String RESPONDING_BACK_TO_NATS_ON_CHANNEL = "responding back to NATS on {} channel "; | ||
private final MessagePublisher messagePublisher; | ||
private final EventHandlerService eventHandlerService; | ||
private final Publisher publisher; | ||
|
||
/** | ||
* Instantiates a new Event handler delegator service. | ||
* | ||
* @param messagePublisher the message publisher | ||
* @param eventHandlerService the event handler service | ||
* @param publisher the publisher | ||
*/ | ||
@Autowired | ||
public EventHandlerDelegatorService(MessagePublisher messagePublisher, EventHandlerService eventHandlerService, Publisher publisher) { | ||
this.messagePublisher = messagePublisher; | ||
this.eventHandlerService = eventHandlerService; | ||
this.publisher = publisher; | ||
} | ||
|
||
/** | ||
* Handle event. | ||
* | ||
* @param event the event | ||
* @param message the message | ||
*/ | ||
public void handleEvent(final Event event, final Message message) { | ||
byte[] response; | ||
Pair<byte[], EasEventEntity> pair; | ||
boolean isSynchronous = message.getReplyTo() != null; | ||
try { | ||
switch (event.getEventType()) { | ||
case GET_STUDENT_REGISTRATION: | ||
log.info("received GET_STUDENT event :: {}", event.getSagaId()); | ||
log.trace(PAYLOAD_LOG, event.getEventPayload()); | ||
response = eventHandlerService.handleGetStudentRegistrationEvent(event, isSynchronous); | ||
log.info(RESPONDING_BACK_TO_NATS_ON_CHANNEL, message.getReplyTo() != null ? message.getReplyTo() : event.getReplyTo()); | ||
publishToNATS(event, message, isSynchronous, response); | ||
break; | ||
case CREATE_STUDENT_REGISTRATION: | ||
log.info("received create student event :: {}", event.getSagaId()); | ||
log.trace(PAYLOAD_LOG, event.getEventPayload()); | ||
pair = eventHandlerService.handleCreateStudentRegistrationEvent(event); | ||
log.info(RESPONDING_BACK_TO_NATS_ON_CHANNEL, message.getReplyTo() != null ? message.getReplyTo() : event.getReplyTo()); | ||
publishToNATS(event, message, isSynchronous, pair.getLeft()); | ||
publishToJetStream(pair.getRight()); | ||
break; | ||
default: | ||
log.info("silently ignoring other events :: {}", event); | ||
break; | ||
} | ||
} catch (final Exception e) { | ||
log.error("Exception", e); | ||
} | ||
} | ||
|
||
private void publishToNATS(Event event, Message message, boolean isSynchronous, byte[] left) { | ||
if (isSynchronous) { // sync, req/reply pattern of nats | ||
messagePublisher.dispatchMessage(message.getReplyTo(), left); | ||
} else { // async, pub/sub | ||
messagePublisher.dispatchMessage(event.getReplyTo(), left); | ||
} | ||
} | ||
|
||
private void publishToJetStream(final EasEventEntity event) { | ||
publisher.dispatchChoreographyEvent(event); | ||
} | ||
} |
Oops, something went wrong.