标签:script dex erb catch 停止 using tde RKE new
<properties>
<!-- spring版本号 -->
<spring.version>4.2.2.RELEASE</spring.version> <!-- Quartz的版本 -->
</properties>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.2.2</version>
</dependency>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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.xsd">
<!-- 配置Job类 -->
<bean id="myJob" class="cn.enjoyedu.quartz.xml.CronJob"></bean>
<!-- 配置JobDetail -->
<bean id="jobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<!-- 执行目标job -->
<property name="targetObject" ref="myJob"></property>
<!-- 要执行的方法 -->
<property name="targetMethod" value="execute"></property>
</bean>
<!-- 配置tirgger触发器 -->
<bean id="cronTriggerFactoryBean" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<!-- jobDetail -->
<property name="jobDetail" ref="jobDetail"></property>
<!-- cron表达式,执行时间 每5秒执行一次 -->
<property name="cronExpression" value="0/5 * * * * ?"></property>
</bean>
<!-- 配置调度工厂 -->
<bean id="springJobSchedulerFactoryBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="cronTriggerFactoryBean"></ref>
</list>
</property>
</bean>
<!--<bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean">-->
<!--<!–配置具体要执行的jobDetail–>-->
<!--<property name="jobDetail" ref="simpleJobDetail" />-->
<!--<!–初始延迟时间 1s–>-->
<!--<property name="startDelay" value="1000" />-->
<!--<!–间隔执行时间每2s执行一次–>-->
<!--<property name="repeatInterval" value="2000" />-->
<!--</bean>-->
</beans>
package cn.enjoyedu.quartz.xml;
public class CronJob {
public void execute(){
System.out.println("定时任务执行...每5秒执行一次");
}
}
package cn.enjoyedu.quartz.xml;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringQuartzXmlTest {
public static void main(String[] args) {
//启动spring容器
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:application.xml");
System.out.println("initContext successfully");
}
}
initContext successfully
定时任务执行...每5秒执行一次
定时任务执行...每5秒执行一次
<!-- 开启组件扫描 -->
<context:component-scan base-package="cn.enjoyedu.quartz.annotation"></context:component-scan>
<!-- 开启定时任务,spring的定时任务。不是原生quartz -->
<task:annotation-driven/>
package cn.enjoyedu.quartz.annotation;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
public class AnnotationJob {
@Scheduled(cron = "0/5 * * * * ?")
public void excute(){
System.out.println("AnnotationJob 执行 5秒1次");
}
}
initContext successfully
AnnotationJob 执行 5秒1次
AnnotationJob 执行 5秒1次
@RequestMapping(value = "/initJob", method = RequestMethod.POST)
@ResponseBody
public String initJob() {
try {
ObjectResult result = timetaskBiz.initJob();
return JSONHelper.toJsonDataAPI(result, result.getApiStatus(), result.getErrorMsg());
} catch (Exception e) {
return JSONHelper.toJsonErrorDataAPI(e);
}
}
/**
* 初始化job
* @param timetask
*/
@Override
public ObjectResult initJob() {
ObjectResult result = new ObjectResult();
try {
Scheduler scheduler = factoryBean.getScheduler();
//这里从数据库中获取任务信息数据
Timetask timetask = new Timetask();
//0 表示定时任务停止中 1 表示定时任务执行中
timetask.setJobStatus("0");
//从数据库中查询定时印务记录。
List<Timetask> timetaskList = this.getTimetaskListCus(timetask).getListTimetask();
for (Timetask task : timetaskList) {
addJob(task);
}
} catch (SchedulerException e) {
e.printStackTrace();
result.setErrorMsg(e.getMessage());
result.setApiStatus(ApiData.STATUS_EXCEPTION);
}
return result;
}
/**
* 添加job
* @param timetask
*/
private void addJob(Timetask timetask) {
if(timetask == null || (timetask.getJobStatus() == ScheduleJob.STATUS_RUNNING)) {
return;
}
try {
Scheduler scheduler = factoryBean.getScheduler();
TriggerKey triggerKey = TriggerKey.triggerKey(timetask.getName(),timetask.getGroupName());
CronTrigger cronTrigger = (CronTrigger) scheduler.getTrigger(triggerKey);
//如果不存在cronTrigger则重新创建一个
if(null == cronTrigger){
Class clazz = null;
try {
clazz = Class.forName(timetask.getBeanClass());
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
JobDetail jobDetail = JobBuilder.newJob(clazz)
.withIdentity(timetask.getName(),timetask.getGroupName())
.usingJobData("data","")
.build();
jobDetail.getJobDataMap().put("scheduleJob",timetask);
CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule(timetask.getCron());
cronTrigger = TriggerBuilder.newTrigger().withDescription(timetask.getIdStr())
.withIdentity(timetask.getName(),timetask.getGroupName())
.withSchedule(cronScheduleBuilder)
.build();
scheduler.scheduleJob(jobDetail,cronTrigger);
} else {
CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule(timetask.getCron());
//TODO usingJobData的参数
cronTrigger = cronTrigger.getTriggerBuilder().withIdentity(triggerKey).usingJobData("data","")
.withSchedule(cronScheduleBuilder).build();
scheduler.rescheduleJob(triggerKey,cronTrigger);
}
//执行
scheduler.start();
} catch (SchedulerException e) {
e.printStackTrace();
}
package com.zt.uni.api.task.jobs;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
public class MyJob implements Job {
@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
System.out.println("MyJob ... execute ...");
}
}
package com.zt.uni.api.task.jobs;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
public class MyJob2 implements Job {
@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
System.out.println("MyJob2 ... execute ...");
}
}
MyJob ... execute ...
MyJob2 ... execute ...
MyJob ... execute ...
MyJob ... execute ...
MyJob2 ... execute ...
MyJob ... execute ...
MyJob ... execute ...
标签:script dex erb catch 停止 using tde RKE new
原文地址:https://www.cnblogs.com/codetree/p/9899077.html