实现一个页面来维护定时任务,并在状态更改时实时启动或停止Job
要实现一个页面来维护表A,并在状态更改时实时启动或停止Job,可以按照以下步骤进行:
- 创建一个Controller来处理页面请求:
package com.example.demo.controller;
import com.example.demo.entity.JobConfig;
import com.example.demo.repository.JobConfigRepository;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/jobs")
public class JobController {
@Autowired
private JobConfigRepository jobConfigRepository;
@Autowired
private JobService jobService;
@GetMapping
public List<JobConfig> getAllJobs() {
return jobConfigRepository.findAll();
}
@PostMapping
public JobConfig createJob(@RequestBody JobConfig jobConfig) {
return jobConfigRepository.save(jobConfig);
}
@PutMapping("/{id}")
public JobConfig updateJob(@PathVariable Long id, @RequestBody JobConfig jobConfig) throws SchedulerException {
JobConfig existingJob = jobConfigRepository.findById(id).orElseThrow(() -> new RuntimeException("Job not found"));
existingJob.setStatus(jobConfig.getStatus());
existingJob.setScheduleFrequency(jobConfig.getScheduleFrequency());
existingJob.setJobClass(jobConfig.getJobClass());
jobConfigRepository.save(existingJob);
jobService.updateJob(existingJob);
return existingJob;
}
}
- 创建一个Service来处理Job的启动和停止:
package com.example.demo.service;
import com.example.demo.entity.JobConfig;
import com.example.demo.repository.JobConfigRepository;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import java.util.List;
@Service
public class JobService {
@Autowired
private Scheduler scheduler;
@Autowired
private JobConfigRepository jobConfigRepository;
@PostConstruct
public void init() throws SchedulerException {
List<JobConfig> jobConfigs = jobConfigRepository.findAll();
for (JobConfig jobConfig : jobConfigs) {
if ("RUNNING".equalsIgnoreCase(jobConfig.getStatus())) {
scheduleJob(jobConfig);
}
}
}
public void updateJob(JobConfig jobConfig) throws SchedulerException {
if ("RUNNING".equalsIgnoreCase(jobConfig.getStatus())) {
scheduleJob(jobConfig);
} else {
unscheduleJob(jobConfig);
}
}
private void scheduleJob(JobConfig jobConfig) throws SchedulerException {
try {
Class<? extends Job> jobClass = (Class<? extends Job>) Class.forName(jobConfig.getJobClass());
JobDetail jobDetail = JobBuilder.newJob(jobClass)
.withIdentity(jobConfig.getJobName())
.build();
Trigger trigger = TriggerBuilder.newTrigger()
.withIdentity(jobConfig.getJobName() + "Trigger")
.withSchedule(CronScheduleBuilder.cronSchedule(jobConfig.getScheduleFrequency()))
.build();
scheduler.scheduleJob(jobDetail, trigger);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
private void unscheduleJob(JobConfig jobConfig) throws SchedulerException {
scheduler.deleteJob(new JobKey(jobConfig.getJobName()));
}
}
- 在前端页面中创建一个表单来维护表A:
<!DOCTYPE html>
<html>
<head>
<title>Job Management</title>
</head>
<body>
<h1>Job Management</h1>
<form id="jobForm">
<label for="jobName">Job Name:</label>
<input type="text" id="jobName" name="jobName"><br><br>
<label for="jobClass">Job Class:</label>
<input type="text" id="jobClass" name="jobClass"><br><br>
<label for="scheduleFrequency">Schedule Frequency:</label>
<input type="text" id="scheduleFrequency" name="scheduleFrequency"><br><br>
<label for="status">Status:</label>
<select id="status" name="status">
<option value="RUNNING">RUNNING</option>
<option value="STOPPED">STOPPED</option>
</select><br><br>
<button type="button" onclick="submitJob()">Submit</button>
</form>
<script>
function submitJob() {
const job = {
jobName: document.getElementById('jobName').value,
jobClass: document.getElementById('jobClass').value,
scheduleFrequency: document.getElementById('scheduleFrequency').value,
status: document.getElementById('status').value
};
fetch('/jobs', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(job)
}).then(response => response.json())
.then(data => console.log(data));
}
</script>
</body>
</html>
通过以上步骤,可以简单创建一个页面来维护表A,并在状态更改时实时启动或停止Job。