added valueObjects & CustomErrors

This commit is contained in:
Marcel 2021-10-17 10:33:26 +02:00
parent a61f111879
commit 46969deb0f
34 changed files with 214 additions and 103 deletions

View File

@ -1,12 +1,8 @@
package ch.unisg.assignment.assignment.adapter.in.web; package ch.unisg.assignment.assignment.adapter.in.web;
import javax.validation.ConstraintViolationException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.server.ResponseStatusException;
import ch.unisg.assignment.assignment.application.port.in.ApplyForTaskCommand; import ch.unisg.assignment.assignment.application.port.in.ApplyForTaskCommand;
import ch.unisg.assignment.assignment.application.port.in.ApplyForTaskUseCase; import ch.unisg.assignment.assignment.application.port.in.ApplyForTaskUseCase;
@ -23,14 +19,11 @@ public class ApplyForTaskController {
@PostMapping(path = "/task/apply", consumes = {"application/json"}) @PostMapping(path = "/task/apply", consumes = {"application/json"})
public Task applyForTask(@RequestBody ExecutorInfo executorInfo) { public Task applyForTask(@RequestBody ExecutorInfo executorInfo) {
try {
ApplyForTaskCommand command = new ApplyForTaskCommand(executorInfo.getExecutorType(),
executorInfo.getIp(), executorInfo.getPort());
return applyForTaskUseCase.applyForTask(command); ApplyForTaskCommand command = new ApplyForTaskCommand(executorInfo.getExecutorType(),
executorInfo.getIp(), executorInfo.getPort());
return applyForTaskUseCase.applyForTask(command);
} catch (ConstraintViolationException e) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, e.getMessage());
}
} }
} }

View File

@ -1,13 +1,10 @@
package ch.unisg.assignment.assignment.adapter.in.web; package ch.unisg.assignment.assignment.adapter.in.web;
import javax.validation.ConstraintViolationException;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.server.ResponseStatusException;
import ch.unisg.assignment.assignment.application.port.in.NewTaskCommand; import ch.unisg.assignment.assignment.application.port.in.NewTaskCommand;
import ch.unisg.assignment.assignment.application.port.in.NewTaskUseCase; import ch.unisg.assignment.assignment.application.port.in.NewTaskUseCase;
@ -22,11 +19,9 @@ public class NewTaskController {
} }
@PostMapping(path = "/task", consumes = {"application/json"}) @PostMapping(path = "/task", consumes = {"application/json"})
public ResponseEntity<Void> addNewTaskTaskToTaskList(@RequestBody Task task) { public ResponseEntity<Void> newTaskController(@RequestBody Task task) {
try {
NewTaskCommand command = new NewTaskCommand( NewTaskCommand command = new NewTaskCommand(task.getTaskID(), task.getTaskType());
task.getTaskID(), task.getTaskType()
);
boolean success = newTaskUseCase.addNewTaskToQueue(command); boolean success = newTaskUseCase.addNewTaskToQueue(command);
@ -34,8 +29,6 @@ public class NewTaskController {
return new ResponseEntity<>(HttpStatus.CREATED); return new ResponseEntity<>(HttpStatus.CREATED);
} }
return new ResponseEntity<>(HttpStatus.CONFLICT); return new ResponseEntity<>(HttpStatus.CONFLICT);
} catch (ConstraintViolationException e) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, e.getMessage());
}
} }
} }

View File

