(办公)定时任务quartz入门
程序员文章站
2022-04-14 18:40:43
1.简单入门. 2.定时任务注入service. 入门案例: 1.1. 加jar 1.2.编写个job任务. 1.3 写一个main方法调用任务: 接下来,再来一个简单的springmvc与quartz的结合. 2.1 定时任务注入service,注入service,为null? 然后注入的serv ......
1.简单入门.
2.定时任务注入service.
入门案例:
1.1. 加jar
<dependency> <groupid>org.quartz-scheduler</groupid> <artifactid>quartz</artifactid> <version>2.2.3</version> </dependency>
1.2.编写个job任务.
/** * quartz中的job可以理解为一个任务. */ public class hellojob implements job { @override public void execute(jobexecutioncontext jobexecutioncontext) throws jobexecutionexception { //打印当前的时间: date date = new date(); simpledateformat sf = new simpledateformat("yyyy-mm-dd hh:mm:ss"); system.out.println("job run time:" + sf.format(date)); //编写具体的业务逻辑. system.out.println("hello word!"); } }
1.3 写一个main方法调用任务:
public static void main(string[] args){ //1.创建一个jobdetail实例,将该实例与hellojob class绑定. jobdetail jobdetail = jobbuilder.newjob(hellojob.class) .withidentity("myjob", "group1").build(); //withidentity 创建名字,以什么组入伙,build()创建. //2.创建trigger实例,主要用来,定义job执行. //每隔2秒重复执行一次, // 解释:因为不同类,所以并不是同一个组的. // startnow 立即执行. // repeatforever 不间断. // withintervalinseconds 时间间隔秒,执行. trigger trigger = triggerbuilder.newtrigger() .withidentity("mytrigger","group1") .startnow().withschedule( simpleschedulebuilder.simpleschedule().withintervalinseconds(2).repeatforever() ).build(); // 3.创建scheduler 实例: schedulerfactory sfactory = new stdschedulerfactory(); try { scheduler scheduler = sfactory.getscheduler(); scheduler.start();//执行. scheduler.schedulejob(jobdetail,trigger);//绑定要执行的任务. } catch (schedulerexception e) { e.printstacktrace(); } }
接下来,再来一个简单的springmvc与quartz的结合.
1.第一步,加入核心的jar. spring上下文. <dependency> <groupid>org.springframework</groupid> <artifactid>spring-aop</artifactid> <version>${spring.version}</version> </dependency> spring事务: <dependency> <groupid>org.springframework</groupid> <artifactid>spring-tx</artifactid> <version>${spring.version}</version> </dependency> 定时任务: <dependency> <groupid>org.quartz-scheduler</groupid> <artifactid>quartz</artifactid> <version>2.2.3</version> </dependency> 2.quartz配置作业: 2.1. anotherbean类 import org.springframework.stereotype.component; @component("anotherbean") public class anotherbean { public void printanothermessage() { system.out.println("anothermessage"); } } 2.2.firstscheduledjob类 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("10 :firstscheduledjob executes!" + sf.format(date)); this.anotherbean.printanothermessage(); } } 2.3.mybean 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)); } } 2.4.配置文件里配置一下: <bean id="simplejobdetail" class="org.springframework.scheduling.quartz.methodinvokingjobdetailfactorybean"> <property name="targetobject" ref="mybean" /> <property name="targetmethod" value="printmessage" /> </bean> <bean id="firstcomplexjobdetail" class="org.springframework.scheduling.quartz.jobdetailfactorybean"> <property name="jobclass" value="com.imooc.springquartz.quartz.firstscheduledjob" /> <property name="jobdatamap"> <map> <entry key="anotherbean" value-ref="anotherbean" /> </map> </property> <property name="durability" value="true"/> </bean> <!-- 距离当前时间1秒之后执行,之后每隔两秒钟执行一次 --> <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> <!-- 每隔5秒钟执行一次 --> <bean id="mycrontrigger" class="org.springframework.scheduling.quartz.crontriggerfactorybean"> <property name="jobdetail" ref="firstcomplexjobdetail"/> <property name="cronexpression" value="0 0/3 * * * ? *"/> </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>
2.1 定时任务注入service,注入service,为null?
import org.quartz.spi.triggerfiredbundle; import org.springframework.beans.factory.annotation.autowired; import org.springframework.beans.factory.config.autowirecapablebeanfactory; import org.springframework.scheduling.quartz.adaptablejobfactory; public class myjobfactory extends adaptablejobfactory { //这个对象spring会帮我们自动注入进来,也属于spring技术范畴. @autowired private autowirecapablebeanfactory capablebeanfactory; protected object createjobinstance(triggerfiredbundle bundle) throws exception { //调用父类的方法 object jobinstance = super.createjobinstance(bundle); //进行注入,这属于spring的技术,不清楚的可以查看spring的api. capablebeanfactory.autowirebean(jobinstance); return jobinstance; } }
放到bean里 <bean id="jobfactory" class="cn.wz.ssm.quartz.myjobfactory"></bean>
<bean class="org.springframework.scheduling.quartz.schedulerfactorybean"> <property name="jobfactory" ref="jobfactory"></property>
然后注入的service就可以使用了。
分享一个:http://cron.qqe2.com/ 写定时任务表达式的网址。
本篇文章,通过东拼西凑,瞎划拉资料,不过东西测试过了,可以使用.
上一篇: LinqToSQL4
下一篇: makedown使用