Added abstraction
This commit is contained in:
parent
1d78bb63ab
commit
3bc39b70aa
|
@ -9,6 +9,7 @@ import org.springframework.web.bind.annotation.RestController;
|
|||
|
||||
import ch.unisg.executor1.executor.application.port.in.TaskAvailableCommand;
|
||||
import ch.unisg.executor1.executor.application.port.in.TaskAvailableUseCase;
|
||||
import ch.unisg.executor1.executor.domain.ExecutorType;
|
||||
|
||||
@RestController
|
||||
public class TaskAvailableController {
|
||||
|
@ -20,9 +21,12 @@ public class TaskAvailableController {
|
|||
|
||||
@GetMapping(path = "/newtask/{taskType}")
|
||||
public ResponseEntity<String> retrieveTaskFromTaskList(@PathVariable("taskType") String taskType) {
|
||||
TaskAvailableCommand command = new TaskAvailableCommand(taskType);
|
||||
|
||||
taskAvailableUseCase.newTaskAvailable(command);
|
||||
if (ExecutorType.contains(taskType.toUpperCase())) {
|
||||
TaskAvailableCommand command = new TaskAvailableCommand(
|
||||
ExecutorType.valueOf(taskType.toUpperCase()));
|
||||
taskAvailableUseCase.newTaskAvailable(command);
|
||||
}
|
||||
|
||||
// Add the content type as a response header
|
||||
HttpHeaders responseHeaders = new HttpHeaders();
|
||||
|
|
|
@ -13,7 +13,7 @@ import com.fasterxml.jackson.core.JsonProcessingException;
|
|||
import ch.unisg.executor1.executor.application.port.out.ExecutionFinishedEventPort;
|
||||
import ch.unisg.executor1.executor.domain.ExecutionFinishedEvent;
|
||||
|
||||
public class ExecutionFinishedAdapter implements ExecutionFinishedEventPort {
|
||||
public class ExecutionFinishedEventAdapter implements ExecutionFinishedEventPort {
|
||||
|
||||
//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:8082";
|
||||
|
@ -51,7 +51,7 @@ public class ExecutionFinishedAdapter implements ExecutionFinishedEventPort {
|
|||
}
|
||||
**/
|
||||
|
||||
System.out.println("Finish execution event sent");
|
||||
System.out.println("Finish execution event sent with result:" + event.getResult());
|
||||
|
||||
}
|
||||
|
|
@ -8,6 +8,7 @@ import org.springframework.context.annotation.Primary;
|
|||
import org.springframework.stereotype.Component;
|
||||
|
||||
import ch.unisg.executor1.executor.application.port.out.GetAssignmentPort;
|
||||
import ch.unisg.executor1.executor.domain.ExecutorType;
|
||||
import ch.unisg.executor1.executor.domain.Task;
|
||||
|
||||
@Component
|
||||
|
@ -18,7 +19,7 @@ public class GetAssignmentAdapter implements GetAssignmentPort {
|
|||
String server = "http://127.0.0.1:8082";
|
||||
|
||||
@Override
|
||||
public Task getAssignment(String executorType) {
|
||||
public Task getAssignment(ExecutorType executorType) {
|
||||
|
||||
HttpClient client = HttpClient.newHttpClient();
|
||||
HttpRequest request = HttpRequest.newBuilder()
|
||||
|
@ -38,7 +39,7 @@ public class GetAssignmentAdapter implements GetAssignmentPort {
|
|||
|
||||
// TODO return null or a new Task here depending on the response of the http call
|
||||
|
||||
return new Task("1234");
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -12,6 +12,7 @@ import org.springframework.context.annotation.Primary;
|
|||
import org.springframework.stereotype.Component;
|
||||
|
||||
import ch.unisg.executor1.executor.application.port.out.NotifyExecutorPoolPort;
|
||||
import ch.unisg.executor1.executor.domain.ExecutorType;
|
||||
|
||||
@Component
|
||||
@Primary
|
||||
|
@ -21,12 +22,12 @@ public class NotifyExecutorPoolAdapter implements NotifyExecutorPoolPort {
|
|||
String server = "http://127.0.0.1:8083";
|
||||
|
||||
@Override
|
||||
public boolean notifyExecutorPool(String ip, int port, String executorType) {
|
||||
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);
|
||||
put("executorType", executorType.toString());
|
||||
}};
|
||||
|
||||
var objectMapper = new ObjectMapper();
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package ch.unisg.executor1.executor.application.port.in;
|
||||
|
||||
import ch.unisg.executor1.common.SelfValidating;
|
||||
import ch.unisg.executor1.executor.domain.ExecutorType;
|
||||
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
|
@ -10,9 +11,9 @@ import lombok.Value;
|
|||
public class TaskAvailableCommand extends SelfValidating<TaskAvailableCommand> {
|
||||
|
||||
@NotNull
|
||||
private final String taskType;
|
||||
private final ExecutorType taskType;
|
||||
|
||||
public TaskAvailableCommand(String taskType) {
|
||||
public TaskAvailableCommand(ExecutorType taskType) {
|
||||
this.taskType = taskType;
|
||||
this.validateSelf();
|
||||
}
|
||||
|
|
|
@ -1,7 +1,8 @@
|
|||
package ch.unisg.executor1.executor.application.port.out;
|
||||
|
||||
import ch.unisg.executor1.executor.domain.ExecutorType;
|
||||
import ch.unisg.executor1.executor.domain.Task;
|
||||
|
||||
public interface GetAssignmentPort {
|
||||
Task getAssignment(String executorType);
|
||||
Task getAssignment(ExecutorType executorType);
|
||||
}
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
package ch.unisg.executor1.executor.application.port.out;
|
||||
|
||||
import ch.unisg.executor1.executor.domain.ExecutorType;
|
||||
|
||||
public interface NotifyExecutorPoolPort {
|
||||
boolean notifyExecutorPool(String ip, int port, String executorType);
|
||||
boolean notifyExecutorPool(String ip, int port, ExecutorType executorType);
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|||
import org.springframework.stereotype.Component;
|
||||
|
||||
import ch.unisg.executor1.executor.application.port.out.NotifyExecutorPoolPort;
|
||||
import ch.unisg.executor1.executor.domain.ExecutorType;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
@RequiredArgsConstructor
|
||||
|
@ -11,7 +12,7 @@ public class NotifyExecutorPoolService {
|
|||
|
||||
private final NotifyExecutorPoolPort notifyExecutorPoolPort;
|
||||
|
||||
public boolean notifyExecutorPool(String ip, int port, String executorType) {
|
||||
public boolean notifyExecutorPool(String ip, int port, ExecutorType executorType) {
|
||||
return notifyExecutorPoolPort.notifyExecutorPool(ip, port, executorType);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -19,7 +19,7 @@ public class TaskAvailableService implements TaskAvailableUseCase {
|
|||
public void newTaskAvailable(TaskAvailableCommand command) {
|
||||
Executor executor = Executor.getExecutor();
|
||||
|
||||
if (executor.getExecutorType().equalsIgnoreCase(command.getTaskType()) &&
|
||||
if (executor.getExecutorType() == command.getTaskType() &&
|
||||
executor.getStatus() == ExecutorStatus.IDLING) {
|
||||
executor.getAssignment();
|
||||
}
|
||||
|
|
|
@ -1,77 +1,22 @@
|
|||
package ch.unisg.executor1.executor.domain;
|
||||
|
||||
import ch.unisg.executor1.executor.application.port.out.ExecutionFinishedEventPort;
|
||||
import ch.unisg.executor1.executor.application.port.out.GetAssignmentPort;
|
||||
import ch.unisg.executor1.executor.application.port.out.NotifyExecutorPoolPort;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import javax.transaction.Transactional;
|
||||
public class Executor extends ExecutorBase {
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Configurable;
|
||||
|
||||
import ch.unisg.executor1.executor.adapter.out.web.ExecutionFinishedAdapter;
|
||||
import ch.unisg.executor1.executor.adapter.out.web.GetAssignmentAdapter;
|
||||
import ch.unisg.executor1.executor.adapter.out.web.NotifyExecutorPoolAdapter;
|
||||
import ch.unisg.executor1.executor.application.service.NotifyExecutorPoolService;
|
||||
import lombok.Getter;
|
||||
|
||||
public class Executor {
|
||||
|
||||
@Getter
|
||||
private String ip;
|
||||
|
||||
@Getter
|
||||
private String executorType = "addition";
|
||||
|
||||
@Getter
|
||||
private int port;
|
||||
|
||||
@Getter
|
||||
private ExecutorStatus status;
|
||||
|
||||
private static final Executor executor = new Executor();
|
||||
|
||||
// TODO Violation of the Dependency Inversion Principle?, but we havn't really got a better solutions to send a http request / access a service from a domain model
|
||||
// TODO I guess we can somehow autowire this but I don't know why it's not working :D
|
||||
private final NotifyExecutorPoolPort notifyExecutorPoolPort = new NotifyExecutorPoolAdapter();
|
||||
private final NotifyExecutorPoolService notifyExecutorPoolService = new NotifyExecutorPoolService(notifyExecutorPoolPort);
|
||||
private final GetAssignmentPort getAssignmentPort = new GetAssignmentAdapter();
|
||||
private final ExecutionFinishedEventPort executionFinishedEventPort = new ExecutionFinishedAdapter();
|
||||
|
||||
private Executor() {
|
||||
System.out.println("Starting Executor");
|
||||
// TODO set this automaticly
|
||||
this.ip = "localhost";
|
||||
this.port = 8084;
|
||||
|
||||
this.status = ExecutorStatus.STARTING_UP;
|
||||
if(!notifyExecutorPoolService.notifyExecutorPool(this.ip, this.port, this.executorType)) {
|
||||
System.exit(0);
|
||||
} else {
|
||||
System.out.println(true);
|
||||
this.status = ExecutorStatus.IDLING;
|
||||
getAssignment();
|
||||
}
|
||||
}
|
||||
private static final Executor executor = new Executor(ExecutorType.ADDITION);
|
||||
|
||||
public static Executor getExecutor() {
|
||||
return executor;
|
||||
}
|
||||
|
||||
public void getAssignment() {
|
||||
Task newTask = getAssignmentPort.getAssignment(this.getExecutorType());
|
||||
if (newTask != null) {
|
||||
this.executeTask(newTask);
|
||||
} else {
|
||||
this.status = ExecutorStatus.IDLING;
|
||||
}
|
||||
private Executor(ExecutorType executorType) {
|
||||
super(executorType);
|
||||
}
|
||||
|
||||
private void executeTask(Task task) {
|
||||
System.out.println("Starting execution");
|
||||
this.status = ExecutorStatus.EXECUTING;
|
||||
@Override
|
||||
String execution() {
|
||||
|
||||
int a = 10;
|
||||
int b = 20;
|
||||
try {
|
||||
|
@ -82,12 +27,7 @@ public class Executor {
|
|||
|
||||
int result = a + b;
|
||||
|
||||
task.setResult(Integer.toString(result));
|
||||
|
||||
executionFinishedEventPort.publishExecutionFinishedEvent(new ExecutionFinishedEvent(task.getTaskID(), task.getResult(), "SUCCESS"));
|
||||
|
||||
System.out.println("Finish execution");
|
||||
getAssignment();
|
||||
return Integer.toString(result);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,87 @@
|
|||
package ch.unisg.executor1.executor.domain;
|
||||
|
||||
import ch.unisg.executor1.executor.application.port.out.ExecutionFinishedEventPort;
|
||||
import ch.unisg.executor1.executor.application.port.out.GetAssignmentPort;
|
||||
import ch.unisg.executor1.executor.application.port.out.NotifyExecutorPoolPort;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import javax.transaction.Transactional;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Configurable;
|
||||
|
||||
import ch.unisg.executor1.executor.adapter.out.web.ExecutionFinishedEventAdapter;
|
||||
import ch.unisg.executor1.executor.adapter.out.web.GetAssignmentAdapter;
|
||||
import ch.unisg.executor1.executor.adapter.out.web.NotifyExecutorPoolAdapter;
|
||||
import ch.unisg.executor1.executor.application.service.NotifyExecutorPoolService;
|
||||
import lombok.Getter;
|
||||
|
||||
abstract class ExecutorBase {
|
||||
|
||||
@Getter
|
||||
private String ip;
|
||||
|
||||
@Getter
|
||||
private ExecutorType executorType;
|
||||
|
||||
@Getter
|
||||
private int port;
|
||||
|
||||
@Getter
|
||||
private ExecutorStatus status;
|
||||
|
||||
// private static final ExecutorBase executor = new ExecutorBase();
|
||||
|
||||
// TODO Violation of the Dependency Inversion Principle?, but we havn't really got a better solutions to send a http request / access a service from a domain model
|
||||
// TODO I guess we can somehow autowire this but I don't know why it's not working :D
|
||||
private final NotifyExecutorPoolPort notifyExecutorPoolPort = new NotifyExecutorPoolAdapter();
|
||||
private final NotifyExecutorPoolService notifyExecutorPoolService = new NotifyExecutorPoolService(notifyExecutorPoolPort);
|
||||
private final GetAssignmentPort getAssignmentPort = new GetAssignmentAdapter();
|
||||
private final ExecutionFinishedEventPort executionFinishedEventPort = new ExecutionFinishedEventAdapter();
|
||||
|
||||
public ExecutorBase(ExecutorType executorType) {
|
||||
System.out.println("Starting Executor");
|
||||
// TODO set this automaticly
|
||||
this.ip = "localhost";
|
||||
this.port = 8084;
|
||||
this.executorType = executorType;
|
||||
|
||||
this.status = ExecutorStatus.STARTING_UP;
|
||||
if(!notifyExecutorPoolService.notifyExecutorPool(this.ip, this.port, this.executorType)) {
|
||||
System.exit(0);
|
||||
} else {
|
||||
System.out.println(true);
|
||||
this.status = ExecutorStatus.IDLING;
|
||||
getAssignment();
|
||||
}
|
||||
}
|
||||
|
||||
// public static ExecutorBase getExecutor() {
|
||||
// return executor;
|
||||
// }
|
||||
|
||||
public void getAssignment() {
|
||||
Task newTask = getAssignmentPort.getAssignment(this.getExecutorType());
|
||||
if (newTask != null) {
|
||||
this.executeTask(newTask);
|
||||
} else {
|
||||
this.status = ExecutorStatus.IDLING;
|
||||
}
|
||||
}
|
||||
|
||||
private void executeTask(Task task) {
|
||||
System.out.println("Starting execution");
|
||||
this.status = ExecutorStatus.EXECUTING;
|
||||
|
||||
task.setResult(execution());
|
||||
|
||||
executionFinishedEventPort.publishExecutionFinishedEvent(new ExecutionFinishedEvent(task.getTaskID(), task.getResult(), "SUCCESS"));
|
||||
|
||||
System.out.println("Finish execution");
|
||||
getAssignment();
|
||||
}
|
||||
|
||||
abstract String execution();
|
||||
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
package ch.unisg.executor1.executor.domain;
|
||||
|
||||
public enum ExecutorType {
|
||||
ADDITION, ROBOT;
|
||||
|
||||
public static boolean contains(String test) {
|
||||
|
||||
for (ExecutorType x : ExecutorType.values()) {
|
||||
if (x.name().equals(test)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user