@ -0,0 +1,31 @@
package ch.unisg.assignment.assignment.adapter.in.web;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import ch.unisg.assignment.common.exception.ErrorResponse;
import ch.unisg.assignment.common.exception.InvalidIP4Exception;
import ch.unisg.assignment.common.exception.PortOutOfRangeException;
@ControllerAdvice
public class WebControllerExceptionHandler {
@ExceptionHandler(PortOutOfRangeException.class)
public ResponseEntity<ErrorResponse> handleException(PortOutOfRangeException e){
ErrorResponse error = new ErrorResponse(HttpStatus.BAD_REQUEST, e.getLocalizedMessage());
return new ResponseEntity<>(error, error.getHttpStatus());
}
@ExceptionHandler(InvalidIP4Exception.class)
public ResponseEntity<ErrorResponse> handleException(InvalidIP4Exception e){
ErrorResponse error = new ErrorResponse(HttpStatus.BAD_REQUEST, e.getLocalizedMessage());
return new ResponseEntity<>(error, error.getHttpStatus());
}
}

View File

@ -10,7 +10,7 @@ import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import ch.unisg.assignment.assignment.application.port.out.NewTaskEventPort; import ch.unisg.assignment.assignment.application.port.out.NewTaskEventPort;
import ch.unisg.assignment.assignment.domain.NewTaskEvent; import ch.unisg.assignment.assignment.domain.event.NewTaskEvent;
@Component @Component
@Primary @Primary
@ -23,7 +23,7 @@ public class PublishNewTaskEventAdapter implements NewTaskEventPort {
HttpClient client = HttpClient.newHttpClient(); HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder() HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(server + "/newtask/" + event.taskType)) .uri(URI.create(server + "/newtask/" + event.taskType.getValue()))
.GET() .GET()
.build(); .build();

View File

@ -11,7 +11,7 @@ import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import ch.unisg.assignment.assignment.application.port.out.TaskAssignedEventPort; import ch.unisg.assignment.assignment.application.port.out.TaskAssignedEventPort;
import ch.unisg.assignment.assignment.domain.TaskAssignedEvent; import ch.unisg.assignment.assignment.domain.event.TaskAssignedEvent;
@Component @Component
@Primary @Primary

View File

@ -11,7 +11,7 @@ import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import ch.unisg.assignment.assignment.application.port.out.TaskCompletedEventPort; import ch.unisg.assignment.assignment.application.port.out.TaskCompletedEventPort;
import ch.unisg.assignment.assignment.domain.TaskCompletedEvent; import ch.unisg.assignment.assignment.domain.event.TaskCompletedEvent;
@Component @Component
@Primary @Primary

View File

