public static final class Configuration.Builder
extends java.lang.Object
| Constructor and Description |
|---|
Builder(android.content.Context context) |
| Modifier and Type | Method and Description |
|---|---|
Configuration |
build() |
Configuration.Builder |
consumerKeepAlive(int keepAlive)
When JobManager runs out of `ready` jobs, it will keep consumers alive for this duration.
|
Configuration.Builder |
consumerThreadPriority(int threadPriority)
Sets the priority for the threads of this manager.
|
Configuration.Builder |
customLogger(CustomLogger logger)
you can provide a custom logger to get logs from JobManager.
|
Configuration.Builder |
id(java.lang.String id)
Provide and ID for this job manager to be used while creating persistent queue.
|
Configuration.Builder |
injector(DependencyInjector injector)
JobManager is suitable for DependencyInjection.
|
Configuration.Builder |
inTestMode()
Sets the JobManager in test mode.
|
Configuration.Builder |
jobSerializer(SqliteJobQueue.JobSerializer jobSerializer)
convenient configuration to replace job serializer while using
SqliteJobQueue
queue for persistence. |
Configuration.Builder |
loadFactor(int loadFactor)
calculated by # of jobs (running+waiting) per thread
for instance, at a given time, if you have two consumers and 10 jobs in waiting queue (or running right now), load is
(10/2) =5
defaults to
Configuration.DEFAULT_LOAD_FACTOR_PER_CONSUMER |
Configuration.Builder |
maxConsumerCount(int count)
# of max consumers to run concurrently.
|
Configuration.Builder |
minConsumerCount(int count)
you can specify to keep minConsumers alive even if there are no ready jobs.
|
Configuration.Builder |
networkUtil(NetworkUtil networkUtil)
By default, Job Manager comes with a simple
NetworkUtilImpl that queries ConnectivityManager
to check if network connection exists. |
Configuration.Builder |
queueFactory(QueueFactory queueFactory)
JobManager needs one persistent and one non-persistent
JobQueue to function. |
Configuration.Builder |
resetDelaysOnRestart()
JobManager 1.x versions used to clear delay for existing jobs when the application is
restarted because there is no reliable way to measure time difference between device
reboots (and from the app's perspective, device reboot is no different than app restart).
|
Configuration.Builder |
scheduler(Scheduler scheduler)
Assigns a scheduler that can be used to wake up the application when JobManager has jobs
to execute.
|
Configuration.Builder |
scheduler(Scheduler scheduler,
boolean batch)
Assigns a scheduler that can be used to wake up the application when JobManager has jobs
to execute.
|
Configuration.Builder |
threadFactory(java.util.concurrent.ThreadFactory threadFactory)
Provide a factory class to create new worker instances when JobManager needs them.
|
Configuration.Builder |
timer(Timer timer)
You can specify a custom timer to control task execution.
|
@NonNull public Configuration.Builder id(@NonNull java.lang.String id)
Configuration.DEFAULT_ID
You can only use alphanumeric characters, - and _ .
id - if you have multiple instances of job manager, you should provide an id to
distinguish their persistent files.@NonNull public Configuration.Builder consumerKeepAlive(int keepAlive)
Configuration.DEFAULT_THREAD_KEEP_ALIVE_SECONDSkeepAlive - in seconds@NonNull public Configuration.Builder resetDelaysOnRestart()
This may cause unexpected behaviors as delayed persistent jobs instantly become available when application restarts.
JobManager 2.x versions change this behavior and does not reset the delay of persistent jobs on restart. This may create a problem if jobs were added when the device's clock is set to some unreasonable time but for common cases, it is more desirable.
You can get the v1 behavior by calling this method. Note that it will also effect jobs which require network with a timeout. Their timeouts will be triggered on restart if you call this method.
@NonNull public Configuration.Builder queueFactory(@Nullable QueueFactory queueFactory)
JobQueue to function.
By default, it will use SqliteJobQueue and
SimpleInMemoryPriorityQueue
You can provide your own implementation if they don't fit your needs. Make sure it passes all tests in
JobQueueTestBase to ensure it will work fine.queueFactory - your custom queue factory.@NonNull public Configuration.Builder jobSerializer(@NonNull SqliteJobQueue.JobSerializer jobSerializer)
SqliteJobQueue
queue for persistence. By default, it uses a
SqliteJobQueue.JavaSerializer
which will use default Java serialization.jobSerializer - The serializer to be used to persist jobs.@NonNull public Configuration.Builder networkUtil(@Nullable NetworkUtil networkUtil)
NetworkUtilImpl that queries ConnectivityManager
to check if network connection exists. You can provide your own if you need a custom logic (e.g. check your
server health etc).networkUtil - The NetworkUtil to be used by the JobManager. If it is null, JobManager
will use NetworkUtilImpl@NonNull public Configuration.Builder injector(@Nullable DependencyInjector injector)
injector - your dependency injector interface, if using one@NonNull public Configuration.Builder maxConsumerCount(int count)
Configuration.MAX_CONSUMER_COUNTcount - The max number of threads that JobManager can create to run jobs@NonNull public Configuration.Builder minConsumerCount(int count)
Configuration.MIN_CONSUMER_COUNTcount - The min of of threads that JobManager will keep alive even if they are idle.@NonNull public Configuration.Builder timer(@Nullable Timer timer)
timer - The timer to use@NonNull public Configuration.Builder customLogger(@Nullable CustomLogger logger)
Log.e.logger - The logger to be used by the JobManager.@NonNull public Configuration.Builder loadFactor(int loadFactor)
Configuration.DEFAULT_LOAD_FACTOR_PER_CONSUMERloadFactor - Number of available jobs per thread@NonNull public Configuration.Builder inTestMode()
SqliteJobQueue
to use an in-memory database.@NonNull public Configuration.Builder scheduler(@Nullable Scheduler scheduler, boolean batch)
JobScheduler.
Batching
By default, JobManager batches scheduling requests so that it will not call JobScheduler
too many times. For instance, if a persistent job that requires network is added, when
batching is enabled, JobManager creates the JobScheduler request with
BatchingScheduler.DEFAULT_BATCHING_PERIOD_IN_MS delay.
Any subsequent job request that has the same criteria will use the previous batching
request. This way, JobManager can avoid making a JobScheduler request for every job.
It will still execute the Job if it becomes available without waiting for the delay but
if the application is killed, the JobScheduler will wait until the delay passes before
waking up the application to consume the jobs.
scheduler - The scheduler to be usedbatch - Defines whether the scheduling requests should be batched or not.@NonNull public Configuration.Builder consumerThreadPriority(int threadPriority)
Configuration.DEFAULT_THREAD_PRIORITY.
If a ThreadFactory is provided, this value is ignored.
threadPriority - The thread priority to be used for new jobs@NonNull public Configuration.Builder scheduler(@Nullable Scheduler scheduler)
JobScheduler.
Batching
By default, JobManager batches scheduling requests so that it will not call JobScheduler
too many times. For instance, if a persistent job that requires network is added, when
batching is enabled, JobManager creates the JobScheduler request with
BatchingScheduler.DEFAULT_BATCHING_PERIOD_IN_MS delay.
Any subsequent job request that has the same criteria will use the previous batching
request. This way, JobManager can avoid making a JobScheduler request for every job.
It will still execute the Job if it becomes available without waiting for the delay but
if the application is killed, the JobScheduler will wait until the delay passes before
waking up the application to consume the jobs.
scheduler - The scheduler to be used@NonNull public Configuration.Builder threadFactory(@Nullable java.util.concurrent.ThreadFactory threadFactory)
When a factory is installed, it becomes its responsibility to configure
the Thread with proper group and priority. JobManager will use the Thread
as is.
threadFactory - The factory to be used@NonNull public Configuration build()