Roster requests auction when an internal executor is missing

This commit is contained in:
rahimiankeanu 2021-12-12 20:26:14 +01:00
parent 32bf461026
commit 13b02aa115
7 changed files with 105 additions and 3 deletions

View File

@ -31,7 +31,7 @@ public class NewTaskController {
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());
boolean success = newTaskUseCase.addNewTaskToQueue(command);

View File

@ -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();
}
}
}

View File

@ -1,5 +1,7 @@
package ch.unisg.roster.roster.application.port.in;
import java.net.URI;
import javax.validation.constraints.NotNull;
import ch.unisg.roster.roster.domain.valueobject.ExecutorType;
@ -20,8 +22,12 @@ public class NewTaskCommand extends SelfValidating<NewTaskCommand> {
@NotNull
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.taskUri = taskUri;
this.taskType = taskType;
this.inputData = inputData;
this.validateSelf();

View File

@ -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);
}

View File

@ -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.NewTaskUseCase;
import ch.unisg.roster.roster.application.port.out.LaunchAuctionEventPort;
import ch.unisg.roster.roster.application.port.out.NewTaskEventPort;
import ch.unisg.roster.roster.domain.ExecutorRegistry;
import ch.unisg.roster.roster.domain.Roster;
import ch.unisg.roster.roster.domain.Task;
import ch.unisg.roster.roster.domain.event.LaunchAuctionEvent;
import ch.unisg.roster.roster.domain.event.NewTaskEvent;
import lombok.RequiredArgsConstructor;
@ -20,6 +22,8 @@ public class NewTaskService implements NewTaskUseCase {
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.
* 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();
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());

View File

@ -12,6 +12,9 @@ public class Task {
@Getter
private ExecutorType taskType;
@Getter
private String taskUri;
@Getter
@Setter
private String inputData;
@ -40,6 +43,12 @@ public class Task {
this.inputData = inputData;
}
public Task(String taskID, String taskUri, ExecutorType taskType) {
this.taskID = taskID;
this.taskUri = taskUri;
this.taskType = taskType;
}
public Task() {}
}

View File

@ -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;
}
}