Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Can';t在Quartz作业中注入依赖项_Java_Spring_Spring Boot_Quartz Scheduler - Fatal编程技术网

Java Can';t在Quartz作业中注入依赖项

Java Can';t在Quartz作业中注入依赖项,java,spring,spring-boot,quartz-scheduler,Java,Spring,Spring Boot,Quartz Scheduler,我试图将依赖项注入Quartz作业,但每当触发作业时,注入的服务上都会出现NullPointerException 我关注了一些关于如何解决这个问题的教程/博客,但它仍然存在 注意,根据一些业务逻辑,我将在第一个作业中创建几个作业。 /!\ : 调试时:JobExecutionContext.getScheduler().getContext().get(“applicationContext”)为空 关于创建作业和触发器(甚至cron)的一切都很好。唯一的问题是我总是在“CampagneseS

我试图将依赖项注入Quartz作业,但每当触发作业时,注入的服务上都会出现NullPointerException

我关注了一些关于如何解决这个问题的教程/博客,但它仍然存在

注意,根据一些业务逻辑,我将在第一个作业中创建几个作业。 /!\ : 调试时:JobExecutionContext.getScheduler().getContext().get(“applicationContext”)为空 关于创建作业和触发器(甚至cron)的一切都很好。唯一的问题是我总是在“CampagneseService”上使用NPE

我的工作:

@Component
@DisallowConcurrentExecution
public class CreateAndCheckFirstCommandeJob implements Job {
            
            @Autowired
            private CampagneService campagneService;

            @Override
            public void execute(JobExecutionContext context) throws JobExecutionException {
               *** buisiness Logic ****
Scheduler scheduler = new StdSchedulerFactory().getScheduler();
    scheduler.start();
JobDetail jobDetail= QuartzConfig.createQuartzJobDetail(a,b,c );

    Trigger trigger= QuartzConfig.createQuartzSimpleTrigger(x, y, z ,t);
    
    scheduler.scheduleJob(jobDetail, trigger);
}
}
配置:

@Configuration
public class QuartzConfig {
    
    private ApplicationContext applicationContext;
    
   
    public QuartzConfig(ApplicationContext applicationContext, DataSource dataSource) {
        this.applicationContext = applicationContext;
        this.dataSource = dataSource;
    }
    
    @Bean
    public SpringBeanJobFactory springBeanJobFactory() {
        AutowiringSpringBeanJobFactory jobFactory = new AutowiringSpringBeanJobFactory();
        jobFactory.setApplicationContext(applicationContext);
        return jobFactory;
    }
    
    @Bean
    public SchedulerFactoryBean scheduler(Trigger... triggers) {
        SchedulerFactoryBean schedulerFactory = new SchedulerFactoryBean();
        Properties properties = new Properties();
        properties.setProperty("org.quartz.scheduler.instanceName", "SCHEDULER");
        properties.setProperty("org.quartz.scheduler.instanceId", "Scheduler-1");
        schedulerFactory.setOverwriteExistingJobs(true);
        schedulerFactory.setAutoStartup(true);
        schedulerFactory.setQuartzProperties(properties);
        schedulerFactory.setDataSource(dataSource);
        schedulerFactory.setJobFactory(springBeanJobFactory());
        schedulerFactory.setWaitForJobsToCompleteOnShutdown(true);
        if (ArrayUtils.isNotEmpty(triggers)) {
            schedulerFactory.setTriggers(triggers);
        }
        return schedulerFactory;
    }

static JobDetail createQuartzJobDetail(Class jobClazz, String jobGroup, Map<String, Object> jobDataMap) {
    String jobName = jobClazz.getSimpleName()
    JobKey jobUniqueKey = new JobKey(jobName, jobGroup);
    JobDetail jobDetail = JobBuilder.newJob(jobClazz)
                        .withIdentity(jobUniqueKey).build();
    

    return jobDetail;
}

static Trigger createQuartzSimpleTrigger(JobDetail jobDetail,String triggerName, String triggerGroup, Date startingDate) {

    return TriggerBuilder.newTrigger()
            .withIdentity(triggerName , triggerGroup).forJob(jobDetail)
            .startAt(startingDate) 
            .build();
}

static Trigger createQuartzCronTrigger(JobDetail jobDetail, String triggerName, String triggerGroup, Date endDate, String cronExpression) {
    return TriggerBuilder.newTrigger()
            .withIdentity( triggerName, triggerGroup)
            .withSchedule(
                    CronScheduleBuilder.cronSchedule(cronExpression))
            .endAt(endDate).forJob(jobDetail)
            .build();
}
}
堆栈策略(在:D的情况下) 第71行是我呼叫服务的地方(this.campageneservice.xxx)


你能发布异常堆栈跟踪吗?@VijayC:我根据跟踪将堆栈跟踪添加到post中,我认为初始化服务bean时可能会出现问题。CampagneseService是否实例化和初始化?我的意思是CampagneseService中的所有依赖项都正确注入了吗?启用spring跟踪日志记录,以便获得有关异常的更多信息。是的,CampagneseService没有问题,因为我将其注入并在其他地方使用,它在quartz上下文中不起作用。campagne服务只是其中的一个。REtutn
autowiring SpringBeanJobFactory
而不是
SpringBeanJobFactory
方法中的
SpringBeanJobFactory
。接下来,您应该注入调度程序,而不是像这样获取它,也不应该启动它。
public final class AutowiringSpringBeanJobFactory extends SpringBeanJobFactory implements ApplicationContextAware {

    private AutowireCapableBeanFactory beanFactory;
    


    @Override
    public void setApplicationContext(final ApplicationContext ctx) {
        beanFactory = ctx.getAutowireCapableBeanFactory();
        
    }

    @Override
    protected Object createJobInstance(final TriggerFiredBundle bundle) throws Exception {
        final Object job = super.createJobInstance(bundle);
        beanFactory.autowireBean(job);
        return job; 
    }
   
}
2020-08-31 15:56:24.647 ERROR 8056 --- [eduler_Worker-1] org.quartz.core.JobRunShell              : Job Cammande.CreateAndCheckPremiereCommandeJob15:56:14.434 threw an unhandled Exception:
    
    java.lang.NullPointerException: null
            at com.gtmt.scheduler.quartz.config.CreateAndCheckFirstCommandeJob.execute(CreateAndCheckFirstCommandeJob.java:71)
            at org.quartz.core.JobRunShell.run(JobRunShell.java:202)
            at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:573)
    
    2020-08-31 15:56:24.650 ERROR 8056 --- [eduler_Worker-1] org.quartz.core.ErrorLogger              : Job (Cammande.CreateAndCheckFirstCommandeJob15:56:14.434 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.gtmt.scheduler.quartz.config.CreateAndCheckFirstCommandeJob.execute(CreateAndCheckFirstCommandeJob.java:71)
            at org.quartz.core.JobRunShell.run(JobRunShell.java:202)
            ... 1 common frames omitted
    
   


Thanks.