How to pass instance variables into Quartz job?

quartz job pass parameters
c# quartz pass parameters to job
quartz scheduler
quartz properties environment variables
quartz job state
how to pass parameter in cron job in java
quartz trigger job data
quartz concurrent jobs

I wonder how to pass an instance variable externally in Quartz?

Below is pseudo code I would like to write. How can I pass externalInstance into this Job?

public class SimpleJob implements Job {
        @Override
        public void execute(JobExecutionContext context)
                throws JobExecutionException {

            float avg = externalInstance.calculateAvg();
        }
}

you can put your instance in the schedulerContext.When you are going to schedule the job ,just before that you can do below:

getScheduler().getContext().put("externalInstance", externalInstance);

Your job class would be like below:

public class SimpleJob implements Job {
    @Override
    public void execute(JobExecutionContext context)
            throws JobExecutionException {
        SchedulerContext schedulerContext = null;
        try {
            schedulerContext = context.getScheduler().getContext();
        } catch (SchedulerException e1) {
            e1.printStackTrace();
        }
        ExternalInstance externalInstance =
            (ExternalInstance) schedulerContext.get("externalInstance");

        float avg = externalInstance.calculateAvg();
    }
}

If you are using Spring ,you can actually using spring's support to inject the whole applicationContext like answered in the Link

Quartz Scheduler: How to pass custom objects as JobParameter , There are two ways to pass an object that can be retrieved when a Quartz job executes: Pass the instance in the data map. When you set the  org.quartz.JobDataMap is used to put and get values for a job instance. If the job is annotated with @PersistJobDataAfterExecution, then after job execution job states are re-persisted. JobDataMap is used to pass parameters to scheduled job. Using JobDetail instance we get the instance of JobDataMap and then add the values which needs to pass to scheduled job.

While scheduling the job using a trigger, you would have defined JobDataMap that is added to the JobDetail. That JobDetail object will be present in the JobExecutionContext passed to the execute() method in your Job. So, you should figure out a way to pass your externalInstance through the JobDataMap. HTH.

Examples, pass run-time parameters into quartz jobs and how you can maintain state in a job. will pass a run-time job parameter of “Red” to the second job instance  Start up the Quartz Scheduler; Schedule two jobs, each job will execute the every ten seconds for a total of times; The scheduler will pass a run-time job parameter of “Green” to the first job instance; The scheduler will pass a run-time job parameter of “Red” to the second job instance

Solve this problem by creating one interface with one HashMap putting required information there.

Implement this interface in your Quartz Job class this information will be accessible.

In IFace

Map<JobKey,Object> map = new HashMap<>();

In Job

map.get(context.getJobDetail().getKey()) =>  will give you Object

Quartz 2 Scheduler Pass Parameters to Job with JobDataMap using , In every execution of job, the new instance of job is created. To check it, I have taken a member variable with the value "new object" and within  While a job class that you implement has the code that knows how to do the actual work of the particular type of job, Quartz.NET needs to be informed about various attributes that you may wish an instance of that job to have. This is done via the JobDetail class, which was mentioned briefly in the previous section.

Add the object to the JobDataMap:

JobDetail job = JobBuilder.newJob(MyJobClass.class)
                          .withIdentity("MyIdentity",
                                        "MyGroup")
                          .build();
job.getJobDataMap()
   .put("MyObject",
        myObject);

Access the data from the JobDataMap:

var myObject = (MyObjectClass) context.getJobDetail()
                                      .getJobDataMap()
                                      .get("carrier");

How to pass instance variables into Quartz job?, I wonder how to pass an instance variable externally in Quartz? Below is pseudo code I would like to write. How can I pass externalInstance into this Job? public  None of the primary properties are required, and all have 'reasonable' defaults. When using Quartz via RMI, you need to start an instance of Quartz with it configured to "export" its services via RMI. You then create clients to the server by configuring a Quartz scheduler to "proxy" its work to the server.

How to pass parameters to a Quartz job, //Below line sets a variable named myContextVar in SchedulerContext. //Not only strings, you can set any type of object here. scheduler. Instance variables are not preserved between individual job executions simply because Quartz creates a new JobDetail (CampaignJob) instance for each job execution. Please note that the JobExecutionContext instance is always specific to a particular job execution (e.g. it refers to the trigger that executed the job) and therefore it does

org.quartz.SchedulerContext.get java code examples, How to pass instance variables into Quartz job? public class SimpleJob implements Job { @Override public void execute(JobExecutionContext context) throws  Passing parameters to Quartz job automatically injected into your job: which can be used to store state information for a given instance of your job

Quartz Scheduler: How to pass custom objects as JobParameter?, Pass parameters to Quartz Job Scheduler, Pass object to Quartz job using JobDataMap. How to pass parameter to Quartz Scheduler Cron Trigger example in  Things an object knows are its instance variables (state). Things an object does are its methods (behavior). Methods can use instance variables so that objects of the same type can behave differently. A method can have parameters, which means you can pass one or more values in to the method.

Passing parameters to Quartz job (Groovy forum at Coderanch), Does anyone know how to pass parameters to quartz job in grails and object (​such as a Grails service) automatically injected into your job: ? Job definitions are split into four basic parts: the job definition name, the type of the job definition, parameter substitution placeholder defaults, and the container properties for the job.

Comments
  • Inject the externalInstance in your job
  • This works fine for serializable objects, and it'll work for classes that you have access to. What if I want to inject something from an external jar? For instance, I'm trying to write a StatefulJob that lists all the files in an S3 bucket since a certain date and stores the files it listed in JobDetailMap. I want to inject a spring configured AmazonS3Client into my job. I cannot do this because that class is not serializable, and so I have to inject the secret key, access key and write code to manage creation of an s3 client. Is that the best way to do it? Any suggestions would be helpful.
  • Answering my own question - autowire the job, found this really good solution online - stackoverflow.com/questions/4258313/…
  • I was having a tough time to get a Spring managed bean in my Job class. I tried setting my object to JobDetail map but it could not serialize and was failing. Setting in scheduler context worked like a charm. Thank you!
  • what if you are passing different instance of object for every schedule you made? i mean the object parameter should be tied to job not the scheduler
  • Best and simple answer i have found. Thanks!
  • external instance I mean could be any custom data type, not limited to native data type, like String, Float, Int etc. I read from the documentation, it says that instance needs to be serializable.
  • In our case, the objects to work on are DB objects, so we used store the criteria to retrieve them from DB, in the JobDataMap and retrieve them in the execute method. So, you also need to figure out a way to pass the information required to retrieve the external instance from some store iff you cannot make all of them serializable.
  • The answer didn't added a value, actually I came to this question trying to figure out this "way"
  • @MohammedElrashidy, one of the ways is to store the jobdatamap (key, value) pairs in the database. This is under the assumption that the values are serializable. If they are not serializable, then more details on the nature of such data will help suggest more "ways" to pass this information. HTH.
  • @Vikdor I understand, my concern was that your answer was really general without giving real working answer. Thanks, it is still giving hints BTW I was able to pass instances and left that as an answer in this question stackoverflow.com/a/58801887/2152704
  • Did you mean String text = data.getText();? Data does not have a getB() method.