Skip to content

Commit 0d10d6b

Browse files
committed
worm init done
1 parent 9833148 commit 0d10d6b

File tree

5 files changed

+58
-12
lines changed

5 files changed

+58
-12
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.berry.oss.quartz;
2+
3+
import org.quartz.spi.TriggerFiredBundle;
4+
import org.springframework.beans.factory.annotation.Autowired;
5+
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
6+
import org.springframework.scheduling.quartz.AdaptableJobFactory;
7+
import org.springframework.stereotype.Component;
8+
9+
/**
10+
* Created with IntelliJ IDEA.
11+
*
12+
* @author Berry_Cooper.
13+
* @version 1.0
14+
* @date 2020/12/22 12:57
15+
* fileName:JobFactory
16+
* Use:
17+
*/
18+
@Component
19+
public class JobFactory extends AdaptableJobFactory {
20+
21+
@Autowired
22+
private AutowireCapableBeanFactory capableBeanFactory;
23+
24+
@Override
25+
protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception {
26+
// 调用父类的方法
27+
Object jobInstance = super.createJobInstance(bundle);
28+
// 进行注入
29+
capableBeanFactory.autowireBean(jobInstance);
30+
return jobInstance;
31+
}
32+
}

src/main/java/com/berry/oss/quartz/QuartzConfig.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
import com.berry.oss.common.utils.CsvUtils;
44
import com.berry.oss.quartz.job.NonReferenceObjectCleanJob;
55
import org.quartz.*;
6+
import org.springframework.beans.factory.annotation.Autowired;
67
import org.springframework.context.annotation.Bean;
78
import org.springframework.context.annotation.Configuration;
9+
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
810

911
/**
1012
* Title QuartzConfig
@@ -16,6 +18,22 @@
1618
@Configuration
1719
public class QuartzConfig {
1820

21+
@Autowired
22+
private JobFactory jobFactory;
23+
24+
@Bean
25+
public SchedulerFactoryBean schedulerFactoryBean() {
26+
SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();
27+
schedulerFactoryBean.setJobFactory(jobFactory);
28+
return schedulerFactoryBean;
29+
}
30+
31+
@Bean
32+
public Scheduler scheduler() {
33+
return schedulerFactoryBean().getScheduler();
34+
}
35+
36+
1937
@Bean
2038
public JobDetail nonRefObjectCleanJob() {
2139
return JobBuilder.newJob(NonReferenceObjectCleanJob.class).withIdentity("non_ref_clean_job", "sys_job").storeDurably().build();

src/main/java/com/berry/oss/quartz/QuartzJobManagement.java

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
import org.quartz.*;
44
import org.quartz.impl.StdSchedulerFactory;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.context.annotation.Bean;
7+
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
58
import org.springframework.stereotype.Service;
69

710
import javax.annotation.PostConstruct;
@@ -20,21 +23,13 @@
2023
public class QuartzJobManagement {
2124
private static final String JOB_GROUP_NAME = "WORM_CHECK_JOB_GROUP";
2225
private static final String TRIGGER_GROUP_NAME = "WORM_CHECK_TRIGGER_GROUP";
26+
2327
/**
2428
* Quartz调度器
2529
*/
30+
@Autowired
2631
private Scheduler scheduler;
2732

28-
/**
29-
* 初始化调度器
30-
* @throws SchedulerException e
31-
*/
32-
@PostConstruct
33-
public void init() throws SchedulerException {
34-
SchedulerFactory schedulerFactory = new StdSchedulerFactory();
35-
scheduler = schedulerFactory.getScheduler();
36-
}
37-
3833
/**
3934
* 添加一个定时任务
4035
*/

src/main/java/com/berry/oss/quartz/dynamic/WormCheckJob.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public class WormCheckJob implements Job {
3333

3434
private static final Logger logger = LoggerFactory.getLogger(WormCheckJob.class);
3535

36+
@Autowired
3637
private IWormStrategyDaoService wormStrategyDaoService;
3738

3839
@Override
@@ -51,7 +52,7 @@ public void execute(JobExecutionContext context) throws JobExecutionException {
5152
return;
5253
}
5354
wormStrategy.setWormState(CommonConstant.WormState.Expired.name());
54-
wormStrategyDaoService.save(wormStrategy);
55+
wormStrategyDaoService.updateById(wormStrategy);
5556
logger.info("策略:【{}】使失效成功", jobName);
5657
}
5758
}

src/main/java/com/berry/oss/service/impl/WormStrategyServiceImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public WormStrategy initWormStrategy(String bucket, Integer days) {
7575
wormStrategy.setWormState(CommonConstant.WormState.InProgress.name());
7676
wormStrategy.setCreateTime(new Date());
7777
// 策略生效开始时间(在此之前 状态未提交锁定(Locked),策略自动失效:Expired)
78-
Date activeTime = DateTime.now().plusMinutes(1).toDate();
78+
Date activeTime = DateTime.now().plusHours(24).toDate();
7979
wormStrategy.setActiveTime(activeTime);
8080
wormStrategyDaoService.save(wormStrategy);
8181
// 启动一个定时任务, 在 activeTime 检查状态, 如果不为 Locked ,则设置为 Expired

0 commit comments

Comments
 (0)