ruchiD commented on code in PR #2891:
URL: https://github.com/apache/fineract/pull/2891#discussion_r1073184573
##########
fineract-provider/src/main/java/org/apache/fineract/infrastructure/event/external/jobs/SendAsynchronousEventsTasklet.java:
##########
@@ -69,20 +79,66 @@ private boolean isDownstreamChannelEnabled() {
return
fineractProperties.getEvents().getExternal().getProducer().getJms().isEnabled();
}
- private List<ExternalEvent> getQueuedEventsBatch() {
+ private List<ExternalEventView> getQueuedEventsBatch() {
int readBatchSize = getBatchSize();
Pageable batchSize = PageRequest.ofSize(readBatchSize);
- return
repository.findByStatusOrderById(ExternalEventStatus.TO_BE_SENT, batchSize);
+ return measure(() ->
repository.findByStatusOrderById(ExternalEventStatus.TO_BE_SENT, batchSize),
+ (events, timeTaken) -> log.debug("Loaded {} events in {}ms",
events.size(), timeTaken.toMillis()));
+ }
+
+ private void sendEvents(List<ExternalEventView> queuedEvents) {
+ Map<Long, List<byte[]>> partitions = generatePartitions(queuedEvents);
+ List<Long> eventIds =
queuedEvents.stream().map(ExternalEventView::getId).toList();
+ sendEventsToProducer(partitions);
+ markEventsAsSent(eventIds);
}
- private void processEvents(List<ExternalEvent> queuedEvents) throws
IOException {
- for (ExternalEvent event : queuedEvents) {
- MessageV1 message = messageFactory.createMessage(event);
- byte[] byteMessage =
byteBufferConverter.convert(message.toByteBuffer());
- eventProducer.sendEvent(byteMessage);
- event.setStatus(ExternalEventStatus.SENT);
- event.setSentAt(DateUtils.getOffsetDateTimeOfTenant());
- repository.save(event);
+ private void sendEventsToProducer(Map<Long, List<byte[]>> partitions) {
+ eventProducer.sendEvents(partitions);
+ }
+
+ private void markEventsAsSent(List<Long> eventIds) {
+ OffsetDateTime sentAt = DateUtils.getOffsetDateTimeOfTenant();
+
+ // Partitioning dataset to avoid exception: PreparedStatement can have
at most 65,535 parameters
+ List<List<Long>> partitions = Lists.partition(eventIds, 5_000);
+ partitions.forEach(partitionedEventIds -> {
+ measure(() -> {
+ repository.markEventsSent(partitionedEventIds, sentAt);
+ }, timeTaken -> {
+ log.debug("Took {}ms to update {} events",
timeTaken.toMillis(), partitionedEventIds.size());
+ });
+ });
+ }
+
+ private Map<Long, List<byte[]>> generatePartitions(List<ExternalEventView>
queuedEvents) {
+ Map<Long, List<ExternalEventView>> initialPartitions =
queuedEvents.stream().collect(groupingBy(externalEvent -> {
+ Long aggregateRootId = externalEvent.getAggregateRootId();
Review Comment:
Should we consider type of aggregate also like (client,loan,savings etc)?
Will there be a case that different type of aggregates have same aggregate id?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]