Build quartz job with logic from different class

42 Views Asked by At
package fit.naiz.community.sheduler

import fit.naiz.community.dao.ChallengeParticipantDAO

import org.quartz.{Job, JobBuilder, JobDataMap, JobExecutionContext, SimpleScheduleBuilder, TriggerBuilder}

class MyJob(challengeParticipantDAO: ChallengeParticipantDAO) extends Job {
  override def execute(context: JobExecutionContext): Unit = {
    val result = challengeParticipantDAO.getExpiredParticipants()
    println(result)

    println("QUARTZ JOB")
  }

  def scheduleMyJob(): Unit = {

    val jobDetail = JobBuilder
      .newJob(classOf[MyJob])
      .withIdentity("myJob", "Challenge Participants group")
      .build()

    val trigger = TriggerBuilder
      .newTrigger()
      .withIdentity("myJobTrigger", "Challenge Participants group") // Set a unique identifier for your trigger
      .startNow() // Start the job immediately
      .withSchedule(SimpleScheduleBuilder.repeatMinutelyForever)
      .build()

    JobScheduler.scheduleJob(jobDetail, trigger)
  }
}

My logs:

{"timeStamp":"2023-11-16T18:11:01.284162Z","message":"An error occured instantiating job to be executed. job= 'Challenge Participants group.myJob'","logger":"org.quartz.core.ErrorLogger","thread":"DefaultQuartzScheduler_QuartzSchedulerThread","level":"ERROR","stackTrace":"org.quartz.SchedulerException: Problem instantiating class 'fit.naiz.community.sheduler.MyJob'\n\tat org.quartz.simpl.SimpleJobFactory.newJob(SimpleJobFactory.java:58)\n\tat org.quartz.simpl.PropertySettingJobFactory.newJob(PropertySettingJobFactory.java:69)\n\tat org.quartz.core.JobRunShell.initialize(JobRunShell.java:127)\n\tat org.quartz.core.QuartzSchedulerThread.run(QuartzSchedulerThread.java:375)\nCaused by: java.lang.InstantiationException: fit.naiz.community.sheduler.MyJob\n\tat java.base/java.lang.Class.newInstance(Class.java:639)\n\tat org.quartz.simpl.SimpleJobFactory.newJob(SimpleJobFactory.java:56)\n\t... 3 common frames omitted\nCaused by: java.lang.NoSuchMethodException: fit.naiz.community.sheduler.MyJob.<init>()\n\tat java.base/java.lang.Class.getConstructor0(Class.java:3585)\n\tat java.base/java.lang.Class.newInstance(Class.java:626)\n\t... 4 common frames omitted\n"}
{"timeStamp":"2023-11-16T18:11:01.285892Z","message":"All triggers of Job Challenge Participants group.myJob set to ERROR state.","logger":"org.quartz.simpl.RAMJobStore","thread":"DefaultQuartzScheduler_QuartzSchedulerThread","level":"INFO"}

Here's a better-formatted version of the stack trace:

org.quartz.SchedulerException: Problem instantiating class 'fit.naiz.community.sheduler.MyJob'
    at org.quartz.simpl.SimpleJobFactory.newJob(SimpleJobFactory.java:58)
    at org.quartz.simpl.PropertySettingJobFactory.newJob(PropertySettingJobFactory.java:69)
    at org.quartz.core.JobRunShell.initialize(JobRunShell.java:127)
    at org.quartz.core.QuartzSchedulerThread.run(QuartzSchedulerThread.java:375)
Caused by: java.lang.InstantiationException: fit.naiz.community.sheduler.MyJob
    at java.base/java.lang.Class.newInstance(Class.java:639)
    at org.quartz.simpl.SimpleJobFactory.newJob(SimpleJobFactory.java:56)
    ... 3 common frames omitted
Caused by: java.lang.NoSuchMethodException: fit.naiz.community.sheduler.MyJob.<init>()
    at java.base/java.lang.Class.getConstructor0(Class.java:3585)
    at java.base/java.lang.Class.newInstance(Class.java:626)
    ... 4 common frames omitted


Stack trace made readable by https://TidyTrace.com

As a result, I can't start MyJob and get some results from daoLogic, the reason for this behavior is to add a class to the constructor, because without it everything works and runs normally.

Can someone guide me or advise me on how I can use some logic from different classes?

0

There are 0 best solutions below