Skip to content

Commit 48e8256

Browse files
committed
Small updates to the inteactions
1 parent 0091579 commit 48e8256

File tree

1 file changed

+27
-21
lines changed

1 file changed

+27
-21
lines changed

JeMPI_Apps/JeMPI_Linker/src/main/java/org/jembi/jempi/linker/SPInteractions.java

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.jembi.jempi.shared.serdes.JsonPojoSerializer;
2121
import org.jetbrains.annotations.NotNull;
2222

23+
import java.time.Duration;
2324
import java.util.List;
2425
import java.util.Properties;
2526
import java.util.concurrent.ExecutionException;
@@ -45,24 +46,30 @@ public static SPInteractions create(final String topic_) {
4546
return new SPInteractions(topic_);
4647
}
4748

49+
private void linkPatientProcess(final ActorSystem<Void> system, final ActorRef<BackEnd.Request> backEnd, final String key, final InteractionEnvelop interactionEnvelop) {
50+
final var completableFuture = Ask.runStartEndHooks(system, backEnd, key, interactionEnvelop).toCompletableFuture();
51+
try {
52+
List<MpiGeneralError> hookErrors = completableFuture.get(65, TimeUnit.SECONDS).hooksResults();
53+
if (!hookErrors.isEmpty()) {
54+
LOGGER.error(hookErrors);
55+
}
56+
} catch (InterruptedException | ExecutionException | TimeoutException ex) {
57+
LOGGER.error(ex.getLocalizedMessage(), ex);
58+
this.closingLinkingStream();
59+
}
60+
}
4861
private void linkPatient(
4962
final ActorSystem<Void> system,
5063
final ActorRef<BackEnd.Request> backEnd,
5164
final String key,
5265
final InteractionEnvelop interactionEnvelop) {
5366

54-
if (interactionEnvelop.contentType() == InteractionEnvelop.ContentType.BATCH_START_SENTINEL
55-
|| interactionEnvelop.contentType() == BATCH_END_SENTINEL) {
56-
final var completableFuture = Ask.runStartEndHooks(system, backEnd, key, interactionEnvelop).toCompletableFuture();
57-
try {
58-
List<MpiGeneralError> hookErrors = completableFuture.get(65, TimeUnit.SECONDS).hooksResults();
59-
if (!hookErrors.isEmpty()) {
60-
LOGGER.error(hookErrors);
61-
}
62-
} catch (InterruptedException | ExecutionException | TimeoutException ex) {
63-
LOGGER.error(ex.getLocalizedMessage(), ex);
64-
this.closingLinkingStream();
65-
}
67+
if (interactionEnvelop.contentType() == InteractionEnvelop.ContentType.BATCH_START_SENTINEL) {
68+
LOGGER.info(String.format("SPInteractions Stream Processor -> Starting linking for tag '%s'", interactionEnvelop.tag()));
69+
linkPatientProcess(system, backEnd, key, interactionEnvelop);
70+
} else if (interactionEnvelop.contentType() == BATCH_END_SENTINEL) {
71+
LOGGER.info(String.format("SPInteractions Stream Processor -> Ended linking for tag '%s'", interactionEnvelop.tag()));
72+
linkPatientProcess(system, backEnd, key, interactionEnvelop);
6673
}
6774

6875
if (interactionEnvelop.contentType() != BATCH_INTERACTION) {
@@ -110,10 +117,11 @@ private StreamsBuilder getMatchingStream(final ActorSystem<Void> system, final A
110117
new JsonPojoDeserializer<>(InteractionEnvelop.class));
111118
final StreamsBuilder streamsBuilder = new StreamsBuilder();
112119
final KStream<String, InteractionEnvelop> matchStream =
113-
streamsBuilder.stream(GlobalConstants.TOPIC_NOTIFICATIONS, Consumed.with(stringSerde, interactionEnvelopSerde));
120+
streamsBuilder.stream(GlobalConstants.TOPIC_INTERACTION_LINKER_MATCHING, Consumed.with(stringSerde, interactionEnvelopSerde));
114121
matchStream.foreach((key, matchEnvelop) -> {
115122
matchPatient(system, backEnd, key, matchEnvelop);
116123
if (matchEnvelop.contentType() == BATCH_END_SENTINEL) {
124+
LOGGER.info(String.format("SPInteractions Stream Processor -> Ended matching for tag '%s'", matchEnvelop.tag()));
117125
this.closingMatchingStream();
118126
}
119127
});
@@ -130,9 +138,8 @@ private StreamsBuilder getLinkingStream(final ActorSystem<Void> system, final Ac
130138
interactionStream.foreach((key, interactionEnvelop) -> {
131139
linkPatient(system, backEnd, key, interactionEnvelop);
132140
if (!CustomMU.SEND_INTERACTIONS_TO_EM && interactionEnvelop.contentType() == BATCH_END_SENTINEL) {
133-
LOGGER.info("SPInteractions Stream Processor -> Starting matching processor");
141+
LOGGER.info(String.format("SPInteractions Stream Processor -> Starting matching for tag '%s'", interactionEnvelop.tag()));
134142
matchingStream.start();
135-
this.closingLinkingStream();
136143
}
137144
});
138145
return streamsBuilder;
@@ -142,9 +149,8 @@ public void open(
142149
final ActorSystem<Void> system,
143150
final ActorRef<BackEnd.Request> backEnd) {
144151
LOGGER.info("SPInteractions Stream Processor");
145-
final Properties props = loadConfig();
146-
matchingEnvelopeKafkaStream = new KafkaStreams(getMatchingStream(system, backEnd).build(), props);
147-
interactionEnvelopKafkaStreams = new KafkaStreams(getLinkingStream(system, backEnd, matchingEnvelopeKafkaStream).build(), props);
152+
matchingEnvelopeKafkaStream = new KafkaStreams(getMatchingStream(system, backEnd).build(), loadConfig(topic));
153+
interactionEnvelopKafkaStreams = new KafkaStreams(getLinkingStream(system, backEnd, matchingEnvelopeKafkaStream).build(), loadConfig(GlobalConstants.TOPIC_INTERACTION_LINKER_MATCHING));
148154
interactionEnvelopKafkaStreams.cleanUp();
149155
LOGGER.info("SPInteractions Stream Processor -> Starting linking processor");
150156
interactionEnvelopKafkaStreams.start();
@@ -158,13 +164,13 @@ private void closingLinkingStream() {
158164

159165
private void closingMatchingStream() {
160166
LOGGER.info("SPInteractions Stream Processor -> Closing matching processor");
161-
interactionEnvelopKafkaStreams.close(new KafkaStreams.CloseOptions().leaveGroup(true));
167+
matchingEnvelopeKafkaStream.close(new KafkaStreams.CloseOptions().leaveGroup(true).timeout(Duration.ofSeconds(2)));
162168
}
163169

164-
private Properties loadConfig() {
170+
private Properties loadConfig(final String inTopic) {
165171
final Properties props = new Properties();
166172
props.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, AppConfig.KAFKA_BOOTSTRAP_SERVERS);
167-
props.put(StreamsConfig.APPLICATION_ID_CONFIG, AppConfig.KAFKA_APPLICATION_ID_INTERACTIONS + topic);
173+
props.put(StreamsConfig.APPLICATION_ID_CONFIG, AppConfig.KAFKA_APPLICATION_ID_INTERACTIONS + inTopic);
168174
return props;
169175
}
170176

0 commit comments

Comments
 (0)