DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

How does AI transform chaos engineering from an experiment into a critical capability? Learn how to effectively operationalize the chaos.

Data quality isn't just a technical issue: It impacts an organization's compliance, operational efficiency, and customer satisfaction.

Are you a front-end or full-stack developer frustrated by front-end distractions? Learn to move forward with tooling and clear boundaries.

Developer Experience: Demand to support engineering teams has risen, and there is a shift from traditional DevOps to workflow improvements.

Related

  • Mastering Concurrency: An In-Depth Guide to Java's ExecutorService
  • Java Concurrency Evolution
  • Ulyp: Recording Java Execution Flow for Faster Debugging
  • The Challenges and Pitfalls of Using Executors in Java

Trending

  • Enhancing SQL Server Security With AI-Driven Anomaly Detection
  • Integrating Apache Spark With Drools: A Loan Approval Demo
  • The Missing Infrastructure Layer: Why AI's Next Evolution Requires Distributed Systems Thinking
  • Mastering Fluent Bit: Controlling Logs With Fluent Bit on Kubernetes (Part 4)
  1. DZone
  2. Coding
  3. Java
  4. Distributed Tasks Execution and Scheduling in Java, Powered By Redis

Distributed Tasks Execution and Scheduling in Java, Powered By Redis

A walk through the new features of Redisson node, a way to perform distributed task execution using Redis.

By 
Nikita Koksharov user avatar
Nikita Koksharov
·
Sep. 13, 16 · Tutorial
Likes (11)
Comment
Save
Tweet
Share
43.0K Views

Join the DZone community and get the full member experience.

Join For Free

The ability to immediately execute or schedule a task or job is becoming a typical requirement for a modern distributed Java application. Such requirements have became more essential for those who also use Redis.

Redisson is now providing a new convinient way to perform such distributed task execution and scheduling through the standard JDK ExecutorService and ScheduledExecutorService API, with submitted tasks executed on Redisson nodes, which are connected to the same Redis database.

Image title


Redisson Node

Redisson Node is a standalone Java application whose sole purpose is to execute tasks that have been submitted to the same Redis instance it's connected to. A Redisson Node can be understood as a remote worker node in a distributed environment.

It can also be spawned as an independant process inside the main application via a Redisson instance.

All task classes are loaded dynamically so you don't need to have them in the classpath of Redisson Node and restart it due to task class changes.

Task Definition

A task should implement either java.util.concurrent.Callable or java.lang.Runnable interface.

Here is an example using Callable interface:

public class CallableTask implements Callable<Long> {

    @RInject
    private RedissonClient redissonClient;

    private long anyParam;

    public CallableTask() {
    }

    public CallableTask(long anyParam) {
        this.anyParam = anyParam;
    }

    @Override
    public Long call() throws Exception {
        // ...
    }

}

This is an example using Runnable interface:

public class RunnableTask implements Runnable {

    @RInject
    private RedissonClient redissonClient;

    private long anyParam;

    public RunnableTask() {
    }

    public RunnableTask(long anyParam) {
        this.anyParam = anyParam;
    }

    @Override
    public void run() {
        // ...
    }

}

Task can be parameterized via a constructor. A submitted task can be given access to a Redisson instance via @RInject annotation. This provides the task access to all Redisson features: Redis based Maps, Multimaps, Sets, Lists, Queues, Locks, Semaphores, PublishSubscribe, and many other objects, collections, locks, and services.

Submitting Task for Execution

It's pretty easy to submit a task through our ExecutorService API since RExecutorService already implements java.util.concurrent.ExecutorService.

RExecutorService executorService = redisson.getExecutorService("myExecutor");

executorService.submit(new RunnableTask());
// or with parameter
executorService.submit(new RunnableTask(41));

executorService.submit(new CallableTask());
// or with parameter
executorService.submit(new CallableTask(53));

Submitting Task for Scheduled Execution

