java.lang.Object
com.dlsc.gemsfx.util.ServiceInvocation<T>
- Type Parameters:
T
- the type of the result object wrapped inside the retrofit response
- All Implemented Interfaces:
javafx.concurrent.Worker<T>
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 it 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.
-
Property Summary
TypePropertyDescriptionjavafx.beans.property.ReadOnlyObjectProperty<Throwable>
javafx.beans.property.ReadOnlyStringProperty
javafx.beans.property.ReadOnlyDoubleProperty
javafx.beans.property.ReadOnlyBooleanProperty
javafx.beans.property.ReadOnlyObjectProperty<javafx.concurrent.Worker.State>
Returns a read-only property for observing the state of the service invocation.javafx.beans.property.ReadOnlyStringProperty
javafx.beans.property.ReadOnlyDoubleProperty
javafx.beans.property.ReadOnlyObjectProperty<T>
javafx.beans.property.ReadOnlyDoubleProperty
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic interface
A functional supplier interface used for providing the response object of a retrofit call.Nested classes/interfaces inherited from interface javafx.concurrent.Worker
javafx.concurrent.Worker.State
-
Method Summary
Modifier and TypeMethodDescriptionboolean
cancel()
static <T> ServiceInvocation<T>
create
(String name, ServiceInvocation.ServiceSupplier<T> supplier) Creates a new service invocation instance.javafx.beans.property.ReadOnlyObjectProperty<Throwable>
execute()
Executes the service invocation with the default executor.Executes the service invocation with the given executor.Gets the value of theexception
property.Gets the value of themessage
property.getName()
Returns the name of the service invocation.double
Gets the value of theprogress
property.javafx.concurrent.Worker.State
getState()
Returns the worker state of the service invocation (running, succeeded, ...).getTitle()
Gets the value of thetitle
property.double
Gets the value of thetotalWork
property.getValue()
Gets the value of thevalue
property.double
Gets the value of theworkDone
property.boolean
Gets the value of therunning
property.boolean
Returns true if the service invocation will intentionally fail (see alsowithSimulatingFailure(boolean)
).javafx.beans.property.ReadOnlyStringProperty
onException
(BiConsumer<String, Exception> onException) A consumer that will be invoked in case of an exception during the execution of the service invocation.onExceptionDefault
(BiConsumer<String, Exception> onExceptionDefault) A default consumer that will be invoked in case of an exception during the execution of the service invocation.onFailure
(BiConsumer<String, String> onFailure) A consumer that will be invoked when the backend service invocation was not successful.onFailureDefault
(BiConsumer<String, String> onFailureDefault) A default consumer that will be invoked when the backend service invocation was not successful.onFailureDetailed
(BiConsumer<String, retrofit2.Response> onFailureDetailed) A consumer that will be invoked when the backend service invocation was not successful.onFailureDetailedDefault
(BiConsumer<String, retrofit2.Response> onFailureDetailedDefault) A default consumer that will be invoked when the backend service invocation was not successful.A runnable that will be invoked after the service invocation has completed.A consumer that will be invoked first when theexecute()
method gets invoked.onStatusCode
(HttpStatusCode code, BiConsumer<String, String> onStatusCode) A consumer that will be invoked when the backend returns the given HTTP status code.onStatusCodeDefault
(HttpStatusCode code, BiConsumer<String, String> onStatusCodeDefault) A default consumer that will be invoked when the backend returns the given HTTP status code.A consumer that will be invoked when the backend service invocation was successful.onSuccessDetailed
(Consumer<retrofit2.Response<T>> onSuccessDetailed) A consumer that will be invoked when the backend service invocation was successful.javafx.beans.property.ReadOnlyDoubleProperty
javafx.beans.property.ReadOnlyBooleanProperty
javafx.beans.property.ReadOnlyObjectProperty<javafx.concurrent.Worker.State>
Returns a read-only property for observing the state of the service invocation.javafx.beans.property.ReadOnlyStringProperty
javafx.beans.property.ReadOnlyDoubleProperty
javafx.beans.property.ReadOnlyObjectProperty<T>
withDelay
(long delay) Introduces an artificial delay for the invocation.withSimulatingFailure
(boolean failure) An easy way to explicitly make the service invocation fail in order to test the handling of failures by the application.javafx.beans.property.ReadOnlyDoubleProperty
-
Property Details
-
state
public javafx.beans.property.ReadOnlyObjectProperty<javafx.concurrent.Worker.State> statePropertyReturns a read-only property for observing the state of the service invocation.- Specified by:
stateProperty
in interfacejavafx.concurrent.Worker<T>
- See Also:
-
value
- Specified by:
valueProperty
in interfacejavafx.concurrent.Worker<T>
- See Also:
-
exception
- Specified by:
exceptionProperty
in interfacejavafx.concurrent.Worker<T>
- See Also:
-
workDone
public javafx.beans.property.ReadOnlyDoubleProperty workDoneProperty- Specified by:
workDoneProperty
in interfacejavafx.concurrent.Worker<T>
- See Also:
-
totalWork
public javafx.beans.property.ReadOnlyDoubleProperty totalWorkProperty- Specified by:
totalWorkProperty
in interfacejavafx.concurrent.Worker<T>
- See Also:
-
progress
public javafx.beans.property.ReadOnlyDoubleProperty progressProperty- Specified by:
progressProperty
in interfacejavafx.concurrent.Worker<T>
- See Also:
-
running
public javafx.beans.property.ReadOnlyBooleanProperty runningProperty- Specified by:
runningProperty
in interfacejavafx.concurrent.Worker<T>
- See Also:
-
message
public javafx.beans.property.ReadOnlyStringProperty messageProperty- Specified by:
messageProperty
in interfacejavafx.concurrent.Worker<T>
- See Also:
-
title
public javafx.beans.property.ReadOnlyStringProperty titleProperty- Specified by:
titleProperty
in interfacejavafx.concurrent.Worker<T>
- See Also:
-
-
Method Details
-
create
public static <T> ServiceInvocation<T> create(String name, ServiceInvocation.ServiceSupplier<T> supplier) Creates a new service invocation instance.- Type Parameters:
T
- the type of the response object- Parameters:
name
- the name of this invocationsupplier
- the supplier returning the service for invocation- Returns:
- a service invocation
-
execute
Executes the service invocation with the default executor.- Returns:
- a completable future object usable for chaining
-
execute
Executes the service invocation with the given executor.- Returns:
- a completable future object usable for chaining
-
getName
Returns the name of the service invocation.- Returns:
- the name
-
withDelay
Introduces an artificial delay for the invocation. The call to the backend will happen after the delay time has passed.- Parameters:
delay
- the delay in milliseconds- Returns:
- the service invocation
-
withSimulatingFailure
An easy way to explicitly make the service invocation fail in order to test the handling of failures by the application.- Parameters:
failure
- enables / disables the failure- Returns:
- the service invocation
-
isSimulatingFailure
public boolean isSimulatingFailure()Returns true if the service invocation will intentionally fail (see alsowithSimulatingFailure(boolean)
).- Returns:
- true if the invocation will fail
-
onStart
A consumer that will be invoked first when theexecute()
method gets invoked.- Parameters:
onStart
- the "on start" handler.- Returns:
- the service invocation
-
onSuccess
A consumer that will be invoked when the backend service invocation was successful. The consumer will receive the expected result object.- Parameters:
onSuccess
- the "on success" handler.- Returns:
- the service invocation
-
onSuccessDetailed
A consumer that will be invoked when the backend service invocation was successful. The consumer will receive the response object wrapping the expected result object.- Parameters:
onSuccessDetailed
- the "on success detailed" handler.- Returns:
- the service invocation
-
onFailure
A consumer that will be invoked when the backend service invocation was not successful. The consumer will receive the name of the service invocation and an error message.- Parameters:
onFailure
- the "on failure" handler.- Returns:
- the service invocation
-
onFailureDefault
A default consumer that will be invoked when the backend service invocation was not successful. The consumer will receive the name of the service invocation and an error message.- Parameters:
onFailureDefault
- the "on failure default" handler.- Returns:
- the service invocation
-
onFailureDetailed
public ServiceInvocation<T> onFailureDetailed(BiConsumer<String, retrofit2.Response> onFailureDetailed) A consumer that will be invoked when the backend service invocation was not successful. The consumer will receive the name of the service invocation and the full response object.- Parameters:
onFailureDetailed
- the "on failure detailed" handler.- Returns:
- the service invocation
-
onFailureDetailedDefault
public ServiceInvocation<T> onFailureDetailedDefault(BiConsumer<String, retrofit2.Response> onFailureDetailedDefault) A default consumer that will be invoked when the backend service invocation was not successful. The consumer will receive the name of the service invocation and the full response object.- Parameters:
onFailureDetailedDefault
- the "on failure detailed default" handler.- Returns:
- the service invocation
-
onStatusCode
public ServiceInvocation<T> onStatusCode(HttpStatusCode code, BiConsumer<String, String> onStatusCode) A consumer that will be invoked when the backend returns the given HTTP status code. The consumer receives the name of the service invocation and the status message.- Parameters:
code
- the status code for which the consumer will be registeredonStatusCode
- the "on status code" handler.- Returns:
- the service invocation
-
onStatusCodeDefault
public ServiceInvocation<T> onStatusCodeDefault(HttpStatusCode code, BiConsumer<String, String> onStatusCodeDefault) A default consumer that will be invoked when the backend returns the given HTTP status code. The consumer receives the name of the service invocation and the status message.- Parameters:
code
- the status code for which the consumer will be registeredonStatusCodeDefault
- the "on status code" handler.- Returns:
- the service invocation
-
onException
A consumer that will be invoked in case of an exception during the execution of the service invocation. The consumer will receive the name of the service invocation and the exception object.- Parameters:
onException
- the consumer- Returns:
- the service invocation
-
onExceptionDefault
A default consumer that will be invoked in case of an exception during the execution of the service invocation. The consumer will receive the name of the service invocation and the exception object.- Parameters:
onExceptionDefault
- the consumer- Returns:
- the service invocation
-
onFinally
A runnable that will be invoked after the service invocation has completed. No matter if an exception occurred or not.- Parameters:
onFinally
- the runnable- Returns:
- the service invocation
-
getState
public javafx.concurrent.Worker.State getState()Returns the worker state of the service invocation (running, succeeded, ...).- Specified by:
getState
in interfacejavafx.concurrent.Worker<T>
- Returns:
- the state of the service invocation according to the
Worker
interface
-
stateProperty
public javafx.beans.property.ReadOnlyObjectProperty<javafx.concurrent.Worker.State> stateProperty()Returns a read-only property for observing the state of the service invocation.- Specified by:
stateProperty
in interfacejavafx.concurrent.Worker<T>
- Returns:
- the state property
- See Also:
-
getValue
Gets the value of thevalue
property.- Specified by:
getValue
in interfacejavafx.concurrent.Worker<T>
- Property description:
- Returns:
- the value of the
value
property - See Also:
-
valueProperty
- Specified by:
valueProperty
in interfacejavafx.concurrent.Worker<T>
- Returns:
- the
value
property - See Also:
-
getException
Gets the value of theexception
property.- Specified by:
getException
in interfacejavafx.concurrent.Worker<T>
- Property description:
- Returns:
- the value of the
exception
property - See Also:
-
exceptionProperty
- Specified by:
exceptionProperty
in interfacejavafx.concurrent.Worker<T>
- Returns:
- the
exception
property - See Also:
-
getWorkDone
public double getWorkDone()Gets the value of theworkDone
property.- Specified by:
getWorkDone
in interfacejavafx.concurrent.Worker<T>
- Property description:
- Returns:
- the value of the
workDone
property - See Also:
-
workDoneProperty
public javafx.beans.property.ReadOnlyDoubleProperty workDoneProperty()- Specified by:
workDoneProperty
in interfacejavafx.concurrent.Worker<T>
- Returns:
- the
workDone
property - See Also:
-
getTotalWork
public double getTotalWork()Gets the value of thetotalWork
property.- Specified by:
getTotalWork
in interfacejavafx.concurrent.Worker<T>
- Property description:
- Returns:
- the value of the
totalWork
property - See Also:
-
totalWorkProperty
public javafx.beans.property.ReadOnlyDoubleProperty totalWorkProperty()- Specified by:
totalWorkProperty
in interfacejavafx.concurrent.Worker<T>
- Returns:
- the
totalWork
property - See Also:
-
getProgress
public double getProgress()Gets the value of theprogress
property.- Specified by:
getProgress
in interfacejavafx.concurrent.Worker<T>
- Property description:
- Returns:
- the value of the
progress
property - See Also:
-
progressProperty
public javafx.beans.property.ReadOnlyDoubleProperty progressProperty()- Specified by:
progressProperty
in interfacejavafx.concurrent.Worker<T>
- Returns:
- the
progress
property - See Also:
-
runningProperty
public javafx.beans.property.ReadOnlyBooleanProperty runningProperty()- Specified by:
runningProperty
in interfacejavafx.concurrent.Worker<T>
- Returns:
- the
running
property - See Also:
-
isRunning
public boolean isRunning()Gets the value of therunning
property.- Specified by:
isRunning
in interfacejavafx.concurrent.Worker<T>
- Property description:
- Returns:
- the value of the
running
property - See Also:
-
getMessage
Gets the value of themessage
property.- Specified by:
getMessage
in interfacejavafx.concurrent.Worker<T>
- Property description:
- Returns:
- the value of the
message
property - See Also:
-
messageProperty
public javafx.beans.property.ReadOnlyStringProperty messageProperty()- Specified by:
messageProperty
in interfacejavafx.concurrent.Worker<T>
- Returns:
- the
message
property - See Also:
-
getTitle
Gets the value of thetitle
property.- Specified by:
getTitle
in interfacejavafx.concurrent.Worker<T>
- Property description:
- Returns:
- the value of the
title
property - See Also:
-
titleProperty
public javafx.beans.property.ReadOnlyStringProperty titleProperty()- Specified by:
titleProperty
in interfacejavafx.concurrent.Worker<T>
- Returns:
- the
title
property - See Also:
-
cancel
public boolean cancel()- Specified by:
cancel
in interfacejavafx.concurrent.Worker<T>
-