In many real-world applications, we often need a background process that runs at regular intervals — for example, to keep the server active, check system health, or trigger maintenance tasks automatically.
In this tutorial, you’ll learn how to create a simple Self-Ping Scheduler in Spring Boot using the @Scheduled annotation.
What is a Self-Ping Scheduler?
A self-ping is a mechanism where your application periodically sends a request to itself to ensure that it’s responsive or to perform background tasks without any external trigger.
This concept is very useful for:
- Keeping web servers active
- Running periodic health checks
- Triggering cache refresh or cleanup tasks
- Performing routine maintenance in the background
Step 1 – Enable Scheduling in Spring Boot
Before using @Scheduled, you must enable scheduling in your Spring Boot application.
Open your main application class and add @EnableScheduling annotation.
package com.example.selfping;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication
@EnableScheduling
public class SelfPingApplication {
public static void main(String[] args) {
SpringApplication.run(SelfPingApplication.class, args);
}
}
Step 2 – Create a Simple Ping Endpoint
Next, let’s create a simple API endpoint that your scheduler can call.
This endpoint just returns "OK" to confirm the app is running.
package com.example.selfping.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class PingController {
@GetMapping("/api/ping")
public String ping() {
return "OK";
}
}
Step 3 – Implement the Self-Ping Scheduler
Now, we’ll create a scheduler that automatically calls this endpoint every few minutes using RestTemplate.
package com.example.selfping.service;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
@Component
public class SelfPingService {
private final RestTemplate restTemplate = new RestTemplate();
private static final String SELF_URL = "http://localhost:8080/api/ping";
@Scheduled(fixedRate = 5 * 60 * 1000) // every 5 minutes
public void pingSelf() {
try {
restTemplate.getForObject(SELF_URL, String.class);
System.out.println("🔁 Self-ping successful");
} catch (Exception e) {
System.out.println("⚠️ Self-ping failed: " + e.getMessage());
}
}
}
How @Scheduled Works
The @Scheduled annotation allows you to run methods automatically at specific time intervals.
You can use different scheduling strategies like:
// Runs every 10 seconds
@Scheduled(fixedRate = 10000)
// Runs after 10 seconds delay from method completion
@Scheduled(fixedDelay = 10000)
// Runs at specific time using cron expression (e.g. every day at midnight)
@Scheduled(cron = "0 0 0 * * ?")
Step 4 – Test the Scheduler
Run your Spring Boot application. You should see output in your console every 5 minutes:
🔁 Self-ping successful
If the endpoint is unreachable, you’ll see:
⚠️ Self-ping failed: Connection refused
Use Cases for Self-Ping
- Keeping your application active during low traffic
- Monitoring uptime or app health
- Refreshing access tokens automatically
- Running background cleanup or sync jobs
Conclusion
Creating a Self-Ping Scheduler in Spring Boot is simple yet powerful. It’s an elegant way to perform periodic background tasks without external tools like cron jobs or external uptime monitors.
Using @Scheduled, you can easily build heartbeat, cleanup, and monitoring mechanisms directly into your Spring Boot application.
Next Step: Try combining this with a health-check endpoint or database validation to make your scheduler even more useful 🚀.
0 Comments