我已经在jHipster服务器中配置了quartz作业,并且它适用于System.out.println
语句但是当我尝试使用持久性相关的工作时它给了我一个例外。我有@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>
答案 0 :(得分:0)
1)为什么我的bean没有被自动配置是因为它没有正确配置。
更新了课程 的 QuartzJobSchedulerConf.java 强>
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" />
<link href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.98.2/css/materialize.min.css" rel="stylesheet" />
<div class="container">
<div class="row">
<div class="col s10 m10 l6 center-align white z-depth-3" id="app">
<a href="https://www.twitch.tv/" target="_blank"><img src="http://www.stickpng.com/assets/images/580b57fcd9996e24bc43c53d.png" alt="Twitch logo" class="responsive-img" id="twitch-logo"></a>
<a class="waves-effect waves-light btn">all</a>
<a class="waves-effect waves-light btn purple">online</a>
<a class="waves-effect waves-light btn purple">offline</a>
<!-- CHANNELS START -->
<div class="col s12" id="channels">
</div>
<!-- CHANNELS END -->
</div>
<!-- COL S6 -->
</div>
<!-- ROW -->
</div>
<!-- CONTAINER -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.100.2/js/materialize.min.js"></script>
添加了类 的 ComplexJobService.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;
}
}
<强> ComplexScheduledJob.java 强>
@Component("complexJobService")
public class ComplexJobService {
@Autowired
JdbcTemplate jdbcTemplate;
@Autowired
UtilityService utilityService;
protected void execute() {
// utilityService.fetchCountriesDTO();
}
}
此java配置取代了Quartz Scheduler的XML配置,例如
@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;
}
}