From 396f24e0076875933bdab96fa4c34fd7b055f552 Mon Sep 17 00:00:00 2001 From: reynisson Date: Sun, 14 Nov 2021 16:34:45 +0100 Subject: [PATCH] Implemented the executor removed event over mqtt --- .../PublishExecutorRemovedEventAdapter.java | 41 +++++++++++++++++++ .../port/out/ExecutorRemovedEventPort.java | 7 ++++ ...RemoveExecutorFromExecutorPoolService.java | 19 ++++++++- .../domain/ExecutorRemovedEvent.java | 11 +++++ 4 files changed, 76 insertions(+), 2 deletions(-) create mode 100644 executor-pool/src/main/java/ch/unisg/executorpool/adapter/out/messaging/PublishExecutorRemovedEventAdapter.java create mode 100644 executor-pool/src/main/java/ch/unisg/executorpool/application/port/out/ExecutorRemovedEventPort.java create mode 100644 executor-pool/src/main/java/ch/unisg/executorpool/domain/ExecutorRemovedEvent.java diff --git a/executor-pool/src/main/java/ch/unisg/executorpool/adapter/out/messaging/PublishExecutorRemovedEventAdapter.java b/executor-pool/src/main/java/ch/unisg/executorpool/adapter/out/messaging/PublishExecutorRemovedEventAdapter.java new file mode 100644 index 0000000..aa01165 --- /dev/null +++ b/executor-pool/src/main/java/ch/unisg/executorpool/adapter/out/messaging/PublishExecutorRemovedEventAdapter.java @@ -0,0 +1,41 @@ +package ch.unisg.executorpool.adapter.out.messaging; + +import ch.unisg.executorpool.adapter.common.clients.TapasMqttClient; +import ch.unisg.executorpool.adapter.common.formats.ExecutorJsonRepresentation; +import ch.unisg.executorpool.application.port.out.ExecutorRemovedEventPort; +import ch.unisg.executorpool.domain.ExecutorAddedEvent; +import ch.unisg.executorpool.domain.ExecutorRemovedEvent; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.eclipse.paho.client.mqttv3.MqttException; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Primary; +import org.springframework.core.env.Environment; +import org.springframework.stereotype.Component; + +@Component +@Primary +public class PublishExecutorRemovedEventAdapter implements ExecutorRemovedEventPort { + + private static final Logger LOGGER = LogManager.getLogger(PublishExecutorAddedEventAdapter.class); + + // TODO Can't autowire. Find fix + /* + @Autowired + private ConfigProperties config; + */ + + @Autowired + private Environment environment; + + @Override + public void publishExecutorRemovedEvent(ExecutorRemovedEvent event){ + try{ + var mqttClient = TapasMqttClient.getInstance(environment.getProperty("mqtt.broker.uri")); + mqttClient.publishMessage("ch/unisg/tapas/executors/removed", ExecutorJsonRepresentation.serialize(event.getExecutorClass())); + } + catch (MqttException e){ + LOGGER.error(e.getMessage(), e); + } + } +} diff --git a/executor-pool/src/main/java/ch/unisg/executorpool/application/port/out/ExecutorRemovedEventPort.java b/executor-pool/src/main/java/ch/unisg/executorpool/application/port/out/ExecutorRemovedEventPort.java new file mode 100644 index 0000000..b905858 --- /dev/null +++ b/executor-pool/src/main/java/ch/unisg/executorpool/application/port/out/ExecutorRemovedEventPort.java @@ -0,0 +1,7 @@ +package ch.unisg.executorpool.application.port.out; + +import ch.unisg.executorpool.domain.ExecutorRemovedEvent; + +public interface ExecutorRemovedEventPort { + void publishExecutorRemovedEvent(ExecutorRemovedEvent event); +} diff --git a/executor-pool/src/main/java/ch/unisg/executorpool/application/service/RemoveExecutorFromExecutorPoolService.java b/executor-pool/src/main/java/ch/unisg/executorpool/application/service/RemoveExecutorFromExecutorPoolService.java index a606f57..4d2457d 100644 --- a/executor-pool/src/main/java/ch/unisg/executorpool/application/service/RemoveExecutorFromExecutorPoolService.java +++ b/executor-pool/src/main/java/ch/unisg/executorpool/application/service/RemoveExecutorFromExecutorPoolService.java @@ -2,21 +2,36 @@ package ch.unisg.executorpool.application.service; import ch.unisg.executorpool.application.port.in.RemoveExecutorFromExecutorPoolCommand; import ch.unisg.executorpool.application.port.in.RemoveExecutorFromExecutorPoolUseCase; +import ch.unisg.executorpool.application.port.out.ExecutorRemovedEventPort; import ch.unisg.executorpool.domain.ExecutorClass; import ch.unisg.executorpool.domain.ExecutorPool; +import ch.unisg.executorpool.domain.ExecutorRemovedEvent; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Component; import javax.transaction.Transactional; import java.util.Optional; -@RequiredArgsConstructor @Component @Transactional public class RemoveExecutorFromExecutorPoolService implements RemoveExecutorFromExecutorPoolUseCase { + + private final ExecutorRemovedEventPort executorRemovedEventPort; + + public RemoveExecutorFromExecutorPoolService(ExecutorRemovedEventPort executorRemovedEventPort){ + this.executorRemovedEventPort = executorRemovedEventPort; + } + @Override public Optional removeExecutorFromExecutorPool(RemoveExecutorFromExecutorPoolCommand command){ ExecutorPool executorPool = ExecutorPool.getExecutorPool(); - return executorPool.removeExecutorByIpAndPort(command.getExecutorUri()); + var removedExecutor = executorPool.removeExecutorByIpAndPort(command.getExecutorUri()); + + if(removedExecutor.isPresent()){ + var executorRemovedEvent = new ExecutorRemovedEvent(removedExecutor.get()); + executorRemovedEventPort.publishExecutorRemovedEvent(executorRemovedEvent); + } + + return removedExecutor; } } diff --git a/executor-pool/src/main/java/ch/unisg/executorpool/domain/ExecutorRemovedEvent.java b/executor-pool/src/main/java/ch/unisg/executorpool/domain/ExecutorRemovedEvent.java new file mode 100644 index 0000000..a038928 --- /dev/null +++ b/executor-pool/src/main/java/ch/unisg/executorpool/domain/ExecutorRemovedEvent.java @@ -0,0 +1,11 @@ +package ch.unisg.executorpool.domain; + +import ch.unisg.executorpool.domain.ExecutorClass; +import lombok.Getter; + +public class ExecutorRemovedEvent { + @Getter + private ExecutorClass executorClass; + + public ExecutorRemovedEvent(ExecutorClass executorClass) { this.executorClass = executorClass; } +}