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
PropertiesTypePropertyDescriptionjavafx.beans.property.ReadOnlyObjectProperty<Throwable>javafx.beans.property.ReadOnlyStringPropertyjavafx.beans.property.ReadOnlyDoublePropertyjavafx.beans.property.ReadOnlyBooleanPropertyjavafx.beans.property.ReadOnlyObjectProperty<javafx.concurrent.Worker.State>Returns a read-only property for observing the state of the service invocation.javafx.beans.property.ReadOnlyStringPropertyjavafx.beans.property.ReadOnlyDoublePropertyjavafx.beans.property.ReadOnlyObjectProperty<T>javafx.beans.property.ReadOnlyDoubleProperty -
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceA 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 TypeMethodDescriptionbooleancancel()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 theexceptionproperty.Gets the value of themessageproperty.getName()Returns the name of the service invocation.doubleGets the value of theprogressproperty.javafx.concurrent.Worker.StategetState()Returns the worker state of the service invocation (running, succeeded, ...).getTitle()Gets the value of thetitleproperty.doubleGets the value of thetotalWorkproperty.getValue()Gets the value of thevalueproperty.doubleGets the value of theworkDoneproperty.booleanGets the value of therunningproperty.booleanReturns true if the service invocation will intentionally fail (see alsowithSimulatingFailure(boolean)).javafx.beans.property.ReadOnlyStringPropertyonException(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.ReadOnlyDoublePropertyjavafx.beans.property.ReadOnlyBooleanPropertyjavafx.beans.property.ReadOnlyObjectProperty<javafx.concurrent.Worker.State>Returns a read-only property for observing the state of the service invocation.javafx.beans.property.ReadOnlyStringPropertyjavafx.beans.property.ReadOnlyDoublePropertyjavafx.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:
statePropertyin interfacejavafx.concurrent.Worker<T>- See Also:
-
value
- Specified by:
valuePropertyin interfacejavafx.concurrent.Worker<T>- See Also:
-
exception
- Specified by:
exceptionPropertyin interfacejavafx.concurrent.Worker<T>- See Also:
-
workDone
public javafx.beans.property.ReadOnlyDoubleProperty workDoneProperty- Specified by:
workDonePropertyin interfacejavafx.concurrent.Worker<T>- See Also:
-
totalWork
public javafx.beans.property.ReadOnlyDoubleProperty totalWorkProperty- Specified by:
totalWorkPropertyin interfacejavafx.concurrent.Worker<T>- See Also:
-
progress
public javafx.beans.property.ReadOnlyDoubleProperty progressProperty- Specified by:
progressPropertyin interfacejavafx.concurrent.Worker<T>- See Also:
-
running
public javafx.beans.property.ReadOnlyBooleanProperty runningProperty- Specified by:
runningPropertyin interfacejavafx.concurrent.Worker<T>- See Also:
-
message
public javafx.beans.property.ReadOnlyStringProperty messageProperty- Specified by:
messagePropertyin interfacejavafx.concurrent.Worker<T>- See Also:
-
title
public javafx.beans.property.ReadOnlyStringProperty titleProperty- Specified by:
titlePropertyin 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:
getStatein interfacejavafx.concurrent.Worker<T>- Returns:
- the state of the service invocation according to the
Workerinterface
-
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:
statePropertyin interfacejavafx.concurrent.Worker<T>- Returns:
- the state property
- See Also:
-
getValue
Gets the value of thevalueproperty.- Specified by:
getValuein interfacejavafx.concurrent.Worker<T>- Property description:
- Returns:
- the value of the
valueproperty - See Also:
-
valueProperty
- Specified by:
valuePropertyin interfacejavafx.concurrent.Worker<T>- Returns:
- the
valueproperty - See Also:
-
getException
Gets the value of theexceptionproperty.- Specified by:
getExceptionin interfacejavafx.concurrent.Worker<T>- Property description:
- Returns:
- the value of the
exceptionproperty - See Also:
-
exceptionProperty
- Specified by:
exceptionPropertyin interfacejavafx.concurrent.Worker<T>- Returns:
- the
exceptionproperty - See Also:
-
getWorkDone
public double getWorkDone()Gets the value of theworkDoneproperty.- Specified by:
getWorkDonein interfacejavafx.concurrent.Worker<T>- Property description:
- Returns:
- the value of the
workDoneproperty - See Also:
-
workDoneProperty
public javafx.beans.property.ReadOnlyDoubleProperty workDoneProperty()- Specified by:
workDonePropertyin interfacejavafx.concurrent.Worker<T>- Returns:
- the
workDoneproperty - See Also:
-
getTotalWork
public double getTotalWork()Gets the value of thetotalWorkproperty.- Specified by:
getTotalWorkin interfacejavafx.concurrent.Worker<T>- Property description:
- Returns:
- the value of the
totalWorkproperty - See Also:
-
totalWorkProperty
public javafx.beans.property.ReadOnlyDoubleProperty totalWorkProperty()- Specified by:
totalWorkPropertyin interfacejavafx.concurrent.Worker<T>- Returns:
- the
totalWorkproperty - See Also:
-
getProgress
public double getProgress()Gets the value of theprogressproperty.- Specified by:
getProgressin interfacejavafx.concurrent.Worker<T>- Property description:
- Returns:
- the value of the
progressproperty - See Also:
-
progressProperty
public javafx.beans.property.ReadOnlyDoubleProperty progressProperty()- Specified by:
progressPropertyin interfacejavafx.concurrent.Worker<T>- Returns:
- the
progressproperty - See Also:
-
runningProperty
public javafx.beans.property.ReadOnlyBooleanProperty runningProperty()- Specified by:
runningPropertyin interfacejavafx.concurrent.Worker<T>- Returns:
- the
runningproperty - See Also:
-
isRunning
public boolean isRunning()Gets the value of therunningproperty.- Specified by:
isRunningin interfacejavafx.concurrent.Worker<T>- Property description:
- Returns:
- the value of the
runningproperty - See Also:
-
getMessage
Gets the value of themessageproperty.- Specified by:
getMessagein interfacejavafx.concurrent.Worker<T>- Property description:
- Returns:
- the value of the
messageproperty - See Also:
-
messageProperty
public javafx.beans.property.ReadOnlyStringProperty messageProperty()- Specified by:
messagePropertyin interfacejavafx.concurrent.Worker<T>- Returns:
- the
messageproperty - See Also:
-
getTitle
Gets the value of thetitleproperty.- Specified by:
getTitlein interfacejavafx.concurrent.Worker<T>- Property description:
- Returns:
- the value of the
titleproperty - See Also:
-
titleProperty
public javafx.beans.property.ReadOnlyStringProperty titleProperty()- Specified by:
titlePropertyin interfacejavafx.concurrent.Worker<T>- Returns:
- the
titleproperty - See Also:
-
cancel
public boolean cancel()- Specified by:
cancelin interfacejavafx.concurrent.Worker<T>
-