From f652a9ecafbbb565a649a41c47c6eb891cdd086b Mon Sep 17 00:00:00 2001 From: rahimiankeanu Date: Fri, 12 Nov 2021 08:51:43 +0100 Subject: [PATCH] MQTT event adapter --- ...ecutorRemovedEventListenerHttpAdapter.java | 6 ++- .../mqtt/ExecutorRemovedEventListener | 46 +++++++++++++++++++ 2 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 tapas-auction-house/src/main/java/ch/unisg/tapas/auctionhouse/adapter/in/messaging/mqtt/ExecutorRemovedEventListener diff --git a/tapas-auction-house/src/main/java/ch/unisg/tapas/auctionhouse/adapter/in/messaging/http/ExecutorRemovedEventListenerHttpAdapter.java b/tapas-auction-house/src/main/java/ch/unisg/tapas/auctionhouse/adapter/in/messaging/http/ExecutorRemovedEventListenerHttpAdapter.java index fcf9b52..58bbb95 100644 --- a/tapas-auction-house/src/main/java/ch/unisg/tapas/auctionhouse/adapter/in/messaging/http/ExecutorRemovedEventListenerHttpAdapter.java +++ b/tapas-auction-house/src/main/java/ch/unisg/tapas/auctionhouse/adapter/in/messaging/http/ExecutorRemovedEventListenerHttpAdapter.java @@ -18,7 +18,8 @@ import org.springframework.web.bind.annotation.RestController; public class ExecutorRemovedEventListenerHttpAdapter { // TODO: add annotations for request method, request URI, etc. - public void handleExecutorRemovedEvent(@PathVariable("executorId") String executorId) { + @PostMapping(path = "/executors/{taskType}/{executorId}") + public ResponseEntity handleExecutorRemovedEvent(@PathVariable("executorId") String executorId) { // TODO: implement logic ExecutorRemovedEvent executorRemovedEvent = new ExecutorRemovedEvent( @@ -27,6 +28,7 @@ public class ExecutorRemovedEventListenerHttpAdapter { ExecutorRemovedHandler newExecutorHandler = new ExecutorRemovedHandler(); newExecutorHandler.handleExecutorRemovedEvent(executorRemovedEvent); - + + return new ResponseEntity<>(HttpStatus.NO_CONTENT); } } diff --git a/tapas-auction-house/src/main/java/ch/unisg/tapas/auctionhouse/adapter/in/messaging/mqtt/ExecutorRemovedEventListener b/tapas-auction-house/src/main/java/ch/unisg/tapas/auctionhouse/adapter/in/messaging/mqtt/ExecutorRemovedEventListener new file mode 100644 index 0000000..087479c --- /dev/null +++ b/tapas-auction-house/src/main/java/ch/unisg/tapas/auctionhouse/adapter/in/messaging/mqtt/ExecutorRemovedEventListener @@ -0,0 +1,46 @@ +package ch.unisg.tapas.auctionhouse.adapter.in.messaging.mqtt; + +import ch.unisg.tapas.auctionhouse.application.handler.ExecutorRemovedHandler; +import ch.unisg.tapas.auctionhouse.application.port.in.ExecutorRemovedEvent; +import ch.unisg.tapas.auctionhouse.domain.Auction; +import ch.unisg.tapas.auctionhouse.domain.ExecutorRegistry; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.eclipse.paho.client.mqttv3.MqttMessage; + +/** + * Listener that handles events when an executor was removed to this TAPAS application. + * + * This class is only provided as an example to help you bootstrap the project. + */ +public class ExecutorRemovedEventListenerMqttAdapter extends AuctionEventMqttListener { + private static final Logger LOGGER = LogManager.getLogger(ExecutorRemovedEventListenerMqttAdapter.class); + + @Override + public boolean handleEvent(MqttMessage message) { + String payload = new String(message.getPayload()); + + try { + // Note: this messge representation is provided only as an example. You should use a + // representation that makes sense in the context of your application. + JsonNode data = new ObjectMapper().readTree(payload); + + String executorId = data.get("executorId").asText(); + + ExecutorRemovedEvent executorRemovedEvent = new ExecutorRemovedEvent( + new ExecutorRegistry.ExecutorIdentifier(executorId) + ); + + ExecutorRemovedHandler newExecutorHandler = new ExecutorRemovedHandler(); + newExecutorHandler.handleNewExecutorEvent(executorRemovedEvent); + } catch (JsonProcessingException | NullPointerException e) { + LOGGER.error(e.getMessage(), e); + return false; + } + + return true; + } +}