I am looking to replace Quartz as a job scheduler in our project. We already use Redis with cluster support as a distributed cache layer and we thought that maybe we could use Redis for job scheduling too. Has anyone implemented job scheduling in Java using Redis? I searched but could not find a library for this purpose. So I am starting to think that this is maybe not a popular solution?
Take a look at Redisson. It allows to schedule and execute tasks (with cron-expression support) using simple ScheduledExecutorService api and based on Redis queue.
Here is an example. First define a task using java.lang.Runnable
interface. Each task can access to Redis instance via injected RedissonClient
object.
public class RunnableTask implements Runnable {
@RInject
private RedissonClient redissonClient;
@Override
public void run() throws Exception {
RMap<String, Integer> map = redissonClient.getMap("myMap");
Long result = 0;
for (Integer value : map.values()) {
result += value;
}
redissonClient.getTopic("myMapTopic").publish(result);
}
}
Submit it to Redis based ExecutorService.
RScheduledExecutorService executorService = redisson.getExecutorService("myExecutor");
RScheduledFuture<?> future = executorService.schedule(new CallableTask(), 10, 20, TimeUnit.MINUTES);
future.get();
// or cancel it
future.cancel(true);
// cancel by taskId
executorService.cancelTask(future.getTaskId());
Examples with cron expressions:
executorService.schedule(new RunnableTask(), CronSchedule.of("10 0/5 * * * ?"));
executorService.schedule(new RunnableTask(), CronSchedule.dailyAtHourAndMinute(10, 5));
executorService.schedule(new RunnableTask(), CronSchedule.weeklyOnDayAndHourAndMinute(12, 4, Calendar.MONDAY, Calendar.FRIDAY));
All tasks are distributed across Redisson nodes. You can run these nodes as much as you need.
How about Redis Labs' redis-quartz:
RedisJobStore A Quartz Scheduler JobStore that uses Redis for persistent storage.
We'd appreciate any feedback you have :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With