code4craft/webmagic · error · IllegalArgumentException

emptySleepTime should be more than zero!

Error message

emptySleepTime should be more than zero!

What it means

Spider.setEmptySleepTime(long) validates that the wait interval when the scheduler is empty is strictly positive; zero or negative values throw IllegalArgumentException because the thread would spin or wait indefinitely.

Solutions

  1. Pass a positive millisecond value, e.g. 1000
  2. If you truly want no wait, choose a small positive value like 1 ms
  3. Validate the configured value before applying it

Example fix

// before
spider.setEmptySleepTime(0);
// after
spider.setEmptySleepTime(1000);
Defensive patterns

Strategy: validation

Validate before calling

if (emptySleepTime <= 0) emptySleepTime = 1000; spider.setEmptySleepTime(emptySleepTime);

Try / catch

try { spider.setEmptySleepTime(t); } catch (IllegalArgumentException e) { spider.setEmptySleepTime(1000L); }

Prevention

When it happens

Trigger: Calling spider.setEmptySleepTime(0) or setEmptySleepTime(-100); passing a mis-parsed duration value.

Common situations: Treating 0 as 'no sleep' when the API requires a positive value; unit mixups (passing seconds as 0.x fractions truncated to 0).

Understand the failure class

Background: "Invalid value" and "allowed values are" config errors: what your library rejected and how to fix it — this error's family across 41 libraries.

Related errors


AI-assisted analysis of code4craft/webmagic@67816a19d6 (2026-09-08). Data as JSON: /api/errors/19164d0ac18b045e. Report an issue: GitHub.

Appendix: source

Thrown at webmagic-core/src/main/java/us/codecraft/webmagic/Spider.java:781

    }

    public Date getStartTime() {
        return startTime;
    }

    public Scheduler getScheduler() {
        return scheduler.getScheduler();
    }

    /**
     * Set wait time when no url is polled.<br><br>
     *
     * @param emptySleepTime In MILLISECONDS.
     * @return this
     */
    public Spider setEmptySleepTime(long emptySleepTime) {
        if(emptySleepTime<=0){
            throw new IllegalArgumentException("emptySleepTime should be more than zero!");
        }
        this.emptySleepTime = emptySleepTime;
        return this;
    }
}

View on GitHub (pinned to 67816a19d6)