Shifted RequestManager to Service

This commit is contained in:
Rohit Awate 2018-01-28 18:47:02 +05:30
parent 03c6b7c048
commit 8189de624d
4 changed files with 67 additions and 60 deletions

View file

@ -74,13 +74,15 @@ public class DashboardController implements Initializable {
try {
String address = addressField.getText();
if (address.equals("")) {
snackBar.show("Please enter a valid address", 7000);
snackBar.show("Please enter a valid address.", 7000);
return;
}
switch (httpMethodBox.getValue()) {
case "GET":
if (requestManager == null)
requestManager = new GETRequestManager();
GETRequest getRequest = new GETRequest(addressField.getText());
requestManager = new GETRequestManager(getRequest);
requestManager.setRequest(getRequest);
cancelButton.setOnAction(e -> requestManager.cancel());
requestManager.setOnRunning(e -> {
responseArea.clear();
@ -89,13 +91,14 @@ public class DashboardController implements Initializable {
requestManager.setOnSucceeded(e -> {
updateDashboard(requestManager.getValue());
loadingLayer.setVisible(false);
requestManager.reset();
});
requestManager.setOnCancelled(e -> {
loadingLayer.setVisible(false);
snackBar.show("Request canceled", 5000);
snackBar.show("Request canceled.", 5000);
requestManager.reset();
});
Thread taskThread = new Thread(requestManager);
taskThread.start();
requestManager.start();
break;
default:
loadingLayer.setVisible(false);

View file

@ -20,7 +20,7 @@ import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
abstract class RestaurantRequest {
public abstract class RestaurantRequest {
private URL target;
private HashMap<String, String> headers;

View file

@ -19,20 +19,18 @@ package com.rohitawate.restaurant.requestsmanager;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.rohitawate.restaurant.models.requests.GETRequest;
import com.rohitawate.restaurant.models.responses.RestaurantResponse;
import javafx.concurrent.Task;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Response;
import java.io.IOException;
public class GETRequestManager extends RequestManager {
private GETRequest request;
public GETRequestManager(GETRequest request) {
this.request = request;
}
@Override
protected Task<RestaurantResponse> createTask() {
return new Task<RestaurantResponse>() {
@Override
protected RestaurantResponse call() throws Exception {
RestaurantResponse response = new RestaurantResponse();
@ -89,4 +87,6 @@ public class GETRequestManager extends RequestManager {
return response;
}
};
}
}

View file

@ -15,18 +15,22 @@
*/
package com.rohitawate.restaurant.requestsmanager;
import com.rohitawate.restaurant.models.requests.RestaurantRequest;
import com.rohitawate.restaurant.models.responses.RestaurantResponse;
import javafx.concurrent.Task;
import javafx.concurrent.Service;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
public abstract class RequestManager extends Task<RestaurantResponse> {
public abstract class RequestManager extends Service<RestaurantResponse> {
final Client client;
RestaurantRequest request;
RequestManager() {
client = ClientBuilder.newClient();
}
public void setRequest(RestaurantRequest request) {
this.request = request;
}
}