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

如何在JHIPSTER中配置Quartz作业?[在jHipster中配置Quartz作业时出现的问题]

柯乐童
2023-03-14
 Scheduler meta-data: Quartz Scheduler (v2.2.1) 'schedulerFactoryBean' 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**.

2017-09-26 10:56:10.523  INFO 10188 --- [  restartedMain] org.quartz.impl.StdSchedulerFactory      : Quartz scheduler **'schedulerFactoryBean' initialized from an externally provided properties** instance.
2017-09-26 10:56:10.523  INFO 10188 --- [  restartedMain] org.quartz.impl.StdSchedulerFactory      : Quartz scheduler version: 2.2.1
2017-09-26 10:56:10.523  INFO 10188 --- [  restartedMain] org.quartz.core.QuartzScheduler          : JobFactory set to: org.springframework.scheduling.quartz
@Configuration
@ComponentScan("com.jobs")
public class QuartzJobSchedulerConf {
//http://www.concretepage.com/spring-4/spring-4-quartz-2-scheduler integration-annotation-example-using-javaconfig
//todo make common function for simple jobs to set attributes
//todo make common function for complex jobs to set attributes

    @Bean //this is simple job1
public MethodInvokingJobDetailFactoryBean methodInvokingJobDetailFactoryBean() {
    MethodInvokingJobDetailFactoryBean obj = new MethodInvokingJobDetailFactoryBean();
    obj.setTargetBeanName("jobone");
    obj.setTargetMethod("myTask");
    return obj;
}
@Bean
public SimpleTriggerFactoryBean simpleTriggerFactoryBean(){
    //This trigger will schedule the job after 3 seconds and repeat after every 30 seconds for 3+1 times.
    SimpleTriggerFactoryBean stFactory = new SimpleTriggerFactoryBean();
    stFactory.setJobDetail(methodInvokingJobDetailFactoryBean().getObject());
    stFactory.setStartDelay(3000);
    stFactory.setRepeatInterval(30000);
    stFactory.setRepeatCount(3);//todo repeat counter remove or use
    return stFactory;
}

@Bean //this is simple job2
public MethodInvokingJobDetailFactoryBean methodInvokingJobDetailFactoryBean2() {
    MethodInvokingJobDetailFactoryBean obj = new MethodInvokingJobDetailFactoryBean();
    obj.setTargetBeanName("jobtwo");
    obj.setTargetMethod("myTask");
    return obj;
}
@Bean
public SimpleTriggerFactoryBean simpleTriggerFactoryBean2(){
    SimpleTriggerFactoryBean stFactory = new SimpleTriggerFactoryBean();
    stFactory.setJobDetail(methodInvokingJobDetailFactoryBean2().getObject());
    stFactory.setStartDelay(3000);
    stFactory.setRepeatInterval(30000);
    stFactory.setRepeatCount(3);//todo repeat counter remove or use
    return stFactory;
}
@Bean//this is complex job1
public JobDetailFactoryBean jobDetailFactoryBean(){
    /*To pass the parameter to job by JavaConfig, we can have setter method and
    the property should be configured with setJobDataAsMap() in JobDetailFactoryBean
     configuration in JavaConfig*/
    JobDetailFactoryBean factory = new JobDetailFactoryBean();
    factory.setJobClass(SampleComplexJob.class);
    Map<String,Object> map = new HashMap<String,Object>();
    map.put("name", "RAM");
    map.put(SampleComplexJob.COUNT, 1);
    factory.setJobDataAsMap(map);
    factory.setGroup("mygroup");
    factory.setName("myjob");
    return factory;
}
@Bean
public CronTriggerFactoryBean cronTriggerFactoryBean(){
    CronTriggerFactoryBean stFactory = new CronTriggerFactoryBean();
    stFactory.setJobDetail(jobDetailFactoryBean().getObject());
    stFactory.setStartDelay(3000);
    stFactory.setName("mytrigger");
    stFactory.setGroup("mygroup");
    stFactory.setCronExpression("0 0/1 * 1/1 * ? *");
    return stFactory;
}

@Bean
public SchedulerFactoryBean schedulerFactoryBean() {
    SchedulerFactoryBean scheduler = new SchedulerFactoryBean();
    scheduler.setTriggers(simpleTriggerFactoryBean().getObject(),
        simpleTriggerFactoryBean2().getObject(),
        cronTriggerFactoryBean().getObject());
    return scheduler;
}
}
/*If we want to persist the changes in JobDataMap, we will annotate our class by @PersistJobDataAfterExecution */
@PersistJobDataAfterExecution

/*if there is more than one trigger which are scheduling same job then to avoid race condition, we have to annotate our job with @DisallowConcurrentExecution.*/
@DisallowConcurrentExecution

