<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.2.3</version>
</dependency>
启动定时:
<!-- 第一种定时方法 -->
<bean id="simpleJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="myBean"/>
<property name="targetMethod" value="printMessage"/>
</bean>
<bean id="mySimpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean">
<property name="jobDetail" ref="simpleJobDetail"/>
<property name="startDelay" value="5000"/>
<property name="repeatInterval" value="3000"/>
</bean>
<!-- 第二种定时方法 -->
<bean id="myJobDetail" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
<property name="jobClass" value="com.bosque.config.quartz.MyScheduledJob"/>
<property name="jobDataMap">
<map>
<entry key="name" value="时代"/>
<entry key="accountService" value-ref="accountService"/>
</map>
</property>
<property name="Durability" value="true"></property>
</bean>
<bean id="myCronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="myJobDetail" />
<property name="cronExpression" value="0/5 * * ? * *"></property>
</bean>
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="jobDetails">
<list>
<!-- <ref bean="simpleJobDetail"/> -->
<ref bean="myJobDetail"/>
</list>
</property>
<property name="triggers">
<list>
<!-- <ref bean="mySimpleTrigger"/> -->
<ref bean="myCronTrigger"/>
</list>
</property>
</bean>
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.bosque.rest.account.services.AccountService;
@Component("myBean")
public class MyBean {
@Autowired
private AccountService accountService;
public void printMessage(){
System.out.println("运行printMessage");
accountService.testQuartz("何佳");
}
}
import java.text.SimpleDateFormat;
import java.util.Calendar;
import org.springframework.stereotype.Service;
@Service
public class AccountService {
/**
* @comment 测试Quartz
* @author HE JIA
* @date 2017年8月7日 下午1:40:51
*/
public void testQuartz(String value) {
Calendar calendar = Calendar.getInstance();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(format.format(calendar.getTime())+"开始执行,参数为:"+value);
}
}
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;
import com.bosque.rest.account.services.AccountService;
public class MyScheduledJob extends QuartzJobBean{
private AccountService accountService;
private String name;
@Override
protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
accountService.testQuartz(name);
}
public void setName(String name) {
this.name = name;
}
public void setAccountService(AccountService accountService) {
this.accountService = accountService;
}
}
动态定时(个人写法,不正确的地方欢迎指正):
import java.io.Serializable;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.stereotype.Component;
import com.bosque.rest.account.services.AccountService;
@Component("myJob")
public class MyJob implements Job,Serializable {
private static final long serialVersionUID = 1L;
public void execute(JobExecutionContext context) throws JobExecutionException {
String value = context.getJobDetail().getJobDataMap().getString("value");
AccountService accountService = (AccountService) context.getJobDetail().getJobDataMap().get("accountService");
accountService.testQuartz(value);
}
}
Controller:
/**
* @comment quertz测试
* @author HE JIA
* @date 2017年8月7日 下午1:44:51
*/
@RequestMapping("/quartzTest")
@ResponseBody
public Map<String,Object> quarzTest(String value,String time) {
return accountService.quartzTest(value,time);
}
Service层:
/**
* @comment 测试Quartz
* @author HE JIA
* @date 2017年8月7日 下午1:46:05
*/
public Map<String, Object> quartzTest(String value,String time) {
Map<String,Object> resultMap = new HashMap<>();
try {
JobDetail jobDetail = JobBuilder.newJob(MyJob.class).withIdentity(new Date().getTime()+"").build();
jobDetail.getJobDataMap().put("accountService", accountService);
jobDetail.getJobDataMap().put("value", value);
Trigger trigger = TriggerBuilder.newTrigger().withIdentity(new Date().getTime()+"")
.withSchedule(CronScheduleBuilder.cronSchedule(time)).build();
SchedulerFactory schedulerFactory = new StdSchedulerFactory();
Scheduler scheduler = schedulerFactory.getScheduler();
scheduler.start();
scheduler.scheduleJob(jobDetail, trigger);
resultMap.put("state", "success");
} catch (SchedulerException e) {
resultMap.put("state", "error");
e.printStackTrace();
}
return resultMap;
}
html:
<html>
<head>
<meta charset="utf-8">
<title>测试</title>
<script type="text/javascript" src="js/jquery-1.8.0.min.js"></script>
</head>
<body>
<input type="text" id="time">
<input type="button" value="测试" id="test">
<script type="text/javascript">
$("#test").click(function(){
$.ajax({
type: "POST",
url: "http://10.0.0.46:9000/bosque/account/quartzTest",
data: {
value:"时代",
time:$("#time").val()
},
xhrFields: {
withCredentials: true
},
dataType: "json",
success: function(data){
console.log(data);
}
});
});
</script>
</body>
</html>