简体   繁体   English

如何使用 Spring Boot 修复有问题的计划任务

[英]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我的计划任务如下但是当我使用@Scheduled(cron="0 46 17 * * *")时应用程序没有响应,当我使用@Scheduled(fixedRate = 50000) ,在mongoDB上保存信息时程序给出了一个错误

[ scheduling-1] osssTaskUtils$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);

        }   

    }
}

There is nothing wrong with your code.您的代码没有任何问题。 The only thing that is missing is the annotation @EnableScheduling .唯一缺少的是注释@EnableScheduling

Use this annotation at class level and your code will work fine.类级别使用此注释,您的代码将正常工作。

Please don't forgot to up-vote if this solution works for you.如果此解决方案适合您,请不要忘记投票

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

package com.winner.service;包 com.winner.service;

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

    }






}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM