Merge pull request #51 from SCS-ASSE-FS21-Group1/ExecutorRemovedEventListenerMqttAdapter

Implemented RemovedEventListener...
This commit is contained in:
reynisson 2021-11-14 16:12:10 +01:00 committed by GitHub
commit e3d68b88d9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 68 additions and 1 deletions

View File

@ -1,6 +1,14 @@
package ch.unisg.tapas.auctionhouse.adapter.in.messaging.http; package ch.unisg.tapas.auctionhouse.adapter.in.messaging.http;
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 org.springframework.http.HttpEntity;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
/** /**
@ -10,7 +18,17 @@ import org.springframework.web.bind.annotation.RestController;
public class ExecutorRemovedEventListenerHttpAdapter { public class ExecutorRemovedEventListenerHttpAdapter {
// TODO: add annotations for request method, request URI, etc. // TODO: add annotations for request method, request URI, etc.
public void handleExecutorRemovedEvent(@PathVariable("executorId") String executorId) { @PostMapping(path = "/executors/{taskType}/{executorId}")
public ResponseEntity<String> handleExecutorRemovedEvent(@PathVariable("executorId") String executorId) {
// TODO: implement logic // TODO: implement logic
ExecutorRemovedEvent executorRemovedEvent = new ExecutorRemovedEvent(
new ExecutorRegistry.ExecutorIdentifier(executorId)
);
ExecutorRemovedHandler newExecutorHandler = new ExecutorRemovedHandler();
newExecutorHandler.handleExecutorRemovedEvent(executorRemovedEvent);
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
} }
} }

View File

@ -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;
}
}

View File

@ -16,4 +16,7 @@ public class ExecutorRemovedHandler implements ExecutorRemovedEventHandler {
public boolean handleExecutorRemovedEvent(ExecutorRemovedEvent executorRemovedEvent) { public boolean handleExecutorRemovedEvent(ExecutorRemovedEvent executorRemovedEvent) {
return ExecutorRegistry.getInstance().removeExecutor(executorRemovedEvent.getExecutorId()); return ExecutorRegistry.getInstance().removeExecutor(executorRemovedEvent.getExecutorId());
} }
public void handleNewExecutorEvent(ExecutorRemovedEvent executorRemovedEvent) {
}
} }