Top Description Inners Fields Constructors Methods
com.dlsc.retrofitfx

public final Class ServiceInvocation<T>

extends Object
implements Worker<T>
Class Inheritance
All Implemented Interfaces
javafx.concurrent.Worker
Type Parameters
<T>
the type of the result object wrapped inside the retrofit response
Imports
javafx.application.Platform, javafx.beans.property.*, javafx.concurrent.Worker, okhttp3.ResponseBody, org.apache.logging.log4j.LogManager, .Logger, retrofit2.Response, java.time.Duration, .Instant, java.util.HashMap, .Map, .Objects, java.util.concurrent.CompletableFuture, .ExecutionException, .Executor, .Executors, java.util.function.BiConsumer, .Consumer

A utility class used to invoke backend services via Retrofit on a separate thread and to handle the response on the UI thread. This class makes it extremely easy to do this, and using its results in very compact and nicely structured code (also thanks to a fluent API).

Example 1:

In this example we assume that "myService.loadData()" is a Retrofit call. Executing it returns a Retrofit "Response" object. Hence, this lambda implements the "ServiceSupplier" interface required by the service invocation.
        ServiceInvocation.create("Load data ...", () -> myService.loadData().execute())
             .onSuccess(data -> listView.getItems().setAll(data)).execute();
    

One can see that in this fluent API the last call goes to the service invocation's execute() method. However, it is advised to execute service invocations in a central place within the application's user interface codebase. This makes it easier to register default error handlers and to provide feedback to the user regarding the status of the execution of a service invocation. Example 2 shows how this is done.

Example 2:

        ui.execute(ServiceInvocation.create("Load data ...", () -> myService.loadData().execute())
             .onSuccess(data -> listView.getItems().setAll(data)));
    

Various consumers can be registered with a service invocation before it gets executed. Some of these consumers have a second version with the postfix "default" to their name, e.g. "onFailure" and "onFailureDefault". The idea behind this is that an application might provide a central place where service invocations are being executed (e.g. Window.execute(si) or Workbench.execute(si)) and wants to set "default" handlers in that location. At the same time each specific occurrence of a service invocation might require its own / more specific handlers.

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static interface
ServiceInvocation.ServiceSupplier<
the result type
T
>

A functional supplier interface used for providing the response object of a retrofit call.

Field Summary

Modifier and TypeField and Description
private boolean
private long
private final ReadOnlyObjectWrapper<Throwable>
private static final Executor
private final Logger
private final ReadOnlyStringWrapper
private final String
private BiConsumer<String, HttpStatusCode>
private BiConsumer<String, HttpStatusCode>
private BiConsumer<String, Exception>
private BiConsumer<String, Exception>
private BiConsumer<String, String>
private BiConsumer<String, String>
private BiConsumer<String, Response<T>>
private BiConsumer<String, Response<T>>
private Runnable
private Runnable
private Consumer<String>
private Consumer<String>
private Map<HttpStatusCode, BiConsumer<String, String>>
private Map<HttpStatusCode, BiConsumer<String, String>>
private Consumer<T>
private Consumer<Response<T>>
private final ReadOnlyDoubleWrapper
private final ReadOnlyBooleanWrapper
private final ServiceInvocation.ServiceSupplier<T>
private boolean
private final ReadOnlyObjectWrapper<Worker.State>
private final ReadOnlyStringWrapper
private final ReadOnlyDoubleWrapper
private final ReadOnlyObjectWrapper<T>
private final ReadOnlyDoubleWrapper

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public boolean

Returns:

always true
cancel
()

Implements javafx.concurrent.Worker.cancel.

Marks the invocation as cancelled.

public static <
the type of the response object
T
>
ServiceInvocation<T>

Returns:

a service invocation
create
(String
the name of this invocation
name
,
ServiceInvocation.ServiceSupplier<T>
the supplier returning the service for invocation
supplier
)

Creates a new service invocation instance.

private void
private void
private void
public ReadOnlyObjectProperty<Throwable>
exceptionProperty()

