2
votes

I'm just implements quartz scheduler with kotlin and spring boot.

kotlin.UninitializedPropertyAccessException: lateinit property userController has not been initialized

I know userController Object is not created with @autowired basically in kotlin lateinit create object lazy so i d'not have idea to fix this issue. any possible way to create object in kotlin with eagerly to fix this issue


I'm Getting the following error:

Full Error Log : For reference


21:02:00.037 [DatabaseScheduler_Worker-1] ERROR org.quartz.core.JobRunShell - Job commentJobGroup.commentJob threw an unhandled Exception: 
kotlin.UninitializedPropertyAccessException: lateinit property userController has not been initialized
    at com.lovevirus.kotlinQuartzScheduler.scheduler.Jobs.CommentJob.getUserController(CommentJob.kt:17)
    at com.lovevirus.kotlinQuartzScheduler.scheduler.Jobs.CommentJob.execute(CommentJob.kt:21)
    at org.quartz.core.JobRunShell.run(JobRunShell.java:202)
    at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:573)
21:02:00.038 [DatabaseScheduler_Worker-1] ERROR org.quartz.core.ErrorLogger - Job (commentJobGroup.commentJob threw an exception.
org.quartz.SchedulerException: Job threw an unhandled exception.
    at org.quartz.core.JobRunShell.run(JobRunShell.java:213)
    at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:573)
Caused by: kotlin.UninitializedPropertyAccessException: lateinit property userController has not been initialized
    at com.lovevirus.kotlinQuartzScheduler.scheduler.Jobs.CommentJob.getUserController(CommentJob.kt:17)
    at com.lovevirus.kotlinQuartzScheduler.scheduler.Jobs.CommentJob.execute(CommentJob.kt:21)
    at org.quartz.core.JobRunShell.run(JobRunShell.java:202)
    ... 1 common frames omitted 

Code:


import org.quartz.Job
import org.quartz.JobExecutionContext
import org.springframework.stereotype.Component
import com.lovevirus.kotlinQuartzScheduler.controller.UserController;
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired


@Component
class CommentJob : Job {
    private val logger: Logger = LoggerFactory.getLogger(CommentJob::class.java)
    @Autowired
    lateinit var userController : UserController;

    override fun execute(p0: JobExecutionContext?) {
      logger.debug("Inside Comment Job");
        userController.getUser();
       logger.debug("End Comment Job");
    }
}



Thanks in advance
2
What is UserController class? - MMG
It’s a controller call where business logic are available on this class - user9731810
Why you have used lateinit? Use var instead of that - MMG
In Kotlin i'm not able to use var without lateinit. if i do that IDE Throws an error. I'm also done constructor injection also but it's not working - user9731810
You should initialize it - MMG

2 Answers

0
votes

Have you implemented a custom SpringBeanJobFactory that would allow dependency autowiring?

class AutowiringSpringBeanJobFactory : SpringBeanJobFactory(), ApplicationContextAware {

@Transient
private var beanFactory: AutowireCapableBeanFactory? = null

override fun setApplicationContext(applicationContext: ApplicationContext) {
    beanFactory = applicationContext.autowireCapableBeanFactory
}

override fun createJobInstance(bundle: TriggerFiredBundle): Any {
    val job = super.createJobInstance(bundle)
    beanFactory!!.autowireBean(job)
    return job
}

}

0
votes

The controller can also be passed on via the constructor:

@Component
class CommentJob(@Autowired userController: UserController): Job {

    private val logger: Logger = LoggerFactory.getLogger(CommentJob::class.java)
   
    override fun execute(p0: JobExecutionContext?) {
        logger.debug("Inside Comment Job");
        userController.getUser();
        logger.debug("End Comment Job");
    }
}

@Autowired can also be omitted. For more information about Kotlin and Spring Boot:

https://www.baeldung.com/spring-boot-kotlin