Quartz implements timed tasks

The Quartz framework provides a handful of trigger types, but the two most commonly used are SimpleTrigger and CronTrigger.

  • SimpleTrigger is designed for the need for simple ignition scheduling. SimpleTrigger is for you if you need to fire a job at a given time and number of repetitions or the number of seconds to wait between fires.
  • If you have a lot of complex job scheduling, you may need CronTrigger.

Add dependencies in pom.xml

<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>1.84.</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>3.217..RELEASE</version>
</dependency>
Copy the code

Method 1: Directly use the configuration file

  • Jobs can be implemented through ordinary classes

The task class

@Component
@Configurable
@EnableScheduling
public class ScheduledTasks{


public void reportCurrentTime(a){
System.out.println ("Scheduling Tasks Examples: The time is now " + dateFormat ().format (new Date()));
}

// Execute this command every 1 minute
public void reportCurrentByCron(a){
System.out.println ("Scheduling Tasks Examples By Cron: The time is now " + dateFormat ().format (new Date ()));
}

private SimpleDateFormat dateFormat(a){
return new SimpleDateFormat ("HH:mm:ss"); }}Copy the code

Trigger configuration class

@Configuration
public class quartzconfiguration {
// Use the jobDetail workshop to create tasks
@Bean(name = "detailFactoryBean")
public MethodInvokingJobDetailFactoryBean detailFactoryBean(ScheduledTasks scheduledTasks){
MethodInvokingJobDetailFactoryBean bean = new MethodInvokingJobDetailFactoryBean ();
bean.setTargetObject (scheduledTasks);
bean.setTargetMethod ("reportCurrentByCron"); // Select the task to execute, that is, the method
bean.setConcurrent (false);
return bean;
}

// Set the trigger
// Bind trigger to job and use time expression for timing
@Bean(name = "cronTriggerBean")
public CronTriggerBean cronTriggerBean(MethodInvokingJobDetailFactoryBean detailFactoryBean){
CronTriggerBean tigger = new CronTriggerBean ();
tigger.setJobDetail (detailFactoryBean.getObject ());
try {
tigger.setCronExpression ("0/5 * * * *? ");// Execute every 5 seconds
} catch (ParseException e) {
e.printStackTrace ();
}
return tigger;

}


//SchedulerFactoryBean: The primary management factory, which is the primary bean. Quartz manages its triggers through this facility.
// Put the trigger into the schedule factory
@Bean
public SchedulerFactoryBean schedulerFactory(CronTriggerBean[] cronTriggerBean){
SchedulerFactoryBean bean = new SchedulerFactoryBean();
System.err.println (cronTriggerBean[0]);
bean.setTriggers (cronTriggerBean);
returnbean; }}Copy the code

Method 2: Encapsulate Quartz

  • Implement the job interface to create a job overwrite the execute method
public class ScheduledTasks implements Job {
@Override
public void execute(JobExecutionContext context) throws JobExecutionException{
System.out.println ("Scheduling Tasks Examples By Cron: The time is now " + dateFormat ().format (new Date ()));
}

private SimpleDateFormat dateFormat(a){
return new SimpleDateFormat ("HH:mm:ss"); }}Copy the code

Abstract classes for triggers

public abstract class BaseCronTrigger extends CronTriggerBean implements Serializable {
private static final long serialVersionUID = 1L;
public void init(a){
// Get the task
JobDetail jobdetail = new JobDetail (this.getClass ().getSimpleName (),this.getMyTargetObject ().getClass ());
this.setJobDetail (jobdetail);
this.setJobName (jobdetail.getName ());
this.setName (this.getClass ().getSimpleName ());
try {
this.setCronExpression (this.getMyCronExpression ());
} catch(java.text.ParseException e) { e.printStackTrace (); }}public abstract String getMyCronExpression(a);

public abstract Job getMyTargetObject(a);
}
Copy the code

The concrete class of the trigger

  • It can be customized based on the business and uploaded to SchedulerFactoryBean
  • SchedulerFactoryBean: The primary administrative factory, which is the primary one.
  • Quartz manages its triggers through this facility.
// This is a specific task bound trigger
@Component
public class InitializingCronTrigger extends BaseCronTrigger implements Serializable {

@Autowired
private org.springframework.scheduling.quartz.SchedulerFactoryBean schedulerFactoryBean;

// Automatically bind in this way
// The injected IOC container automatically invokes the constructor
public InitializingCronTrigger(a) {
super.init ();
}


// Set task cron
@Override
public String getMyCronExpression(a) {
return "0/5 * * * *?";
}

// Reverse of control
// Change set to get
@Override
public Job getMyTargetObject(a) {
return new ScheduledTasks ();
}


//SchedulerFactoryBean: The primary management factory, which is the primary bean. Quartz manages its triggers through this facility.
public void parse(a){
try {
schedulerFactoryBean.getObject ().pauseAll ();
} catch(SchedulerException e) { e.printStackTrace (); }}}Copy the code

The configuration class

@Configuration
public class quartzconfiguration {

//SchedulerFactoryBean: The primary management factory, which is the primary bean. Quartz manages its triggers through this facility.
// Put the trigger into the schedule factory
@Bean
public SchedulerFactoryBean schedulerFactory(CronTriggerBean[] cronTriggerBean){
SchedulerFactoryBean bean = new SchedulerFactoryBean();
System.err.println (cronTriggerBean[0]);
bean.setTriggers (cronTriggerBean);
returnbean; }}Copy the code