44import jakarta .annotation .PreDestroy ;
55import lombok .RequiredArgsConstructor ;
66import lombok .extern .log4j .Log4j2 ;
7+ import org .apache .commons .lang3 .StringUtils ;
78import org .quartz .*;
89import org .quartz .impl .matchers .GroupMatcher ;
910import org .springframework .stereotype .Service ;
@@ -31,9 +32,9 @@ public void onInit() {
3132 public void scheduleJob (Class <? extends Job > clazz , JobTimer jobTimer ) {
3233 final JobDetail job = buildJob (clazz , jobTimer );
3334 final Trigger trigger = buildTrigger (jobTimer );
34-
3535 try {
3636 scheduler .scheduleJob (job , trigger );
37+ log .info ("Quartz job has been scheduled with id : " + jobTimer .getJobId ());
3738 } catch (SchedulerException e ) {
3839 log .error (e .getMessage (), e );
3940 throw new RuntimeException (e );
@@ -53,10 +54,11 @@ public JobTimer getRunningTimer(String timerId, String group) {
5354
5455
5556 public void updateTimer (String timerId , String group , JobTimer jobTimer ) {
56- final JobKey jobKey = new JobKey (timerId , group );
57+ final JobKey jobKey = new JobKey (timerId );
5758 try {
5859 final JobDetail job = scheduler .getJobDetail (jobKey );
5960 job .getJobDataMap ().put (jobKey .getName (), jobTimer );
61+ scheduler .addJob (job , true , true );
6062 } catch (SchedulerException e ) {
6163 log .error (e .getMessage (), e );
6264 }
@@ -84,29 +86,39 @@ public List<JobTimer> getRunningTimers() {
8486 }
8587
8688 private Trigger buildTrigger (JobTimer jobTimer ) {
87- SimpleScheduleBuilder builder = SimpleScheduleBuilder .simpleSchedule ()
88- .withIntervalInMilliseconds (jobTimer .getRepeatIntervalMs ());
89-
90- if (jobTimer .isRunForever ()) {
91- builder = builder .repeatForever ();
89+ if (StringUtils .isNotEmpty (jobTimer .getCronExpression ())) {
90+ return TriggerBuilder .newTrigger ()
91+ .withIdentity (jobTimer .getJobId ())
92+ .withSchedule (CronScheduleBuilder .cronSchedule (jobTimer .getCronExpression ()))
93+ .startAt (new Date (System .currentTimeMillis () + jobTimer .getInitialOffsetMs ()))
94+ .build ();
9295 } else {
93- builder = builder .withRepeatCount (jobTimer .getTotalFireCount () - 1 );
96+ SimpleScheduleBuilder builder = SimpleScheduleBuilder .simpleSchedule ()
97+ .withIntervalInMilliseconds (jobTimer .getRepeatIntervalMs ());
98+
99+ if (jobTimer .isRunForever ()) {
100+ builder = builder .repeatForever ();
101+ } else {
102+ builder = builder .withRepeatCount (jobTimer .getTotalFireCount () - 1 );
103+ }
104+
105+ return TriggerBuilder
106+ .newTrigger ()
107+ .withIdentity (jobTimer .getJobId ())
108+ // .withIdentity(jobTimer.getJobId(), jobTimer.getGroupName())
109+ .withSchedule (builder )
110+ .startAt (new Date (System .currentTimeMillis () + jobTimer .getInitialOffsetMs ()))
111+ .build ();
94112 }
95-
96- return TriggerBuilder
97- .newTrigger ()
98- .withIdentity (jobTimer .getJobId (), jobTimer .getGroupName ())
99- .withSchedule (builder )
100- .startAt (new Date (System .currentTimeMillis () + jobTimer .getInitialOffsetMs ()))
101- .build ();
102113 }
103114
104115 private JobDetail buildJob (Class <? extends Job > clazz , JobTimer jobTimer ) {
105116 final JobDataMap dataMap = new JobDataMap ();
106117 dataMap .put (jobTimer .getJobId (), jobTimer );
107118 return JobBuilder
108119 .newJob (clazz )
109- .withIdentity (jobTimer .getJobId (), jobTimer .getGroupName ())
120+ .withIdentity (jobTimer .getJobId ())
121+ //.withIdentity(jobTimer.getJobId(), jobTimer.getGroupName())
110122 .setJobData (dataMap )
111123 .build ();
112124 }
@@ -121,4 +133,33 @@ public void onDestroy() {
121133 }
122134 }
123135
136+ public boolean deleteJob (String jobId ) {
137+ try {
138+ boolean deletedJob = scheduler .deleteJob (new JobKey (jobId ));
139+ log .info ("Quartz job has been deleted with id : " + jobId );
140+ return deletedJob ;
141+ } catch (SchedulerException e ) {
142+ return false ;
143+ }
144+ }
145+
146+ public boolean pauseJob (String jobId ) {
147+ try {
148+ scheduler .pauseJob (new JobKey (jobId ));
149+ log .info ("Quartz job has been pause with id : " + jobId );
150+ return true ;
151+ } catch (SchedulerException e ) {
152+ return false ;
153+ }
154+ }
155+
156+ public boolean resumeJob (String jobId ) {
157+ try {
158+ log .info ("Quartz job has been resumed with id : " + jobId );
159+ scheduler .resumeJob (new JobKey (jobId ));
160+ return true ;
161+ } catch (SchedulerException e ) {
162+ return false ;
163+ }
164+ }
124165}
0 commit comments