柚子快报激活码778899分享:quartz简单使用

http://yzkb.51969.com/

            quartz具备如下特点

强大的调度功能,例如支持丰富多样的调度方法,可以满足各种常规及特殊需求;

灵活的应用方式,例如支持任务和调度的多种组合方式,支持调度数据的多种存储方式;

分布式和集群能力,Terracotta 收购后在原来功能基础上作了进一步提升。

          quartz核心元素

Scheduler:任务调度器,是实际执行任务调度的控制器。在spring中通过SchedulerFactoryBean封装起来。

Trigger:触发器,用于定义任务调度的时间规则,有SimpleTrigger,CronTrigger,DateIntervalTrigger和NthIncludedDayTrigger,其中CronTrigger用的比较多,本文主要介绍这种方式。CronTrigger在spring中封装在CronTriggerFactoryBean中。

Calendar:它是一些日历特定时间点的集合。一个trigger可以包含多个Calendar,以便排除或包含某些时间点。

JobDetail:用来描述Job实现类及其它相关的静态信息,如Job名字、关联监听器等信息。在spring中有JobDetailFactoryBean和 MethodInvokingJobDetailFactoryBean两种实现,如果任务调度只需要执行某个类的某个方法,就可以通过MethodInvokingJobDetailFactoryBean来调用。

Job:是一个接口,只有一个方法void execute(JobExecutionContext context),开发者实现该接口定义运行任务,JobExecutionContext类提供了调度上下文的各种信息。Job运行时的信息保存在JobDataMap实例中。实现Job接口的任务,默认是无状态的,若要将Job设置成有状态的,在quartz中是给实现的Job添加@DisallowConcurrentExecution注解(以前是实现StatefulJob接口,现在已被Deprecated),在与spring结合中可以在spring配置文件的job detail中配置concurrent参数。

 

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

com.java

time-job

1.0-SNAPSHOT

org.springframework.boot

spring-boot-starter-parent

2.5.7

org.springframework.boot

spring-boot-starter-web

org.springframework.boot

spring-boot-starter

org.springframework.boot

spring-boot-starter-test

test

org.springframework.boot

spring-boot-starter-quartz

junit

junit

4.12

org.apache.maven.plugins

maven-compiler-plugin

3.1

1.8

1.8

utf-8

  

package com.java;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

/**

* @author yourheart

* @Description

* @create 2022-05-23 17:40

*/

@SpringBootApplication

public class TimeApplication {

public static void main(String[] args) {

SpringApplication.run(TimeApplication.class,args);

}

}

  

package com.java;

import org.quartz.Job;

import org.quartz.JobExecutionContext;

import org.quartz.JobExecutionException;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

/**

* @author yourheart

* @Description

* @create 2022-05-23 19:42

*/

public class MyJob implements Job {

private final static Logger log= LoggerFactory.getLogger(MyJob.class);

@Override

public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {

Object name = jobExecutionContext.getJobDetail().getJobDataMap().get("name");

log.warn(name+"搞卫生");

}

}

  

package com.java.config;

import com.java.MyJob;

import org.quartz.JobBuilder;

import org.quartz.JobDetail;

import org.quartz.SimpleScheduleBuilder;

import org.quartz.Trigger;

import org.quartz.TriggerBuilder;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

/**

* @author yourheart

* @Description

* @create 2022-05-23 19:52

*/

@Configuration

public class QuartzConfiguration {

@Bean

public JobDetail jobDetail() {

return JobBuilder.newJob(MyJob.class)

/**

* 自己取名

*/

.withIdentity("fileHandleQuartzJobDetail")

.usingJobData("name", "张三")

.storeDurably().build();

}

/**

* 构建触发器

* @return

*/

@Bean

public Trigger trigger() {

/**

* 时间自己定,根据方法进行修改时间

*/

SimpleScheduleBuilder schedBuilder=SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(5).repeatForever();

return TriggerBuilder.newTrigger().forJob( jobDetail()).

/**

* 自己取名

*/

withIdentity("fileHandleQuartzJobTriger")

.withSchedule(schedBuilder).build();

}

}

  application.propeties没有配置

 

柚子快报激活码778899分享:quartz简单使用

http://yzkb.51969.com/

推荐文章

评论可见,请评论后查看内容,谢谢!!!评论后请刷新页面。