Scheduled tasks are submitted through using the java.util.concurrent.ScheduledExecutorService interface, which is implemented by the RScheduledExecutorService.

RScheduledExecutorService executorService = redisson.getExecutorService("myExecutor");

executorService.schedule(new CallableTask(), 10, TimeUnit.MINUTES);
// or 
executorService.schedule(new RunnableTask(), 5, TimeUnit.SECONDS);

executorService.scheduleAtFixedRate(new RunnableTask(), 10, 25, TimeUnit.HOURS);
// or
executorService.scheduleWithFixedDelay(new RunnableTask(), 5, 10, TimeUnit.HOURS);

Scheduling a Task With Cron Expression

As we all understand how high the level of complexity of a moden application can be, the tasks scheduler service allows a user to define more complex scheduling using cron expressions. It is fully compatible with Quartz cron format.

RScheduledExecutorService executorService = redisson.getExecutorService("myExecutor");

executorService.schedule(new RunnableTask(), CronSchedule.of("10 0/5 * * * ?"));
// or
executorService.schedule(new RunnableTask(), CronSchedule.dailyAtHourAndMinute(10, 5));
// or
executorService.schedule(new RunnableTask(), CronSchedule.weeklyOnDayAndHourAndMinute(12, 4, Calendar.MONDAY, Calendar.FRIDAY));

Task Cancellation

Any task can be cancelled at any stage:

Future<?> f = executorService.schedule(...);
// or
Future<?> f = executorService.submit(...);

f.cancel(true);

No extra work is required to handle cancellation except in some cases when a task is already in the running stage. In this case, cancellation handling is similar to performing a thread interruption in Java.

Let's assume we have a task which aggregates all values in a very big Redis map, which may take a long time:

public class CallableTask implements Callable<Long> {

    @RInject
    private RedissonClient redissonClient;

    @Override
    public Long call() throws Exception {
        RMap<String, Integer> map = redissonClient.getMap("myMap");
        Long result = 0;
        for (Integer value : map.values()) {           
        // check if task has been canceled
           if (Thread.currentThread().isInterrupted()) {
                // task has been canceled
                return null;
           }
           result += value;
        }
        return result;
    }

}

Extended Asynchronous Mode

All standard methods exposed in the java.util.concurrent.ScheduledExecutorService and java.util.concurrent.ExecutorService interfaces submit tasks synchronously and receive results in a asynchronous manner through the standard java.util.concurrent.Future object. Redisson also offers a set of RExecutorServiceAsync.*Async companion methods which can also be used to submit tasks fully asynchronously, and allows get task id and binds Listeners to a Future object.

RScheduledExecutorService executorService = redisson.getExecutorService("myExecutor");

RFuture<MyResultObject> future = executorService.submitAsync(new CallableTask());
// or
RScheduledFuture<MyResultObject> future = executorService.scheduleAsync(new RunnableTask(), 5, TimeUnit.SECONDS);
// or
RScheduledFuture<MyResultObject> future = executorService.scheduleAtFixedRateAsync(new RunnableTask(), 10, 25, TimeUnit.HOURS);

future.addListener(new FutureListener<MyResultObject>() {
     public void operationComplete(Future<MyResultObject> f) { 
         // ...
     }
});

// cancel task by id
String taskId = future.getId();
// ...
executorService.cancelScheduledTask(taskId);

Redisson Node is the latest game-changing feature we have added to the collection of useful toolsets provided by Redisson. We are commited to bring more features into the family in the coming future, please sit tight and be patient.

Task (computing) job scheduling Redis (company) Java (programming language) Execution (computing)

Opinions expressed by DZone contributors are their own.

Related

  • Mastering Concurrency: An In-Depth Guide to Java's ExecutorService
  • Java Concurrency Evolution
  • Ulyp: Recording Java Execution Flow for Faster Debugging
  • The Challenges and Pitfalls of Using Executors in Java

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • [email protected]

Let's be friends: