2

I'm trying to create a Job in Quartz 1.6, but with the necessity to execute only once, because I have two test instances with the same version of a .war file.

This is my TestPlugin class, the Job will be executed every 60 seconds:

public class TestPlugin implements PlugIn {

    public TestPlugin() {
        super();
    }

    public void destroy() {
    }

    public void init(ActionServlet arg0, ModuleConfig arg1)
            throws ServletException {

        try {
            JobDetail job = JobBuilder.newJob(TestDemonio.class)
                    .withIdentity("anyJobName", "group1").build();
            Trigger trigger = TriggerBuilder
                    .newTrigger()
                    .withIdentity("anyTriggerName", "group1")
                    .withSchedule(CronScheduleBuilder.cronSchedule("0/60 * * ? * * *"))
                    .build();

            Scheduler scheduler = new StdSchedulerFactory().getScheduler();
            scheduler.scheduleJob(job, trigger);
            scheduler.start();

        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }
}

Then I have my class TestExecute to print a simple output:

@DisallowConcurrentExecution
public class TestDemonio implements Job {

    public void execute(JobExecutionContext arg0) throws JobExecutionException {
        System.out.println("QUARTZ JOB MESSAGE");
    }
}

I have researched on how to achieve what I want by adding the annotation @DisallowConcurrentExecution, to only execute once the job, but I receive a message printed on each instance.

This is my quartz.properties file:

# Default Properties file for use by StdSchedulerFactory
# to create a Quartz Scheduler Instance, if a different
# properties file is not explicitly specified.
#

org.quartz.scheduler.instanceName: DefaultQuartzScheduler
org.quartz.scheduler.rmi.export: false
org.quartz.scheduler.rmi.proxy: false
org.quartz.scheduler.wrapJobExecutionInUserTransaction: false

org.quartz.threadPool.class: org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount: 10
org.quartz.threadPool.threadPriority: 5
org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread: true

org.quartz.jobStore.misfireThreshold: 60000

org.quartz.jobStore.class: org.quartz.simpl.RAMJobStore
TimeToCode
  • 901
  • 2
  • 16
  • 34
  • could you also share the quartz.properties file? Also, check your job entries in the database tables as to how many job instances does it shows? – Ankur Aug 06 '19 at 01:50
  • Also, note that `@DisallowConcurrentExecution`, supposedly caters to single job execution on a single node. For multiple nodes, quartz clustering is responsible. – Ankur Aug 06 '19 at 01:56
  • Statements `necessity to execute only once` and `will be executed every 60 seconds` exclude each other. Please clarify the desired behavior – Nikolai Shevchenko Aug 06 '19 at 05:32
  • @Ankur I added the quartz properties file. – TimeToCode Aug 06 '19 at 13:48

1 Answers1

4

You need to add following property to your quartz.property file(source: click here):

org.quartz.jobStore.isClustered : true

Read this for more information about isClustered property, refer to this link.

Please note:

@DisallowConcurrentExecution works when you have 2 different jobs with same jobkey running on same node.

While isClustered property is used to make sure that single instance of a job is executed when app is running of multiple nodes communicating via database tables for atomicity.

Community
  • 1
  • 1
Ankur
  • 892
  • 6
  • 11
  • To be safe we need to set the org.quartz.scheduler.instanceId=AUTO as the clustering in the quartz is done at the database level.If two instances of quartz has the same name that is (org.quartz.scheduler.instanceName) but different instanceId (org.quartz.scheduler.instanceId) they will form a cluster. – user06062019 Aug 07 '19 at 14:52
  • I added the quartz jar to the project, the quartz.properties file is inside, when I execute the Job it will be automatically recognized? – TimeToCode Aug 07 '19 at 16:38
  • @Ankur- isClustered: true is for multiple instances of Quartz or multiple instances of our application? I checked the reference you have given and it's written here for IsClustered flag Set to “true” in order to turn on clustering features. This property must be set to “true” if you are having multiple instances of Quartz use the same set of database tables… otherwise, you will experience havoc. See the configuration docs for clustering for more information. – Vipul Jain Feb 23 '21 at 08:23