Skip to content

Commit

Permalink
feat($Quartz): run Quartz job immediately
Browse files Browse the repository at this point in the history
scheduler.triggerJob()
  • Loading branch information
johnnymillergh committed Oct 1, 2021
1 parent 099d109 commit bb2ae84
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ public ResponseBodyBean<CreateOrModifyQuartzJobConfigurationResponse> patch(
return ResponseBodyBean.ofSuccess(this.service.patch(id, property, payload));
}

@PostMapping("/quartz-job-configurations/{id}/run-immediately")
@ApiOperation(value = "Run Quartz job immediately", notes = "Run Quartz job immediately")
public ResponseBodyBean<RunImmediatelyResponse> runImmediately(@PathVariable Long id) {
return ResponseBodyBean.ofSuccess(this.service.runImmediately(id));
}

@Override
protected void onExceptionOccurred() {
log.error("Exception occurred when uploading excel. Excel class: {}", QuartzJobConfigurationExcel.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.jmsoftware.maf.springcloudstarter.quartz.entity;

import lombok.AllArgsConstructor;
import lombok.Data;

/**
* <h1>RunImmediatelyResponse</h1>
* <p>
* Change description here.
*
* @author Johnny Miller (鍾俊), email: johnnysviva@outlook.com, 10/2/21 1:28 AM
**/
@Data
@AllArgsConstructor
public class RunImmediatelyResponse {
private Long id;
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,12 @@ CreateOrModifyQuartzJobConfigurationResponse patch(
@NotNull Long id,
@NotBlank String property,
@NotNull CreateOrModifyQuartzJobConfigurationPayload payload);

/**
* Run immediately.
*
* @param id the id
* @return the run immediately response
*/
RunImmediatelyResponse runImmediately(@NotNull Long id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.quartz.JobDataMap;
import org.quartz.SchedulerException;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.stereotype.Service;
Expand All @@ -34,6 +35,7 @@

import static cn.hutool.core.text.CharSequenceUtil.format;
import static com.jmsoftware.maf.springcloudstarter.function.BooleanCheck.requireTrue;
import static com.jmsoftware.maf.springcloudstarter.quartz.constant.ScheduleConstant.QUARTZ_JOB_CONFIGURATION;
import static java.util.Objects.requireNonNull;

/**
Expand Down Expand Up @@ -172,4 +174,23 @@ public CreateOrModifyQuartzJobConfigurationResponse patch(
).orElseThrow(() -> new IllegalStateException(format("Failed to patch {}", property)));
return new CreateOrModifyQuartzJobConfigurationResponse(id);
}

@Override
@SneakyThrows
public RunImmediatelyResponse runImmediately(@NotNull Long id) {
val quartzJobConfiguration = this.getById(id);
requireNonNull(quartzJobConfiguration, format("Quartz job(id:{}) must not be null", id));
val scheduler = this.schedulerFactoryBean.getScheduler();
val jobDataMap = new JobDataMap();
jobDataMap.put(QUARTZ_JOB_CONFIGURATION, quartzJobConfiguration);
scheduler.triggerJob(
ScheduleUtil.getJobKey(
quartzJobConfiguration.getId(),
quartzJobConfiguration.getGroup(),
this.mafProjectProperty.getProjectArtifactId()),
jobDataMap
);
log.warn("Triggered Quartz job successfully, {}", quartzJobConfiguration);
return new RunImmediatelyResponse(id);
}
}

0 comments on commit bb2ae84

Please sign in to comment.