- Type Parameters:
DataType- the type of data to be provided by thisAsyncDataLink
- All Implemented Interfaces:
AsyncDataLink<DataType>
AsyncDataLink which allows to read an arbitrary
Channel and process its content to be provided as asynchronously.
The implementations relies on two executors, namely:
processorExecutor and cancelExecutor. When the data is
requested from AsyncChannelLink it will submit a task to
processorExecutor which will actually
open a new channel which is then
processed
in the same task. In this task will the AsyncDataListener be notified
of the data to be provided.
When a data retrieval request is canceled through the returned
AsyncDataController, the basic action taken is to cancel the task
submitted to the processorExecutor and if the task has not yet been
started processing the channel, it will be canceled without actually starting
to process it. When however, the opened channel implements the
InterruptibleChannel, it will even be asynchronously closed, so reads
while processing the channel will cause ClosedChannelException to be
thrown which is interpreted as a cancellation. Since closing the channel
might be an external call, it is actually done in a task submitted to
cancelExecutor.
Note that although actual canceling of the processing of a channel may take
some time, the AsyncChannelLink listening for the data will see as
if requesting a cancel request is almost instantaneous regardless how long
actually the canceling takes.
Thread safety
As required byAsyncDataLink, methods of this class are safe to be
accessed by multiple threads concurrently.
Synchronization transparency
The methods of this class are not synchronization transparent.- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionAsyncChannelLink(TaskExecutor processorExecutor, TaskExecutor cancelExecutor, ChannelOpener<? extends ChannelType> channelOpener, ChannelProcessor<? extends DataType, ChannelType> channelProcessor) Creates a newAsyncChannelLinkwith the specified executors,ChannelOpenerandChannelProcessor. -
Method Summary
Modifier and TypeMethodDescriptiongetData(CancellationToken cancelToken, AsyncDataListener<? super DataType> dataListener) Starts retrieving the data which is linked to thisAsyncDataLink.
-
Constructor Details
-
AsyncChannelLink
public AsyncChannelLink(TaskExecutor processorExecutor, TaskExecutor cancelExecutor, ChannelOpener<? extends ChannelType> channelOpener, ChannelProcessor<? extends DataType, ChannelType> channelProcessor) Creates a newAsyncChannelLinkwith the specified executors,ChannelOpenerandChannelProcessor.- Type Parameters:
ChannelType- the type of the channel to be opened by thechannelOpenerand then processed bychannelProcessor- Parameters:
processorExecutor- the executor to which tasks will be submitted to process a channel opened bychannelOpener. This argument may not benull.cancelExecutor- the executor to which tasks will be submitted to asynchronously close channels if the request for processing it has been canceled. This executor can be the same asprocessorExecutor, note however that in this case, the processing of the channel may block the task which is submitted to cancel it (by closing the channel). This argument cannot benull.channelOpener- theChannelOpenerwhich must open a new channel to be processed bychannelProcessor. TheChannelOpenermust always open a channel to the same source (e.g.: with files, to the same file) to preserve the contract of theAsyncDataLink. This argument cannot benull.channelProcessor- theChannelProcessorwhich must process open channels and forward the data it reads from it to theAsyncDataLink. This argument cannot benull.- Throws:
NullPointerException- thrown if any of the arguments isnull
-
-
Method Details
-
getData
public AsyncDataController getData(CancellationToken cancelToken, AsyncDataListener<? super DataType> dataListener) Starts retrieving the data which is linked to thisAsyncDataLink. The data will be forwarded to the specified listener usually asynchronously on a separate thread. Note however that this method may also forward the data synchronously on the current thread to the listener if it is readily available and does not need some expensive operation to load.Once this method has been called successfully, the
onDoneReceivemethod of the listener must be called eventually regardless what happens. Failing to call this method is a serious failure of theAsyncDataLinkimplementation.Implementation note: The
state of progressof the data retrieval process can possibly be a different type of object than the one provided by thechannel processor.- Specified by:
getDatain interfaceAsyncDataLink<DataType>- Parameters:
cancelToken- theCancellationTokensignaling if the data retrieval process need to be canceled. Implementations may ignore cancellation requests but they should cancel the data retrieval and call theonDoneReceivemethod of the listener, signaling that the data retrieval cannot be completed due to cancellation. This argument cannot benull.dataListener- the listener to which the data is to be forwarded. This argument cannot benull.- Returns:
- the
AsyncDataControllerwhich can be used to control the way it is being loaded and request the progress of the data retrieving process. This method must never returnnull.
-