0

I'm reading Quartz documentation and trying to understand can I pass inside Job instance method instead of class.

For example, in case with class I need to write:

public class MyJobClass implements Job {

    public MyJobClass() {
        // Instances of Job must have a public no-argument constructor.
    }

    public void execute(JobExecutionContext context)
            throws JobExecutionException {

        JobDataMap data = context.getMergedJobDataMap();
        System.out.println("someProp = " + data.getString("someProp"));
    }    
}

And defining a Job Instance like:

JobDetail job1 = newJob(MyJobClass.class) // what about method here
    .withIdentity("job1", "group1")
    .usingJobData("someProp", "someValue")
    .build();

By the same principle, I tried to define job instance passing method like:

// define the job
JobDetail job = newJob(testMethod())
               .withIdentity("job1", "group1")
               .build();

And method looks like:

private Class<? extends Job> testMethod() {
    //...  
    return null;
}

But I get the error:

java.lang.IllegalArgumentException: Job class cannot be null.

Updated: I return null in method, because if I don't do this I get: enter image description here

invzbl3
  • 5,872
  • 9
  • 36
  • 76

1 Answers1

1

Your testMethod() method returns null. Quartz does not accept null and fails.

Quartz wants to manage jobs by itself so it is why you are only allowed to pass class not instance.

Why do you need to provide your own instance? If you want to make it "persistent" to keep state between executions then Quartz provides stateful job concept, see http://www.quartz-scheduler.org/api/2.3.0/org/quartz/StatefulJob.html

Alexander Pavlov
  • 2,264
  • 18
  • 25
  • With `null` I fully agree, but if I pass as `void` method with some logic then I get the warning. I updated my question. – invzbl3 Apr 22 '19 at 18:49
  • 1
    You _must_ provide class because Quartz needs it to instantiate job later – Alexander Pavlov Apr 22 '19 at 18:51
  • Do I understand correctly that `testMethod` is job body which you want to execute later? – Alexander Pavlov Apr 22 '19 at 18:52
  • `testMethod()` is the method which I define inside job instance instead of additional class. I thought that I can pass it if I don't want to create separately new class. To clarify: I have several classes with some logic and as I understand from documentation I need to create new class for job instance, but I tried to check can I pass simply method not another class. – invzbl3 Apr 22 '19 at 19:01
  • I see. Short answer - you cannot instantiate lambda using its class, see https://stackoverflow.com/questions/38490815/instantiate-java-lambda-function-by-name – Alexander Pavlov Apr 22 '19 at 19:03
  • 1
    i.e. Quartz always instantiates instances by itself. Lambdas are classes behind the scene so in ideal world you could expect that you can define lambda, get its class, pass to Quartz and it instantiates lambda using its class but it is not ideal world, see explanation in the link above – Alexander Pavlov Apr 22 '19 at 19:05
  • Actually, the answer is helpful. Especially in comments. But can you clarify a bit it, as I understand, we speak about difference between passing class and method inside `job instance`, right? But what do you mean by `instance` in the answer? Can you, please, explain. – invzbl3 Apr 22 '19 at 20:31
  • 1
    `MyJobClass.class` - is class, `new MyJobClass()` is instance of MyJobClass class. Quartz only accepts references to class ( e.g. `MyJobClass.class`) so later it can instantiate job instance (`instance = MyJobClass.class.newInstance()` and execute. In Java you cannot "pass method". You can pass lambda which is instance of automatically generated class. Unfortunately, Java is designed that way that even if lambda has class, you cannot create new instance of lambda using `lambda.class.newInstance()` – Alexander Pavlov Apr 22 '19 at 20:39