当前位置: 首页 > 知识库问答 >
问题:

在Spring Boot中部署Quartz时出现NullPointerException

都才俊
2023-03-14

我正在尝试使用石英2.2。1带Spring靴。我试图声明一个计划任务,该任务应该将一些数据写入文件。我的工作定义如下:

public class JobTask implements Job {

@Autowired
JobController controller;

@Override
public void execute(JobExecutionContext arg0) throws JobExecutionException {

                try {
                    controller.doPrintData();
                } catch (Exception e) {
                    e.printStackTrace();
                }
    }
}

然后呢:

public class StartJob {

  public static void main(final String[] args) {
    final SchedulerFactory factory = new StdSchedulerFactory();
    Scheduler scheduler;
    try {
        scheduler = factory.getScheduler();
        scheduler.start();
    } catch (SchedulerException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }


    try {
      scheduler = factory.getScheduler();

      final JobDetailImpl jobDetail = new JobDetailImpl();
      jobDetail.setName("My job");
      jobDetail.setJobClass(JobTask.class);

      final SimpleTriggerImpl simpleTrigger = new SimpleTriggerImpl();
      simpleTrigger.setStartTime(new Date(System.currentTimeMillis() + 5000));
      //simpleTrigger.setStartTime(dateOf(12, 58, 00,06,05,2016));
      simpleTrigger.setRepeatCount(SimpleTrigger.REPEAT_INDEFINITELY);
      simpleTrigger.setRepeatInterval(5000);
      simpleTrigger.setName("Trigger execution every 5 secondes");

      scheduler.start();
      scheduler.scheduleJob(jobDetail, simpleTrigger);

      System.in.read();
      if (scheduler != null) {
        scheduler.shutdown();
      }
    } catch (final SchedulerException e) {
      e.printStackTrace();
    } catch (final IOException e) {
      e.printStackTrace();
    }
  }
}

PS:我已经测试了我的控制器方法doPrintData和它的工作原理。但是当我把它放在执行方法中时,我面对的是javaNullPointerExcure。

共有2个答案

巩镜
2023-03-14

SpringBoot为您管理它。删除quartz依赖项,只需创建一个服务,以进行计划执行:

@Service
public class JobScheduler{

    @Autowired
    JobController controller;

    //Executes each 5000 ms
    @Scheduled(fixedRate=5000)
    public void performJob() {
        controller.doPrintData();
    }
}

并为应用程序启用任务计划:

@SpringBootApplication
@EnableScheduling
public class Application {

    public static void main(String[] args) throws Exception {
        SpringApplication.run(Application.class);
    }
}

另见:

  • 在Spring Boot中调度任务
黄查猛
2023-03-14

您需要使用SpringBeanJobFactory使用Spring的自动连接bean创建作业。

class AutowiringSpringBeanJobFactory extends SpringBeanJobFactory implements ApplicationContextAware {
    private transient AutowireCapableBeanFactory beanFactory;

    public void setApplicationContext(final ApplicationContext context) {
        beanFactory = context.getAutowireCapableBeanFactory();
    }

    @Override
    public Object createJobInstance(final TriggerFiredBundle bundle) throws Exception {
       final Object job = super.createJobInstance(bundle);
       beanFactory.autowireBean(job);  //the magic is done here
       return job;
    }
}

然后当你这么做的时候

    SchedulerFactory schedFact = new org.quartz.impl.StdSchedulerFactory();
    scheduler = schedFact.getScheduler();

    AutowiringSpringBeanJobFactory autowiringSpringBeanJobFactory = new AutowiringSpringBeanJobFactory();
    autowiringSpringBeanJobFactory.setApplicationContext(applicationContext);
    scheduler.setJobFactory(autowiringSpringBeanJobFactory);
 类似资料:
  • 问题内容: 我正在尝试将Quartz 2.2.1与Spring Boot一起使用。我试图声明一个计划的任务,该任务应该将一些数据写入文件中。我的工作定义如下: 然后 : PS:我已经测试了我的控制器方法“ doPrintData”,它可以工作。但是,当我将其放入面向 javaNullPointerException 的execute方法中时。 问题答案: Spring Boot为您管理它。删除石英

  • 我在springboot应用程序中有筛选器。在embedded tomcate 9.0.34中运行该应用程序时,它可以正常工作。然而,当我创建war并将其部署在外部Tomcate8.5上时,它会抛出以下错误。

  • 本文向大家介绍SpringBoot在IDEA中实现热部署(JRebel实用版),包括了SpringBoot在IDEA中实现热部署(JRebel实用版)的使用技巧和注意事项,需要的朋友参考一下 JRebel简介: JRebel是与应用程序服务器集成的JVM Java代理,可使用现有的类加载器重新加载类。只有更改的类会重新编译并立即重新加载到正在运行的应用程序中,JRebel特别不依赖任何IDE或开发

  • 我试图在GlassFish server 4.1.0中部署ADF 12.1.3应用程序,但在部署过程中出现以下错误:将ADF essentials 3.1.2复制到GlassFish中的lib文件夹,ADF应用程序为12.1.3版本,GlassFish版本为4.1.0 部署过程中发生错误:部署应用程序时出现异常[MegatronApplicationOCS_07012015]:org.xml.sa

  • null 并得到错误。知道吗?

  • 本文向大家介绍SpringBoot项目在IntelliJ IDEA中如何实现热部署,包括了SpringBoot项目在IntelliJ IDEA中如何实现热部署的使用技巧和注意事项,需要的朋友参考一下 spring-boot-devtools是一个为开发者服务的一个模块,其中最重要的功能就是自动应用代码更改到最新的App上面去。 原理是在发现代码有更改之后,重新启动应用,但是速度比手动停止后再启动更