有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何使用spring boot修复有问题的计划任务

我预定的任务如下 但是当我使用@Scheduled(cron="0 46 17 * * *")时,应用程序没有响应 当我使用@Scheduled(fixedRate = 50000)时,在mongoDB上保存信息时,程序会给出一个错误

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

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

请帮我修一下鳕鱼


    @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);

        }   

    }
}

共 (2) 个答案

  1. # 1 楼答案

       Use the below code for your reference and do the changes according to your need 
    

    包裹通讯。赢家服务

    import java.util.List;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.scheduling.annotation.EnableScheduling ;
    import org.springframework.scheduling.annotation.Scheduled;
    com.winner.etyping.domain.RegParentDomain;
    import com.winner.etyping.domain.SchoolRegistrationDomain;
    import com.winner.etyping.domain.StudentDomain;
    @EnableScheduling
    @Servic
    public class StudentServiceImpl implements StudentService{
    
        @Autowired
        private ClassService classserice;
    
        @Autowired
        private DivisionService div;
    
        @Autowired
        private RegParentService parent;
    
        @Autowired
        private SchoolregistrationService school;
    
        @Autowired
        private StudentDao studendao;
    
    
    
    
    
        @Transactional
        public List<StudentDomain> getByClassAndDivId(Integer classid, Integer div,Integer school) {
            return studendao.getByClassAndDivId(classid,div,school);
        }
    
        **@Scheduled(cron="0 0 10 * * ?")**
        @Transactional
        public void sendBirthdayWish() throws Exception {
            studendao.sendBirthdayWish();
    
        }
    
        @Transactional
        public void sendNotificationToClassStudent(ClassDomain classid, SchoolRegistrationDomain schoolid , String Notification) throws Exception {
            studendao.sendNotificationToClassStudent(classid,schoolid,Notification);
    
        }
    
    
    
    
    
    
    }
    
  2. # 2 楼答案

    你的代码没有问题。唯一缺少的是注释@EnableScheduling

    类级别使用这个注释,你的代码就会运行良好

    如果这个解决方案对你有效,请不要忘记向上投票