Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaFX : Supply arguments to Task

Tags:

java

task

javafx

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.

like image 567
We are Borg Avatar asked Mar 02 '16 14:03

We are Borg


2 Answers

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.

like image 70
Brendon Dugan Avatar answered Oct 17 '22 14:10

Brendon Dugan


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;
    }
}
like image 33
Dmitry Ginzburg Avatar answered Oct 17 '22 13:10

Dmitry Ginzburg