Roster requests auction when an internal executor is missing
This commit is contained in:
parent
32bf461026
commit
13b02aa115
|
@ -31,7 +31,7 @@ public class NewTaskController {
|
||||||
|
|
||||||
logger.info("New task with id:" + task.getTaskID());
|
logger.info("New task with id:" + task.getTaskID());
|
||||||
|
|
||||||
NewTaskCommand command = new NewTaskCommand(task.getTaskID(), task.getTaskType(),
|
NewTaskCommand command = new NewTaskCommand(task.getTaskID(), task.getTaskUri(), task.getTaskType(),
|
||||||
task.getInputData());
|
task.getInputData());
|
||||||
|
|
||||||
boolean success = newTaskUseCase.addNewTaskToQueue(command);
|
boolean success = newTaskUseCase.addNewTaskToQueue(command);
|
||||||
|
|
|
@ -0,0 +1,58 @@
|
||||||
|
package ch.unisg.roster.roster.adapter.out.web;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
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.core.JsonProcessingException;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
|
||||||
|
import ch.unisg.roster.roster.application.port.out.LaunchAuctionEventPort;
|
||||||
|
import ch.unisg.roster.roster.domain.event.LaunchAuctionEvent;
|
||||||
|
|
||||||
|
public class LaunchAuctionEventAdapter implements LaunchAuctionEventPort {
|
||||||
|
|
||||||
|
@Value("${auction.house.uri}")
|
||||||
|
String server;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void launchAuctionEvent(LaunchAuctionEvent launchAuctionEvent) {
|
||||||
|
|
||||||
|
var values = new HashMap<String, String>() {{
|
||||||
|
|
||||||
|
put("taskUri", launchAuctionEvent.taskUri);
|
||||||
|
put("taskType", launchAuctionEvent.taskType.getValue());
|
||||||
|
|
||||||
|
}};
|
||||||
|
|
||||||
|
var objectMapper = new ObjectMapper();
|
||||||
|
String requestBody = null;
|
||||||
|
try {
|
||||||
|
requestBody = objectMapper.writeValueAsString(values);
|
||||||
|
} catch (JsonProcessingException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
HttpClient client = HttpClient.newHttpClient();
|
||||||
|
HttpRequest request = HttpRequest.newBuilder()
|
||||||
|
.uri(URI.create(server+"/auctions/"))
|
||||||
|
.header("Content-Type", "application/task+json")
|
||||||
|
.POST(HttpRequest.BodyPublishers.ofString(requestBody))
|
||||||
|
.build();
|
||||||
|
|
||||||
|
try {
|
||||||
|
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -1,5 +1,7 @@
|
||||||
package ch.unisg.roster.roster.application.port.in;
|
package ch.unisg.roster.roster.application.port.in;
|
||||||
|
|
||||||
|
import java.net.URI;
|
||||||
|
|
||||||
import javax.validation.constraints.NotNull;
|
import javax.validation.constraints.NotNull;
|
||||||
|
|
||||||
import ch.unisg.roster.roster.domain.valueobject.ExecutorType;
|
import ch.unisg.roster.roster.domain.valueobject.ExecutorType;
|
||||||
|
@ -20,8 +22,12 @@ public class NewTaskCommand extends SelfValidating<NewTaskCommand> {
|
||||||
@NotNull
|
@NotNull
|
||||||
private final String inputData;
|
private final String inputData;
|
||||||
|
|
||||||
public NewTaskCommand(String taskID, ExecutorType taskType, String inputData) {
|
@NotNull
|
||||||
|
private final String taskUri;
|
||||||
|
|
||||||
|
public NewTaskCommand(String taskID, String taskUri, ExecutorType taskType, String inputData) {
|
||||||
this.taskID = taskID;
|
this.taskID = taskID;
|
||||||
|
this.taskUri = taskUri;
|
||||||
this.taskType = taskType;
|
this.taskType = taskType;
|
||||||
this.inputData = inputData;
|
this.inputData = inputData;
|
||||||
this.validateSelf();
|
this.validateSelf();
|
||||||
|
|
|
@ -0,0 +1,8 @@
|
||||||
|
package ch.unisg.roster.roster.application.port.out;
|
||||||
|
|
||||||
|
import ch.unisg.roster.roster.domain.event.LaunchAuctionEvent;
|
||||||
|
|
||||||
|
public interface LaunchAuctionEventPort {
|
||||||
|
|
||||||
|
void launchAuctionEvent(LaunchAuctionEvent launchAuctionEvent);
|
||||||
|
}
|
|
@ -6,10 +6,12 @@ import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
import ch.unisg.roster.roster.application.port.in.NewTaskCommand;
|
import ch.unisg.roster.roster.application.port.in.NewTaskCommand;
|
||||||
import ch.unisg.roster.roster.application.port.in.NewTaskUseCase;
|
import ch.unisg.roster.roster.application.port.in.NewTaskUseCase;
|
||||||
|
import ch.unisg.roster.roster.application.port.out.LaunchAuctionEventPort;
|
||||||
import ch.unisg.roster.roster.application.port.out.NewTaskEventPort;
|
import ch.unisg.roster.roster.application.port.out.NewTaskEventPort;
|
||||||
import ch.unisg.roster.roster.domain.ExecutorRegistry;
|
import ch.unisg.roster.roster.domain.ExecutorRegistry;
|
||||||
import ch.unisg.roster.roster.domain.Roster;
|
import ch.unisg.roster.roster.domain.Roster;
|
||||||
import ch.unisg.roster.roster.domain.Task;
|
import ch.unisg.roster.roster.domain.Task;
|
||||||
|
import ch.unisg.roster.roster.domain.event.LaunchAuctionEvent;
|
||||||
import ch.unisg.roster.roster.domain.event.NewTaskEvent;
|
import ch.unisg.roster.roster.domain.event.NewTaskEvent;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
|
|
||||||
|
@ -20,6 +22,8 @@ public class NewTaskService implements NewTaskUseCase {
|
||||||
|
|
||||||
private final NewTaskEventPort newTaskEventPort;
|
private final NewTaskEventPort newTaskEventPort;
|
||||||
|
|
||||||
|
private final LaunchAuctionEventPort launchAuctionEventPort;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Checks if we can execute the give task, if yes the task gets added to the task queue and return true.
|
* Checks if we can execute the give task, if yes the task gets added to the task queue and return true.
|
||||||
* If the task can not be executed by an internal or auction house executor, the method return false.
|
* If the task can not be executed by an internal or auction house executor, the method return false.
|
||||||
|
@ -31,7 +35,10 @@ public class NewTaskService implements NewTaskUseCase {
|
||||||
ExecutorRegistry executorRegistry = ExecutorRegistry.getInstance();
|
ExecutorRegistry executorRegistry = ExecutorRegistry.getInstance();
|
||||||
|
|
||||||
if (!executorRegistry.containsTaskType(command.getTaskType())) {
|
if (!executorRegistry.containsTaskType(command.getTaskType())) {
|
||||||
return false;
|
LaunchAuctionEvent launchAuctionEvent = new LaunchAuctionEvent( command.getTaskUri(),
|
||||||
|
command.getTaskType());
|
||||||
|
launchAuctionEventPort.launchAuctionEvent(launchAuctionEvent);
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
Task task = new Task(command.getTaskID(), command.getTaskType(), command.getInputData());
|
Task task = new Task(command.getTaskID(), command.getTaskType(), command.getInputData());
|
||||||
|
|
|
@ -12,6 +12,9 @@ public class Task {
|
||||||
@Getter
|
@Getter
|
||||||
private ExecutorType taskType;
|
private ExecutorType taskType;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
private String taskUri;
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
@Setter
|
@Setter
|
||||||
private String inputData;
|
private String inputData;
|
||||||
|
@ -40,6 +43,12 @@ public class Task {
|
||||||
this.inputData = inputData;
|
this.inputData = inputData;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Task(String taskID, String taskUri, ExecutorType taskType) {
|
||||||
|
this.taskID = taskID;
|
||||||
|
this.taskUri = taskUri;
|
||||||
|
this.taskType = taskType;
|
||||||
|
}
|
||||||
|
|
||||||
public Task() {}
|
public Task() {}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,14 @@
|
||||||
|
package ch.unisg.roster.roster.domain.event;
|
||||||
|
|
||||||
|
import ch.unisg.roster.roster.domain.valueobject.ExecutorType;
|
||||||
|
|
||||||
|
public class LaunchAuctionEvent {
|
||||||
|
public final String taskUri;
|
||||||
|
public final ExecutorType taskType;
|
||||||
|
|
||||||
|
public LaunchAuctionEvent(String taskUri, ExecutorType taskType) {
|
||||||
|
this.taskUri = taskUri;
|
||||||
|
this.taskType = taskType;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user