Implements javafx.concurrent.Worker.exceptionProperty.

Gets the ReadOnlyObjectProperty representing any exception which occurred.

public CompletableFuture<T>

Returns:

a completable future object usable for chaining
execute
()

Executes the service invocation with the default executor.

public CompletableFuture<T>

Returns:

a completable future object usable for chaining
execute
(Executor executor)

Executes the service invocation with the given executor.

private void
failure(Response<T> response, String errorBody)

public Throwable
getException()

Implements javafx.concurrent.Worker.getException.

Indicates the exception which occurred while the Worker was running, if any.

public String
getMessage()

Implements javafx.concurrent.Worker.getMessage.

Gets a message associated with the current state of this Worker.

public String

Returns:

the name
getName
()

Returns the name of the service invocation.

private BiConsumer<String, Exception>
private BiConsumer<String, String>
private BiConsumer<String, Response<T>>
private BiConsumer<String, String>
public double
getProgress()

Implements javafx.concurrent.Worker.getProgress.

Indicates the current progress of this Worker in terms of percent complete.

public Worker.State

Returns:

the state of the service invocation according to the Worker interface
getState
()

Implements javafx.concurrent.Worker.getState.

Returns the worker state of the service invocation (running, succeeded, ...).

public String
getTitle()

Implements javafx.concurrent.Worker.getTitle.

An optional title that should be associated with this Worker.

public double
getTotalWork()

Implements javafx.concurrent.Worker.getTotalWork.

Indicates a maximum value for the workDoneProperty property.

public T
getValue()

Implements javafx.concurrent.Worker.getValue.

Specifies the value, or result, of this Worker.

public double
getWorkDone()

Implements javafx.concurrent.Worker.getWorkDone.

Indicates the current amount of work that has been completed.

public boolean

Returns:

true or false depending on whether the invocation has been cancelled or not
isCancelled
()

Returns true if the application has invoked the cancel method.

public boolean
isRunning()

Implements javafx.concurrent.Worker.isRunning.

True if the state is either SCHEDULED or RUNNING.

public boolean

Returns:

true if the invocation will fail
isSimulatingFailure
()

Returns true if the service invocation will intentionally fail (see also withSimulatingFailure(boolean)).

public ReadOnlyStringProperty
messageProperty()

Implements javafx.concurrent.Worker.messageProperty.

Gets the ReadOnlyStringProperty representing the message.

public ServiceInvocation<T>

Returns:

the service invocation
onAnyStatusCode
(BiConsumer<String, HttpStatusCode>
the "on any status code" handler.
onAnyStatusCode
)

A consumer that will be invoked when the backend service invocation was not successful.

public ServiceInvocation<T>

Returns:

the service invocation
onAnyStatusCodeDefault
(BiConsumer<String, HttpStatusCode>
the "on any status code default" handler.
onAnyStatusCodeDefault
)

A consumer that will be invoked when the backend service invocation was not successful.

public ServiceInvocation<T>

Returns:

the service invocation
onException
(BiConsumer<String, Exception>
the consumer
onException
)

A consumer that will be invoked in case of an exception during the execution of the service invocation.

public ServiceInvocation<T>

Returns:

the service invocation
onExceptionDefault
(BiConsumer<String, Exception>
the consumer
onExceptionDefault
)

A default consumer that will be invoked in case of an exception during the execution of the service invocation.

public ServiceInvocation<T>

Returns:

the service invocation
onFailure
(BiConsumer<String, String>
the "on failure" handler.
onFailure
)

A consumer that will be invoked when the backend service invocation was not successful.

public ServiceInvocation<T>

Returns:

the service invocation
onFailureDefault
(BiConsumer<String, String>
the "on failure default" handler.
onFailureDefault
)

A default consumer that will be invoked when the backend service invocation was not successful.

public ServiceInvocation<T>

Returns:

the service invocation
onFailureDetailed
(BiConsumer<String, Response<T>>
the "on failure detailed" handler.
onFailureDetailed
)

