Using CompletableFuture in your Eclipse RCP application

If you want to update your RCP application asynchronously you can use Java 8 CompletableFutures. For example the following starts an CompletableFuture and uses the getData method to read the data and calls afterwards the updateTable method.

[code]

button.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
CompletableFuture.supplyAsync(SamplePart.this::getData).
thenAccept(SamplePart.this::updateTable);
}
});

public List<String> getData() {

// fake slow operation
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
List<String> myList = Arrays.asList("1", "2", "3", "4", "5");
return myList;
}

public void updateTable(List<String> list) {
current.asyncExec(new Runnable() {
@Override
public void run() {
tableViewer.setInput(list);
tableViewer.refresh();
}
});
}
[/code]

I would be nice if CompletableFuture could run the thenAccept in the SWT Display thread similar to the schedule rules in RxJava for Android. but I have not found a way do do that. Suggestions are welcome. 🙂

About Lars Vogel

Lars Vogel is the founder and CEO of the vogella GmbH and works as Eclipse and Android consultant, trainer and book author. He is a regular speaker at international conferences,

He is the primary author of vogella.com. With more than one million visitors per month this website is one of the central sources for Java, Eclipse and Android programming information.

This entry was posted in Eclipse, Lars Vogel and tagged . Bookmark the permalink.

7 Responses to Using CompletableFuture in your Eclipse RCP application

  1. Lars Vogel says:

    No I didn’t know that. Looks pretty cool. Should be port that service to platform?

  2. Thibault says:

    Hey,

    I haven’t used RxJava for android, but you can easily integrate Vertx RxJava [1] in your rcp application. They might do the same purpose.

    I have integrated VertX Websocket into a E4 app [2]

    [1] http://vertx.io/docs/vertx-rx/java/
    [2] http://github.com/thibaultleouay/E4WebSocketChat

    Best,

    Thibault

  3. Max says:

    The answer to the original question is probably to use the overloaded version of thenAcceptAsync that takes an Executor and implement an Executor that simply forwards to the Display#asyncExec method.

  4. Ned Twigg says:

    DurianSwt can do this:
    https://github.com/diffplug/durian-swt

    SwtExec.async().guardOn(widget).subscribe(completableFuture, onCompleted -> {
    // some action that runs on the SWT thread,
    // future gets cancelled if widget is disposed before the future completes
    });

  5. Scott Lewis says:

    One additional thought: One great use of CompletableFuture within Eclipse is in using Remote Services….where it’s much more likely that the remote service will block (and of course blocking the UI thread is bad). For example, here’s a tutorial that shows the utility of ECF’s creation of asynchronous remote service proxies for interacting with Raspberry Pi-based services

  6. Lars Vogel says:

    Thanks everyone for the super interesting pointers to all these different technologies. Lots for me to read up in the future.

Comments are closed.