public class SampleComplexJob extends QuartzJobBean {

@Autowired
JdbcTemplate jdbcTemplate;

public static final String COUNT = "count";
private String name;
protected void executeInternal(JobExecutionContext ctx) throws JobExecutionException {

    for(String role:findRoleByPrivilegeSQLExample("PRIV_USER_SETTING")){
        System.out.println(role);
    }
    JobDataMap dataMap = ctx.getJobDetail().getJobDataMap();
    int cnt = dataMap.getInt(COUNT);
    JobKey jobKey = ctx.getJobDetail().getKey();
    System.out.println(jobKey+": "+name+": "+ cnt);
    cnt++;
    dataMap.put(COUNT, cnt);
}
public void setName(String name) {
    this.name = name;
}

public List<String> findRoleByPrivilegeSQLExample(String privilege){

    ///this is SQL example
    List<String> rolePrivilegesList = new ArrayList<String>();
    if(!privilege.isEmpty()){
        StringBuilder sql=new StringBuilder()
            .append("SELECT ID_ROLE FROM ROLE_PRIVILEGES WHERE ")
            .append("UPPER(ID_PRIVILEGE) = '")
            .append(privilege.toUpperCase()).append("'");
        rolePrivilegesList = jdbcTemplate.queryForList(sql.toString(),String.class);
    }
    return rolePrivilegesList;
}
}

如果有其他的方法来配置jHipster中的石英作业,请告诉我。或者,如果jHipster为调度程序工作提供了开箱即用的功能,那就太好了。

石英作业依赖关系

<dependency>
        <groupId>org.quartz-scheduler</groupId>
        <artifactId>quartz</artifactId>
        <version>2.2.1</version>
    </dependency>

共有1个答案

黄弘深
2023-03-14

1)我的bean没有自动配置是因为它没有正确配置。

更新了类QuartzJobSchedulerConf.java

@Configuration
@ComponentScan("com.jobs")
public class QuartzJobSchedulerConf {

 @Bean//this is complex job1
public JobDetailFactoryBean jobDetailFactoryBean(ComplexJobService complexJobService){

    JobDetailFactoryBean factory = new JobDetailFactoryBean();
    factory.setJobClass(ComplexScheduledJob.class);
    Map<String,Object> map = new HashMap<String,Object>();
    map.put("complexJobService", complexJobService);
    factory.setJobDataAsMap(map);
    factory.setDurability(true);

    return factory;
}
@Bean
public CronTriggerFactoryBean cronTriggerFactoryBean(ComplexJobService complexJobService){

    CronTriggerFactoryBean stFactory = new CronTriggerFactoryBean();
    stFactory.setJobDetail(jobDetailFactoryBean(complexJobService).getObject());
    stFactory.setCronExpression("0 0/1 * 1/1 * ? *");
    stFactory.setStartDelay(3000);

    return stFactory;
}

@Bean
public SchedulerFactoryBean schedulerFactoryBean(ComplexJobService complexJobService) {

    SchedulerFactoryBean scheduler = new SchedulerFactoryBean();
    scheduler.setConfigLocation(new ClassPathResource("properties/quartz.properties"));
    scheduler.setTriggers(
        cronTriggerFactoryBean(complexJobService).getObject());
    return scheduler;
    }
}

添加的类ComplexJObservice.java

@Component("complexJobService")
public class ComplexJobService {

@Autowired
JdbcTemplate jdbcTemplate;

@Autowired
UtilityService utilityService;

protected void execute() {

//        utilityService.fetchCountriesDTO();

}
}
@PersistJobDataAfterExecution
@DisallowConcurrentExecution
@Component
public class ComplexScheduledJob extends QuartzJobBean{

@Autowired
private ComplexJobService complexJobService;
private static int count;

@Override
protected void executeInternal(JobExecutionContext jobContext)
        throws JobExecutionException {

    complexJobService.execute();
    count++;
    System.out.println("Job count " + count);
}

public void setComplexJobService(ComplexJobService complexJobService) {
    this.complexJobService = complexJobService;
}
}
<bean name="complexJobDetail"    class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
    <property name="jobClass" value="com.jobs.ComplexScheduledJo" />
    <property name="jobDataMap">
        <map>
            <entry key="complexJobService" value-ref="complexJobService" />
        </map>
    </property>
    <property name="durability" value="true" />
</bean>
 类似资料:
  • 我目前正试图在我当前的Jhipster应用程序中实现ssl。到目前为止,我已经使用keytool-genkey-alias iroApp-storetype PKCS12-keyalg RSA-keysize 2048-keystore密钥库生成了一个证书。p12-有效期3650。 在application-dev.yml中,我更改了以下服务器选项: 致: 代理。conf.json:From }

  • 我想在linux jenkins中配置IOS项目作业。在没有OSX PC的情况下,是否可以在Linux Jenkins中配置IOS作业。

  • 我是新的spring批处理框架和石英调度器。我的任务是使用quartz调度器动态调度一个新的spring批处理作业。所有新的spring批处理作业的条目都在我的数据库中,具有触发器表达式。问题是,对于每一个来自数据库的新的spring批处理作业,我们需要将其包装在Quartz的调度器作业中。这就意味着,当有许多spring批处理作业时,就应该有许多批处理作业类来包装它们,并由quartz调度程序运

  • 我有每天的cron作业,它应该在00:00运行在所有时区,但当应用程序在维护(可能是一两个小时),部分计划的作业丢失。 是否可以运行在维护期间错过的任务? 在Quartz shoutdown之前(从记录): 石英启动后: