How to implement the designated machine running in a Quartz cluster?

To achieve the function of running on specified machines in the Quartz cluster, follow the steps outlined below:

  1. Set up a Quartz cluster by configuring the cluster mode, specifying the cluster name, and instance ID in the Quartz configuration file. For example:
<quartz>
  <cluster>
    <instanceId>AUTO</instanceId>
    <instanceName>MyCluster</instanceName>
  </cluster>
  ...
</quartz>
  1. Machine configuration: Each machine needs to have a unique identifier, which can be specified by setting system properties. For example, an environment variable can be set in the startup script.
export MACHINE_ID=machine1
  1. perform
public void execute(JobExecutionContext context) throws JobExecutionException {
  String machineId = System.getProperty("MACHINE_ID");
  if (machineId.equals("machine1")) {
    // 执行任务
  } else {
    // 不执行任务
  }
}
  1. Deploy and start a Quartz cluster: Deploy the configured Quartz cluster to multiple machines and start them. Make sure each machine has a unique machine identifier (MACHINE_ID).

By following the steps above, it is possible to achieve the functionality of specifying which machines run in a Quartz cluster. In this configuration, only machines with the designated identifier will execute the relevant tasks, while others will not.

bannerAds