# 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
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://ondrej-kvasnovsky-2.gitbook.io/java-handbook/quartz-scheduling.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
