From 4540e7c0e7718f1c6636f382078defe7b37f1112 Mon Sep 17 00:00:00 2001 From: Johannes Buechele <johannes@bujo.at> Date: Thu, 24 Aug 2023 15:30:13 +0200 Subject: [PATCH] fixed: send message after db commit --- Frontend/src/services/ApiService.js | 2 +- .../faircommons/api_service/api/WorkApi.java | 4 --- .../listener/WorkRegisteredEventListener.java | 28 +++++++++++++++++++ .../api_service/service/WorkService.java | 21 ++++++-------- .../service/WorkStatusService.java | 2 -- .../strategy/MessageDispatchStrategy.java | 1 + 6 files changed, 38 insertions(+), 20 deletions(-) create mode 100644 faircommons-services/api-service/src/main/java/eu/fairkom/faircommons/api_service/listener/WorkRegisteredEventListener.java diff --git a/Frontend/src/services/ApiService.js b/Frontend/src/services/ApiService.js index 49d9a7e..8c62397 100644 --- a/Frontend/src/services/ApiService.js +++ b/Frontend/src/services/ApiService.js @@ -121,7 +121,7 @@ const uploadWork = async ( try { const keycloakUserId = UserService.getUserId(); const response = await HttpClient.get( - `/files/upload/presigned-url?filename=${filename}&userId=${keycloakUserId}`, + `/works/upload/presigned-url?filename=${filename}&userId=${keycloakUserId}`, ); const presignedUploadUrl = response.data?.url; const prefix = response.data?.prefix; diff --git a/faircommons-services/api-service/src/main/java/eu/fairkom/faircommons/api_service/api/WorkApi.java b/faircommons-services/api-service/src/main/java/eu/fairkom/faircommons/api_service/api/WorkApi.java index c4be0c3..7918af1 100644 --- a/faircommons-services/api-service/src/main/java/eu/fairkom/faircommons/api_service/api/WorkApi.java +++ b/faircommons-services/api-service/src/main/java/eu/fairkom/faircommons/api_service/api/WorkApi.java @@ -1,14 +1,10 @@ package eu.fairkom.faircommons.api_service.api; import eu.fairkom.faircommons.api_service.service.WorkService; -import eu.fairkom.faircommons.common.models.DownloadFileResponse; import eu.fairkom.faircommons.common.models.views.WorkCreateView; import eu.fairkom.faircommons.common.models.views.WorkView; import jakarta.persistence.EntityNotFoundException; import jakarta.validation.Valid; -import org.springframework.core.io.InputStreamResource; -import org.springframework.http.HttpHeaders; -import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import org.springframework.web.servlet.support.ServletUriComponentsBuilder; diff --git a/faircommons-services/api-service/src/main/java/eu/fairkom/faircommons/api_service/listener/WorkRegisteredEventListener.java b/faircommons-services/api-service/src/main/java/eu/fairkom/faircommons/api_service/listener/WorkRegisteredEventListener.java new file mode 100644 index 0000000..45c850d --- /dev/null +++ b/faircommons-services/api-service/src/main/java/eu/fairkom/faircommons/api_service/listener/WorkRegisteredEventListener.java @@ -0,0 +1,28 @@ +package eu.fairkom.faircommons.api_service.listener; + +import eu.fairkom.faircommons.common.rabbitmq.messages.WorkFileMessage; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.amqp.rabbit.core.RabbitTemplate; +import org.springframework.stereotype.Component; +import org.springframework.transaction.event.TransactionPhase; +import org.springframework.transaction.event.TransactionalEventListener; + +import static eu.fairkom.faircommons.common.rabbitmq.RabbitMqConfiguration.HASHING_QUEUE; + +@Component +public class WorkRegisteredEventListener { + private static final Logger logger = LoggerFactory.getLogger(WorkRegisteredEventListener.class); + + private final RabbitTemplate rabbitTemplate; + + public WorkRegisteredEventListener(RabbitTemplate rabbitTemplate) { + this.rabbitTemplate = rabbitTemplate; + } + + @TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT) + public void handleWorkRegisteredEvent(WorkFileMessage message) { + rabbitTemplate.convertAndSend(HASHING_QUEUE, message); + logger.info("Message sent to hashing queue for work ID: {}", message.getWorkId()); + } +} diff --git a/faircommons-services/api-service/src/main/java/eu/fairkom/faircommons/api_service/service/WorkService.java b/faircommons-services/api-service/src/main/java/eu/fairkom/faircommons/api_service/service/WorkService.java index 70640a4..6f4124b 100644 --- a/faircommons-services/api-service/src/main/java/eu/fairkom/faircommons/api_service/service/WorkService.java +++ b/faircommons-services/api-service/src/main/java/eu/fairkom/faircommons/api_service/service/WorkService.java @@ -16,8 +16,7 @@ import eu.fairkom.faircommons.common.rabbitmq.messages.WorkFileMessage; import jakarta.persistence.EntityNotFoundException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.springframework.amqp.rabbit.core.RabbitTemplate; -import org.springframework.core.io.InputStreamResource; +import org.springframework.context.ApplicationEventPublisher; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; @@ -25,27 +24,24 @@ import org.springframework.transaction.annotation.Transactional; import java.util.UUID; -import static eu.fairkom.faircommons.common.rabbitmq.RabbitMqConfiguration.HASHING_QUEUE; - @Service public class WorkService { private static final Logger logger = LoggerFactory.getLogger(WorkService.class); - private final RabbitTemplate rabbitTemplate; private final WorkRepository workRepository; private final WorkMapper workMapper; private final TagMapper tagMapper; private final TagRepository tagRepository; + private final ApplicationEventPublisher eventPublisher; - public WorkService(RabbitTemplate rabbitTemplate, - WorkRepository workRepository, + public WorkService(WorkRepository workRepository, WorkMapper workMapper, TagMapper tagMapper, - TagRepository tagRepository) { - this.rabbitTemplate = rabbitTemplate; + TagRepository tagRepository, ApplicationEventPublisher eventPublisher) { this.workRepository = workRepository; this.workMapper = workMapper; this.tagMapper = tagMapper; this.tagRepository = tagRepository; + this.eventPublisher = eventPublisher; } @Transactional @@ -57,11 +53,10 @@ public class WorkService { var message = WorkFileMessage.builder() .workId(work.getId()) .workFile(workCreateView.getWorkFile()) - .userId(workCreateView.getRegistrar()); - - rabbitTemplate.convertAndSend(HASHING_QUEUE, message.build()); - logger.debug("Message sent to hashing queue for work ID: {}", work.getId()); + .userId(workCreateView.getRegistrar()) + .build(); + eventPublisher.publishEvent(message); return work.getId(); } diff --git a/faircommons-services/api-service/src/main/java/eu/fairkom/faircommons/api_service/service/WorkStatusService.java b/faircommons-services/api-service/src/main/java/eu/fairkom/faircommons/api_service/service/WorkStatusService.java index dd820c9..476ef11 100644 --- a/faircommons-services/api-service/src/main/java/eu/fairkom/faircommons/api_service/service/WorkStatusService.java +++ b/faircommons-services/api-service/src/main/java/eu/fairkom/faircommons/api_service/service/WorkStatusService.java @@ -9,8 +9,6 @@ import org.slf4j.LoggerFactory; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; -import java.util.UUID; - @Service public class WorkStatusService { private static final Logger logger = LoggerFactory.getLogger(WorkStatusService.class); diff --git a/faircommons-services/api-service/src/main/java/eu/fairkom/faircommons/api_service/strategy/MessageDispatchStrategy.java b/faircommons-services/api-service/src/main/java/eu/fairkom/faircommons/api_service/strategy/MessageDispatchStrategy.java index 0439a13..0763965 100644 --- a/faircommons-services/api-service/src/main/java/eu/fairkom/faircommons/api_service/strategy/MessageDispatchStrategy.java +++ b/faircommons-services/api-service/src/main/java/eu/fairkom/faircommons/api_service/strategy/MessageDispatchStrategy.java @@ -5,5 +5,6 @@ import eu.fairkom.faircommons.common.models.entities.WorkStatus; public interface MessageDispatchStrategy { void dispatch(Work work); + WorkStatus.WorkStatusType getType(); } -- GitLab