gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 11:06:33 25 4
gpt4 key购买 nike

我已经在 jHipster 服务器中配置了quartz 作业,它在 System.out.println 上运行良好。声明但是当我尝试使用与持久性相关的工作时它给了我一个异常(exception)。我有@Autowire JdbcTemplate jdbcTemplate;但它仍然为空,原因是当 jHipster 服务器运行时它告诉:

 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

这些是 jhipster 打印的日志,请告诉我如何将持久性与 jdbcTemplet 结合使用,我不想打开单独的 JDBC 连接。如果可能的话,我想使用现有的服务类,但这也会出错。

这是我的配置文件和作业文件

@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 中配置 Quartz 作业,请告诉我。或者,如果 jHipster 为 Scheduler 工作提供开箱即用的功能,那就太好了。

quartz 作业依赖

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

最佳答案

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();

}
}

ComplexScheduledJob.java

@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;
}
}

This java configuration replaces the XML configuration of Quartz Scheduler e.g.

<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>

关于java - 如何在jHipster中配置Quartz作业? [在 jHipster 中配置 Quartz 作业时出现问题],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46419678/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com