Schedule your events by JobRunr – firt touch

Today, I would like to show you how to easily schedule your events in your backend using the JobRunr framework.

I’ve been searching for a framework that allows scheduling jobs and monitoring their statuses, and JobRunr stands out for offering a GUI to track job statuses. This feature is a significant benefit from my perspective.

In my use case, I consume data from a queue and need to schedule services to start mining data on specific dates. JobRunr supports a fluent API and integrates with Spring Starter, Micronaut, and Quarkus.

I use Spring Web and its integration with JobRunr. The dependency setup looks like this:

implementation("org.springframework.boot:spring-boot-starter-data-jpa")
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
implementation("org.jobrunr:jobrunr-spring-boot-3-starter:7.2.0")
implementation("org.jetbrains.kotlin:kotlin-reflect")
runtimeOnly("com.h2database:h2")

You will need a database because JobRunr persists all of its jobs and schedules into a database. For testing purposes, I used H2.

Here’s an example of the application.yaml where you can see how to set up the GUI and job schedulers:

org:
  jobrunr:
    job-scheduler:
      enabled: true
    background-job-server:
      enabled: true
  • job-scheduler.enabled: true: This line enables the job scheduling feature of JobRunr, allowing you to schedule jobs to run at specific times or intervals.
  • background-job-server.enabled: true: This line enables the background job server feature of JobRunr, allowing the application to process and run the scheduled or enqueued jobs.

Or you can use initJobRunr bean:

    @Bean
    fun initJobRunr(dataSource: DataSource, jobActivator: JobActivator): JobRunrConfigurationResult {
        return JobRunr.configure()
            .useJobActivator(jobActivator)
            .useStorageProvider(SqlStorageProviderFactory.using(dataSource))
            .useBackgroundJobServer()
            .useDashboard(8801)
            .initialize()
    }

  @Bean
    fun initJobScheduler(jobRunrConfigurationResult: JobRunrConfigurationResult): JobScheduler {
        return jobRunrConfigurationResult.jobScheduler
    }

    @Bean
    fun initJobRequestScheduler(jobRunrConfigurationResult: JobRunrConfigurationResult): JobRequestScheduler {
        return jobRunrConfigurationResult.jobRequestScheduler
    }
  • JobRunr.configure(): Starts JobRunr configuration.
  • useJobActivator(jobActivator): Sets the job activator for creating job instances.
  • useStorageProvider(SqlStorageProviderFactory.using(dataSource)): Configures SQL storage provider with the specified data source.
  • useBackgroundJobServer(): Configures and enables the background job server.
  • useDashboard(8801): Sets up a web dashboard on port 8801 for job monitoring and management.
  • initialize(): Finalizes and activates the configuration, starting the job server and dashboard.

This configuration sets up the JobScheduler and JobRequestScheduler beans, enabling you to schedule and manage your jobs effectively.

After start up your application you can see init db scripts for jobrunr:

MigrationByPath{path=/org/jobrunr/storage/sql/common/migrations/v000__create_migrations_table.sql}
MigrationByPath{path=/org/jobrunr/storage/sql/common/migrations/v001__create_job_table.sql}
MigrationByPath{path=/org/jobrunr/storage/sql/common/migrations/v002__create_recurring_job_table.sql}
MigrationByPath{path=/org/jobrunr/storage/sql/common/migrations/v003__create_background_job_server_table.sql}
MigrationByPath{path=/org/jobrunr/storage/sql/common/migrations/v004__create_job_stats_view.sql}
MigrationByPath{path=/org/jobrunr/storage/sql/common/migrations/v005__update_job_stats_view.sql}
MigrationByPath{path=/org/jobrunr/storage/sql/common/migrations/v006__alter_table_jobs_add_recurringjob.sql}
MigrationByPath{path=/org/jobrunr/storage/sql/common/migrations/v007__alter_table_backgroundjobserver_add_delete_config.sql}
MigrationByPath{path=/org/jobrunr/storage/sql/common/migrations/v008__alter_table_jobs_increase_jobAsJson_size.sql}
MigrationByPath{path=/org/jobrunr/storage/sql/h2/migrations/v009__change_jobrunr_job_counters_to_jobrunr_metadata.sql}
MigrationByPath{path=/org/jobrunr/storage/sql/h2/migrations/v010__change_job_stats.sql}
MigrationByPath{path=/org/jobrunr/storage/sql/common/migrations/v011__change_sqlserver_text_to_varchar.sql}
MigrationByPath{path=/org/jobrunr/storage/sql/common/migrations/v012__change_oracle_alter_jobrunr_metadata_column_size.sql}
MigrationByPath{path=/org/jobrunr/storage/sql/common/migrations/v013__alter_table_recurring_job_add_createdAt.sql}
MigrationByPath{path=/org/jobrunr/storage/sql/common/migrations/v014__improve_job_stats.sql}
MigrationByPath{path=/org/jobrunr/storage/sql/common/migrations/v015__alter_table_backgroundjobserver_add_name.sql}

Dashboard is available on you specific port:

JobRunr Dashboard using H2StorageProvider started at http://localhost:8801/dashboard

There is first look how UI looks like

Now we need prepare our service which we want to use as a job

After that we need to create job scheduler service which will be resposible for creating jobs at the first time we want to create a single job fire and forgot, it does not matter if will be success or not just send for this purpose we will use enqueueing jobs

And because I need to run this job scheduler, for demo purposes i create application runner where after start up a call jobScheduler service and run a specific method for job creation.

Example of notification service which will send notification for demo purposes we just

There you can si how our first job run successfully

Whole implementation you can find in our github repo

In the next lecture we will focus on another jobs which you can use with JobRunr

Related Post

Leave a Reply

Your email address will not be published. Required fields are marked *

×