public class AsyncWriterPool
extends java.lang.Object
implements java.io.Closeable
This creates a writer pool allowing for M
writers and N
threads where writers are not tied to an
individual thread. This introduces a small amount of overhead compared to a writer-per-thread model and is best
suited to scenarios where M
is greater than M
.
Constructor and Description |
---|
AsyncWriterPool()
Create an AsyncWriterPool using all available processors.
|
AsyncWriterPool(int threads)
Create an AsyncWriterPool using the specified number of
threads . |
Modifier and Type | Method and Description |
---|---|
void |
close()
Asynchronously closes each writer in the pool.
|
int |
getTimeoutSeconds()
Get the
timeoutSeconds value. |
<A> Writer<A> |
pool(Writer<A> writer,
java.util.concurrent.BlockingQueue<A> queue,
int writeThreshold)
Exchange a class implementing
Writer for a PooledWriter . |
void |
setTimeoutSeconds(int timeoutSeconds)
Set the
timeoutSeconds value. |
public AsyncWriterPool(int threads)
threads
. The number of threads
in use at
one time will grow and shrink with load.
Note, any calls to the pool, or any created writers should come from the same thread. Behavior of calling methods on either AsyncWriterPool or its associated Writers from different threads is undefined.
threads
- max number of threads to usepublic AsyncWriterPool()
public void close() throws java.io.IOException
.close()
in a CompletableFuture, which
allows the writers to wait for any ongoing writes, drain any remaining elements from the queue, and then close
the inner writer. All writers will immediately cease to accept new items to write and any call to .write()
after calling this method will throw an exception. This method waits for all CompletableFuture
s to
complete and then shuts down the executor.
This method blocks and till all writers have closed and pool has shut down.
close
in interface java.io.Closeable
close
in interface java.lang.AutoCloseable
java.io.IOException
- if any writer raises an exceptionpublic int getTimeoutSeconds()
timeoutSeconds
value.public void setTimeoutSeconds(int timeoutSeconds)
timeoutSeconds
value. timeoutSeconds
is used by the writers to determine how long they
should wait when trying to place an item in the queue for writing. This timeout only comes into play if the
writer has failed and prevents a call to write
from hanging.timeoutSeconds
- the number of senconds a writer will wait to emplace an item in a queue for writing.