石英定時任務-quartz
阿新 • • 發佈:2018-03-17
take 技術 java jobdetail als 獲取 狀態 技術分享 ide
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"> <!-- 定義任務bean --> <bean name="paymentOrderJobDetail" class="org.springframework.scheduling.quartz.JobDetailFactoryBean"> <!-- 指定具體的job類 --> <property name="jobClass" value="com.jt.order.job.PaymentOrderJob" /> <!-- 指定job的名稱 --> <property name="name" value="paymentOrder" /> <!-- 指定job的分組 --> <property name="group" value="Order" /> <!-- 必須設置為true,如果為false,當沒有活動的觸發器與之關聯時會在調度器中刪除該任務 --> <property name="durability" value="true"/> <!-- 指定spring容器的key,如果不設定在job中的jobmap中是獲取不到spring容器的 --> <property name="applicationContextJobDataKey" value="applicationContext"/> </bean> <!-- 定義觸發器 --> <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"> <property name="jobDetail" ref="paymentOrderJobDetail" /> <!-- 每一分鐘執行一次 --> <property name="cronExpression" value="0 0/1 * * * ?" /> </bean> <!-- 定義調度器 --> <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref bean="cronTrigger" /> </list> </property> </bean> </beans>
XML萬用轉義字符
<![CDATA[]]
java類
public class PaymentOrderJob extends QuartzJobBean{ /** * 1、從上下文中獲取spring容器 * 2、從spring容器中獲取 orderMapper對象 * 3、執行mapper操作將超時訂單狀態設置為6:已取消(未付款訂單,超時取消) */ @Override protected void executeInternal(JobExecutionContext context) throws JobExecutionException { //獲取spring容器 ApplicationContext ac = (ApplicationContext) context.getJobDetail().getJobDataMap().get("applicationContext"); //獲取mapper對象 OrderMapper orderMapper = ac.getBean(OrderMapper.class); //設置超時訂單狀態 Date agoDate = new DateTime().minusDays(2).toDate(); System.out.println(agoDate); orderMapper.updateStatus(agoDate); }
public class PaymentOrderJob extends QuartzJobBean{/** * 1、從上下文中獲取spring容器 * 2、從spring容器中獲取 orderMapper對象 * 3、執行mapper操作將超時訂單狀態設置為6:已取消(未付款訂單,超時取消) */@Overrideprotected void executeInternal(JobExecutionContext context) throws JobExecutionException {//獲取spring容器ApplicationContext ac = (ApplicationContext) context.getJobDetail().getJobDataMap().get("applicationContext");//獲取mapper對象OrderMapper orderMapper = ac.getBean(OrderMapper.class);//設置超時訂單狀態Date agoDate = new DateTime().minusDays(2).toDate();System.out.println(agoDate);orderMapper.updateStatus(agoDate);}
石英定時任務-quartz