Home > Back-end >  Task on Java Spring Boot doesn't work properly (working for short term only)
Task on Java Spring Boot doesn't work properly (working for short term only)

Time:10-31

so I have a problem with this task that I have made. it works only for the short term and after a few hours kinda dies. Iv'e made it to run daily. for instance, I made this task that's run for the next half an hour (if now it's 12:30 PM so 1:00 PM), and it works perfectly fine. but if I check the log and the DB the next day, it shows nothing regarding this task, and it will not do what it's supposed to do.

so this is my code:

@Component
public class DeleteGraduateStudentsTimerTask extends TimerTask {

    private final StudentsService studentsService;

    @Autowired
    public DeleteGraduateStudentsTimerTask(StudentsService studentsService) {
        this.studentsService = studentsService;
    }

    @PostConstruct
    public void init() {
        Calendar date = Calendar.getInstance();
        date.set(Calendar.HOUR_OF_DAY, 13);
        date.set(Calendar.MINUTE, 0);
        date.set(Calendar.SECOND, 0);
        Timer timer = new Timer();
        timer.schedule(this, date.getTime());
    }

  
    @Override
    public void run() {
        System.out.println("Running to check if there is graduated student to delete");
            List<Student> studentsList = studentsService.getAllGraduateStudents();
            for (Student student: studentsList) {
                studentsService.deleteStudent(student.getId());
            }

        }
    }

i don't even know how to approach this, beacause it IS working, but after one day it clearly does not.

if anyone have any idea please help me.

thank you so much!

CodePudding user response:

So as pointed out, I get my mistake and it is a one-timer. I have added @Scheduled job with fixed rate of one day in ML and now its working.

  • Related