How do I get JobRunr to detect my scheduled background job in a Spring controller/service?

3.3k views Asked by At

I have been looking into using JobRunr for starting background jobs on my Spring MVC application, as I really like the simplicity of it, and the ease of integrating it into an IoC container.

I am trying to create a simple test scheduled job that writes a line of text to my configured logger every minute, but I'm struggling to figure out how to get the JobRunr background job server to detect it and queue it up. I am not using Spring Boot so I am just using the generic jobrunr Maven artifact rather than the "Spring Boot Starter". My setup is as follows:

pom.xml

        <dependency>
            <groupId>org.jobrunr</groupId>
            <artifactId>jobrunr</artifactId>
            <version>2.0.0</version>
        </dependency>

ApplicationConfig.java

    @Bean
    public JobMapper jobMapper() {
        return new JobMapper(new JacksonJsonMapper());
    }

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

    @Bean
    @DependsOn("storageProvider")
    public JobScheduler jobScheduler(StorageProvider storageProvider, ApplicationContext applicationContext) {
        return JobRunr.configure().useStorageProvider(storageProvider)
                                                       .useJobActivator(applicationContext::getBean)
                                                       .useDefaultBackgroundJobServer()
                                                       .useDashboard()
                                                       .useJmxExtensions()
                                                       .initialize();
    }

BackgroundJobsController.java

@Controller
public class BackgroundJobsController {
    private final Logger logger = LoggerFactory.getLogger(getClass());
    private @Autowired JobScheduler jobScheduler;

    @Job(name = "Test")
    public void executeJob() {
        BackgroundJob.scheduleRecurrently(Cron.minutely(), () -> logger.debug("It works!"));
        jobScheduler.scheduleRecurrently(Cron.minutely(), () -> logger.debug("It works too!"));
    }
}

As you can see, I have tried both methods of initiating the background job in the executeJob method. The issue is basically getting Jobrunr to detect the jobs - is it simply a case of somehow triggering the executeJob method upon startup of the application? If so, does anyone know the most simple way to do that? Previously I have used the Spring @Scheduled annotation to automatically run through methods in a Service/Controller class upon startup of the application, so I was hoping there was a straightforward way to get Jobrunr to pick up the scheduled tasks I am trying to create. Apologies if it is something stupid that I have overlooked. I've spent a good few hours trying different things and reading through the documentation!

Thanks in advance!

3

There are 3 answers

1
Ben On BEST ANSWER

Have you tried annotating your executeJob Method with a @PostConstruct ? That way upon initialisation of your application, the jobs would be registered to the JobServer.

I believe the @Job annotation is meant fo the method of the job itself. (In your case the debug method).

1
rdehuyss On

There are different ways for doing so:

This is one, annotating a method with @PostConstruct is indeed another.

@SpringBootApplication
@Import(JobRunrExampleConfiguration.class)
public class JobRunrApplication {

    public static void main(String[] args) {
        ConfigurableApplicationContext applicationContext = SpringApplication.run(JobRunrApplication.class, args);

        JobScheduler jobScheduler = applicationContext.getBean(JobScheduler.class);
        jobScheduler.<SampleJobService>scheduleRecurrently("recurring-sample-job", every5minutes(), x -> x.executeSampleJob("Hello from recurring job"));
    }
}

You can see an example here: https://github.com/jobrunr/example-java-mag/blob/main/src/main/java/org/jobrunr/examples/JobRunrApplication.java

0
Bart Mertens On

There is now a new way to do so:

You can add @Recurring to any Spring Boot, Micronaut or Quarkus bean method. A Spring Boot example:

@Component
public class SomeService {

    @Recurring(id="recurring-job-every-5-min" interval = "PT5M")
    @Job(name="job name for the dashboard")
    public void runEvery5Minutes() {
        // business logic comes here
    }
}

For more info, see the JobRunr documentation.