Skip to content

Commit c3a5c88

Browse files
committed
添加更新Schedule写法。
1 parent 1ce9cdb commit c3a5c88

3 files changed

Lines changed: 37 additions & 123 deletions

File tree

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
11
package com.xwintop.xJavaFxTool.job;
22

3+
import com.xwintop.xJavaFxTool.services.littleTools.FileCopyService;
34
import org.quartz.Job;
45
import org.quartz.JobExecutionContext;
56
import org.quartz.JobExecutionException;
67

7-
import com.xwintop.xJavaFxTool.services.littleTools.FileCopyService;
8-
9-
public class FileCopyJob implements Job{
8+
public class FileCopyJob implements Job {
109

11-
@Override
12-
public void execute(JobExecutionContext context) throws JobExecutionException {
13-
FileCopyService fileCopyService = (FileCopyService) context.getMergedJobDataMap().get("fileCopyService");
14-
try {
15-
fileCopyService.copyAction();
16-
} catch (Exception e) {
17-
e.printStackTrace();
18-
}
10+
@Override
11+
public void execute(JobExecutionContext context) throws JobExecutionException {
12+
FileCopyService fileCopyService = (FileCopyService) context.getMergedJobDataMap().get("Service");
13+
try {
14+
fileCopyService.copyAction();
15+
} catch (Exception e) {
16+
e.printStackTrace();
17+
}
1918
// JobKey jobKey = context.getJobDetail().getKey();
2019
// System.out.println("执行了"+jobKey);
21-
}
20+
}
2221
}

src/main/java/com/xwintop/xJavaFxTool/manager/ScheduleManager.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.xwintop.xJavaFxTool.manager;
22

3+
import com.xwintop.xcore.util.javafx.TooltipUtil;
34
import lombok.Getter;
45
import lombok.Setter;
56
import lombok.extern.log4j.Log4j;
@@ -27,15 +28,27 @@ public enum QuartzType {
2728
, CRON //Cron表达式
2829
}
2930

30-
public void runQuartzAction(Class <? extends Job> jobClass, Object jobDataMap, int interval, int repeatCount) throws Exception {
31+
public void runQuartzAction(Class<? extends Job> jobClass, String quartzType, Object jobDataMap, String cronText, int interval, int repeatCount) throws Exception {
32+
if ("简单表达式".equals(quartzType)) {
33+
this.runQuartzAction(jobClass, jobDataMap, interval, repeatCount);
34+
} else if ("Cron表达式".equals(quartzType)) {
35+
if (StringUtils.isEmpty(cronText)) {
36+
TooltipUtil.showToast("cron表达式不能为空。");
37+
throw new Exception("cron表达式不能为空。");
38+
}
39+
this.runQuartzAction(jobClass, jobDataMap, cronText);
40+
}
41+
}
42+
43+
public void runQuartzAction(Class<? extends Job> jobClass, Object jobDataMap, int interval, int repeatCount) throws Exception {
3144
runQuartzAction(jobClass, jobDataMap, QuartzType.SIMPLE, null, interval, repeatCount);
3245
}
3346

34-
public void runQuartzAction(Class <? extends Job> jobClass, Object jobDataMap, String cronText) throws Exception {
47+
public void runQuartzAction(Class<? extends Job> jobClass, Object jobDataMap, String cronText) throws Exception {
3548
runQuartzAction(jobClass, jobDataMap, QuartzType.CRON, cronText, 0, 0);
3649
}
3750

38-
public void runQuartzAction(Class <? extends Job> jobClass, Object jobDataMap, QuartzType quartzType, String cronText, int interval, int repeatCount) throws Exception {
51+
public void runQuartzAction(Class<? extends Job> jobClass, Object jobDataMap, QuartzType quartzType, String cronText, int interval, int repeatCount) throws Exception {
3952
schedulerKeyGroup = jobClass.toString();
4053
schedulerKeyName = schedulerKeyGroup + System.currentTimeMillis();
4154
JobDetail jobDetail = JobBuilder.newJob(jobClass).withIdentity(schedulerKeyName, schedulerKeyGroup)

src/main/java/com/xwintop/xJavaFxTool/services/littleTools/FileCopyService.java

Lines changed: 10 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,23 @@
11
package com.xwintop.xJavaFxTool.services.littleTools;
22

3-
import java.io.File;
4-
import java.util.Collection;
5-
import java.util.function.Consumer;
6-
7-
import org.apache.commons.configuration.PropertiesConfiguration;
8-
import org.apache.commons.io.FileUtils;
9-
import org.apache.commons.lang.StringUtils;
10-
import org.quartz.CronScheduleBuilder;
11-
import org.quartz.JobBuilder;
12-
import org.quartz.JobDetail;
13-
import org.quartz.JobKey;
14-
import org.quartz.ScheduleBuilder;
15-
import org.quartz.Scheduler;
16-
import org.quartz.SchedulerFactory;
17-
import org.quartz.SimpleScheduleBuilder;
18-
import org.quartz.Trigger;
19-
import org.quartz.TriggerBuilder;
20-
import org.quartz.TriggerKey;
21-
import org.quartz.impl.StdSchedulerFactory;
22-
233
import com.xwintop.xJavaFxTool.job.FileCopyJob;
4+
import com.xwintop.xJavaFxTool.manager.ScheduleManager;
245
import com.xwintop.xJavaFxTool.model.FileCopyTableBean;
256
import com.xwintop.xJavaFxTool.utils.ConfigureUtil;
267
import com.xwintop.xcore.util.FileUtil;
278
import com.xwintop.xcore.util.javafx.FileChooserUtil;
289
import com.xwintop.xcore.util.javafx.TooltipUtil;
29-
3010
import javafx.collections.ObservableList;
3111
import javafx.stage.FileChooser;
12+
import org.apache.commons.configuration.PropertiesConfiguration;
13+
import org.apache.commons.io.FileUtils;
14+
15+
import java.io.File;
16+
import java.util.Collection;
3217

3318
public class FileCopyService {
3419
private ObservableList<FileCopyTableBean> tableData;
35-
private SchedulerFactory sf = new StdSchedulerFactory();
36-
private String schedulerKeyGroup = "runFileCopy";
37-
private String schedulerKeyName = "runFileCopy" + System.currentTimeMillis();
20+
private ScheduleManager scheduleManager = new ScheduleManager();
3821

3922
public void saveConfigure() throws Exception {
4023
saveConfigure(ConfigureUtil.getConfigureFile("fileCopyConfigure.properties"));
@@ -69,12 +52,7 @@ public void loadingConfigure(File file) {
6952
try {
7053
tableData.clear();
7154
PropertiesConfiguration xmlConfigure = new PropertiesConfiguration(file);
72-
xmlConfigure.getKeys().forEachRemaining(new Consumer<String>() {
73-
@Override
74-
public void accept(String t) {
75-
tableData.add(new FileCopyTableBean(xmlConfigure.getString(t)));
76-
}
77-
});
55+
xmlConfigure.getKeys().forEachRemaining(t -> tableData.add(new FileCopyTableBean(xmlConfigure.getString(t))));
7856
} catch (Exception e) {
7957
try {
8058
TooltipUtil.showToast("加载配置失败:" + e.getMessage());
@@ -95,55 +73,6 @@ public void copyAction() throws Exception {
9573
for (FileCopyTableBean tableBean : tableData) {
9674
if (tableBean.getIsCopy()) {
9775
copyAction(tableBean);
98-
// int number = Integer.parseInt(tableBean.getCopyNumber());
99-
// File fileOriginal = new File(tableBean.getCopyFileOriginalPath());
100-
// File fileTarget = new File(tableBean.getCopyFileTargetPath());
101-
// for (int i = 0; i < number; i++) {
102-
// if (fileOriginal.isDirectory()) {
103-
// if(tableBean.getIsRename()){
104-
// Collection<File> files = FileUtils.listFiles(fileOriginal, null, false);
105-
// for (File file : files) {
106-
// String fileName = FileUtil.getRandomFileName(file);
107-
// if (i != 0) {
108-
// fileName = i+fileName;
109-
// }
110-
// FileUtils.copyFile(file, new File(fileTarget.getPath(), fileName));
111-
// }
112-
// }else{
113-
// if (number == 1) {
114-
// FileUtils.copyDirectory(fileOriginal, fileTarget, false);
115-
// } else {
116-
// Collection<File> files = FileUtils.listFiles(fileOriginal, null, false);
117-
// for (File file : files) {
118-
// FileUtils.copyFile(file, new File(fileTarget.getPath(), (i==0?"":i) + file.getName()));
119-
// }
120-
// }
121-
// }
122-
// } else {
123-
// if(tableBean.getIsRename()){
124-
// String fileName = FileUtil.getRandomFileName(fileOriginal);
125-
// if (i != 0) {
126-
// fileName = i+fileName;
127-
// }
128-
// File file = new File(fileTarget.getPath(), fileName);
129-
// FileUtils.copyFile(fileOriginal,file);
130-
// }else{
131-
// if (i == 0) {
132-
// FileUtils.copyFileToDirectory(fileOriginal, fileTarget);
133-
// } else {
134-
// FileUtils.copyFile(fileOriginal,
135-
// new File(fileTarget.getPath(), i + fileOriginal.getName()));
136-
// }
137-
// }
138-
// }
139-
// }
140-
// if (tableBean.getIsDelete()) {
141-
// if (fileOriginal.isDirectory()) {
142-
// FileUtils.deleteDirectory(fileOriginal);
143-
// } else {
144-
// FileUtils.deleteQuietly(fileOriginal);
145-
// }
146-
// }
14776
}
14877
}
14978
}
@@ -207,39 +136,12 @@ public void copyAction(FileCopyTableBean tableBean) throws Exception {
207136

208137
@SuppressWarnings({"unchecked", "rawtypes"})
209138
public boolean runQuartzAction(String quartzType, String cronText, int interval, int repeatCount) throws Exception {
210-
JobDetail jobDetail = JobBuilder.newJob(FileCopyJob.class).withIdentity(schedulerKeyName, schedulerKeyGroup).build();
211-
jobDetail.getJobDataMap().put("fileCopyService", this);
212-
ScheduleBuilder scheduleBuilder = null;
213-
if ("简单表达式".equals(quartzType)) {
214-
scheduleBuilder = SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(interval)// 时间间隔
215-
.withRepeatCount(repeatCount);// 重复次数(将执行6次)
216-
} else if ("Cron表达式".equals(quartzType)) {
217-
if (StringUtils.isEmpty(cronText)) {
218-
TooltipUtil.showToast("cron表达式不能为空。");
219-
return false;
220-
}
221-
scheduleBuilder = CronScheduleBuilder.cronSchedule(cronText);
222-
}
223-
// 描叙触发Job执行的时间触发规则,Trigger实例化一个触发器
224-
Trigger trigger = TriggerBuilder.newTrigger()// 创建一个新的TriggerBuilder来规范一个触发器
225-
.withIdentity(schedulerKeyName, schedulerKeyGroup)// 给触发器一个名字和组名
226-
.startNow()// 立即执行
227-
.withSchedule(scheduleBuilder).build();// 产生触发器
228-
229-
// 运行容器,使用SchedulerFactory创建Scheduler实例
230-
Scheduler scheduler = sf.getScheduler();
231-
// 向Scheduler添加一个job和trigger
232-
scheduler.scheduleJob(jobDetail, trigger);
233-
if (!scheduler.isStarted()) {
234-
scheduler.start();
235-
}
139+
scheduleManager.runQuartzAction(FileCopyJob.class, quartzType, this, cronText, interval, repeatCount);
236140
return true;
237141
}
238142

239143
public boolean stopQuartzAction() throws Exception {
240-
Scheduler sched = sf.getScheduler();
241-
sched.unscheduleJob(new TriggerKey(schedulerKeyName, schedulerKeyGroup));
242-
sched.deleteJob(new JobKey(schedulerKeyName, schedulerKeyGroup));
144+
scheduleManager.stopQuartzAction();
243145
return true;
244146
}
245147

0 commit comments

Comments
 (0)