Spec-Zone .ru
спецификации, руководства, описания, API
|
V
- public abstract class Service<V> extends java.lang.Object implements Worker<V>
A Service is a non-visual component encapsulating the information required to perform some work on one or more background threads. As part of the JavaFX UI library, the Service knows about the JavaFX Application thread and is designed to relieve the application developer from the burden of manging multithreaded code that interacts with the user interface. As such, all of the methods and state on the Service are intended to be invoked exclusively from the JavaFX Application thread.
Service implements Worker
. As such, you can observe the state of
the background operation and optionally cancel it. Service is a reusable
Worker, meaning that it can be reset and restarted. Due to this, a Service
can be constructed declarativley and restarted on demand.
If an Executor
is specified on the Service,
then it will be used to actually execute the service. Otherwise,
a daemon thread will be created and executed. If you wish to create
non-daemon threads, then specify a custom Executor (for example,
you could use a ThreadPoolExecutor
with a custom
ThreadFactory
).
Because a Service is intended to simplify declarative use cases, subclasses
should expose as properties the input parameters to the work to be done.
For example, suppose I wanted to write a Service which read the first line
from any URL and returned it as a String. Such a Service might be defined,
such that it had a single property, url
. It might be implemented
as:
public class FirstLineService extends Service {
private StringProperty url = new StringProperty();
public final void setUrl(String value) { url.set(value); }
public final String getUrl() { return url.get(); }
public final StringProperty urlProperty() { return url; }
protected Task createTask() {
final String _url = getUrl();
return new Task<InputStream>() {
protected String call() {
URL u = new URL(_url);
BufferedReader in = new BufferedReader(
new InputStreamReader(u.openStream()));
String result = in.readLine();
in.close();
return result;
}
}
}
}
The Service by default uses a thread pool Executor with some unspecified default or maximum thread pool size. This is done so that naive code will not completely swamp the system by creating thousands of Threads.
Type | Property and Description |
---|---|
ReadOnlyObjectProperty<java.lang.Throwable> |
exception
Gets the ReadOnlyObjectProperty representing any exception which occurred.
|
ObjectProperty<java.util.concurrent.Executor> |
executor
The executor to use for running this Service.
|
ReadOnlyStringProperty |
message
Gets the ReadOnlyStringProperty representing the message.
|
ReadOnlyDoubleProperty |
progress
Gets the ReadOnlyLongProperty representing the progress.
|
ReadOnlyBooleanProperty |
running
Gets the ReadOnlyBooleanProperty representing whether the Worker is running.
|
ReadOnlyObjectProperty<Worker.State> |
state
Gets the ReadOnlyObjectProperty representing the current state.
|
ReadOnlyStringProperty |
title
Gets the ReadOnlyStringProperty representing the title.
|
ReadOnlyDoubleProperty |
totalWork
Gets the ReadOnlyLongProperty representing the maximum amount of work
that needs to be done.
|
ReadOnlyObjectProperty<V> |
value
Gets the ReadOnlyObjectProperty representing the value.
|
ReadOnlyDoubleProperty |
workDone
Gets the ReadOnlyLongProperty representing the current progress.
|
Worker.State
Constructor and Description |
---|
Service() |
Modifier and Type | Method and Description |
---|---|
boolean |
cancel()
Terminates execution of this Worker.
|
protected abstract Task |
createTask()
Invoked after the Service is started on the JavaFX Application Thread.
|
ReadOnlyObjectProperty<java.lang.Throwable> |
exceptionProperty()
Gets the ReadOnlyObjectProperty representing any exception which occurred.
|
ObjectProperty<java.util.concurrent.Executor> |
executorProperty()
The executor to use for running this Service.
|
java.lang.Throwable |
getException()
Indicates the exception which occurred while the Worker was running, if any.
|
java.util.concurrent.Executor |
getExecutor()
The executor to use for running this Service.
|
java.lang.String |
getMessage()
Gets a message associated with the current state of this Worker.
|
double |
getProgress()
Indicates the current progress of this Worker in terms of percent complete.
|
Worker.State |
getState()
Specifies the current state of this Worker.
|
java.lang.String |
getTitle()
An optional title that should be associated with this Worker.
|
double |
getTotalWork()
Indicates a maximum value for the
Worker.workDoneProperty() property. |
V |
getValue()
Specifies the value, or result, of this Worker.
|
double |
getWorkDone()
Indicates the current amount of work that has been completed.
|
boolean |
isRunning()
True if the state is either SCHEDULED or RUNNING.
|
ReadOnlyStringProperty |
messageProperty()
Gets the ReadOnlyStringProperty representing the message.
|
ReadOnlyDoubleProperty |
progressProperty()
Gets the ReadOnlyLongProperty representing the progress.
|
void |
reset()
Resets the Service.
|
void |
restart()
Cancels any currently running Task, if any, and restarts this Service.
|
ReadOnlyBooleanProperty |
runningProperty()
Gets the ReadOnlyBooleanProperty representing whether the Worker is running.
|
void |
setExecutor(java.util.concurrent.Executor value)
The executor to use for running this Service.
|
void |
start()
Starts this Service.
|
ReadOnlyObjectProperty<Worker.State> |
stateProperty()
Gets the ReadOnlyObjectProperty representing the current state.
|
ReadOnlyStringProperty |
titleProperty()
Gets the ReadOnlyStringProperty representing the title.
|
ReadOnlyDoubleProperty |
totalWorkProperty()
Gets the ReadOnlyLongProperty representing the maximum amount of work
that needs to be done.
|
ReadOnlyObjectProperty<V> |
valueProperty()
Gets the ReadOnlyObjectProperty representing the value.
|
ReadOnlyDoubleProperty |
workDoneProperty()
Gets the ReadOnlyLongProperty representing the current progress.
|
stateProperty
in interface Worker<V>
getState()
valueProperty
in interface Worker<V>
getValue()
exceptionProperty
in interface Worker<V>
getException()
workDoneProperty
in interface Worker<V>
getWorkDone()
totalWorkProperty
in interface Worker<V>
getTotalWork()
progressProperty
in interface Worker<V>
getProgress()
runningProperty
in interface Worker<V>
isRunning()
messageProperty
in interface Worker<V>
getMessage()
titleProperty
in interface Worker<V>
getTitle()
getExecutor()
,
setExecutor(Executor)
public final Worker.State getState()
Worker
public final ReadOnlyObjectProperty<Worker.State> stateProperty()
Worker
stateProperty
in interface Worker<V>
getState()
public final V getValue()
Worker
public final ReadOnlyObjectProperty<V> valueProperty()
Worker
valueProperty
in interface Worker<V>
getValue()
public final java.lang.Throwable getException()
Worker
null
, there is no known exception, even if
the status is FAILED. If this property is not null
, it will most
likely contain an exception that describes the cause of failure.getException
in interface Worker<V>
public final ReadOnlyObjectProperty<java.lang.Throwable> exceptionProperty()
Worker
exceptionProperty
in interface Worker<V>
getException()
public final double getWorkDone()
Worker
getWorkDone
in interface Worker<V>
Worker.totalWorkProperty()
,
Worker.progressProperty()
public final ReadOnlyDoubleProperty workDoneProperty()
Worker
workDoneProperty
in interface Worker<V>
getWorkDone()
public final double getTotalWork()
Worker
Worker.workDoneProperty()
property. The
totalWork will either be -1 (indicating that the amount of work
to do is indeterminate), or it will be a non-zero value less than or
equal to Long.MAX_VALUE.getTotalWork
in interface Worker<V>
Worker.workDoneProperty()
,
Worker.progressProperty()
public final ReadOnlyDoubleProperty totalWorkProperty()
Worker
totalWorkProperty
in interface Worker<V>
getTotalWork()
public final double getProgress()
Worker
getProgress
in interface Worker<V>
Worker.workDoneProperty()
,
Worker.totalWorkProperty()
public final ReadOnlyDoubleProperty progressProperty()
Worker
progressProperty
in interface Worker<V>
getProgress()
public final boolean isRunning()
Worker
ProgressIndicator
, you will typically bind the visibility
of the ProgressIndicator to the Worker's running property, and the progress of the
ProgressIndicator to the Worker's progress property.public final ReadOnlyBooleanProperty runningProperty()
Worker
runningProperty
in interface Worker<V>
isRunning()
public final java.lang.String getMessage()
Worker
getMessage
in interface Worker<V>
public final ReadOnlyStringProperty messageProperty()
Worker
messageProperty
in interface Worker<V>
getMessage()
public final java.lang.String getTitle()
Worker
public final ReadOnlyStringProperty titleProperty()
Worker
titleProperty
in interface Worker<V>
getTitle()
public final void setExecutor(java.util.concurrent.Executor value)
public final java.util.concurrent.Executor getExecutor()
public final ObjectProperty<java.util.concurrent.Executor> executorProperty()
getExecutor()
,
setExecutor(Executor)
public final boolean cancel()
Worker
public void restart()
public void reset()
public void start()
protected abstract Task createTask()
protected Task createTask() {
final String url = myService.getUrl();
return new Task<InputStream>() {
protected String call() {
URL u = new URL("http://www.oracle.com");
BufferedReader in = new BufferedReader(
new InputStreamReader(u.openStream()));
String result = in.readLine();
in.close();
return result;
}
}
}
If the Task is a pre-defined class (as opposed to being an anonymous class), and if it followed the recommended best-practice, then there is no need to save off state prior to constructing the Task since its state is completely provided in its constructor.
protected Task createTask() {
// This is safe because getUrl is called on the FX Application
// Thread and the FirstLineReaderTasks stores it as an
// immutable property
return new FirstLineReaderTask(myService.getUrl());
}
Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved. Use is subject to