Open callingmahendra opened 3 months ago
Implement a scheduled task in your Spring Boot application using the @Scheduled
annotation. Inject the TodoRepository
and use a query to find and delete TODOs older than 3 days.
import org.springframework.scheduling.annotation.Scheduled;
@Service
public class TodoService {
// ... other methods ...
@Scheduled(cron = "0 0 0 * * *") // Runs daily at midnight
public void deleteOldTodos() {
LocalDate threeDaysAgo = LocalDate.now().minusDays(3);
todoRepository.deleteByCreatedAtBefore(threeDaysAgo);
}
}
Explanation:
LocalDate.now().minusDays(3)
: This calculates the date three days before the current date.todoRepository.deleteByCreatedAtBefore(threeDaysAgo)
: This assumes you add a createdAt
field (with appropriate mapping) to your Todo
entity. This line will delete all TODOs created before the calculated date.Remember:
@EnableScheduling
.createdAt
field and repository method according to your implementation.
write a cron job to delete todo after 3 day of creation using springbok