I am working on a JavaFX project in which I am making network calls with Task. Unfortunately, I have not been able to find how I can pass arguments to it. I have searched many links, but none of them provides. One link from java2s claims they are passing, but the code does not reflect that.
As you can see from the code below, I am using a for-loop and passing the ID parameter of RestGroupAccount in the URL. This time it's okay because I anyways need all of the RestCanvas.
But I am interested in knowing how to give parameters to Task
Code :
private Task<List<RestCanvas>> fetchCanvases = new Task<List<RestCanvas>>() {
@Override
protected List<RestCanvas> call() throws Exception {
List<RestCanvas> list = new ArrayList<>();
try{
for(RestGroupAccount groupAccount : groupAccounts) {
RestTemplate rest = StaticRestTemplate.getRest();
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.add("Cookie", "JSESSIONID=" + StaticRestTemplate.jsessionid);
HttpEntity<RestCanvas> requestEntity = new HttpEntity<>(requestHeaders);
rest.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
rest.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
ResponseEntity<RestCanvas[]> responseEntity = rest.exchange(getCanvasForGroupAccount+groupAccount.getGroupId(), HttpMethod.GET, requestEntity, RestCanvas[].class);
RestCanvas[] restCanvasArray = responseEntity.getBody();
Collections.addAll(list, restCanvasArray);
}
}catch (Exception e){
e.printStackTrace();
}
return list;
}
};
If any more information is required, kindly let me know. Thank you.
I had a similar need, where I needed to create tasks that would take in a File and perform tasks on it. In my case I needed to do different things with files in multiple places, so I created the following class:
public abstract class FileOperationTask<V> extends Task<V> {
protected File file;
public FileOperationTask(File file) {
this.file = file;
}
}
This allows me in my controllers to define the following:
FileOperationTask<List<RaffleTicket>> task = new FileOperationTask<List<RaffleTicket>>(file){
@Override
protected List<RaffleTicket> call() throws Exception {
this.file.toString();
return null;
}
};
new Thread(task).run();
As you can see, I'm able to operate on the File object, and implementing my asynchronous task is now more or less identical to implementing a normal task.
If you need to use the code inside your Task more than once, you should consider creating non-anonymous subclass and instantiate it every time you need it with the construction parameter.
In your example this might be:
private Task<List<RestCanvas>> fetchCanvases = new MyTask(getCanvasForGroupAccount + groupAccount.getGroupId());
// ...
// please don't use this name :)
private static class MyTask extends Task<List<RestCanvas>> {
private final String id;
public MyTask(String id) {
this.id = id;
}
@Override
protected List<RestCanvas> call() throws Exception {
List<RestCanvas> list = new ArrayList<>();
try{
for(RestGroupAccount groupAccount : groupAccounts) {
RestTemplate rest = StaticRestTemplate.getRest();
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.add("Cookie", "JSESSIONID=" + StaticRestTemplate.jsessionid);
HttpEntity<RestCanvas> requestEntity = new HttpEntity<>(requestHeaders);
rest.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
rest.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
ResponseEntity<RestCanvas[]> responseEntity = rest.exchange(id, HttpMethod.GET, requestEntity, RestCanvas[].class);
RestCanvas[] restCanvasArray = responseEntity.getBody();
Collections.addAll(list, restCanvasArray);
}
}catch (Exception e){
e.printStackTrace();
}
return list;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With