-
Notifications
You must be signed in to change notification settings - Fork 1
GRAD2-2976 - Adds saga for archive student process #700
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
Open
mightycox
wants to merge
3
commits into
grad-release
Choose a base branch
from
feature/GRAD2-2976
base: grad-release
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
5 changes: 5 additions & 0 deletions
5
api/src/main/java/ca/bc/gov/educ/api/gradstudent/constant/SagaEnum.java
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,5 @@ | ||
package ca.bc.gov.educ.api.gradstudent.constant; | ||
|
||
public enum SagaEnum { | ||
ARCHIVE_STUDENTS_SAGA | ||
} |
23 changes: 23 additions & 0 deletions
23
api/src/main/java/ca/bc/gov/educ/api/gradstudent/constant/SagaStatusEnum.java
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,23 @@ | ||
package ca.bc.gov.educ.api.gradstudent.constant; | ||
|
||
/** | ||
* The enum Saga status enum. | ||
*/ | ||
public enum SagaStatusEnum { | ||
/** | ||
* Started saga status enum. | ||
*/ | ||
STARTED, | ||
/** | ||
* In progress saga status enum. | ||
*/ | ||
IN_PROGRESS, | ||
/** | ||
* Completed saga status enum. | ||
*/ | ||
COMPLETED, | ||
/** | ||
* Force stopped saga status enum. | ||
*/ | ||
FORCE_STOPPED | ||
} |
25 changes: 25 additions & 0 deletions
25
api/src/main/java/ca/bc/gov/educ/api/gradstudent/constant/StudentStatusCodes.java
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,25 @@ | ||
package ca.bc.gov.educ.api.gradstudent.constant; | ||
|
||
import lombok.Getter; | ||
|
||
import java.util.Arrays; | ||
import java.util.Objects; | ||
import java.util.Optional; | ||
|
||
@Getter | ||
public enum StudentStatusCodes { | ||
CURRENT("CUR"), | ||
ARCHIVED("ARC"), | ||
DECEASED("DEC"), | ||
MERGED("MER"), | ||
TERMINATED("TER"); | ||
|
||
private final String code; | ||
StudentStatusCodes(String code) { | ||
this.code = code; | ||
} | ||
|
||
public static Optional<StudentStatusCodes> findByValue(String value) { | ||
return Arrays.stream(values()).filter(e -> Objects.equals(e.code, value)).findFirst(); | ||
} | ||
} |
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
36 changes: 36 additions & 0 deletions
36
api/src/main/java/ca/bc/gov/educ/api/gradstudent/messaging/MessagePublisher.java
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,36 @@ | ||
package ca.bc.gov.educ.api.gradstudent.messaging; | ||
|
||
import io.nats.client.Connection; | ||
import lombok.extern.slf4j.Slf4j; | ||
import lombok.val; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.time.Duration; | ||
import java.util.Optional; | ||
|
||
@Component | ||
@Slf4j | ||
public class MessagePublisher { | ||
private final Connection connection; | ||
|
||
@Autowired | ||
public MessagePublisher(final Connection con) { | ||
this.connection = con; | ||
} | ||
|
||
public void dispatchMessage(final String subject, final byte[] message) { | ||
this.connection.publish(subject, message); | ||
} | ||
|
||
public Optional<String> requestMessage(final String subject, final byte[] message) throws InterruptedException { | ||
log.debug("requesting from NATS on topic :: {} with payload :: {}", subject, new String(message)); | ||
val response = this.connection.request(subject, message, Duration.ofSeconds(30)).getData(); | ||
if (response == null || response.length == 0) { | ||
return Optional.empty(); | ||
} | ||
val responseValue = new String(response); | ||
log.debug("got response from NATS :: {}", responseValue); | ||
return Optional.of(responseValue); | ||
} | ||
} |
91 changes: 91 additions & 0 deletions
91
api/src/main/java/ca/bc/gov/educ/api/gradstudent/messaging/MessageSubscriber.java
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,91 @@ | ||
package ca.bc.gov.educ.api.gradstudent.messaging; | ||
|
||
import ca.bc.gov.educ.api.gradstudent.model.dc.Event; | ||
import ca.bc.gov.educ.api.gradstudent.orchestrator.base.EventHandler; | ||
import ca.bc.gov.educ.api.gradstudent.service.events.EventHandlerDelegatorService; | ||
import ca.bc.gov.educ.api.gradstudent.util.EducGradStudentApiConstants; | ||
import ca.bc.gov.educ.api.gradstudent.util.JsonUtil; | ||
import ca.bc.gov.educ.api.gradstudent.util.LogHelper; | ||
import io.nats.client.Connection; | ||
import io.nats.client.Message; | ||
import io.nats.client.MessageHandler; | ||
import jakarta.annotation.PostConstruct; | ||
import lombok.Getter; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import static ca.bc.gov.educ.api.gradstudent.constant.Topics.GRAD_STUDENT_API_TOPIC; | ||
import static lombok.AccessLevel.PRIVATE; | ||
|
||
@Component | ||
@Slf4j | ||
public class MessageSubscriber { | ||
|
||
@Getter(PRIVATE) | ||
private final Map<String, EventHandler> handlerMap = new HashMap<>(); | ||
@Getter(PRIVATE) | ||
private final EventHandlerDelegatorService eventHandlerDelegatorService; | ||
private final Connection connection; | ||
private final EducGradStudentApiConstants constants; | ||
|
||
@Autowired | ||
public MessageSubscriber(final Connection con, final List<EventHandler> eventHandlers, final EducGradStudentApiConstants constants, final EventHandlerDelegatorService eventHandlerDelegatorService) { | ||
this.eventHandlerDelegatorService = eventHandlerDelegatorService; | ||
this.connection = con; | ||
eventHandlers.forEach(handler -> { | ||
this.handlerMap.put(handler.getTopicToSubscribe(), handler); | ||
this.subscribeForSAGA(handler.getTopicToSubscribe(), handler); | ||
}); | ||
this.constants = constants; | ||
} | ||
|
||
@PostConstruct | ||
public void subscribe() { | ||
final String queue = GRAD_STUDENT_API_TOPIC.toString().replace("_", "-"); | ||
final var dispatcher = this.connection.createDispatcher(this.onMessage()); | ||
dispatcher.subscribe(GRAD_STUDENT_API_TOPIC.toString(), queue); | ||
} | ||
|
||
public MessageHandler onMessage() { | ||
return (Message message) -> { | ||
if (message != null) { | ||
log.info("Message received subject :: {}, replyTo :: {}, subscriptionID :: {}", message.getSubject(), message.getReplyTo(), message.getSID()); | ||
try { | ||
final var eventString = new String(message.getData()); | ||
LogHelper.logMessagingEventDetails(eventString, constants.isSplunkLogHelperEnabled()); | ||
final var event = JsonUtil.getJsonObjectFromString(Event.class, eventString); | ||
eventHandlerDelegatorService.handleEvent(event, message); | ||
} catch (final Exception e) { | ||
log.error("Exception ", e); | ||
} | ||
} | ||
}; | ||
} | ||
|
||
private static MessageHandler onMessageForSAGA(final EventHandler eventHandler) { | ||
return (Message message) -> { | ||
if (message != null) { | ||
log.info("Message received subject :: {}, replyTo :: {}, subscriptionID :: {}", message.getSubject(), message.getReplyTo(), message.getSID()); | ||
try { | ||
final var eventString = new String(message.getData()); | ||
final var event = JsonUtil.getJsonObjectFromString(Event.class, eventString); | ||
eventHandler.handleEvent(event); | ||
} catch (final Exception e) { | ||
log.error("Exception ", e); | ||
} | ||
} | ||
}; | ||
} | ||
|
||
private void subscribeForSAGA(final String topic, final EventHandler eventHandler) { | ||
this.handlerMap.computeIfAbsent(topic, k -> eventHandler); | ||
final String queue = topic.replace("_", "-"); | ||
final var dispatcher = this.connection.createDispatcher(MessageSubscriber.onMessageForSAGA(eventHandler)); | ||
dispatcher.subscribe(topic, queue); | ||
} | ||
} |
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
18 changes: 0 additions & 18 deletions
18
api/src/main/java/ca/bc/gov/educ/api/gradstudent/model/dc/EventOutcome.java
This file was deleted.
Oops, something went wrong.
18 changes: 0 additions & 18 deletions
18
api/src/main/java/ca/bc/gov/educ/api/gradstudent/model/dc/EventType.java
This file was deleted.
Oops, something went wrong.
13 changes: 13 additions & 0 deletions
13
api/src/main/java/ca/bc/gov/educ/api/gradstudent/model/dc/NotificationEvent.java
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,13 @@ | ||
package ca.bc.gov.educ.api.gradstudent.model.dc; | ||
|
||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | ||
import lombok.Data; | ||
import lombok.EqualsAndHashCode; | ||
|
||
@EqualsAndHashCode(callSuper = true) | ||
@Data | ||
@JsonIgnoreProperties(ignoreUnknown = true) | ||
public class NotificationEvent extends Event { | ||
private String sagaStatus; | ||
private String sagaName; | ||
} |
21 changes: 21 additions & 0 deletions
21
api/src/main/java/ca/bc/gov/educ/api/gradstudent/model/dto/ArchiveStudentsSagaData.java
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,21 @@ | ||
package ca.bc.gov.educ.api.gradstudent.model.dto; | ||
|
||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
import java.util.List; | ||
|
||
@Data | ||
@Builder | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
@JsonIgnoreProperties(ignoreUnknown = true) | ||
public class ArchiveStudentsSagaData { | ||
List<String> schoolsOfRecords; | ||
long batchId; | ||
String updateUser; | ||
String studentStatusCode; | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see some redundant codes in this class.
handlerMap is to handle multiple subscribers. I think subscribe() method below could be enhanced to use handlerMap so that onMessage could handle messages from multiple topics instead of having the separate onMessageForSAGA.