Merge branch 'auction_house_adapt_to_new_executor_event' into dev
# Conflicts: # tapas-auction-house/src/main/java/ch/unisg/tapas/auctionhouse/adapter/in/messaging/http/ExecutorRemovedEventListenerHttpAdapter.java
This commit is contained in:
commit
ccb800b6af
|
@ -4,9 +4,11 @@ import ch.unisg.tapas.auctionhouse.adapter.common.clients.TapasMqttClient;
|
||||||
import ch.unisg.tapas.auctionhouse.adapter.in.messaging.mqtt.AuctionEventsMqttDispatcher;
|
import ch.unisg.tapas.auctionhouse.adapter.in.messaging.mqtt.AuctionEventsMqttDispatcher;
|
||||||
import ch.unisg.tapas.auctionhouse.adapter.common.clients.WebSubSubscriber;
|
import ch.unisg.tapas.auctionhouse.adapter.common.clients.WebSubSubscriber;
|
||||||
import ch.unisg.tapas.common.AuctionHouseResourceDirectory;
|
import ch.unisg.tapas.common.AuctionHouseResourceDirectory;
|
||||||
|
import ch.unisg.tapas.common.ConfigProperties;
|
||||||
import org.apache.logging.log4j.LogManager;
|
import org.apache.logging.log4j.LogManager;
|
||||||
import org.apache.logging.log4j.Logger;
|
import org.apache.logging.log4j.Logger;
|
||||||
import org.eclipse.paho.client.mqttv3.MqttException;
|
import org.eclipse.paho.client.mqttv3.MqttException;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
|
||||||
|
@ -20,6 +22,9 @@ import java.util.List;
|
||||||
public class TapasAuctionHouseApplication {
|
public class TapasAuctionHouseApplication {
|
||||||
private static final Logger LOGGER = LogManager.getLogger(TapasAuctionHouseApplication.class);
|
private static final Logger LOGGER = LogManager.getLogger(TapasAuctionHouseApplication.class);
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private ConfigProperties config;
|
||||||
|
|
||||||
public static String RESOURCE_DIRECTORY = "https://api.interactions.ics.unisg.ch/auction-houses/";
|
public static String RESOURCE_DIRECTORY = "https://api.interactions.ics.unisg.ch/auction-houses/";
|
||||||
public static String MQTT_BROKER = "tcp://localhost:1883";
|
public static String MQTT_BROKER = "tcp://localhost:1883";
|
||||||
|
|
||||||
|
|
|
@ -71,7 +71,7 @@ public class TapasMqttClient {
|
||||||
public void publishMessage(String topic, String payload) throws MqttException {
|
public void publishMessage(String topic, String payload) throws MqttException {
|
||||||
mqttClient = new org.eclipse.paho.client.mqttv3.MqttClient(brokerAddress, mqttClientId, new MemoryPersistence());
|
mqttClient = new org.eclipse.paho.client.mqttv3.MqttClient(brokerAddress, mqttClientId, new MemoryPersistence());
|
||||||
mqttClient.connect();
|
mqttClient.connect();
|
||||||
|
|
||||||
MqttMessage message = new MqttMessage(payload.getBytes(StandardCharsets.UTF_8));
|
MqttMessage message = new MqttMessage(payload.getBytes(StandardCharsets.UTF_8));
|
||||||
mqttClient.publish(topic, message);
|
mqttClient.publish(topic, message);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,34 +0,0 @@
|
||||||
package ch.unisg.tapas.auctionhouse.adapter.in.messaging.http;
|
|
||||||
|
|
||||||
import ch.unisg.tapas.auctionhouse.application.handler.ExecutorAddedHandler;
|
|
||||||
import ch.unisg.tapas.auctionhouse.application.port.in.ExecutorAddedEvent;
|
|
||||||
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.PostMapping;
|
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Template for receiving an executor added event via HTTP
|
|
||||||
*/
|
|
||||||
@RestController
|
|
||||||
public class ExecutorAddedEventListenerHttpAdapter {
|
|
||||||
|
|
||||||
@PostMapping(path = "/executors/{taskType}/{executorId}")
|
|
||||||
public ResponseEntity<String> handleExecutorAddedEvent(@PathVariable("taskType") String taskType,
|
|
||||||
@PathVariable("executorId") String executorId) {
|
|
||||||
|
|
||||||
ExecutorAddedEvent executorAddedEvent = new ExecutorAddedEvent(
|
|
||||||
new ExecutorRegistry.ExecutorIdentifier(executorId),
|
|
||||||
new Auction.AuctionedTaskType(taskType)
|
|
||||||
);
|
|
||||||
|
|
||||||
ExecutorAddedHandler newExecutorHandler = new ExecutorAddedHandler();
|
|
||||||
newExecutorHandler.handleNewExecutorEvent(executorAddedEvent);
|
|
||||||
|
|
||||||
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,34 +0,0 @@
|
||||||
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.PostMapping;
|
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Template for handling an executor removed event received via an HTTP request
|
|
||||||
*/
|
|
||||||
@RestController
|
|
||||||
public class ExecutorRemovedEventListenerHttpAdapter {
|
|
||||||
|
|
||||||
// TODO: add annotations for request method, request URI, etc.
|
|
||||||
@PostMapping(path = "/executors/{taskType}/{executorId}")
|
|
||||||
public ResponseEntity<String> handleExecutorRemovedEvent(@PathVariable("executorId") String executorId) {
|
|
||||||
// TODO: implement logic
|
|
||||||
|
|
||||||
ExecutorRemovedEvent executorRemovedEvent = new ExecutorRemovedEvent(
|
|
||||||
new ExecutorRegistry.ExecutorIdentifier(executorId)
|
|
||||||
);
|
|
||||||
|
|
||||||
ExecutorRemovedHandler newExecutorHandler = new ExecutorRemovedHandler();
|
|
||||||
newExecutorHandler.handleExecutorRemovedEvent(executorRemovedEvent);
|
|
||||||
|
|
||||||
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -11,6 +11,8 @@ import org.apache.logging.log4j.LogManager;
|
||||||
import org.apache.logging.log4j.Logger;
|
import org.apache.logging.log4j.Logger;
|
||||||
import org.eclipse.paho.client.mqttv3.MqttMessage;
|
import org.eclipse.paho.client.mqttv3.MqttMessage;
|
||||||
|
|
||||||
|
import java.net.URI;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Listener that handles events when an executor was added to this TAPAS application.
|
* Listener that handles events when an executor was added to this TAPAS application.
|
||||||
*
|
*
|
||||||
|
@ -24,16 +26,16 @@ public class ExecutorAddedEventListenerMqttAdapter extends AuctionEventMqttListe
|
||||||
String payload = new String(message.getPayload());
|
String payload = new String(message.getPayload());
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Note: this messge representation is provided only as an example. You should use a
|
// Note: this message representation is provided only as an example. You should use a
|
||||||
// representation that makes sense in the context of your application.
|
// representation that makes sense in the context of your application.
|
||||||
JsonNode data = new ObjectMapper().readTree(payload);
|
JsonNode data = new ObjectMapper().readTree(payload);
|
||||||
|
|
||||||
String taskType = data.get("taskType").asText();
|
String executorUri = data.get("executorUri").asText();
|
||||||
String executorId = data.get("executorId").asText();
|
String executorTaskType = data.get("executorTaskType").asText();
|
||||||
|
|
||||||
ExecutorAddedEvent executorAddedEvent = new ExecutorAddedEvent(
|
ExecutorAddedEvent executorAddedEvent = new ExecutorAddedEvent(
|
||||||
new ExecutorRegistry.ExecutorIdentifier(executorId),
|
new ExecutorRegistry.ExecutorUri(URI.create(executorUri)),
|
||||||
new Auction.AuctionedTaskType(taskType)
|
new Auction.AuctionedTaskType(executorTaskType)
|
||||||
);
|
);
|
||||||
|
|
||||||
ExecutorAddedHandler newExecutorHandler = new ExecutorAddedHandler();
|
ExecutorAddedHandler newExecutorHandler = new ExecutorAddedHandler();
|
||||||
|
|
|
@ -11,6 +11,6 @@ public class ExecutorAddedHandler implements ExecutorAddedEventHandler {
|
||||||
@Override
|
@Override
|
||||||
public boolean handleNewExecutorEvent(ExecutorAddedEvent executorAddedEvent) {
|
public boolean handleNewExecutorEvent(ExecutorAddedEvent executorAddedEvent) {
|
||||||
return ExecutorRegistry.getInstance().addExecutor(executorAddedEvent.getTaskType(),
|
return ExecutorRegistry.getInstance().addExecutor(executorAddedEvent.getTaskType(),
|
||||||
executorAddedEvent.getExecutorId());
|
executorAddedEvent.getExecutorUri());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,7 +14,7 @@ public class ExecutorRemovedHandler implements ExecutorRemovedEventHandler {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean handleExecutorRemovedEvent(ExecutorRemovedEvent executorRemovedEvent) {
|
public boolean handleExecutorRemovedEvent(ExecutorRemovedEvent executorRemovedEvent) {
|
||||||
return ExecutorRegistry.getInstance().removeExecutor(executorRemovedEvent.getExecutorId());
|
return ExecutorRegistry.getInstance().removeExecutor(executorRemovedEvent.getExecutorUri());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void handleNewExecutorEvent(ExecutorRemovedEvent executorRemovedEvent) {
|
public void handleNewExecutorEvent(ExecutorRemovedEvent executorRemovedEvent) {
|
||||||
|
|
|
@ -1,7 +1,8 @@
|
||||||
package ch.unisg.tapas.auctionhouse.application.port.in;
|
package ch.unisg.tapas.auctionhouse.application.port.in;
|
||||||
|
|
||||||
import ch.unisg.tapas.auctionhouse.domain.Auction.AuctionedTaskType;
|
import ch.unisg.tapas.auctionhouse.domain.Auction.AuctionedTaskType;
|
||||||
import ch.unisg.tapas.auctionhouse.domain.ExecutorRegistry.ExecutorIdentifier;
|
import ch.unisg.tapas.auctionhouse.domain.ExecutorRegistry;
|
||||||
|
import ch.unisg.tapas.auctionhouse.domain.ExecutorRegistry.ExecutorUri;
|
||||||
import ch.unisg.tapas.common.SelfValidating;
|
import ch.unisg.tapas.common.SelfValidating;
|
||||||
import lombok.Value;
|
import lombok.Value;
|
||||||
|
|
||||||
|
@ -13,7 +14,7 @@ import javax.validation.constraints.NotNull;
|
||||||
@Value
|
@Value
|
||||||
public class ExecutorAddedEvent extends SelfValidating<ExecutorAddedEvent> {
|
public class ExecutorAddedEvent extends SelfValidating<ExecutorAddedEvent> {
|
||||||
@NotNull
|
@NotNull
|
||||||
private final ExecutorIdentifier executorId;
|
private final ExecutorRegistry.ExecutorUri executorUri;
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
private final AuctionedTaskType taskType;
|
private final AuctionedTaskType taskType;
|
||||||
|
@ -21,10 +22,10 @@ public class ExecutorAddedEvent extends SelfValidating<ExecutorAddedEvent> {
|
||||||
/**
|
/**
|
||||||
* Constructs an executor added event.
|
* Constructs an executor added event.
|
||||||
*
|
*
|
||||||
* @param executorId the identifier of the executor that was added to this TAPAS application
|
* @param executorUri the identifier of the executor that was added to this TAPAS application
|
||||||
*/
|
*/
|
||||||
public ExecutorAddedEvent(ExecutorIdentifier executorId, AuctionedTaskType taskType) {
|
public ExecutorAddedEvent(ExecutorUri executorUri, AuctionedTaskType taskType) {
|
||||||
this.executorId = executorId;
|
this.executorUri = executorUri;
|
||||||
this.taskType = taskType;
|
this.taskType = taskType;
|
||||||
|
|
||||||
this.validateSelf();
|
this.validateSelf();
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package ch.unisg.tapas.auctionhouse.application.port.in;
|
package ch.unisg.tapas.auctionhouse.application.port.in;
|
||||||
|
|
||||||
import ch.unisg.tapas.auctionhouse.domain.ExecutorRegistry.ExecutorIdentifier;
|
import ch.unisg.tapas.auctionhouse.domain.ExecutorRegistry;
|
||||||
|
import ch.unisg.tapas.auctionhouse.domain.ExecutorRegistry.ExecutorUri;
|
||||||
import ch.unisg.tapas.common.SelfValidating;
|
import ch.unisg.tapas.common.SelfValidating;
|
||||||
import lombok.Value;
|
import lombok.Value;
|
||||||
|
|
||||||
|
@ -12,15 +13,15 @@ import javax.validation.constraints.NotNull;
|
||||||
@Value
|
@Value
|
||||||
public class ExecutorRemovedEvent extends SelfValidating<ExecutorRemovedEvent> {
|
public class ExecutorRemovedEvent extends SelfValidating<ExecutorRemovedEvent> {
|
||||||
@NotNull
|
@NotNull
|
||||||
private final ExecutorIdentifier executorId;
|
private final ExecutorUri executorUri;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructs an executor removed event.
|
* Constructs an executor removed event.
|
||||||
*
|
*
|
||||||
* @param executorId the identifier of the executor that was removed from this TAPAS application
|
* @param executorUri
|
||||||
*/
|
*/
|
||||||
public ExecutorRemovedEvent(ExecutorIdentifier executorId) {
|
public ExecutorRemovedEvent(ExecutorUri executorUri) {
|
||||||
this.executorId = executorId;
|
this.executorUri = executorUri;
|
||||||
this.validateSelf();
|
this.validateSelf();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,7 @@ package ch.unisg.tapas.auctionhouse.domain;
|
||||||
|
|
||||||
import lombok.Value;
|
import lombok.Value;
|
||||||
|
|
||||||
|
import java.net.URI;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -13,7 +14,7 @@ import java.util.*;
|
||||||
public class ExecutorRegistry {
|
public class ExecutorRegistry {
|
||||||
private static ExecutorRegistry registry;
|
private static ExecutorRegistry registry;
|
||||||
|
|
||||||
private final Map<Auction.AuctionedTaskType, Set<ExecutorIdentifier>> executors;
|
private final Map<Auction.AuctionedTaskType, Set<ExecutorUri>> executors;
|
||||||
|
|
||||||
private ExecutorRegistry() {
|
private ExecutorRegistry() {
|
||||||
this.executors = new Hashtable<>();
|
this.executors = new Hashtable<>();
|
||||||
|
@ -31,14 +32,14 @@ public class ExecutorRegistry {
|
||||||
* Adds an executor to the registry for a given task type.
|
* Adds an executor to the registry for a given task type.
|
||||||
*
|
*
|
||||||
* @param taskType the type of the task
|
* @param taskType the type of the task
|
||||||
* @param executorIdentifier the identifier of the executor (can be any string)
|
* @param executorUri the executor's URI
|
||||||
* @return true unless a runtime exception occurs
|
* @return true unless a runtime exception occurs
|
||||||
*/
|
*/
|
||||||
public boolean addExecutor(Auction.AuctionedTaskType taskType, ExecutorIdentifier executorIdentifier) {
|
public boolean addExecutor(Auction.AuctionedTaskType taskType, ExecutorUri executorUri) {
|
||||||
Set<ExecutorIdentifier> taskTypeExecs = executors.getOrDefault(taskType,
|
Set<ExecutorUri> taskTypeExecs = executors.getOrDefault(taskType,
|
||||||
Collections.synchronizedSet(new HashSet<>()));
|
Collections.synchronizedSet(new HashSet<>()));
|
||||||
|
|
||||||
taskTypeExecs.add(executorIdentifier);
|
taskTypeExecs.add(executorUri);
|
||||||
executors.put(taskType, taskTypeExecs);
|
executors.put(taskType, taskTypeExecs);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
@ -47,17 +48,17 @@ public class ExecutorRegistry {
|
||||||
/**
|
/**
|
||||||
* Removes an executor from the registry. The executor is disassociated from all known task types.
|
* Removes an executor from the registry. The executor is disassociated from all known task types.
|
||||||
*
|
*
|
||||||
* @param executorIdentifier the identifier of the executor (can be any string)
|
* @param executorUri the executor's URI
|
||||||
* @return true unless a runtime exception occurs
|
* @return true unless a runtime exception occurs
|
||||||
*/
|
*/
|
||||||
public boolean removeExecutor(ExecutorIdentifier executorIdentifier) {
|
public boolean removeExecutor(ExecutorUri executorUri) {
|
||||||
Iterator<Auction.AuctionedTaskType> iterator = executors.keySet().iterator();
|
Iterator<Auction.AuctionedTaskType> iterator = executors.keySet().iterator();
|
||||||
|
|
||||||
while (iterator.hasNext()) {
|
while (iterator.hasNext()) {
|
||||||
Auction.AuctionedTaskType taskType = iterator.next();
|
Auction.AuctionedTaskType taskType = iterator.next();
|
||||||
Set<ExecutorIdentifier> set = executors.get(taskType);
|
Set<ExecutorUri> set = executors.get(taskType);
|
||||||
|
|
||||||
set.remove(executorIdentifier);
|
set.remove(executorUri);
|
||||||
|
|
||||||
if (set.isEmpty()) {
|
if (set.isEmpty()) {
|
||||||
iterator.remove();
|
iterator.remove();
|
||||||
|
@ -80,7 +81,7 @@ public class ExecutorRegistry {
|
||||||
|
|
||||||
// Value Object for the executor identifier
|
// Value Object for the executor identifier
|
||||||
@Value
|
@Value
|
||||||
public static class ExecutorIdentifier {
|
public static class ExecutorUri {
|
||||||
String value;
|
URI value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user