Java Handbook
  • Introduction
  • Numbers
    • Big numbers
    • Binary representation of numbers
  • Operators
  • Hashing
    • Hash Code & Performance
    • How Hash Table Is Created
  • Java 9
    • Modules
    • Other Improvements
  • Concurrency
    • Locking
  • Reactive Programming
    • Event Driven Non-Blocking Frameworks
    • Vert.x
      • Making a game
      • SockJS
    • Netty
    • RxJava
  • Efficient Coding
    • Lombok
  • Quartz Scheduling
Powered by GitBook
On this page

Was this helpful?

Quartz Scheduling

Issues with Quartz Instance ID with Cluster

SimpleInstanceIdGenerator tries to generate instance ID using InetAddress.getLocalHost().getHostName() that can sometimes fail, when Java is not able to resolve DNS name of the machine. InetAddress.getHostName() is doing a reverse lookup on the server's IP address using the naming service (DNS) configured in your O/S.

import org.apache.commons.lang3.StringUtils;
import org.quartz.spi.InstanceIdGenerator;

import java.util.Random;

/**
 * Generates ID based on system HOSTNAME. If HOSTNAME is not available, it generates random ID.
 */
public class HostnameInstanceIdGenerator implements InstanceIdGenerator {

    public static final String HOSTNAME_KEY = "HOSTNAME";

    @Override
    public String generateInstanceId() {
        String id = System.getenv(HOSTNAME_KEY);
        if (StringUtils.isBlank(id)) {
            // get random ID if hostname is not available
            id = String.valueOf(new Random().nextLong());
        }
        return id;
    }
}

Then we need to set our new ID generator in Quartz properties.

org.quartz.scheduler.instanceIdGenerator.class=mypackage.HostnameInstanceIdGenerator
PreviousLombok

Last updated 5 years ago

Was this helpful?