A Job Queue specifically written for Android to easily schedule jobs (tasks) that run in the background, improving UX and application stability.
Java
2,391
133 commits
updated Feb 24, 2022
==========================

Priority Job Queue is an implementation of a Job Queue specifically written for Android to easily schedule jobs (tasks) that run in the background, improving UX and application stability.
It is written primarily with flexibility & functionality in mind. This is an ongoing project, which we will continue to add stability and performance improvements.
Almost every application does work in a background thread. These "background tasks" are expected to keep the application responsive and robust, especially during unfavorable situations (e.g. limited network connectivity). In Android applications, there are several ways to implement background work:
Job Queue provides you a nice framework to do all of the above and more. You define your background tasks as Jobs and enqueue them to your JobManager instance. Job Manager will take care of prioritization, persistence, load balancing, delaying, network control, grouping etc. It also provides a nice lifecycle for your jobs to provide a better, consistent user experience.
Although not required, it is most useful when used with an event bus. It also supports dependency injection.
Since a code example is worth thousands of documentation pages, here it is.
File: PostTweetJob.java
// A job to send a tweet
public class PostTweetJob extends Job {
public static final int PRIORITY = 1;
private String text;
public PostTweetJob(String text) {
// This job requires network connectivity,
// and should be persisted in case the application exits before job is completed.
super(new Params(PRIORITY).requireNetwork().persist());
}
@Override
public void onAdded() {
// Job has been saved to disk.
// This is a good place to dispatch a UI event to indicate the job will eventually run.
// In this example, it would be good to update the UI with the newly posted tweet.
}
@Override
public void onRun() throws Throwable {
// Job logic goes here. In this example, the network call to post to Twitter is done here.
webservice.postTweet(text);
}
@Override
protected boolean shouldReRunOnThrowable(Throwable throwable) {
// An error occurred in onRun.
// Return value determines whether this job should retry running (true) or abort (false).
}
@Override
protected void onCancel() {
// Job has exceeded retry attempts or shouldReRunOnThrowable() has returned false.
}
}
File: TweetActivity.java
//...
public void onSendClick() {
final String status = editText.getText().toString();
if(status.trim().length() > 0) {
jobManager.addJobInBackground(new PostTweetJob(status));
editText.setText("");
}
}
...
That's it. :) Job Manager allows you to enjoy:
When user clicked the send button, onSendClick() was called, which creates a PostTweetJob and adds it to Job Queue for execution.
It runs on a background thread because Job Queue will make a disk access to persist the job.
Right after PostTweetJob is synchronized to disk, Job Queue calls DependencyInjector (if provided) which will inject fields into our job instance.
At PostTweetJob.onAdded() callback, we saved PostTweetJob to disk. Since there has been no network access up to this point, the time between clicking the send button and reaching onAdded() is within fracions of a second. This allows the implementation of onAdded() to inform UI to display the newly sent tweet almost instantly, creating a "fast" user experience. Beware, onAdded() is called on the thread job was added.
When it's time for PostTweetJob to run, Job Queue will call onRun() (and it will only be called if there is an active network connection, as dictated at the job's constructor).
By default, Job Queue uses a simple connection utility that checks ConnectivityManager (ensure you have ACCESS_NETWORK_STATE permission in your manifest). You can provide a custom implementation which can
add additional checks (e.g. your server stability). You should also provide a NetworkUtil which can notify Job Queue when network
is recovered so that Job Queue will avoid a busy loop and decrease # of consumers(default configuration does it for you).
Job Queue will keep calling onRun() until it succeeds (or reaches a retry limit). If onRun() throws an exception,
Job Queue will call shouldReRunOnThrowable() to allow you to handle the exception and decide whether to retry job execution or abort.
If all retry attempts fail (or when shouldReRunOnThrowable() returns false), Job Queue will call onCancel() to allow you to clean
your database, inform the user, etc.
AsyncTask lifecycles. This is true assuming you use an event bus to update your UI (you should).
At Path, we use greenrobot's EventBus; however, you can also go with your favorite. (e.g. [Square's Otto] (https://github.com/square/otto))SendMessageToNetwork jobs, you can group them by conversation ID. Through this approach, messages in the same conversation will send in the order they were enqueued, while messages between different conversations are still sent in parallel. This lets you effortlessly maximize network utilization and ensure data integrity.NetworkUtil if you need custom logic (e.g. you can create another instance of Job Queue which runs only if there is a wireless connection).We distribute artifacts through maven central repository.
Gradle: compile 'com.path:android-priority-jobqueue:1.1.2'
Maven:
<dependency>
<groupId>com.path</groupId>
<artifactId>android-priority-jobqueue</artifactId>
<version>1.1.2</version>
</dependency>
You can also download library jar, sources and javadoc from Maven Central.
We highly recommend checking how you can configure job manager and individual jobs.
BaseJob in favor of a more complete Job class.We are in the process of moving build system from ant to gradle. Right now, you can build with gradle but if you want to run tests, you'll need ant.
> cd jobqueue> ant clean build-jarThis will create a jar file under release folder.
cd jobqueue
ant clean test
Android Priority Jobqueue is made available under the MIT license:
The MIT License (MIT) Copyright (c) 2013 Path, Inc. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
18,957 followers · starred Jan 2014
2,619 followers · starred Jan 2014
270 followers · starred Jul 2015
258 followers · starred Jun 2015
A Job Queue specifically written for Android to easily schedule jobs (tasks) that run in the background, improving UX and application stability.
Java
2,391
133 commits
updated Feb 24, 2022
==========================

Priority Job Queue is an implementation of a Job Queue specifically written for Android to easily schedule jobs (tasks) that run in the background, improving UX and application stability.
It is written primarily with flexibility & functionality in mind. This is an ongoing project, which we will continue to add stability and performance improvements.
Almost every application does work in a background thread. These "background tasks" are expected to keep the application responsive and robust, especially during unfavorable situations (e.g. limited network connectivity). In Android applications, there are several ways to implement background work:
Job Queue provides you a nice framework to do all of the above and more. You define your background tasks as Jobs and enqueue them to your JobManager instance. Job Manager will take care of prioritization, persistence, load balancing, delaying, network control, grouping etc. It also provides a nice lifecycle for your jobs to provide a better, consistent user experience.
Although not required, it is most useful when used with an event bus. It also supports dependency injection.
Since a code example is worth thousands of documentation pages, here it is.
File: PostTweetJob.java
// A job to send a tweet
public class PostTweetJob extends Job {
public static final int PRIORITY = 1;
private String text;
public PostTweetJob(String text) {
// This job requires network connectivity,
// and should be persisted in case the application exits before job is completed.
super(new Params(PRIORITY).requireNetwork().persist());
}
@Override
public void onAdded() {
// Job has been saved to disk.
// This is a good place to dispatch a UI event to indicate the job will eventually run.
// In this example, it would be good to update the UI with the newly posted tweet.
}
@Override
public void onRun() throws Throwable {
// Job logic goes here. In this example, the network call to post to Twitter is done here.
webservice.postTweet(text);
}
@Override
protected boolean shouldReRunOnThrowable(Throwable throwable) {
// An error occurred in onRun.
// Return value determines whether this job should retry running (true) or abort (false).
}
@Override
protected void onCancel() {
// Job has exceeded retry attempts or shouldReRunOnThrowable() has returned false.
}
}
File: TweetActivity.java
//...
public void onSendClick() {
final String status = editText.getText().toString();
if(status.trim().length() > 0) {
jobManager.addJobInBackground(new PostTweetJob(status));
editText.setText("");
}
}
...
That's it. :) Job Manager allows you to enjoy:
When user clicked the send button, onSendClick() was called, which creates a PostTweetJob and adds it to Job Queue for execution.
It runs on a background thread because Job Queue will make a disk access to persist the job.
Right after PostTweetJob is synchronized to disk, Job Queue calls DependencyInjector (if provided) which will inject fields into our job instance.
At PostTweetJob.onAdded() callback, we saved PostTweetJob to disk. Since there has been no network access up to this point, the time between clicking the send button and reaching onAdded() is within fracions of a second. This allows the implementation of onAdded() to inform UI to display the newly sent tweet almost instantly, creating a "fast" user experience. Beware, onAdded() is called on the thread job was added.
When it's time for PostTweetJob to run, Job Queue will call onRun() (and it will only be called if there is an active network connection, as dictated at the job's constructor).
By default, Job Queue uses a simple connection utility that checks ConnectivityManager (ensure you have ACCESS_NETWORK_STATE permission in your manifest). You can provide a custom implementation which can
add additional checks (e.g. your server stability). You should also provide a NetworkUtil which can notify Job Queue when network
is recovered so that Job Queue will avoid a busy loop and decrease # of consumers(default configuration does it for you).
Job Queue will keep calling onRun() until it succeeds (or reaches a retry limit). If onRun() throws an exception,
Job Queue will call shouldReRunOnThrowable() to allow you to handle the exception and decide whether to retry job execution or abort.
If all retry attempts fail (or when shouldReRunOnThrowable() returns false), Job Queue will call onCancel() to allow you to clean
your database, inform the user, etc.
AsyncTask lifecycles. This is true assuming you use an event bus to update your UI (you should).
At Path, we use greenrobot's EventBus; however, you can also go with your favorite. (e.g. [Square's Otto] (https://github.com/square/otto))SendMessageToNetwork jobs, you can group them by conversation ID. Through this approach, messages in the same conversation will send in the order they were enqueued, while messages between different conversations are still sent in parallel. This lets you effortlessly maximize network utilization and ensure data integrity.NetworkUtil if you need custom logic (e.g. you can create another instance of Job Queue which runs only if there is a wireless connection).We distribute artifacts through maven central repository.
Gradle: compile 'com.path:android-priority-jobqueue:1.1.2'
Maven:
<dependency>
<groupId>com.path</groupId>
<artifactId>android-priority-jobqueue</artifactId>
<version>1.1.2</version>
</dependency>
You can also download library jar, sources and javadoc from Maven Central.
We highly recommend checking how you can configure job manager and individual jobs.
BaseJob in favor of a more complete Job class.We are in the process of moving build system from ant to gradle. Right now, you can build with gradle but if you want to run tests, you'll need ant.
> cd jobqueue> ant clean build-jarThis will create a jar file under release folder.
cd jobqueue
ant clean test
Android Priority Jobqueue is made available under the MIT license:
The MIT License (MIT) Copyright (c) 2013 Path, Inc. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
18,957 followers · starred Jan 2014
2,619 followers · starred Jan 2014
270 followers · starred Jul 2015
258 followers · starred Jun 2015