kotlinmicronautjobrunr

How to inject a JobScheduler on a micronaut controller?


I'm trying to @Inject a JobScheduler from jobrunr into my micronaut @Controller but I'm getting this error:

Internal Server Error: Failed to inject value for parameter [jobScheduler] of class: com.myapp.http.controllers.MyController\n\nMessage: No bean of type [org.jobrunr.scheduling.JobScheduler] exists. Make sure the bean is not disabled by bean requirements (enable trace logging for 'io.micronaut.context.condition' to check) and if the bean is enabled then ensure the class is declared a bean and annotation processing is enabled (for Java and Kotlin the 'micronaut-inject-java' dependency should be configured as an annotation processor).\nPath Taken: new ReturnController(JobScheduler jobScheduler) --> new ReturnController([JobScheduler jobScheduler])

I've installed micronaut-inject-java, org.jobrunr:jobrunr-micronaut-feature:5.2.0 and my controller look like this:

@Controller("/return")
class MyController(private val jobScheduler: JobScheduler) {

    @Post
    fun create() = jobScheduler.schedule(Instant.now()) { println("scheduled...") }
}

On my application.yml I've also configured the datasource:

datasources:
  default:
    url: jdbc:postgresql://localhost:5432/postgres
    driverClassName: org.postgresql.Driver
    username: postgres
    password: postgres
    dialect: POSTGRES

How can I inject JobScheduler into my controller?


Solution

  • See the project at github.com/jeffbrown/injectjobscheduler.

    src/main/kotlin/com/example/MyController.kt

    package com.example
    
    import io.micronaut.http.MediaType
    import io.micronaut.http.annotation.Controller
    import io.micronaut.http.annotation.Get
    import org.jobrunr.scheduling.JobScheduler
    
    @Controller("/return")
    class MyController(private val jobScheduler: JobScheduler) {
    
        @Get(produces = [MediaType.TEXT_PLAIN])
        fun index(): String {
            return jobScheduler.javaClass.name
        }
    }
    

    It may be that you have not enabled the scheduler. See src/main/resources/application.yml#L8-L11:

    ---
    jobrunr:
      job-scheduler:
        enabled: true