Spec-Zone .ru
спецификации, руководства, описания, API
|
V
- public abstract class Service<V> extends java.lang.Object implements Worker<V>, EventTarget
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 declaratively 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 static class FirstLineService extends Service<String> {
private StringProperty url = new SimpleStringProperty(this, "url");
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<String>() {
protected String call() throws Exception {
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.
|
ObjectProperty<EventHandler<WorkerStateEvent>> |
onCancelled
The onCancelled event handler is called whenever the Task state
transitions to the CANCELLED state.
|
ObjectProperty<EventHandler<WorkerStateEvent>> |
onFailed
The onFailed event handler is called whenever the Task state
transitions to the FAILED state.
|
ObjectProperty<EventHandler<WorkerStateEvent>> |
onReady
The onReady event handler is called whenever the Task state transitions
to the READY state.
|
ObjectProperty<EventHandler<WorkerStateEvent>> |
onRunning
The onRunning event handler is called whenever the Task state
transitions to the RUNNING state.
|
ObjectProperty<EventHandler<WorkerStateEvent>> |
onScheduled
The onSchedule event handler is called whenever the Task state
transitions to the SCHEDULED state.
|
ObjectProperty<EventHandler<WorkerStateEvent>> |
onSucceeded
The onSucceeded event handler is called whenever the Task state
transitions to the SUCCEEDED state.
|
ReadOnlyDoubleProperty |
progress
Gets the ReadOnlyDoubleProperty 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 ReadOnlyDoubleProperty representing the maximum amount of work
that needs to be done.
|
ReadOnlyObjectProperty<V> |
value
Gets the ReadOnlyObjectProperty representing the value.
|
ReadOnlyDoubleProperty |
workDone
Gets the ReadOnlyDoubleProperty representing the current progress.
|
Worker.State
Modifier | Constructor and Description |
---|---|
protected |
Service()
Create a new Service.
|
Modifier and Type | Method and Description |
---|---|
<T extends Event> |
addEventFilter(EventType<T> eventType,
EventHandler<? super T> eventFilter)
Registers an event filter to this task.
|
<T extends Event> |
addEventHandler(EventType<T> eventType,
EventHandler<? super T> eventHandler)
Registers an event handler to this task.
|
EventDispatchChain |
buildEventDispatchChain(EventDispatchChain tail)
Construct an event dispatch chain for this target.
|
boolean |
cancel()
Terminates execution of this Worker.
|
protected void |
cancelled()
A protected convenience method for subclasses, called whenever the
state of the Task has transitioned to the CANCELLED state.
|
protected abstract Task<V> |
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.
|
protected void |
executeTask(Task<V> task)
Uses the
executor defined on this Service to execute the
given task. |
ObjectProperty<java.util.concurrent.Executor> |
executorProperty()
The executor to use for running this Service.
|
protected void |
failed()
A protected convenience method for subclasses, called whenever the
state of the Task has transitioned to the FAILED state.
|
protected void |
fireEvent(Event event)
Fires the specified event.
|
java.lang.Throwable |
getException()
Gets the value of the property exception.
|
java.util.concurrent.Executor |
getExecutor()
Gets the value of the property executor.
|
java.lang.String |
getMessage()
Gets the value of the property message.
|
EventHandler<WorkerStateEvent> |
getOnCancelled()
The onCancelled event handler is called whenever the Task state
transitions to the CANCELLED state.
|
EventHandler<WorkerStateEvent> |
getOnFailed()
The onFailed event handler is called whenever the Task state
transitions to the FAILED state.
|
EventHandler<WorkerStateEvent> |
getOnReady()
The onReady event handler is called whenever the Task state transitions
to the READY state.
|
EventHandler<WorkerStateEvent> |
getOnRunning()
The onRunning event handler is called whenever the Task state
transitions to the RUNNING state.
|
EventHandler<WorkerStateEvent> |
getOnScheduled()
The onSchedule event handler is called whenever the Task state
transitions to the SCHEDULED state.
|
EventHandler<WorkerStateEvent> |
getOnSucceeded()
The onSucceeded event handler is called whenever the Task state
transitions to the SUCCEEDED state.
|
double |
getProgress()
Gets the value of the property progress.
|
Worker.State |
getState()
Gets the value of the property state.
|
java.lang.String |
getTitle()
Gets the value of the property title.
|
double |
getTotalWork()
Gets the value of the property totalWork.
|
V |
getValue()
Gets the value of the property value.
|
double |
getWorkDone()
Gets the value of the property workDone.
|
boolean |
isRunning()
Gets the value of the property running.
|
ReadOnlyStringProperty |
messageProperty()
Gets the ReadOnlyStringProperty representing the message.
|
ObjectProperty<EventHandler<WorkerStateEvent>> |
onCancelledProperty()
The onCancelled event handler is called whenever the Task state
transitions to the CANCELLED state.
|
ObjectProperty<EventHandler<WorkerStateEvent>> |
onFailedProperty()
The onFailed event handler is called whenever the Task state
transitions to the FAILED state.
|
ObjectProperty<EventHandler<WorkerStateEvent>> |
onReadyProperty()
The onReady event handler is called whenever the Task state transitions
to the READY state.
|
ObjectProperty<EventHandler<WorkerStateEvent>> |
onRunningProperty()
The onRunning event handler is called whenever the Task state
transitions to the RUNNING state.
|
ObjectProperty<EventHandler<WorkerStateEvent>> |
onScheduledProperty()
The onSchedule event handler is called whenever the Task state
transitions to the SCHEDULED state.
|
ObjectProperty<EventHandler<WorkerStateEvent>> |
onSucceededProperty()
The onSucceeded event handler is called whenever the Task state
transitions to the SUCCEEDED state.
|
ReadOnlyDoubleProperty |
progressProperty()
Gets the ReadOnlyDoubleProperty representing the progress.
|
protected void |
ready()
A protected convenience method for subclasses, called whenever the
state of the Task has transitioned to the READY state.
|
<T extends Event> |
removeEventFilter(EventType<T> eventType,
EventHandler<? super T> eventFilter)
Unregisters a previously registered event filter from this task.
|
<T extends Event> |
removeEventHandler(EventType<T> eventType,
EventHandler<? super T> eventHandler)
Unregisters a previously registered event handler from this task.
|
void |
reset()
Resets the Service.
|
void |
restart()
Cancels any currently running Task, if any, and restarts this Service.
|
protected void |
running()
A protected convenience method for subclasses, called whenever the
state of the Task has transitioned to the RUNNING state.
|
ReadOnlyBooleanProperty |
runningProperty()
Gets the ReadOnlyBooleanProperty representing whether the Worker is running.
|
protected void |
scheduled()
A protected convenience method for subclasses, called whenever the
state of the Task has transitioned to the SCHEDULED state.
|
protected <T extends Event> |
setEventHandler(EventType<T> eventType,
EventHandler<? super T> eventHandler)
Sets the handler to use for this event type.
|
void |
setExecutor(java.util.concurrent.Executor value)
Sets the value of the property executor.
|
void |
setOnCancelled(EventHandler<WorkerStateEvent> value)
The onCancelled event handler is called whenever the Task state
transitions to the CANCELLED state.
|
void |
setOnFailed(EventHandler<WorkerStateEvent> value)
The onFailed event handler is called whenever the Task state
transitions to the FAILED state.
|
void |
setOnReady(EventHandler<WorkerStateEvent> value)
The onReady event handler is called whenever the Task state transitions
to the READY state.
|
void |
setOnRunning(EventHandler<WorkerStateEvent> value)
The onRunning event handler is called whenever the Task state
transitions to the RUNNING state.
|
void |
setOnScheduled(EventHandler<WorkerStateEvent> value)
The onSchedule event handler is called whenever the Task state
transitions to the SCHEDULED state.
|
void |
setOnSucceeded(EventHandler<WorkerStateEvent> value)
The onSucceeded event handler is called whenever the Task state
transitions to the SUCCEEDED state.
|
void |
start()
Starts this Service.
|
ReadOnlyObjectProperty<Worker.State> |
stateProperty()
Gets the ReadOnlyObjectProperty representing the current state.
|
protected void |
succeeded()
A protected convenience method for subclasses, called whenever the
state of the Task has transitioned to the SUCCEEDED state.
|
ReadOnlyStringProperty |
titleProperty()
Gets the ReadOnlyStringProperty representing the title.
|
ReadOnlyDoubleProperty |
totalWorkProperty()
Gets the ReadOnlyDoubleProperty representing the maximum amount of work
that needs to be done.
|
ReadOnlyObjectProperty<V> |
valueProperty()
Gets the ReadOnlyObjectProperty representing the value.
|
ReadOnlyDoubleProperty |
workDoneProperty()
Gets the ReadOnlyDoubleProperty representing the current progress.
|
public final ReadOnlyObjectProperty<Worker.State> stateProperty
stateProperty
in interface Worker<V>
getState()
public final ReadOnlyObjectProperty<V> valueProperty
valueProperty
in interface Worker<V>
getValue()
public final ReadOnlyObjectProperty<java.lang.Throwable> exceptionProperty
exceptionProperty
in interface Worker<V>
getException()
public final ReadOnlyDoubleProperty workDoneProperty
workDoneProperty
in interface Worker<V>
getWorkDone()
public final ReadOnlyDoubleProperty totalWorkProperty
totalWorkProperty
in interface Worker<V>
getTotalWork()
public final ReadOnlyDoubleProperty progressProperty
progressProperty
in interface Worker<V>
getProgress()
public final ReadOnlyBooleanProperty runningProperty
runningProperty
in interface Worker<V>
isRunning()
public final ReadOnlyStringProperty messageProperty
messageProperty
in interface Worker<V>
getMessage()
public final ReadOnlyStringProperty titleProperty
titleProperty
in interface Worker<V>
getTitle()
public final ObjectProperty<java.util.concurrent.Executor> executorProperty
getExecutor()
,
setExecutor(Executor)
public final ObjectProperty<EventHandler<WorkerStateEvent>> onReadyProperty
getOnReady()
,
setOnReady(EventHandler)
public final ObjectProperty<EventHandler<WorkerStateEvent>> onScheduledProperty
getOnScheduled()
,
setOnScheduled(EventHandler)
public final ObjectProperty<EventHandler<WorkerStateEvent>> onRunningProperty
getOnRunning()
,
setOnRunning(EventHandler)
public final ObjectProperty<EventHandler<WorkerStateEvent>> onSucceededProperty
getOnSucceeded()
,
setOnSucceeded(EventHandler)
public final ObjectProperty<EventHandler<WorkerStateEvent>> onCancelledProperty
getOnCancelled()
,
setOnCancelled(EventHandler)
public final ObjectProperty<EventHandler<WorkerStateEvent>> onFailedProperty
getOnFailed()
,
setOnFailed(EventHandler)
public final Worker.State getState()
public final ReadOnlyObjectProperty<Worker.State> stateProperty()
Worker
stateProperty
in interface Worker<V>
getState()
public final V getValue()
public final ReadOnlyObjectProperty<V> valueProperty()
Worker
valueProperty
in interface Worker<V>
getValue()
public final java.lang.Throwable getException()
getException
in interface Worker<V>
public final ReadOnlyObjectProperty<java.lang.Throwable> exceptionProperty()
Worker
exceptionProperty
in interface Worker<V>
getException()
public final double getWorkDone()
getWorkDone
in interface Worker<V>
Worker.totalWorkProperty()
,
Worker.progressProperty()
public final ReadOnlyDoubleProperty workDoneProperty()
Worker
workDoneProperty
in interface Worker<V>
getWorkDone()
public final double getTotalWork()
getTotalWork
in interface Worker<V>
Worker.workDoneProperty()
,
Worker.progressProperty()
public final ReadOnlyDoubleProperty totalWorkProperty()
Worker
totalWorkProperty
in interface Worker<V>
getTotalWork()
public final double getProgress()
getProgress
in interface Worker<V>
Worker.workDoneProperty()
,
Worker.totalWorkProperty()
public final ReadOnlyDoubleProperty progressProperty()
Worker
progressProperty
in interface Worker<V>
getProgress()
public final boolean isRunning()
public final ReadOnlyBooleanProperty runningProperty()
Worker
runningProperty
in interface Worker<V>
isRunning()
public final java.lang.String getMessage()
getMessage
in interface Worker<V>
public final ReadOnlyStringProperty messageProperty()
Worker
messageProperty
in interface Worker<V>
getMessage()
public final java.lang.String getTitle()
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 ObjectProperty<EventHandler<WorkerStateEvent>> onReadyProperty()
getOnReady()
,
setOnReady(EventHandler)
public final EventHandler<WorkerStateEvent> getOnReady()
public final void setOnReady(EventHandler<WorkerStateEvent> value)
value
- the event handler, can be null to clear itprotected void ready()
public final ObjectProperty<EventHandler<WorkerStateEvent>> onScheduledProperty()
getOnScheduled()
,
setOnScheduled(EventHandler)
public final EventHandler<WorkerStateEvent> getOnScheduled()
public final void setOnScheduled(EventHandler<WorkerStateEvent> value)
value
- the event handler, can be null to clear itprotected void scheduled()
public final ObjectProperty<EventHandler<WorkerStateEvent>> onRunningProperty()
getOnRunning()
,
setOnRunning(EventHandler)
public final EventHandler<WorkerStateEvent> getOnRunning()
public final void setOnRunning(EventHandler<WorkerStateEvent> value)
value
- the event handler, can be null to clear itprotected void running()
public final ObjectProperty<EventHandler<WorkerStateEvent>> onSucceededProperty()
getOnSucceeded()
,
setOnSucceeded(EventHandler)
public final EventHandler<WorkerStateEvent> getOnSucceeded()
public final void setOnSucceeded(EventHandler<WorkerStateEvent> value)
value
- the event handler, can be null to clear itprotected void succeeded()
public final ObjectProperty<EventHandler<WorkerStateEvent>> onCancelledProperty()
getOnCancelled()
,
setOnCancelled(EventHandler)
public final EventHandler<WorkerStateEvent> getOnCancelled()
public final void setOnCancelled(EventHandler<WorkerStateEvent> value)
value
- the event handler, can be null to clear itprotected void cancelled()
public final ObjectProperty<EventHandler<WorkerStateEvent>> onFailedProperty()
getOnFailed()
,
setOnFailed(EventHandler)
public final EventHandler<WorkerStateEvent> getOnFailed()
public final void setOnFailed(EventHandler<WorkerStateEvent> value)
value
- the event handler, can be null to clear itprotected void failed()
public final boolean cancel()
Worker
public void restart()
public void reset()
public void start()
protected void executeTask(Task<V> task)
Uses the executor
defined on this Service to execute the
given task. If the executor
is null, then a default
executor is used which will create a new daemon thread on which to
execute this task.
This method is intended only to be called by the Service implementation.
task
- a non-null task to executepublic final <T extends Event> void addEventHandler(EventType<T> eventType, EventHandler<? super T> eventHandler)
T
- the specific event class of the handlereventType
- the type of the events to receive by the handlereventHandler
- the handler to registerjava.lang.NullPointerException
- if the event type or handler is nullpublic final <T extends Event> void removeEventHandler(EventType<T> eventType, EventHandler<? super T> eventHandler)
T
- the specific event class of the handlereventType
- the event type from which to unregistereventHandler
- the handler to unregisterjava.lang.NullPointerException
- if the event type or handler is nullpublic final <T extends Event> void addEventFilter(EventType<T> eventType, EventHandler<? super T> eventFilter)
T
- the specific event class of the filtereventType
- the type of the events to receive by the filtereventFilter
- the filter to registerjava.lang.NullPointerException
- if the event type or filter is nullpublic final <T extends Event> void removeEventFilter(EventType<T> eventType, EventHandler<? super T> eventFilter)
T
- the specific event class of the filtereventType
- the event type from which to unregistereventFilter
- the filter to unregisterjava.lang.NullPointerException
- if the event type or filter is nullprotected final <T extends Event> void setEventHandler(EventType<T> eventType, EventHandler<? super T> eventHandler)
T
- the specific event class of the handlereventType
- the event type to associate with the given eventHandlereventHandler
- the handler to register, or null to unregisterjava.lang.NullPointerException
- if the event type is nullprotected final void fireEvent(Event event)
This method must be called on the FX user thread.
event
- the event to firepublic EventDispatchChain buildEventDispatchChain(EventDispatchChain tail)
EventTarget
EventTarget
. This event target is
not automatically added to the chain, so if it wants to process events,
it needs to add an EventDispatcher
for itself to the chain.
In the case the event target is part of some hierarchy, the chain for it is usually built from event dispatchers collected from the root of the hierarchy to the event target.
The event dispatch chain is constructed by modifications to the provided initial event dispatch chain. The returned chain should have the initial chain at its end so the dispatchers should be prepended to the initial chain.
The caller shouldn't assume that the initial chain remains unchanged nor that the returned value will reference a different chain.
buildEventDispatchChain
in interface EventTarget
tail
- the initial chain to build fromprotected abstract Task<V> createTask()
protected Task createTask() {
final String url = myService.getUrl();
return new Task<String>() {
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, 2012, Oracle and/or its affiliates. All rights reserved. Use is subject to