Implemented RemovedEventListener... #51

Merged
rahimiankeanu merged 3 commits from ExecutorRemovedEventListenerMqttAdapter into dev 2021-11-14 15:12:10 +00:00
2 changed files with 50 additions and 2 deletions
Showing only changes of commit f652a9ecaf - Show all commits

View File

@ -18,7 +18,8 @@ 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( ExecutorRemovedEvent executorRemovedEvent = new ExecutorRemovedEvent(
@ -28,5 +29,6 @@ public class ExecutorRemovedEventListenerHttpAdapter {
ExecutorRemovedHandler newExecutorHandler = new ExecutorRemovedHandler(); ExecutorRemovedHandler newExecutorHandler = new ExecutorRemovedHandler();
newExecutorHandler.handleExecutorRemovedEvent(executorRemovedEvent); 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;
}
}