use json patch

This commit is contained in:
Marcel 2021-12-22 13:04:03 +01:00
parent 56788d7150
commit 1153b75322
6 changed files with 88 additions and 37 deletions

View File

@ -33,12 +33,11 @@ public abstract class ExecutorBase {
Logger logger = Logger.getLogger(ExecutorBase.class.getName());
protected ExecutorBase(ExecutorType executorType) {
protected ExecutorBase(ExecutorType executorType, String uri) {
logger.info("ExecutorBase | Starting Executor");
this.status = ExecutorStatus.STARTING_UP;
this.executorType = executorType;
// TODO set this automaticly
this.executorURI = new ExecutorURI("http://localhost:8084");
this.executorURI = new ExecutorURI(uri);
// TODO do this in main
// Notify executor-pool about existence. If executor-pools response is successfull start with getting an assignment, else shut down executor.
logger.info("ExecutorBase | Notifying executor-pool about existens");

View File

@ -1,7 +1,6 @@
package ch.unisg.executorcomputation.executor.domain;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.script.ScriptEngine;
@ -15,14 +14,14 @@ public class Executor extends ExecutorBase {
private static Logger executorLogger = Logger.getLogger(Executor.class.getName());
private static final Executor executor = new Executor(ExecutorType.COMPUTATION);
private static final Executor executor = new Executor(ExecutorType.COMPUTATION, "http://localhost:8085");
public static Executor getExecutor() {
return executor;
}
private Executor(ExecutorType executorType) {
super(executorType);
private Executor(ExecutorType executorType, String uri) {
super(executorType, uri);
}
@Override

View File

@ -4,17 +4,16 @@ import ch.unisg.executorbase.executor.domain.ExecutorBase;
import ch.unisg.executorbase.executor.domain.ExecutorType;
import ch.unisg.executorhumidity.executor.adapter.out.GetHumidityAdapter;
import ch.unisg.executorhumidity.executor.application.port.out.GetHumidityPort;
import org.eclipse.californium.elements.exception.ConnectorException;
import java.io.IOException;
public class Executor extends ExecutorBase {
private static final Executor executor = new Executor(ExecutorType.HUMIDITY);
private static final Executor executor = new Executor(ExecutorType.HUMIDITY, "http://localhost:8087");
private final GetHumidityPort getHumidityPort = new GetHumidityAdapter();
private Executor(ExecutorType executorType) {super(executorType);}
private Executor(ExecutorType executorType, String uri) {
super(executorType, uri);
}
public static Executor getExecutor() {return executor;}
@ -22,8 +21,6 @@ public class Executor extends ExecutorBase {
@Override
protected
String execution(String input) {
//TODO: Fill
String result = getHumidityPort.getHumidity();
return result;

View File

@ -7,15 +7,15 @@ import ch.unisg.executorbase.executor.domain.ExecutorType;
public class Executor extends ExecutorBase {
private static final Executor executor = new Executor(ExecutorType.SMALLROBOT);
private static final Executor executor = new Executor(ExecutorType.SMALLROBOT, "http://localhost:8084");
private final UserToRobotPort userToRobotPort = new UserToRobotAdapter();
public static Executor getExecutor() {
return executor;
}
private Executor(ExecutorType executorType) {
super(executorType);
private Executor(ExecutorType executorType, String uri) {
super(executorType, uri);
}
@Override

View File

@ -8,6 +8,7 @@ import java.net.http.HttpResponse;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Primary;
@ -32,19 +33,22 @@ public class PublishTaskAssignedEventAdapter implements TaskAssignedEventPort {
@Override
public void publishTaskAssignedEvent(TaskAssignedEvent event) {
String body = new JSONObject()
.put("taskId", event.taskID)
.toString();
try {
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(server + "/tasks/assignTask"))
JSONObject op1 = new JSONObject()
.put("op", "replace")
.put("path", "/taskStatus")
.put("value", "ASSIGNED");
String body = new JSONArray().put(op1).toString();
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(server + "/tasks/" + event.taskID))
.header("Content-Type", "application/task+json")
.POST(HttpRequest.BodyPublishers.ofString(body))
.method("PATCH", HttpRequest.BodyPublishers.ofString(body))
.build();
try {
client.send(request, HttpResponse.BodyHandlers.ofString());
} catch (InterruptedException e) {
logger.log(Level.SEVERE, e.getLocalizedMessage(), e);
@ -52,6 +56,28 @@ public class PublishTaskAssignedEventAdapter implements TaskAssignedEventPort {
} catch (IOException e) {
logger.log(Level.SEVERE, e.getLocalizedMessage(), e);
}
// String body = new JSONObject()
// .put("taskId", event.taskID)
// .toString();
// HttpClient client = HttpClient.newHttpClient();
// HttpRequest request = HttpRequest.newBuilder()
// .uri(URI.create(server + "/tasks/assignTask"))
// .header("Content-Type", "application/task+json")
// .POST(HttpRequest.BodyPublishers.ofString(body))
// .build();
// try {
// client.send(request, HttpResponse.BodyHandlers.ofString());
// } catch (InterruptedException e) {
// logger.log(Level.SEVERE, e.getLocalizedMessage(), e);
// Thread.currentThread().interrupt();
// } catch (IOException e) {
// logger.log(Level.SEVERE, e.getLocalizedMessage(), e);
// }
}
}

View File

@ -8,6 +8,7 @@ import java.net.http.HttpResponse;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Primary;
@ -32,21 +33,27 @@ public class PublishTaskCompletedEventAdapter implements TaskCompletedEventPort
@Override
public void publishTaskCompleted(TaskCompletedEvent event) {
String body = new JSONObject()
.put("taskId", event.taskID)
.put("status", event.status)
.put("outputData", event.result)
.toString();
try {
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(server + "/tasks/completeTask/"))
JSONObject op1 = new JSONObject()
.put("op", "replace")
.put("path", "/taskStatus")
.put("value", event.status);
JSONObject op2 = new JSONObject()
.put("op", "replace")
.put("path", "/outputData")
.put("value", event.result);
String body = new JSONArray().put(op1).put(op2).toString();
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(server + "/tasks/" + event.taskID))
.header("Content-Type", "application/task+json")
.POST(HttpRequest.BodyPublishers.ofString(body))
.method("PATCH", HttpRequest.BodyPublishers.ofString(body))
.build();
try {
client.send(request, HttpResponse.BodyHandlers.ofString());
} catch (InterruptedException e) {
logger.log(Level.SEVERE, e.getLocalizedMessage(), e);
@ -55,6 +62,29 @@ public class PublishTaskCompletedEventAdapter implements TaskCompletedEventPort
logger.log(Level.SEVERE, e.getLocalizedMessage(), e);
}
// String body = new JSONObject()
// .put("taskId", event.taskID)
// .put("status", event.status)
// .put("outputData", event.result)
// .toString();
// HttpClient client = HttpClient.newHttpClient();
// HttpRequest request = HttpRequest.newBuilder()
// .uri(URI.create(server + "/tasks/completeTask/"))
// .header("Content-Type", "application/task+json")
// .POST(HttpRequest.BodyPublishers.ofString(body))
// .build();
// try {
// client.send(request, HttpResponse.BodyHandlers.ofString());
// } catch (InterruptedException e) {
// logger.log(Level.SEVERE, e.getLocalizedMessage(), e);
// Thread.currentThread().interrupt();
// } catch (IOException e) {
// logger.log(Level.SEVERE, e.getLocalizedMessage(), e);
// }
}
}