Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/385.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 弹簧靴&x2B;库拉茨+;JobStoreTX的Oracle问题_Java_Oracle_Spring Boot_Yaml_Quartz - Fatal编程技术网

Java 弹簧靴&x2B;库拉茨+;JobStoreTX的Oracle问题

Java 弹簧靴&x2B;库拉茨+;JobStoreTX的Oracle问题,java,oracle,spring-boot,yaml,quartz,Java,Oracle,Spring Boot,Yaml,Quartz,我正在使用Spring boot+Quartz+Oracle创建一个应用程序,我希望将调度保存在数据库中(持久化,以防服务器崩溃)。使用RAMJobStore可以很好地工作,但是当我尝试使用JobStoreTX时,它不起作用,它总是使用RAMJobStore,哪里有问题?。我肯定犯了很多错误,但这是我第一次使用SpringBoot+Quartz应用程序,你能给我一个想法吗 事件将动态创建,并在控制器中接收信息 application.yaml(除Quartz外,应用程序还连接到数据库以查询表,但

我正在使用Spring boot+Quartz+Oracle创建一个应用程序,我希望将调度保存在数据库中(持久化,以防服务器崩溃)。使用RAMJobStore可以很好地工作,但是当我尝试使用JobStoreTX时,它不起作用,它总是使用RAMJobStore,哪里有问题?。我肯定犯了很多错误,但这是我第一次使用SpringBoot+Quartz应用程序,你能给我一个想法吗

事件将动态创建,并在控制器中接收信息

application.yaml(除Quartz外,应用程序还连接到数据库以查询表,但应用程序和Quartz将使用相同的数据库)

类计划配置

@Configuration
public class SchedulerConfiguration {
    
    @Bean
    public SchedulerFactoryBean schedulerFactory(ApplicationContext applicationContext) {
        SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();
        schedulerFactoryBean.setJobFactory(new AutoWiringSpringBeanJobFactory());
        return schedulerFactoryBean;
    }

    @Bean
    public Scheduler scheduler(ApplicationContext applicationContext) throws SchedulerException {
        Scheduler scheduler = schedulerFactory(applicationContext).getScheduler();
        scheduler.start();
        return scheduler;
    }

   @Bean
   @QuartzDataSource
   @ConfigurationProperties(prefix = "spring.datasource")
       public DataSource quartzDataSource() {
       return DataSourceBuilder.create().build();
   }
}
类自动布线SpringbeanJobFactor

public class AutoWiringSpringBeanJobFactory extends SpringBeanJobFactory implements
    ApplicationContextAware{
    
    private transient AutowireCapableBeanFactory beanFactory;

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

    @Override
    protected Object createJobInstance(final TriggerFiredBundle bundle) throws Exception {
        final Object job = super.createJobInstance(bundle);
        beanFactory.autowireBean(job);
        return job;
    }
    

}
职业类别

@Component
public class CampaignJob implements Job{
    
        @Override
        public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
            System.out.println("Hi, the job works");
        
    }

}
2020-12-28 16:16:08 INFO com.zaxxer.hikari.HikariDataSource - HikariPool-1 - Starting...

2020-12-28 16:16:09 INFO com.zaxxer.hikari.HikariDataSource - HikariPool-1 - Start completed.

2020-12-28 16:16:09 INFO o.h.jpa.internal.util.LogHelper - HHH000204: Processing PersistenceUnitInfo [name: default]

2020-12-28 16:16:10 INFO org.hibernate.Version - HHH000412: Hibernate ORM core version 5.4.23.Final

2020-12-28 16:16:10 INFO org.quartz.impl.StdSchedulerFactory - Using default implementation for ThreadExecutor

2020-12-28 16:16:10 INFO o.quartz.core.SchedulerSignalerImpl - Initialized Scheduler Signaller of type: class org.quartz.core.SchedulerSignalerImpl

2020-12-28 16:16:10 INFO org.quartz.core.QuartzScheduler - Quartz Scheduler v.2.3.2 created.

2020-12-28 16:16:10 INFO org.quartz.simpl.RAMJobStore - RAMJobStore initialized.

2020-12-28 16:16:10 INFO org.quartz.core.QuartzScheduler - Scheduler meta-data: Quartz Scheduler (v2.3.2) 'schedulerFactory' with instanceId 'NON_CLUSTERED'

Scheduler class: 'org.quartz.core.QuartzScheduler' - running locally.

NOT STARTED.

Currently in standby mode.

Number of jobs executed: 0

Using thread pool 'org.quartz.simpl.SimpleThreadPool' - with 10 threads.

Using job-store 'org.quartz.simpl.RAMJobStore' - which does not support persistence. and is not clustered.
创建调度程序的类

public class ManagerServiceImpl implements ManagerService {
    
    @Autowired
    private ProducerQueue producer;
    
    @Autowired
    private ManagementDatabase managementDatabase;
    
