https://github.com/davidmarquis/redis-scheduler
Java implementation of a lightweight distributed task scheduler based on Redis with support for Jedis, Lettuce and Spring Data Redis
https://github.com/davidmarquis/redis-scheduler
java jedis lettuce redis scheduler spring-data-redis tasks
Last synced: 12 months ago
JSON representation
Java implementation of a lightweight distributed task scheduler based on Redis with support for Jedis, Lettuce and Spring Data Redis
- Host: GitHub
- URL: https://github.com/davidmarquis/redis-scheduler
- Owner: davidmarquis
- License: mit
- Created: 2013-12-13T21:22:25.000Z (over 12 years ago)
- Default Branch: master
- Last Pushed: 2023-08-11T09:19:44.000Z (almost 3 years ago)
- Last Synced: 2025-04-25T04:01:55.104Z (about 1 year ago)
- Topics: java, jedis, lettuce, redis, scheduler, spring-data-redis, tasks
- Language: Java
- Homepage:
- Size: 61.5 KB
- Stars: 82
- Watchers: 4
- Forks: 30
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
redis-scheduler
===============
Distributed Scheduler using Redis for Java applications.
What is this?
-------------
`redis-scheduler` is a Java implementation of a distributed scheduler using Redis. It has the following features:
- **Useable in a distributed environment**: Uses Redis transactions for effectively preventing a task to be run on
multiple instances of the same application.
- **Lightweight**: Uses a single thread.
- **Configurable polling**: Polling delay can be configured to tweak execution precision (at the cost of performance)
- **Multiple schedulers support**: You can create multiple schedulers in the same logical application if you need to.
- **Support for multiple client libraries**: Drivers exist for [Jedis](https://github.com/xetorthio/jedis), [Lettuce](https://lettuce.io/) and [Spring Data's RedisTemplate](https://projects.spring.io/spring-data-redis/)
High level concepts
-------------------
#### Scheduled Task
A scheduled task is a job that you need to execute in the future at a particular time.
In `redis-scheduler`, a task is represented solely by an arbitrary string identifier that has no particular meaning to the library.
It's your application that has to make sense of this identifier.
#### Scheduler
`RedisTaskScheduler`: This interface is where you submit your tasks for future execution. Once submitted, a task will only be
executed at or after the trigger time you provide.
#### `TaskTriggerListener` interface
This is the main interface you must implement to actually run the tasks once they are due for execution. The library will
call the `taskTriggered` method for each task that is due for execution.
Building the project
--------------------
``` bash
mvn package
```
Maven dependency
----------------
This artifact is published on Maven Central:
``` xml
com.github.davidmarquis
redis-scheduler
3.0.0
```
You'll need to add one of the specific dependencies to use the different available drivers:
To use with Lettuce:
``` xml
io.lettuce
lettuce-core
5.0.3.RELEASE
```
To use with Jedis:
``` xml
redis.clients
jedis
2.9.0
```
To use with Spring Data Redis:
``` xml
org.springframework.data
spring-data-redis
1.8.11.RELEASE
```
Usage with Lettuce
------------------
The scheduler must be instantiated with the `LettuceDriver`:
``` java
RedisClient client = RedisClient.create(RedisURI.create("localhost", 6379));
RedisTaskScheduler scheduler = new RedisTaskScheduler(new LettuceDriver(client), new YourTaskTriggerListener());
scheduler.start();
```
Usage with Jedis
----------------
The scheduler must be instantiated with the `JedisDriver`:
``` java
JedisPool pool = new JedisPool("localhost", 6379);
RedisTaskScheduler scheduler = new RedisTaskScheduler(new JedisDriver(pool), new YourTaskTriggerListener());
scheduler.start();
```
Usage with Spring
-----------------
First declare the base beans for Redis connectivity (if not already done in your project). This part can be different
for your project.
``` xml
```
Finally, declare the scheduler instance:
``` xml
```
As noted above, `RedisTaskScheduler` expects an implementation of the `TaskTriggerListener` interface which it will notify when a task is due for execution. You must implement this interface yourself and provide it to the scheduler as a constructor argument.
See the the test Spring context in `test/resources/application-context-test.xml` for a complete working example of the setup.
Scheduling a task in the future
-------------------------------
``` java
scheduler.schedule("mytask", new GregorianCalendar(2015, Calendar.JANUARY, 1, 4, 45, 0));
```
This would schedule a task with ID "mytask" to be run at 4:45AM on January 1st 2015.
Be notified once a task is due for execution
--------------------------------------------
``` java
public class MyTaskTriggerListener implements TaskTriggerListener {
public void taskTriggered(String taskId) {
System.out.printf("Task %s is due for execution.", taskId);
}
}
```
Customizing polling delay
----------------------------------
By default, polling delay is set to a few seconds (see implementation `RedisTaskScheduler` for actual value). If
you need your tasks to be triggered with more precision, decrease the polling delay using the `pollingDelayMillis` attribute of `RedisTaskScheduler`:
In Java:
``` java
scheduler.setPollingDelayMillis(500);
```
With Spring:
``` xml
```
Increasing polling delay comes with a cost: higher load on Redis and your connection.
Try to find the best balance for your needs.
Retry polling when a Redis connection error happens
---------------------------------------------------
Retries can be configured using the `maxRetriesOnConnectionFailure` property on `RedisTaskScheduler`:
In Java:
``` java
scheduler.setMaxRetriesOnConnectionFailure(5);
```
With Spring:
``` xml
```
After the specified number of retries, the polling thread will stop and log an error.