added valueObjects & CustomErrors
This commit is contained in:
parent
a61f111879
commit
46969deb0f
|
@ -1,12 +1,8 @@
|
|||
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.RequestBody;
|
||||
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.ApplyForTaskUseCase;
|
||||
|
@ -23,14 +19,11 @@ public class ApplyForTaskController {
|
|||
|
||||
@PostMapping(path = "/task/apply", consumes = {"application/json"})
|
||||
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());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,13 +1,10 @@
|
|||
package ch.unisg.assignment.assignment.adapter.in.web;
|
||||
|
||||
import javax.validation.ConstraintViolationException;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
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.NewTaskUseCase;
|
||||
|
@ -22,11 +19,9 @@ public class NewTaskController {
|
|||
}
|
||||
|
||||
@PostMapping(path = "/task", consumes = {"application/json"})
|
||||
public ResponseEntity<Void> addNewTaskTaskToTaskList(@RequestBody Task task) {
|
||||
try {
|
||||
NewTaskCommand command = new NewTaskCommand(
|
||||
task.getTaskID(), task.getTaskType()
|
||||
);
|
||||
public ResponseEntity<Void> newTaskController(@RequestBody Task task) {
|
||||
|
||||
NewTaskCommand command = new NewTaskCommand(task.getTaskID(), task.getTaskType());
|
||||
|
||||
boolean success = newTaskUseCase.addNewTaskToQueue(command);
|
||||
|
||||
|
@ -34,8 +29,6 @@ public class NewTaskController {
|
|||
return new ResponseEntity<>(HttpStatus.CREATED);
|
||||
}
|
||||
return new ResponseEntity<>(HttpStatus.CONFLICT);
|
||||
} catch (ConstraintViolationException e) {
|
||||
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, e.getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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());
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -10,7 +10,7 @@ import org.springframework.context.annotation.Primary;
|
|||
import org.springframework.stereotype.Component;
|
||||
|
||||
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
|
||||
@Primary
|
||||
|
@ -23,7 +23,7 @@ public class PublishNewTaskEventAdapter implements NewTaskEventPort {
|
|||
|
||||
HttpClient client = HttpClient.newHttpClient();
|
||||
HttpRequest request = HttpRequest.newBuilder()
|
||||
.uri(URI.create(server + "/newtask/" + event.taskType))
|
||||
.uri(URI.create(server + "/newtask/" + event.taskType.getValue()))
|
||||
.GET()
|
||||
.build();
|
||||
|
||||
|
|
|
@ -11,7 +11,7 @@ import org.springframework.context.annotation.Primary;
|
|||
import org.springframework.stereotype.Component;
|
||||
|
||||
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
|
||||
@Primary
|
||||
|
|
|
@ -11,7 +11,7 @@ import org.springframework.context.annotation.Primary;
|
|||
import org.springframework.stereotype.Component;
|
||||
|
||||
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
|
||||
@Primary
|
||||
|
|
|
@ -2,6 +2,9 @@ package ch.unisg.assignment.assignment.application.port.in;
|
|||
|
||||
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 lombok.EqualsAndHashCode;
|
||||
import lombok.Value;
|
||||
|
@ -11,16 +14,16 @@ import lombok.Value;
|
|||
public class ApplyForTaskCommand extends SelfValidating<ApplyForTaskCommand>{
|
||||
|
||||
@NotNull
|
||||
private final String taskType;
|
||||
private final ExecutorType taskType;
|
||||
|
||||
@NotNull
|
||||
private final String executorIP;
|
||||
private final IP4Adress executorIP;
|
||||
|
||||
|
||||
@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.executorIP = executorIP;
|
||||
this.executorPort = executorPort;
|
||||
|
|
|
@ -2,20 +2,22 @@ package ch.unisg.assignment.assignment.application.port.in;
|
|||
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
import ch.unisg.assignment.assignment.domain.valueobject.ExecutorType;
|
||||
import ch.unisg.assignment.common.SelfValidating;
|
||||
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.Value;
|
||||
|
||||
@Value
|
||||
@EqualsAndHashCode(callSuper=false)
|
||||
public class NewTaskCommand extends SelfValidating<NewTaskCommand> {
|
||||
|
||||
@NotNull
|
||||
private final String taskID;
|
||||
|
||||
@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.taskType = taskType;
|
||||
this.validateSelf();
|
||||
|
|
|
@ -2,6 +2,7 @@ package ch.unisg.assignment.assignment.application.port.in;
|
|||
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
import ch.unisg.assignment.assignment.domain.valueobject.ExecutorType;
|
||||
import ch.unisg.assignment.common.SelfValidating;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.Value;
|
||||
|
@ -14,7 +15,7 @@ public class TaskCompletedCommand extends SelfValidating<TaskCompletedCommand>{
|
|||
private final String taskID;
|
||||
|
||||
@NotNull
|
||||
private final String taskType;
|
||||
private final ExecutorType taskType;
|
||||
|
||||
@NotNull
|
||||
private final String taskStatus;
|
||||
|
@ -22,7 +23,7 @@ public class TaskCompletedCommand extends SelfValidating<TaskCompletedCommand>{
|
|||
@NotNull
|
||||
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.taskType = taskType;
|
||||
this.taskStatus = taskStatus;
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
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 {
|
||||
void publishNewTaskEvent(NewTaskEvent event);
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
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 {
|
||||
void publishTaskAssignedEvent(TaskAssignedEvent taskAssignedEvent);
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
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 {
|
||||
void publishTaskCompleted(TaskCompletedEvent event);
|
||||
|
|
|
@ -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.domain.Roster;
|
||||
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;
|
||||
|
||||
@RequiredArgsConstructor
|
||||
|
|
|
@ -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.NewTaskUseCase;
|
||||
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.Task;
|
||||
import ch.unisg.assignment.assignment.domain.event.NewTaskEvent;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
@RequiredArgsConstructor
|
||||
|
@ -26,9 +26,9 @@ public class NewTaskService implements NewTaskUseCase {
|
|||
public boolean addNewTaskToQueue(NewTaskCommand command) {
|
||||
|
||||
// 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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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.out.TaskCompletedEventPort;
|
||||
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;
|
||||
|
||||
@RequiredArgsConstructor
|
||||
|
|
|
@ -1,18 +1,21 @@
|
|||
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.Setter;
|
||||
|
||||
public class ExecutorInfo {
|
||||
@Getter
|
||||
@Setter
|
||||
private String ip;
|
||||
private IP4Adress ip;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
private int port;
|
||||
private Port port;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
private String executorType;
|
||||
private ExecutorType executorType;
|
||||
}
|
||||
|
|
|
@ -1,9 +0,0 @@
|
|||
package ch.unisg.assignment.assignment.domain;
|
||||
|
||||
public class NewTaskEvent {
|
||||
public String taskType;
|
||||
|
||||
public NewTaskEvent(String taskType) {
|
||||
this.taskType = taskType;
|
||||
}
|
||||
}
|
|
@ -4,6 +4,10 @@ import java.util.ArrayList;
|
|||
import java.util.Arrays;
|
||||
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 {
|
||||
|
||||
private static final Roster roster = new Roster();
|
||||
|
@ -19,25 +23,25 @@ public class Roster {
|
|||
private Roster() {}
|
||||
|
||||
public void addTaskToQueue(Task task) {
|
||||
if (queues.containsKey(task.getTaskType().toUpperCase())) {
|
||||
queues.get(task.getTaskType().toUpperCase()).add(task);
|
||||
if (queues.containsKey(task.getTaskType().getValue())) {
|
||||
queues.get(task.getTaskType().getValue()).add(task);
|
||||
} 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) {
|
||||
if (!queues.containsKey(taskType.toUpperCase())) {
|
||||
public Task assignTaskToExecutor(ExecutorType taskType, IP4Adress executorIP, Port executorPort) {
|
||||
if (!queues.containsKey(taskType.getValue())) {
|
||||
return null;
|
||||
}
|
||||
if (queues.get(taskType.toUpperCase()).isEmpty()) {
|
||||
if (queues.get(taskType.getValue()).isEmpty()) {
|
||||
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(),
|
||||
executorIP, executorPort));
|
||||
rosterMap.put(task.getTaskID(), new RosterItem(task.getTaskID(),
|
||||
task.getTaskType().getValue(), executorIP, executorPort));
|
||||
|
||||
return task;
|
||||
}
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
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;
|
||||
|
||||
public class RosterItem {
|
||||
|
@ -11,13 +13,13 @@ public class RosterItem {
|
|||
private String taskType;
|
||||
|
||||
@Getter
|
||||
private String executorIP;
|
||||
private IP4Adress executorIP;
|
||||
|
||||
@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.taskType = taskType;
|
||||
this.executorIP = executorIP;
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package ch.unisg.assignment.assignment.domain;
|
||||
|
||||
import ch.unisg.assignment.assignment.domain.valueobject.ExecutorType;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
|
@ -9,7 +10,7 @@ public class Task {
|
|||
private String taskID;
|
||||
|
||||
@Getter
|
||||
private String taskType;
|
||||
private ExecutorType taskType;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
|
@ -20,8 +21,15 @@ public class Task {
|
|||
private String status;
|
||||
|
||||
public Task(String taskID, String taskType) {
|
||||
this.taskID = taskID;
|
||||
this.taskType = new ExecutorType(taskType);
|
||||
}
|
||||
|
||||
public Task(String taskID, ExecutorType taskType) {
|
||||
this.taskID = taskID;
|
||||
this.taskType = taskType;
|
||||
}
|
||||
|
||||
public Task() {};
|
||||
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
package ch.unisg.assignment.assignment.domain;
|
||||
package ch.unisg.assignment.assignment.domain.event;
|
||||
|
||||
public class TaskAssignedEvent {
|
||||
public String taskID;
|
|
@ -1,11 +1,11 @@
|
|||
package ch.unisg.assignment.assignment.domain;
|
||||
package ch.unisg.assignment.assignment.domain.event;
|
||||
|
||||
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) {
|
||||
this.taskID = taskID;
|
|
@ -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();
|
||||
}
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
package ch.unisg.assignment.common.exception;
|
||||
|
||||
public class InvalidIP4Exception extends Exception {
|
||||
public InvalidIP4Exception() {
|
||||
super("IP4 is invalid");
|
||||
}
|
||||
}
|
|
@ -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)");
|
||||
}
|
||||
}
|
|
@ -5,14 +5,9 @@ import java.net.URI;
|
|||
import java.net.http.HttpClient;
|
||||
import java.net.http.HttpRequest;
|
||||
import java.net.http.HttpResponse;
|
||||
import java.util.HashMap;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
import org.json.JSONObject;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
|
||||
import ch.unisg.executorBase.executor.application.port.out.ExecutionFinishedEventPort;
|
||||
import ch.unisg.executorBase.executor.domain.ExecutionFinishedEvent;
|
||||
|
||||
|
|
|
@ -3,11 +3,8 @@ package ch.unisg.executorBase.executor.adapter.out.web;
|
|||
import java.net.URI;
|
||||
import java.net.http.HttpClient;
|
||||
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.stereotype.Component;
|
||||
|
||||
|
@ -17,31 +14,22 @@ import ch.unisg.executorBase.executor.domain.ExecutorType;
|
|||
@Component
|
||||
@Primary
|
||||
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";
|
||||
|
||||
@Override
|
||||
public boolean notifyExecutorPool(String ip, int port, ExecutorType executorType) {
|
||||
|
||||
var values = new HashMap<String, String>() {{
|
||||
put("ip", ip);
|
||||
put("port", Integer.toString(port));
|
||||
put("executorType", executorType.toString());
|
||||
}};
|
||||
|
||||
var objectMapper = new ObjectMapper();
|
||||
String requestBody = null;
|
||||
try {
|
||||
requestBody = objectMapper.writeValueAsString(values);
|
||||
} catch (JsonProcessingException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
String body = new JSONObject()
|
||||
.put("executorType", executorType)
|
||||
.put("ip", ip)
|
||||
.put("port", port)
|
||||
.toString();
|
||||
|
||||
HttpClient client = HttpClient.newHttpClient();
|
||||
HttpRequest request = HttpRequest.newBuilder()
|
||||
.uri(URI.create(server+"/executor/new/"))
|
||||
.POST(HttpRequest.BodyPublishers.ofString(requestBody))
|
||||
.POST(HttpRequest.BodyPublishers.ofString(body))
|
||||
.build();
|
||||
|
||||
/** Needs the other service running
|
||||
|
|
|
@ -45,6 +45,12 @@
|
|||
<artifactId>executorBase</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.json</groupId>
|
||||
<artifactId>json</artifactId>
|
||||
<version>20210307</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
package ch.unisg.executor2.executor.adapter.in.web;
|
||||
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
|
@ -25,12 +27,9 @@ public class TaskAvailableController {
|
|||
if (ExecutorType.contains(taskType.toUpperCase())) {
|
||||
TaskAvailableCommand command = new TaskAvailableCommand(
|
||||
ExecutorType.valueOf(taskType.toUpperCase()));
|
||||
taskAvailableUseCase.newTaskAvailable(command);
|
||||
CompletableFuture.runAsync(() -> taskAvailableUseCase.newTaskAvailable(command));
|
||||
}
|
||||
|
||||
// Add the content type as a response header
|
||||
HttpHeaders responseHeaders = new HttpHeaders();
|
||||
|
||||
return new ResponseEntity<>("OK", responseHeaders, HttpStatus.OK);
|
||||
return new ResponseEntity<>("OK", new HttpHeaders(), HttpStatus.OK);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package ch.unisg.executor2.executor.application.service;
|
||||
|
||||
import org.springframework.scheduling.annotation.Async;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import ch.unisg.executor2.executor.domain.Executor;
|
||||
|
@ -16,6 +17,7 @@ import javax.transaction.Transactional;
|
|||
public class TaskAvailableService implements TaskAvailableUseCase {
|
||||
|
||||
@Override
|
||||
@Async
|
||||
public void newTaskAvailable(TaskAvailableCommand command) {
|
||||
|
||||
Executor executor = Executor.getExecutor();
|
||||
|
|
Loading…
Reference in New Issue
Block a user