    @Autowired
    private Scheduler scheduler;
    
    @Override
    public String processCampaign(ScheduleCampaign scheduleCampaign) {
        try {
            ZonedDateTime dateTime = ZonedDateTime.of(scheduleCampaign.getDateTime(), scheduleCampaign.getTimeZone());
            JobDetail jobDetail = buildJobDetail(scheduleCampaign);
            Trigger trigger = buildJobTrigger(jobDetail, dateTime);
            scheduler.scheduleJob(jobDetail, trigger);
            } catch (SchedulerException e) {
            System.out.println("There was an error creating the scheduler: "+e);
        }
        
        return "Scheduler created";
    }
    

     private JobDetail buildJobDetail(ScheduleCampaign scheduleCampaign) {
            JobDataMap jobDataMap = new JobDataMap();
            System.out.println("Function: buildJobDetail -  campaign value: "+scheduleCampaign.getCampaign());
            jobDataMap.put("campaign", scheduleCampaign.getCampaign());
            return JobBuilder.newJob(CampaignJob.class)
                    .withIdentity(UUID.randomUUID().toString(), "campaign-jobs")
                    .requestRecovery(true)
                    .storeDurably(true)
                    .withDescription("campaign job planned")
                    .usingJobData(jobDataMap)
                    .storeDurably()
                    .build();
        }

     private Trigger buildJobTrigger(JobDetail jobDetail, ZonedDateTime startAt) {
            return TriggerBuilder.newTrigger()
                    .forJob(jobDetail)
                    .withIdentity(jobDetail.getKey().getName(), "campaign-triggers")
                    .withDescription("campaign job Trigger")
                    .startAt(Date.from(startAt.toInstant()))
                    .withSchedule(SimpleScheduleBuilder.simpleSchedule().withMisfireHandlingInstructionFireNow())
                    .build();
        }

    

}
日志

@Component
public class CampaignJob implements Job{
    
        @Override
        public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
            System.out.println("Hi, the job works");
        
    }

}
2020-12-28 16:16:08 INFO com.zaxxer.hikari.HikariDataSource - HikariPool-1 - Starting...

2020-12-28 16:16:09 INFO com.zaxxer.hikari.HikariDataSource - HikariPool-1 - Start completed.

2020-12-28 16:16:09 INFO o.h.jpa.internal.util.LogHelper - HHH000204: Processing PersistenceUnitInfo [name: default]

2020-12-28 16:16:10 INFO org.hibernate.Version - HHH000412: Hibernate ORM core version 5.4.23.Final

2020-12-28 16:16:10 INFO org.quartz.impl.StdSchedulerFactory - Using default implementation for ThreadExecutor

2020-12-28 16:16:10 INFO o.quartz.core.SchedulerSignalerImpl - Initialized Scheduler Signaller of type: class org.quartz.core.SchedulerSignalerImpl

2020-12-28 16:16:10 INFO org.quartz.core.QuartzScheduler - Quartz Scheduler v.2.3.2 created.

2020-12-28 16:16:10 INFO org.quartz.simpl.RAMJobStore - RAMJobStore initialized.

2020-12-28 16:16:10 INFO org.quartz.core.QuartzScheduler - Scheduler meta-data: Quartz Scheduler (v2.3.2) 'schedulerFactory' with instanceId 'NON_CLUSTERED'

Scheduler class: 'org.quartz.core.QuartzScheduler' - running locally.

NOT STARTED.

Currently in standby mode.

Number of jobs executed: 0

Using thread pool 'org.quartz.simpl.SimpleThreadPool' - with 10 threads.

Using job-store 'org.quartz.simpl.RAMJobStore' - which does not support persistence. and is not clustered.

如果您也为quartz作业指向相同的DB,请尝试自动连接应用程序默认数据源

下面的配置适用于我的PostgreSQL

@Autowired
DataSource dataSource;

@Autowired
JobFactory jobFactory;

@Bean
public JobFactory jobFactory(ApplicationContext applicationContext) {
    AutoWiringSpringBeanJobFactory jobFactory = new AutoWiringSpringBeanJobFactory();
    jobFactory.setApplicationContext(applicationContext);
    return jobFactory;
}

@Bean
public SchedulerFactoryBean schedulerFactoryBean() throws IOException {
    SchedulerFactoryBean factory = new SchedulerFactoryBean();
    factory.setOverwriteExistingJobs(true);
    factory.setAutoStartup(true);
    factory.setDataSource(dataSource);
    factory.setJobFactory(jobFactory);
    factory.setQuartzProperties(quartzProperties());

    return factory;
}

@Bean
public Properties quartzProperties() throws IOException {
    PropertiesFactoryBean propertiesFactoryBean = new PropertiesFactoryBean();
    propertiesFactoryBean.setLocation(new ClassPathResource("/quartz.properties"));
    propertiesFactoryBean.afterPropertiesSet();
    return propertiesFactoryBean.getObject();
}