简体   繁体   English

使用石英调度程序在春季启动的作业调度中存储库为空

[英]Repository is null in job scheduling in spring boot with quartz scheduler

When the user calls the api /bet/{id}/start, the Schedule will immediately be created with name = id, group = Exchanges, every 3rd second on the minute it will automatically call the excute function to process, for example here I will call the findById(id) function of the repository to retrieve and process the data, but the result I get is java.lang.NullPointerException: null当用户调用api /bet/{id}/start时,会立即创建name = id, group = Exchanges的Schedule,每分钟3秒自动调用excute函数进行处理,比如这里我会调用repository的findById(id)函数来检索和处理数据,但是得到的结果是java.lang.NullPointerException: null

BetController.java投注控制器.java

@RestController
@RequestMapping("/api")
@Transactional
public class BetController extends AbstractController {

    private MainScheduler mainScheduler;
    
    @RequestMapping(value = "/bet/{id}/start", method = RequestMethod.POST)
    public String addAndStartScheduleWithBetId(@PathVariable("id") Long id) {
        mainScheduler.addAndStartScheduler(""+id);
        
        return "";
    }
}

MainScheduler.java主调度程序

@Service
public class MainScheduler {
    
    private Scheduler scheduler;
    
    public MainScheduler(Scheduler scheduler) {
        this.scheduler = scheduler;
    }
    
    public Scheduler addAndStartScheduler(String betId) {
        try {
            Trigger trigger = TriggerBuilder.newTrigger().withIdentity(betId,"Exchanges").withSchedule(CronScheduleBuilder.cronSchedule("3 * * ? * * *")).build();
            JobDetail jobDetail = JobBuilder.newJob(ScheduleJob.class).withIdentity(betId, "Exchanges") .build();
            scheduler = StdSchedulerFactory.getDefaultScheduler();
            scheduler.start();
            scheduler.scheduleJob(jobDetail, trigger);
            System.out.println(jobDetail.getKey() + ","+ trigger.getKey() + " will run at: " + new Date());
        } catch(Exception ex) {
            System.out.println(ex);
        }
        return scheduler;
    }
    
    public boolean deleteJobDetail(String name) {
        boolean flag = false;
        try {
            flag = scheduler.deleteJob(jobKey(name, "Exchanges"));
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
        return flag;
    }
    
}

BetRepository.java BetRepository.java

public interface BetRepository extends CrudRepository<Bet, Long> {

    Page<Bet> findAll(Pageable pageable);

    Optional<Bet> findById(Long id) ;
}

ScheduleJob.java调度作业.java

public class ScheduleJob implements Job{
    
    @Autowired
    private BetRepository betRepository;
    
    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        // TODO Auto-generated method stub
        System.out.println("key+group trigger: " + context.getTrigger().getKey());
        Long id = Long.parseLong(context.getJobDetail().getKey().getName());
        System.out.println("Bet repositorys: " + betRepository.findById(id));
    }
    
}
13-07-2021 03:33:03.015 [35m[DefaultQuartzScheduler_Worker-3][0;39m
                [1;31mERROR[0;39m
                org.quartz.core.ErrorLogger.schedulerError - Job (Exchanges.5 threw an exception.
org.quartz.SchedulerException: Job threw an unhandled exception.
    at org.quartz.core.JobRunShell.run(JobRunShell.java:213)
    at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:573)
Caused by: java.lang.NullPointerException: null
    at com.bot.auto.utils.ScheduleJob.execute(ScheduleJob.java:21)
    at org.quartz.core.JobRunShell.run(JobRunShell.java:202)
    ... 1 common frames omitted

Modify your MainScheduler so that you get the autowired BetRepository and put it in the scheduler context before starting the schedular and get it in the Job.修改您的 MainScheduler 以便您获得自动装配的 BetRepository 并将其放入调度程序上下文中,然后再启动调度程序并将其放入作业中。

    @Service
    public class MainScheduler {
        
        private Scheduler scheduler;
        @Autowired
        private BetRepository betRepository;
    
        public MainScheduler(Scheduler scheduler) {
            this.scheduler = scheduler;
        }
        
        public Scheduler addAndStartScheduler(String betId) {
            try {
                Trigger trigger = TriggerBuilder.newTrigger().withIdentity(betId,"Exchanges").withSchedule(CronScheduleBuilder.cronSchedule("3 * * ? * * *")).build();
                JobDetail jobDetail = JobBuilder.newJob(ScheduleJob.class).withIdentity(betId, "Exchanges") .build();
                scheduler = StdSchedulerFactory.getDefaultScheduler();
                schduler.getContext().put("betRepo",betRepository);
                scheduler.start();
                scheduler.scheduleJob(jobDetail, trigger);
                System.out.println(jobDetail.getKey() + ","+ trigger.getKey() + " will run at: " + new Date());
            } catch(Exception ex) {
                System.out.println(ex);
            }
            return scheduler;
        }
<    -----other methods as is---->
    }

Change your Job class as below更改您的工作类别如下

public class ScheduleJob implements Job{公共类 ScheduleJob 实现 Job{

private BetRepository betRepository;

@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
    // TODO Auto-generated method stub
 betRepository=    context.get("betRepo"); or context.getScheduler().getContext().get("betRepo");
    System.out.println("key+group trigger: " + context.getTrigger().getKey());
    Long id = Long.parseLong(context.getJobDetail().getKey().getName());
    System.out.println("Bet repositorys: " + betRepository.findById(id));
}

} }

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

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