@ -2,6 +2,9 @@ package ch.unisg.assignment.assignment.application.port.in;
import javax.validation.constraints.NotNull; import javax.validation.constraints.NotNull;
import ch.unisg.assignment.assignment.domain.valueobject.ExecutorType;
import ch.unisg.assignment.assignment.domain.valueobject.IP4Adress;
import ch.unisg.assignment.assignment.domain.valueobject.Port;
import ch.unisg.assignment.common.SelfValidating; import ch.unisg.assignment.common.SelfValidating;
import lombok.EqualsAndHashCode; import lombok.EqualsAndHashCode;
import lombok.Value; import lombok.Value;
@ -11,16 +14,16 @@ import lombok.Value;
public class ApplyForTaskCommand extends SelfValidating<ApplyForTaskCommand>{ public class ApplyForTaskCommand extends SelfValidating<ApplyForTaskCommand>{
@NotNull @NotNull
private final String taskType; private final ExecutorType taskType;
@NotNull @NotNull
private final String executorIP; private final IP4Adress executorIP;
@NotNull @NotNull
private final int executorPort; private final Port executorPort;
public ApplyForTaskCommand(String taskType, String executorIP, int executorPort) { public ApplyForTaskCommand(ExecutorType taskType, IP4Adress executorIP, Port executorPort) {
this.taskType = taskType; this.taskType = taskType;
this.executorIP = executorIP; this.executorIP = executorIP;
this.executorPort = executorPort; this.executorPort = executorPort;

View File

@ -2,20 +2,22 @@ package ch.unisg.assignment.assignment.application.port.in;
import javax.validation.constraints.NotNull; import javax.validation.constraints.NotNull;
import ch.unisg.assignment.assignment.domain.valueobject.ExecutorType;
import ch.unisg.assignment.common.SelfValidating; import ch.unisg.assignment.common.SelfValidating;
import lombok.EqualsAndHashCode;
import lombok.Value; import lombok.Value;
@Value @Value
@EqualsAndHashCode(callSuper=false)
public class NewTaskCommand extends SelfValidating<NewTaskCommand> { public class NewTaskCommand extends SelfValidating<NewTaskCommand> {
@NotNull @NotNull
private final String taskID; private final String taskID;
@NotNull @NotNull
private final String taskType; private final ExecutorType taskType;
public NewTaskCommand(String taskID, String taskType) { public NewTaskCommand(String taskID, ExecutorType taskType) {
this.taskID = taskID; this.taskID = taskID;
this.taskType = taskType; this.taskType = taskType;
this.validateSelf(); this.validateSelf();

View File

@ -2,6 +2,7 @@ package ch.unisg.assignment.assignment.application.port.in;
import javax.validation.constraints.NotNull; import javax.validation.constraints.NotNull;
import ch.unisg.assignment.assignment.domain.valueobject.ExecutorType;
import ch.unisg.assignment.common.SelfValidating; import ch.unisg.assignment.common.SelfValidating;
import lombok.EqualsAndHashCode; import lombok.EqualsAndHashCode;
import lombok.Value; import lombok.Value;
@ -14,7 +15,7 @@ public class TaskCompletedCommand extends SelfValidating<TaskCompletedCommand>{
private final String taskID; private final String taskID;
@NotNull @NotNull
private final String taskType; private final ExecutorType taskType;
@NotNull @NotNull
private final String taskStatus; private final String taskStatus;
@ -22,7 +23,7 @@ public class TaskCompletedCommand extends SelfValidating<TaskCompletedCommand>{
@NotNull @NotNull
private final String taskResult; private final String taskResult;
public TaskCompletedCommand(String taskID, String taskType, String taskStatus, String taskResult) { public TaskCompletedCommand(String taskID, ExecutorType taskType, String taskStatus, String taskResult) {
this.taskID = taskID; this.taskID = taskID;
this.taskType = taskType; this.taskType = taskType;
this.taskStatus = taskStatus; this.taskStatus = taskStatus;

View File

@ -1,6 +1,6 @@
package ch.unisg.assignment.assignment.application.port.out; package ch.unisg.assignment.assignment.application.port.out;
import ch.unisg.assignment.assignment.domain.NewTaskEvent; import ch.unisg.assignment.assignment.domain.event.NewTaskEvent;
public interface NewTaskEventPort { public interface NewTaskEventPort {
void publishNewTaskEvent(NewTaskEvent event); void publishNewTaskEvent(NewTaskEvent event);

View File

@ -1,6 +1,6 @@
package ch.unisg.assignment.assignment.application.port.out; package ch.unisg.assignment.assignment.application.port.out;
import ch.unisg.assignment.assignment.domain.TaskAssignedEvent; import ch.unisg.assignment.assignment.domain.event.TaskAssignedEvent;
public interface TaskAssignedEventPort { public interface TaskAssignedEventPort {
void publishTaskAssignedEvent(TaskAssignedEvent taskAssignedEvent); void publishTaskAssignedEvent(TaskAssignedEvent taskAssignedEvent);

View File

@ -1,6 +1,6 @@
package ch.unisg.assignment.assignment.application.port.out; package ch.unisg.assignment.assignment.application.port.out;
import ch.unisg.assignment.assignment.domain.TaskCompletedEvent; import ch.unisg.assignment.assignment.domain.event.TaskCompletedEvent;
public interface TaskCompletedEventPort { public interface TaskCompletedEventPort {
void publishTaskCompleted(TaskCompletedEvent event); void publishTaskCompleted(TaskCompletedEvent event);

View File

@ -9,7 +9,7 @@ import ch.unisg.assignment.assignment.application.port.in.ApplyForTaskUseCase;
import ch.unisg.assignment.assignment.application.port.out.TaskAssignedEventPort; import ch.unisg.assignment.assignment.application.port.out.TaskAssignedEventPort;
import ch.unisg.assignment.assignment.domain.Roster; import ch.unisg.assignment.assignment.domain.Roster;
import ch.unisg.assignment.assignment.domain.Task; import ch.unisg.assignment.assignment.domain.Task;
import ch.unisg.assignment.assignment.domain.TaskAssignedEvent; import ch.unisg.assignment.assignment.domain.event.TaskAssignedEvent;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
@RequiredArgsConstructor @RequiredArgsConstructor

View File

@ -10,9 +10,9 @@ import org.springframework.stereotype.Component;
import ch.unisg.assignment.assignment.application.port.in.NewTaskCommand; import ch.unisg.assignment.assignment.application.port.in.NewTaskCommand;
import ch.unisg.assignment.assignment.application.port.in.NewTaskUseCase; import ch.unisg.assignment.assignment.application.port.in.NewTaskUseCase;
import ch.unisg.assignment.assignment.application.port.out.NewTaskEventPort; import ch.unisg.assignment.assignment.application.port.out.NewTaskEventPort;
import ch.unisg.assignment.assignment.domain.NewTaskEvent;
import ch.unisg.assignment.assignment.domain.Roster; import ch.unisg.assignment.assignment.domain.Roster;
import ch.unisg.assignment.assignment.domain.Task; import ch.unisg.assignment.assignment.domain.Task;
import ch.unisg.assignment.assignment.domain.event.NewTaskEvent;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
@RequiredArgsConstructor @RequiredArgsConstructor
@ -26,9 +26,9 @@ public class NewTaskService implements NewTaskUseCase {
public boolean addNewTaskToQueue(NewTaskCommand command) { public boolean addNewTaskToQueue(NewTaskCommand command) {
// TODO Get availableTaskTypes from executor pool // TODO Get availableTaskTypes from executor pool
List<String> availableTaskTypes = Arrays.asList("addition", "robot"); List<String> availableTaskTypes = Arrays.asList("ADDITION", "ROBOT");
if (!availableTaskTypes.contains(command.getTaskType())) { if (!availableTaskTypes.contains(command.getTaskType().getValue())) {
return false; return false;
} }

View File

@ -8,7 +8,7 @@ import ch.unisg.assignment.assignment.application.port.in.TaskCompletedCommand;
import ch.unisg.assignment.assignment.application.port.in.TaskCompletedUseCase; import ch.unisg.assignment.assignment.application.port.in.TaskCompletedUseCase;
import ch.unisg.assignment.assignment.application.port.out.TaskCompletedEventPort; import ch.unisg.assignment.assignment.application.port.out.TaskCompletedEventPort;
import ch.unisg.assignment.assignment.domain.Roster; import ch.unisg.assignment.assignment.domain.Roster;
import ch.unisg.assignment.assignment.domain.TaskCompletedEvent; import ch.unisg.assignment.assignment.domain.event.TaskCompletedEvent;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
@RequiredArgsConstructor @RequiredArgsConstructor

View File

@ -1,18 +1,21 @@
package ch.unisg.assignment.assignment.domain; package ch.unisg.assignment.assignment.domain;
import ch.unisg.assignment.assignment.domain.valueobject.ExecutorType;
import ch.unisg.assignment.assignment.domain.valueobject.IP4Adress;
import ch.unisg.assignment.assignment.domain.valueobject.Port;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
public class ExecutorInfo { public class ExecutorInfo {
@Getter @Getter
@Setter @Setter
private String ip; private IP4Adress ip;
@Getter @Getter
@Setter @Setter
private int port; private Port port;
@Getter @Getter
@Setter @Setter
private String executorType; private ExecutorType executorType;
} }

View File

@ -1,9 +0,0 @@
package ch.unisg.assignment.assignment.domain;
public class NewTaskEvent {
public String taskType;
public NewTaskEvent(String taskType) {
this.taskType = taskType;
}
}

View File

@ -4,6 +4,10 @@ import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import ch.unisg.assignment.assignment.domain.valueobject.ExecutorType;
import ch.unisg.assignment.assignment.domain.valueobject.IP4Adress;
import ch.unisg.assignment.assignment.domain.valueobject.Port;
public class Roster { public class Roster {
private static final Roster roster = new Roster(); private static final Roster roster = new Roster();
@ -19,25 +23,25 @@ public class Roster {
private Roster() {} private Roster() {}
public void addTaskToQueue(Task task) { public void addTaskToQueue(Task task) {
if (queues.containsKey(task.getTaskType().toUpperCase())) { if (queues.containsKey(task.getTaskType().getValue())) {
queues.get(task.getTaskType().toUpperCase()).add(task); queues.get(task.getTaskType().getValue()).add(task);
} else { } else {
queues.put(task.getTaskType().toUpperCase(), new ArrayList<>(Arrays.asList(task))); queues.put(task.getTaskType().getValue(), new ArrayList<>(Arrays.asList(task)));
} }
} }
public Task assignTaskToExecutor(String taskType, String executorIP, int executorPort) { public Task assignTaskToExecutor(ExecutorType taskType, IP4Adress executorIP, Port executorPort) {
if (!queues.containsKey(taskType.toUpperCase())) { if (!queues.containsKey(taskType.getValue())) {
return null; return null;
} }
if (queues.get(taskType.toUpperCase()).isEmpty()) { if (queues.get(taskType.getValue()).isEmpty()) {
return null; return null;
} }
Task task = queues.get(taskType.toUpperCase()).remove(0); Task task = queues.get(taskType.getValue()).remove(0);
rosterMap.put(task.getTaskID(), new RosterItem(task.getTaskID(), task.getTaskType(), rosterMap.put(task.getTaskID(), new RosterItem(task.getTaskID(),
executorIP, executorPort)); task.getTaskType().getValue(), executorIP, executorPort));
return task; return task;
} }

View File

@ -1,5 +1,7 @@
package ch.unisg.assignment.assignment.domain; package ch.unisg.assignment.assignment.domain;
import ch.unisg.assignment.assignment.domain.valueobject.IP4Adress;
import ch.unisg.assignment.assignment.domain.valueobject.Port;
import lombok.Getter; import lombok.Getter;
public class RosterItem { public class RosterItem {
@ -11,13 +13,13 @@ public class RosterItem {
private String taskType; private String taskType;
@Getter @Getter
private String executorIP; private IP4Adress executorIP;
@Getter @Getter
private int executorPort; private Port executorPort;
public RosterItem(String taskID, String taskType, String executorIP, int executorPort) { public RosterItem(String taskID, String taskType, IP4Adress executorIP, Port executorPort) {
this.taskID = taskID; this.taskID = taskID;
this.taskType = taskType; this.taskType = taskType;
this.executorIP = executorIP; this.executorIP = executorIP;

View File

@ -1,5 +1,6 @@
package ch.unisg.assignment.assignment.domain; package ch.unisg.assignment.assignment.domain;
import ch.unisg.assignment.assignment.domain.valueobject.ExecutorType;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
@ -9,7 +10,7 @@ public class Task {
private String taskID; private String taskID;
@Getter @Getter
private String taskType; private ExecutorType taskType;
@Getter @Getter
@Setter @Setter
@ -20,8 +21,15 @@ public class Task {
private String status; private String status;
public Task(String taskID, String taskType) { public Task(String taskID, String taskType) {
this.taskID = taskID;
this.taskType = new ExecutorType(taskType);
}
public Task(String taskID, ExecutorType taskType) {
this.taskID = taskID; this.taskID = taskID;
this.taskType = taskType; this.taskType = taskType;
} }
public Task() {};
} }

View File

@ -0,0 +1,11 @@
package ch.unisg.assignment.assignment.domain.event;
import ch.unisg.assignment.assignment.domain.valueobject.ExecutorType;
public class NewTaskEvent {
public final ExecutorType taskType;
public NewTaskEvent(ExecutorType taskType) {
this.taskType = taskType;
}
}

View File

@ -1,4 +1,4 @@
package ch.unisg.assignment.assignment.domain; package ch.unisg.assignment.assignment.domain.event;
public class TaskAssignedEvent { public class TaskAssignedEvent {
public String taskID; public String taskID;

View File

@ -1,11 +1,11 @@
package ch.unisg.assignment.assignment.domain; package ch.unisg.assignment.assignment.domain.event;
public class TaskCompletedEvent { public class TaskCompletedEvent {
public String taskID; public final String taskID;
public String status; public final String status;
public String result; public final String result;
public TaskCompletedEvent(String taskID, String status, String result) { public TaskCompletedEvent(String taskID, String status, String result) {
this.taskID = taskID; this.taskID = taskID;

View File

@ -0,0 +1,12 @@
package ch.unisg.assignment.assignment.domain.valueobject;
import lombok.Value;
@Value
public class ExecutorType {
private String value;
public ExecutorType(String type) {
this.value = type.toUpperCase();
}
}

View File

@ -0,0 +1,23 @@
package ch.unisg.assignment.assignment.domain.valueobject;
import ch.unisg.assignment.common.exception.InvalidIP4Exception;
import lombok.Value;
@Value
public class IP4Adress {
private String value;
public IP4Adress(String ip4) throws InvalidIP4Exception {
if (ip4.equalsIgnoreCase("localhost") ||
ip4.matches("^((25[0-5]|(2[0-4]|1\\d|[1-9]|)\\d)(\\.(?!$)|$)){4}$")) {
this.value = ip4;
} else {
throw new InvalidIP4Exception();
}
}
}

View File

@ -0,0 +1,17 @@
package ch.unisg.assignment.assignment.domain.valueobject;
import ch.unisg.assignment.common.exception.PortOutOfRangeException;
import lombok.Value;
@Value
public class Port {
private int value;
public Port(int port) throws PortOutOfRangeException {
if (1024 <= port && port <= 65535) {
this.value = port;
} else {
throw new PortOutOfRangeException();
}
}
}

View File

@ -0,0 +1,13 @@
package ch.unisg.assignment.common.exception;
import org.springframework.http.HttpStatus;
import lombok.Data;
import lombok.RequiredArgsConstructor;
@Data
@RequiredArgsConstructor
public class ErrorResponse {
private final HttpStatus httpStatus;
private final String message;
}

View File

@ -0,0 +1,7 @@
package ch.unisg.assignment.common.exception;
public class InvalidIP4Exception extends Exception {
public InvalidIP4Exception() {
super("IP4 is invalid");
}
}

View File

@ -0,0 +1,7 @@
package ch.unisg.assignment.common.exception;
public class PortOutOfRangeException extends Exception {
public PortOutOfRangeException() {
super("Port is out of available range (1024-65535)");
}
}

View File

@ -5,14 +5,9 @@ import java.net.URI;
import java.net.http.HttpClient; import java.net.http.HttpClient;
import java.net.http.HttpRequest; import java.net.http.HttpRequest;
import java.net.http.HttpResponse; import java.net.http.HttpResponse;
import java.util.HashMap;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.json.JSONObject; import org.json.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import ch.unisg.executorBase.executor.application.port.out.ExecutionFinishedEventPort; import ch.unisg.executorBase.executor.application.port.out.ExecutionFinishedEventPort;
import ch.unisg.executorBase.executor.domain.ExecutionFinishedEvent; import ch.unisg.executorBase.executor.domain.ExecutionFinishedEvent;

View File

@ -3,11 +3,8 @@ package ch.unisg.executorBase.executor.adapter.out.web;
import java.net.URI; import java.net.URI;
import java.net.http.HttpClient; import java.net.http.HttpClient;
import java.net.http.HttpRequest; import java.net.http.HttpRequest;
import java.util.HashMap;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.json.JSONObject;
import org.springframework.context.annotation.Primary; import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
@ -17,31 +14,22 @@ import ch.unisg.executorBase.executor.domain.ExecutorType;
@Component @Component
@Primary @Primary
public class NotifyExecutorPoolAdapter implements NotifyExecutorPoolPort { public class NotifyExecutorPoolAdapter implements NotifyExecutorPoolPort {
//This is the base URI of the service interested in this event (in my setup, running locally as separate Spring Boot application)
String server = "http://127.0.0.1:8083"; String server = "http://127.0.0.1:8083";
@Override @Override
public boolean notifyExecutorPool(String ip, int port, ExecutorType executorType) { public boolean notifyExecutorPool(String ip, int port, ExecutorType executorType) {
var values = new HashMap<String, String>() {{ String body = new JSONObject()
put("ip", ip); .put("executorType", executorType)
put("port", Integer.toString(port)); .put("ip", ip)
put("executorType", executorType.toString()); .put("port", port)
}}; .toString();
var objectMapper = new ObjectMapper();
String requestBody = null;
try {
requestBody = objectMapper.writeValueAsString(values);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
HttpClient client = HttpClient.newHttpClient(); HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder() HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(server+"/executor/new/")) .uri(URI.create(server+"/executor/new/"))
.POST(HttpRequest.BodyPublishers.ofString(requestBody)) .POST(HttpRequest.BodyPublishers.ofString(body))
.build(); .build();
/** Needs the other service running /** Needs the other service running

View File

@ -45,6 +45,12 @@
<artifactId>executorBase</artifactId> <artifactId>executorBase</artifactId>
<version>0.0.1-SNAPSHOT</version> <version>0.0.1-SNAPSHOT</version>
</dependency> </dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20210307</version>
</dependency>
</dependencies> </dependencies>
<build> <build>

View File

@ -1,5 +1,7 @@
package ch.unisg.executor2.executor.adapter.in.web; package ch.unisg.executor2.executor.adapter.in.web;
import java.util.concurrent.CompletableFuture;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
@ -25,12 +27,9 @@ public class TaskAvailableController {
if (ExecutorType.contains(taskType.toUpperCase())) { if (ExecutorType.contains(taskType.toUpperCase())) {
TaskAvailableCommand command = new TaskAvailableCommand( TaskAvailableCommand command = new TaskAvailableCommand(
ExecutorType.valueOf(taskType.toUpperCase())); ExecutorType.valueOf(taskType.toUpperCase()));
taskAvailableUseCase.newTaskAvailable(command); CompletableFuture.runAsync(() -> taskAvailableUseCase.newTaskAvailable(command));
} }
// Add the content type as a response header return new ResponseEntity<>("OK", new HttpHeaders(), HttpStatus.OK);
HttpHeaders responseHeaders = new HttpHeaders();
return new ResponseEntity<>("OK", responseHeaders, HttpStatus.OK);
} }
} }

View File

@ -1,5 +1,6 @@
package ch.unisg.executor2.executor.application.service; package ch.unisg.executor2.executor.application.service;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import ch.unisg.executor2.executor.domain.Executor; import ch.unisg.executor2.executor.domain.Executor;
@ -16,6 +17,7 @@ import javax.transaction.Transactional;
public class TaskAvailableService implements TaskAvailableUseCase { public class TaskAvailableService implements TaskAvailableUseCase {
@Override @Override
@Async
public void newTaskAvailable(TaskAvailableCommand command) { public void newTaskAvailable(TaskAvailableCommand command) {
Executor executor = Executor.getExecutor(); Executor executor = Executor.getExecutor();