Skip to main content

haft generate scheduler

Generate Spring Boot scheduled task classes with @Scheduled annotations.

Usage

haft generate scheduler <name> [flags]
haft g sch <name> [flags]

Aliases

  • scheduler
  • sch
  • scheduled
  • task

Description

The scheduler generator creates:

  • A @Component class with @Scheduled method
  • SchedulingConfig.java (if not exists) to enable scheduling via @EnableScheduling

Schedule Types

TypeDescriptionExample
cronRun at specific times using cron syntax0 0 8 * * * (8 AM daily)
fixedRateRun every N milliseconds from method start300000 (every 5 min)
fixedDelayRun N milliseconds after previous completion60000 (1 min after done)

Common Cron Expressions

ExpressionDescription
0 0 * * * *Every hour
0 0 8 * * *Every day at 8 AM
0 0 0 * * MONEvery Monday at midnight
0 */15 * * * *Every 15 minutes
0 0 8 * * MON-FRIWeekdays at 8 AM
0 0 2 * * *Every day at 2 AM

Flags

FlagShortDescription
--cronCron expression (e.g., "0 0 * * * *")
--rateFixed rate in milliseconds
--delayFixed delay in milliseconds
--initialInitial delay (used with --delay)
--package-pOverride base package
--no-interactiveSkip interactive wizard
--refreshForce re-detection of project profile
--jsonOutput result as JSON

Examples

Interactive Mode

# Opens wizard to configure scheduler
haft generate scheduler cleanup

# Short alias
haft g sch report

With Cron Expression

# Run every day at 2 AM
haft generate scheduler cleanup --cron "0 0 2 * * *"

# Run weekdays at 8 AM
haft generate scheduler report --cron "0 0 8 * * MON-FRI"

# Run every 15 minutes
haft generate scheduler sync --cron "0 */15 * * * *"

With Fixed Rate

# Run every 5 minutes (300000ms)
haft generate scheduler sync --rate 300000

# Run every 30 seconds
haft generate scheduler heartbeat --rate 30000

With Fixed Delay

# Run 1 minute after previous execution completes
haft generate scheduler process --delay 60000

# With initial delay of 5 seconds
haft generate scheduler batch --delay 60000 --initial 5000

Non-Interactive Mode

haft generate scheduler report \
--cron "0 0 8 * * MON-FRI" \
--package com.example.app \
--no-interactive

JSON Output (for CI/CD)

haft generate scheduler cleanup --cron "0 0 2 * * *" --no-interactive --json

Generated Files

Layered Architecture

src/main/java/com/example/app/
├── config/
│ └── SchedulingConfig.java # @EnableScheduling
└── scheduler/
└── CleanupTask.java # @Scheduled task

Feature Architecture

src/main/java/com/example/app/
└── common/
├── config/
│ └── SchedulingConfig.java
└── scheduler/
└── CleanupTask.java

Hexagonal/Clean Architecture

src/main/java/com/example/app/
└── infrastructure/
├── config/
│ └── SchedulingConfig.java
└── scheduler/
└── CleanupTask.java

Generated Code Example

SchedulingConfig.java

package com.example.app.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;

@Configuration
@EnableScheduling
public class SchedulingConfig {
}

CleanupTask.java (with Lombok)

package com.example.app.scheduler;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@RequiredArgsConstructor
@Component
public class CleanupTask {

@Scheduled(cron = "0 0 2 * * *")
public void execute() {
log.info("CleanupTask started");
try {
// TODO: Implement your scheduled task logic here

} catch (Exception e) {
log.error("CleanupTask failed", e);
}
log.info("CleanupTask completed");
}
}

CleanupTask.java (without Lombok)

package com.example.app.scheduler;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component
public class CleanupTask {

private static final Logger log = LoggerFactory.getLogger(CleanupTask.class);

@Scheduled(cron = "0 0 2 * * *")
public void execute() {
log.info("CleanupTask started");
try {
// TODO: Implement your scheduled task logic here

} catch (Exception e) {
log.error("CleanupTask failed", e);
}
log.info("CleanupTask completed");
}
}

Tips

  1. Cron format: Spring uses 6-field cron expressions (second, minute, hour, day, month, weekday)

  2. Time zones: Configure timezone in application properties:

    spring:
    task:
    scheduling:
    pool:
    size: 5
  3. Error handling: Always wrap task logic in try-catch to prevent task failures

  4. Logging: Use structured logging to track task execution

  5. Long-running tasks: Consider using fixedDelay instead of fixedRate to prevent overlapping executions

Editor Integration

Use this command from your editor:

  • Neovim: Not yet available in haft.nvim (CLI only)
  • VS Code: Coming soon (preview →)
  • IntelliJ IDEA: Coming soon (preview →)

See Also