A consumer that will be invoked when the backend service invocation was not successful.

public ServiceInvocation<T>

Returns:

the service invocation
onFailureDetailedDefault
(BiConsumer<String, Response<T>>
the "on failure detailed default" handler.
onFailureDetailedDefault
)

A default consumer that will be invoked when the backend service invocation was not successful.

public ServiceInvocation<T>

Returns:

the service invocation
onFinally
(Runnable
the runnable
onFinally
)

A runnable that will be invoked after the service invocation has completed.

public ServiceInvocation<T>

Returns:

the service invocation
onFinallyDefault
(Runnable
the runnable
onFinallyDefault
)

A runnable that will be invoked after the service invocation has completed.

public ServiceInvocation<T>

Returns:

the service invocation
onStart
(Consumer<String>
the "on start" handler.
onStart
)

A consumer that will be invoked first when the execute() method gets invoked.

public ServiceInvocation<T>

Returns:

the service invocation
onStartDefault
(Consumer<String>
the "on start default" handler.
onStartDefault
)

A consumer that will be invoked first when the execute() method gets invoked.

public ServiceInvocation<T>

Returns:

the service invocation
onStatusCode
(HttpStatusCode
the status code for which the consumer will be registered
code
,
BiConsumer<String, String>
the "on status code" handler.
onStatusCode
)

A consumer that will be invoked when the backend returns the given HTTP status code.

public ServiceInvocation<T>

Returns:

the service invocation
onStatusCodeDefault
(HttpStatusCode
the status code for which the consumer will be registered
code
,
BiConsumer<String, String>
the "on status code" handler.
onStatusCodeDefault
)

A default consumer that will be invoked when the backend returns the given HTTP status code.

public ServiceInvocation<T>

Returns:

the service invocation
onSuccess
(Consumer<T>
the "on success" handler.
onSuccess
)

A consumer that will be invoked when the backend service invocation was successful.

public ServiceInvocation<T>

Returns:

the service invocation
onSuccessDetailed
(Consumer<Response<T>>
the "on success detailed" handler.
onSuccessDetailed
)

A consumer that will be invoked when the backend service invocation was successful.

public ReadOnlyDoubleProperty
progressProperty()

Implements javafx.concurrent.Worker.progressProperty.

Gets the ReadOnlyDoubleProperty representing the progress.

private void
runAndWait(Runnable runnable)

public ReadOnlyBooleanProperty
runningProperty()

Implements javafx.concurrent.Worker.runningProperty.

Gets the ReadOnlyBooleanProperty representing whether the Worker is running.

public ReadOnlyObjectProperty<Worker.State>

Returns:

the state property
stateProperty
()

Implements javafx.concurrent.Worker.stateProperty.

Returns a read-only property for observing the state of the service invocation.

private void
success(Response<T> response)

public ReadOnlyStringProperty
titleProperty()

Implements javafx.concurrent.Worker.titleProperty.

Gets the ReadOnlyStringProperty representing the title.

public ReadOnlyDoubleProperty
totalWorkProperty()

Implements javafx.concurrent.Worker.totalWorkProperty.

Gets the ReadOnlyDoubleProperty representing the maximum amount of work that needs to be done.

public ReadOnlyObjectProperty<T>
valueProperty()

Implements javafx.concurrent.Worker.valueProperty.

Gets the ReadOnlyObjectProperty representing the value.

public ServiceInvocation<T>

Returns:

the service invocation
withDelay
(long
the delay in milliseconds
delay
)

Introduces an artificial delay for the invocation.

public ServiceInvocation<T>

Returns:

the service invocation
withSimulatingFailure
(boolean
enables / disables the failure
failure
)

An easy way to explicitly make the service invocation fail in order to test the handling of failures by the application.

public ReadOnlyDoubleProperty
workDoneProperty()

Implements javafx.concurrent.Worker.workDoneProperty.

Gets the ReadOnlyDoubleProperty representing the current progress.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait