javaspringspring-mvc

how to fix problem scheduled tasks with spring boot


My scheduled task is as follows But the application does not respond when i use @Scheduled(cron="0 46 17 * * *") And when I use @Scheduled(fixedRate = 50000) , When saving information on mongoDB The program gives an error

[ scheduling-1] o.s.s.s.TaskUtils$LoggingErrorHandler : Unexpected error occurred in scheduled task.

: An unexpected error occurred during scheduled work. java.lang.NullPointerException

please help me for fix my cod .


    @Scheduled(cron="0 46 17  * * *")
//  @Scheduled(fixedRate = 5000)
    @RequestMapping("/closeAttendance}")
    public void cloceAttendance() {
        System.out.println("-1");
        AttendanceService attendanceService=new AttendanceService();
        Date date=attendanceService.getCurrentDate();
        System.out.println("1");
        List<Attendance> attendances=arepo.findByDate(date);
if(attendances!=null) {
        System.out.println("attendances"+attendances.size());
        System.out.println("2");
//  
        for(Attendance attendance:attendances) {
            System.out.println("3");
            attendance=attendanceService.closeAttendance(attendance);
            System.out.println("4");
            System.out.println("attendance"+attendance.getDate()+" "+ attendance.getPerson());
//          arepo.save(attendance);

        }   

    }
}

Solution

  • There is nothing wrong with your code. The only thing that is missing is the annotation @EnableScheduling.

    Use this annotation at class level and your code will work fine.