quartz极速入门

quartz极速入门
强烈推介IDEA2021.1.3破解激活,IntelliJ IDEA 注册码,2021.1.3IDEA 激活码  

大家好,我是架构君,一个会写代码吟诗的架构师。今天说一说quartz极速入门,希望能够帮助大家进步!!!

入门demo

HelloSchedule .java

import java.text.SimpleDateFormat;
import java.util.Date;

import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

public class HelloSchedule {
    public  static void main(String[] args) throws SchedulerException{
        //创建一个jobdetail 实例,通过该实例与HelloHob.class绑定
        JobDetail jobDetail = JobBuilder.newJob(HelloJob.class).withIdentity("myJob").build();

//        System.out.println("JobDetail'name: " + jobDetail.getKey().getName());
//        System.out.println("JobDetail'group: " + jobDetail.getKey().getGroup());
//        System.out.println("JobDetail'class: " + jobDetail.getKey().getClass());

        //创建trigger实例,定义该job立即执行,并且每隔两秒执行一次知道永远
        Trigger trigger = TriggerBuilder.newTrigger().withIdentity("myTrigger","group1")
                .startNow()
                .withSchedule(
                        SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(2).repeatForever())
                .build();
        //创建schedule实例
        SchedulerFactory sfact = new StdSchedulerFactory();
        Scheduler scheduler = sfact.getScheduler();
        scheduler.start();
        //通过绑定job 和 trigger 来执行 。
        Date date = new Date();
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println("Current Time Is : " + sf.format(date));
        scheduler.scheduleJob(jobDetail,trigger);
    }
}

HelloJob .java

import org.quartz.*;

import java.text.SimpleDateFormat;
import java.util.Date;

public class HelloJob implements Job {

    public void execute(JobExecutionContext arg0) throws  JobExecutionException{
        // 打印当前的时间,格式为2017-01-01 00:00:00
        Date date = new Date();
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println("Current Exec Time Is : " + sf.format(date));
        //编写具体的业务逻辑
        System.out.println("HelloWorld");
    }
}

简介:

简单而言,我们创建一个myjob类,继承JOB接口,然后其中的excute方法就是未来定时执行的方法。
然后我们在main函数里面需要做的事情是:
1、通过Jobdetail方法绑定myjob。
2、trigger就是定时器,即指定执行间隔时间
3、schedule绑定对应的jobdetail和trigger去执行!
那综上不难得知,一个完整的quartz包含四个部分。job,jobdetail,trigger,scheduler

详细介绍:

上面我们已经将完整quartz的流程介绍了,那么接下来我们针对其中的方法进行介绍

  • simplejobdetail

只能绑定job方法

  • complexjobdetail

在绑定了指定job方法后,还可以传入参数

  • simpletrigger

指定简单的定时规则

  • crontrigger

可以指定复杂的定时规则

quartz整合Spring的job定时器

其中涉及的三个类

AnotherBean.java

package com.hejie.springquartz.quartz;

import org.springframework.stereotype.Component;

@Component("anotherBean")
public class AnotherBean {
	public void printAnotherMessage() {
		System.out.println("AnotherMessage");
	}
}

MyBean .java

package com.hejie.springquartz.quartz;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.stereotype.Component;

@Component("myBean")
public class MyBean {
	public void printMessage() {
		// 打印当前的执行时间,格式为2017-01-01 00:00:00
		Date date = new Date();
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		System.out.println("MyBean Executes!" + sf.format(date));
	}
}

FirstScheduledJob .java

package com.hejie.springquartz.quartz;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;

public class FirstScheduledJob extends QuartzJobBean{
     private AnotherBean anotherBean;
     
     public void setAnotherBean(AnotherBean anotherBean){
    	 this.anotherBean = anotherBean;
     }

	@Override
	protected void executeInternal(JobExecutionContext arg0)
			throws JobExecutionException {
		Date date = new Date();
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		System.out.println("FirstScheduledJob Executes!" + sf.format(date));
		this.anotherBean.printAnotherMessage();		
	}
}

dispatch-servlet.xml配置

<!-- 简单的jobdetail配置, 不能传入参数-->
    <bean id="simpleJobDetail"
          class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <property name="targetObject" ref="myBean" />
        <property name="targetMethod" value="printMessage" />
    </bean>
    <!--高级的 jobdetaile 可以传入参数 -->
    <bean id="firstComplexJobDetail"
          class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
        <property name="jobClass"
                  value="com.hejie.springquartz.quartz.FirstScheduledJob" />
        <property name="jobDataMap">
            <map>
                <entry key="anotherBean" value-ref="anotherBean" />
            </map>
        </property>
        <!-- 触发器-->
        <property name="Durability" value="true"/>
    </bean>
    <!--&lt;!&ndash; 距离当前时间1秒之后执行,之后每隔两秒钟执行一次 &ndash;&gt;-->
    <bean id="mySimpleTrigger"
          class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean">
        <property name="jobDetail"  ref="simpleJobDetail"/>
        <property name="startDelay"  value="1000"/>
        <property name="repeatInterval"  value="2000"/>
    </bean>

    <!--&lt;!&ndash; 每隔5秒钟执行一次 &ndash;&gt;-->
    <bean id="myCronTrigger"
          class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
        <property name="jobDetail"  ref="firstComplexJobDetail"/>
        <property name="cronExpression"  value="0/5 * * ? * *"/>
    </bean>

    <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
    <property name="jobDetails">
        <list>
            <ref bean="simpleJobDetail"/>
            <ref bean="firstComplexJobDetail"/>
        </list>
    </property>
    <property name="triggers">
        <list>
            <ref bean="mySimpleTrigger"/>
            <ref bean="myCronTrigger"/>
        </list>
    </property>
    </bean>

xml文件对应的注释:

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

本文来源GroovRain,由架构君转载发布,观点不代表Java架构师必看的立场,转载请标明来源出处:https://javajgs.com/archives/25349

发表评论