added common lib and added service uri's to properties file

This commit is contained in:
2021-10-27 13:15:29 +02:00
parent d08a6d0b67
commit 6752454838
61 changed files with 1231 additions and 931 deletions

View File

@@ -1,13 +0,0 @@
package ch.unisg.executorBase;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Executor1Application {
public static void main(String[] args) {
SpringApplication.run(Executor1Application.class, args);
}
}

View File

@@ -1,30 +0,0 @@
package ch.unisg.executorBase.common;
import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;
import java.util.Set;
public class SelfValidating<T> {
private Validator validator;
public SelfValidating() {
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
validator = factory.getValidator();
}
/**
* Evaluates all Bean Validations on the attributes of this
* instance.
*/
protected void validateSelf() {
@SuppressWarnings("unchecked")
Set<ConstraintViolation<T>> violations = validator.validate((T) this);
if (!violations.isEmpty()) {
throw new ConstraintViolationException(violations);
}
}
}

View File

@@ -1,4 +1,4 @@
package ch.unisg.executorBase.executor.adapter.in.web;
package ch.unisg.executorbase.executor.adapter.in.web;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
@@ -7,9 +7,9 @@ import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import ch.unisg.executorBase.executor.application.port.in.TaskAvailableCommand;
import ch.unisg.executorBase.executor.application.port.in.TaskAvailableUseCase;
import ch.unisg.executorBase.executor.domain.ExecutorType;
import ch.unisg.executorbase.executor.application.port.in.TaskAvailableCommand;
import ch.unisg.executorbase.executor.application.port.in.TaskAvailableUseCase;
import ch.unisg.executorbase.executor.domain.ExecutorType;
@RestController
public class TaskAvailableController {

View File

@@ -1,4 +1,4 @@
package ch.unisg.executorBase.executor.adapter.out.web;
package ch.unisg.executorbase.executor.adapter.out.web;
import java.io.IOException;
import java.net.URI;
@@ -9,13 +9,15 @@ import java.util.logging.Level;
import java.util.logging.Logger;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Value;
import ch.unisg.executorBase.executor.application.port.out.ExecutionFinishedEventPort;
import ch.unisg.executorBase.executor.domain.ExecutionFinishedEvent;
import ch.unisg.executorbase.executor.application.port.out.ExecutionFinishedEventPort;
import ch.unisg.executorbase.executor.domain.ExecutionFinishedEvent;
public class ExecutionFinishedEventAdapter implements ExecutionFinishedEventPort {
String server = "http://127.0.0.1:8082";
@Value("${roster.url}")
String server;
Logger logger = Logger.getLogger(ExecutionFinishedEventAdapter.class.getName());
@@ -37,10 +39,11 @@ public class ExecutionFinishedEventAdapter implements ExecutionFinishedEventPort
try {
client.send(request, HttpResponse.BodyHandlers.ofString());
} catch (IOException | InterruptedException e) {
} catch (InterruptedException e) {
logger.log(Level.SEVERE, e.getLocalizedMessage(), e);
// Restore interrupted state...
Thread.currentThread().interrupt();
} catch (IOException e) {
logger.log(Level.SEVERE, e.getLocalizedMessage(), e);
}
System.out.println("Finish execution event sent with result:" + event.getResult());

View File

@@ -1,4 +1,4 @@
package ch.unisg.executorBase.executor.adapter.out.web;
package ch.unisg.executorbase.executor.adapter.out.web;
import java.io.IOException;
import java.net.URI;
@@ -8,12 +8,14 @@ import java.net.http.HttpResponse;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;
import ch.unisg.executorBase.executor.application.port.out.GetAssignmentPort;
import ch.unisg.executorBase.executor.domain.ExecutorType;
import ch.unisg.executorBase.executor.domain.Task;
import ch.unisg.common.valueobject.ExecutorURI;
import ch.unisg.executorbase.executor.application.port.out.GetAssignmentPort;
import ch.unisg.executorbase.executor.domain.ExecutorType;
import ch.unisg.executorbase.executor.domain.Task;
import org.json.JSONObject;
@@ -21,17 +23,17 @@ import org.json.JSONObject;
@Primary
public class GetAssignmentAdapter implements GetAssignmentPort {
String server = "http://127.0.0.1:8082";
@Value("${roster.url}")
String server;
Logger logger = Logger.getLogger(GetAssignmentAdapter.class.getName());
@Override
public Task getAssignment(ExecutorType executorType, String ip, int port) {
public Task getAssignment(ExecutorType executorType, ExecutorURI executorURI) {
String body = new JSONObject()
.put("executorType", executorType)
.put("ip", ip)
.put("port", port)
.put("executorURI", executorURI.getValue())
.toString();
HttpClient client = HttpClient.newHttpClient();
@@ -49,10 +51,11 @@ public class GetAssignmentAdapter implements GetAssignmentPort {
return new Task(new JSONObject(response.body()).getString("taskID"));
} catch (IOException | InterruptedException e) {
} catch (InterruptedException e) {
logger.log(Level.SEVERE, e.getLocalizedMessage(), e);
// Restore interrupted state...
Thread.currentThread().interrupt();
} catch (IOException e) {
logger.log(Level.SEVERE, e.getLocalizedMessage(), e);
}
return null;

View File

@@ -1,4 +1,4 @@
package ch.unisg.executorBase.executor.adapter.out.web;
package ch.unisg.executorbase.executor.adapter.out.web;
import java.io.IOException;
import java.net.URI;
@@ -9,28 +9,30 @@ import java.util.logging.Level;
import java.util.logging.Logger;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Primary;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import ch.unisg.executorBase.executor.application.port.out.NotifyExecutorPoolPort;
import ch.unisg.executorBase.executor.domain.ExecutorType;
import ch.unisg.common.valueobject.ExecutorURI;
import ch.unisg.executorbase.executor.application.port.out.NotifyExecutorPoolPort;
import ch.unisg.executorbase.executor.domain.ExecutorType;
@Component
@Primary
public class NotifyExecutorPoolAdapter implements NotifyExecutorPoolPort {
String server = "http://127.0.0.1:8083";
@Value("${executor-pool.url}")
String server;
Logger logger = Logger.getLogger(NotifyExecutorPoolAdapter.class.getName());
@Override
public boolean notifyExecutorPool(String ip, int port, ExecutorType executorType) {
public boolean notifyExecutorPool(ExecutorURI executorURI, ExecutorType executorType) {
String body = new JSONObject()
.put("executorTaskType", executorType)
.put("executorIp", ip)
.put("executorPort", Integer.toString(port))
.put("executorURI", executorURI.getValue())
.toString();
HttpClient client = HttpClient.newHttpClient();
@@ -45,10 +47,11 @@ public class NotifyExecutorPoolAdapter implements NotifyExecutorPoolPort {
if (response.statusCode() == HttpStatus.CREATED.value()) {
return true;
}
} catch (IOException | InterruptedException e) {
} catch (InterruptedException e) {
logger.log(Level.SEVERE, e.getLocalizedMessage(), e);
// Restore interrupted state...
Thread.currentThread().interrupt();
} catch (IOException e) {
logger.log(Level.SEVERE, e.getLocalizedMessage(), e);
}
return false;

View File

@@ -1,10 +1,9 @@
package ch.unisg.executorBase.executor.application.port.in;
import ch.unisg.executorBase.common.SelfValidating;
import ch.unisg.executorBase.executor.domain.ExecutorType;
package ch.unisg.executorbase.executor.application.port.in;
import javax.validation.constraints.NotNull;
import ch.unisg.common.validation.SelfValidating;
import ch.unisg.executorbase.executor.domain.ExecutorType;
import lombok.Value;
@Value

View File

@@ -1,4 +1,4 @@
package ch.unisg.executorBase.executor.application.port.in;
package ch.unisg.executorbase.executor.application.port.in;
public interface TaskAvailableUseCase {
void newTaskAvailable(TaskAvailableCommand command);

View File

@@ -1,6 +1,6 @@
package ch.unisg.executorBase.executor.application.port.out;
package ch.unisg.executorbase.executor.application.port.out;
import ch.unisg.executorBase.executor.domain.ExecutionFinishedEvent;
import ch.unisg.executorbase.executor.domain.ExecutionFinishedEvent;
public interface ExecutionFinishedEventPort {
void publishExecutionFinishedEvent(ExecutionFinishedEvent event);

View File

@@ -1,8 +1,9 @@
package ch.unisg.executorBase.executor.application.port.out;
package ch.unisg.executorbase.executor.application.port.out;
import ch.unisg.executorBase.executor.domain.ExecutorType;
import ch.unisg.executorBase.executor.domain.Task;
import ch.unisg.common.valueobject.ExecutorURI;
import ch.unisg.executorbase.executor.domain.ExecutorType;
import ch.unisg.executorbase.executor.domain.Task;
public interface GetAssignmentPort {
Task getAssignment(ExecutorType executorType, String ip, int port);
Task getAssignment(ExecutorType executorType, ExecutorURI executorURI);
}

View File

@@ -1,7 +1,8 @@
package ch.unisg.executorBase.executor.application.port.out;
package ch.unisg.executorbase.executor.application.port.out;
import ch.unisg.executorBase.executor.domain.ExecutorType;
import ch.unisg.common.valueobject.ExecutorURI;
import ch.unisg.executorbase.executor.domain.ExecutorType;
public interface NotifyExecutorPoolPort {
boolean notifyExecutorPool(String ip, int port, ExecutorType executorType);
boolean notifyExecutorPool(ExecutorURI executorURI, ExecutorType executorType);
}

View File

@@ -1,7 +1,8 @@
package ch.unisg.executorBase.executor.application.service;
package ch.unisg.executorbase.executor.application.service;
import ch.unisg.executorBase.executor.application.port.out.NotifyExecutorPoolPort;
import ch.unisg.executorBase.executor.domain.ExecutorType;
import ch.unisg.common.valueobject.ExecutorURI;
import ch.unisg.executorbase.executor.application.port.out.NotifyExecutorPoolPort;
import ch.unisg.executorbase.executor.domain.ExecutorType;
import lombok.RequiredArgsConstructor;
@RequiredArgsConstructor
@@ -9,7 +10,7 @@ public class NotifyExecutorPoolService {
private final NotifyExecutorPoolPort notifyExecutorPoolPort;
public boolean notifyExecutorPool(String ip, int port, ExecutorType executorType) {
return notifyExecutorPoolPort.notifyExecutorPool(ip, port, executorType);
public boolean notifyExecutorPool(ExecutorURI executorURI, ExecutorType executorType) {
return notifyExecutorPoolPort.notifyExecutorPool(executorURI, executorType);
}
}

View File

@@ -1,9 +1,9 @@
package ch.unisg.executorBase.executor.application.service;
package ch.unisg.executorbase.executor.application.service;
import org.springframework.stereotype.Component;
import ch.unisg.executorBase.executor.application.port.in.TaskAvailableCommand;
import ch.unisg.executorBase.executor.application.port.in.TaskAvailableUseCase;
import ch.unisg.executorbase.executor.application.port.in.TaskAvailableCommand;
import ch.unisg.executorbase.executor.application.port.in.TaskAvailableUseCase;
import lombok.RequiredArgsConstructor;
import javax.transaction.Transactional;

View File

@@ -1,12 +1,12 @@
package ch.unisg.executorBase.executor.domain;
package ch.unisg.executorbase.executor.domain;
import lombok.Getter;
public class ExecutionFinishedEvent {
@Getter
private String taskID;
@Getter
private String result;

View File

@@ -1,26 +1,24 @@
package ch.unisg.executorBase.executor.domain;
package ch.unisg.executorbase.executor.domain;
import ch.unisg.executorBase.executor.application.port.out.ExecutionFinishedEventPort;
import ch.unisg.executorBase.executor.application.port.out.GetAssignmentPort;
import ch.unisg.executorBase.executor.application.port.out.NotifyExecutorPoolPort;
import ch.unisg.executorBase.executor.adapter.out.web.ExecutionFinishedEventAdapter;
import ch.unisg.executorBase.executor.adapter.out.web.GetAssignmentAdapter;
import ch.unisg.executorBase.executor.adapter.out.web.NotifyExecutorPoolAdapter;
import ch.unisg.executorBase.executor.application.service.NotifyExecutorPoolService;
import ch.unisg.common.exception.InvalidExecutorURIException;
import ch.unisg.common.valueobject.ExecutorURI;
import ch.unisg.executorbase.executor.adapter.out.web.ExecutionFinishedEventAdapter;
import ch.unisg.executorbase.executor.adapter.out.web.GetAssignmentAdapter;
import ch.unisg.executorbase.executor.adapter.out.web.NotifyExecutorPoolAdapter;
import ch.unisg.executorbase.executor.application.port.out.ExecutionFinishedEventPort;
import ch.unisg.executorbase.executor.application.port.out.GetAssignmentPort;
import ch.unisg.executorbase.executor.application.port.out.NotifyExecutorPoolPort;
import ch.unisg.executorbase.executor.application.service.NotifyExecutorPoolService;
import lombok.Getter;
public abstract class ExecutorBase {
@Getter
private String ip;
private ExecutorURI executorURI;
@Getter
private ExecutorType executorType;
@Getter
private int port;
@Getter
private ExecutorStatus status;
@@ -34,12 +32,17 @@ public abstract class ExecutorBase {
public ExecutorBase(ExecutorType executorType) {
System.out.println("Starting Executor");
// TODO set this automaticly
this.ip = "localhost";
this.port = 8084;
try {
this.executorURI = new ExecutorURI("localhost:8084");
} catch (InvalidExecutorURIException e) {
// Shutdown system if ip or port is not valid
System.exit(1);
}
this.executorType = executorType;
this.status = ExecutorStatus.STARTING_UP;
if(!notifyExecutorPoolService.notifyExecutorPool(this.ip, this.port, this.executorType)) {
if(!notifyExecutorPoolService.notifyExecutorPool(this.executorURI, this.executorType)) {
System.exit(0);
} else {
this.status = ExecutorStatus.IDLING;
@@ -48,8 +51,7 @@ public abstract class ExecutorBase {
}
public void getAssignment() {
Task newTask = getAssignmentPort.getAssignment(this.getExecutorType(), this.getIp(),
this.getPort());
Task newTask = getAssignmentPort.getAssignment(this.getExecutorType(), this.getExecutorURI());
if (newTask != null) {
this.executeTask(newTask);
} else {

View File

@@ -1,4 +1,4 @@
package ch.unisg.executorBase.executor.domain;
package ch.unisg.executorbase.executor.domain;
public enum ExecutorStatus {
STARTING_UP,

View File

@@ -1,4 +1,4 @@
package ch.unisg.executorBase.executor.domain;
package ch.unisg.executorbase.executor.domain;
public enum ExecutorType {
ADDITION, ROBOT;

View File

@@ -1,4 +1,4 @@
package ch.unisg.executorBase.executor.domain;
package ch.unisg.executorbase.executor.domain;
import lombok.Getter;
import lombok.Setter;

View File

@@ -1 +1,6 @@
server.port=8081
roster.url=http://127.0.0.1:8082
executor-pool.url=http://127.0.0.1:8083
executor1.url=http://127.0.0.1:8084
executor2.url=http://127.0.0.1:8085
task-list.url=http://127.0.0.1:8081