11package 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-
233import com .xwintop .xJavaFxTool .job .FileCopyJob ;
4+ import com .xwintop .xJavaFxTool .manager .ScheduleManager ;
245import com .xwintop .xJavaFxTool .model .FileCopyTableBean ;
256import com .xwintop .xJavaFxTool .utils .ConfigureUtil ;
267import com .xwintop .xcore .util .FileUtil ;
278import com .xwintop .xcore .util .javafx .FileChooserUtil ;
289import com .xwintop .xcore .util .javafx .TooltipUtil ;
29-
3010import javafx .collections .ObservableList ;
3111import 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
3318public 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