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
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Compliance Reporting Without Losing the Spreadsheet or the Control
  • Stop Writing Database Migration Scripts: Declarative Schema Sync for Java
  • Searching Kafka Without an Index: Lessons From Building Message Search for Kafdrop
  • ScyllaDB in Enterprise Java: High-Performance NoSQL With Eclipse JNoSQL

Trending

  • New York City Taxi Trips: Putting Neo4j and DuckDB to Work
  • When a Coding Agent Fixes the Test Instead of the Bug
  • Member Spotlight: Shai Almong
  • How to Build Your Own AI SRE From Scratch

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Data Engineering
  3. Big Data
  4. Automate Long Running Routine Task With JobRunr and Spring Boot

Automate Long Running Routine Task With JobRunr and Spring Boot

This article will explain a simple way to deploy, manage and monitor long-running background tasks, especially ETL systems.

By 
MORRISON IDIASIRUE
·
Mar. 28, 23 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
6.9K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

Today I shall explain a simple way to deploy, manage and monitor long-running background tasks, especially ETL systems. The common questions that every big data engineer would ask concerning a deployed ETL tool are the performance of the tool, what is causing the break in the processing pipeline, how easy it is to chain several jobs, and lots more. 

JobRunr makes it easy to schedule background long-running tasks. it is a distributed Java background job scheduler with lots of exciting features like scheduled and delayed jobs, fire and forget jobs, recurring jobs, queuing jobs and batches, and job chaining. Please note that some of these features are only available in the pro version. However, most of JobRunr's features are completely open-sourced. The library also provides a nice interface to manage and monitor all your jobs. I find this really fascinating as end users can adopt this visual for other useful purposes; for instance, from the UI, recurring jobs can be triggered manually, it provides a report on when next a job is expected to run, and users get to see nice-looking reports of each job processing time and also hardware performance information of the server where the application is deployed, isn't this exciting?

Next, let's dive into integrating JobRunr into our Java application:

Technologies Used:

  • Spring Boot
  • JobRunr
  • Jackson
  • Java 11

Start with creating a sample spring boot application; I recommend using Spring Initializr for rapid setup.

Add JobRunr to the Spring Boot Application

Add this to the list of dependencies in your pom file:

Java
 
 <dependency>
            <groupId>org.jobrunr</groupId>
            <artifactId>jobrunr-spring-boot-starter</artifactId>
            <version>${jobrunr.version}</version>
        </dependency>
        
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.11.0</version>
        </dependency>
       


Configure JobRunr to use in-memory storage. Do this by adding a bean class to your project.

Java
 
@Configuration
public class StorageProviderConfig {

    @Bean
    public StorageProvider storageProvider(JobMapper jobMapper) {
        InMemoryStorageProvider storageProvider = new InMemoryStorageProvider();
        storageProvider.setJobMapper(jobMapper);
        return storageProvider;
    }
    
}


JobRunr has an intuitive dashboard that makes it easy to monitor the status of Jobs. Add the following lines of code to your application.properties to activate the UI Dashboard.

 
org.jobrunr.background-job-server.enabled=true
org.jobrunr.dashboard.enabled=true


Next, create a simple service that would contain a logic that mimics a long-running task.

Java
 
@Service
public class SampleJobService {

    public static final long EXECUTION_TIME = 5000L;

    private Logger logger = LoggerFactory.getLogger(getClass());

    private AtomicInteger count = new AtomicInteger();

    @Job(name = "The sample job with variable %0", retries = 2)
    public void executeSampleJob(String variable) {

        logger.info("The sample job has begun. The variable you passed is {}", variable);
        try {
            Thread.sleep(EXECUTION_TIME);
        } catch (InterruptedException e) {
            logger.error("Error while executing sample job", e);
        } finally {
            count.incrementAndGet();
            logger.info("Sample job has finished...");
        }
    }
}


The job above is expected to accept an argument when called, wait for five seconds, and print "Sample job has finished." It has been configured to retry for two attempts (in case of any failure).

Finally, we need to schedule the Job we created previously. Add the following lines of code to your Spring Boot entry class.

Java
 
 @Autowired
    private JobScheduler jobScheduler;

 @PostConstruct
    public void scheduleRecurrently() {
        jobScheduler.<SampleJobService>scheduleRecurrently(Cron.every5minutes(), x -> x.executeSampleJob("a recurring job"));
    }


Voila, we have successfully scheduled the job to execute every five minutes. Run the application, and you should be able to access the JobRunr dashboard on port 8,000(the default port for JobRunr, you can change this for your needs).

JonRunr Dashboard


The purpose of this article is to give you a quick start to using JobRunr. You can build on this to develop more complex workflows; for example, it's possible to chain multiple Jobs sequentially with JobRunr. For more information, kindly visit JobRunr's official website 

Extract, transform, load Job scheduler Java (programming language)

Opinions expressed by DZone contributors are their own.

Related

  • Compliance Reporting Without Losing the Spreadsheet or the Control
  • Stop Writing Database Migration Scripts: Declarative Schema Sync for Java
  • Searching Kafka Without an Index: Lessons From Building Message Search for Kafdrop
  • ScyllaDB in Enterprise Java: High-Performance NoSQL With Eclipse JNoSQL

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

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 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook