Spring定时任务示例
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<!-- 数据拉取 -->
<bean id="getMsg" class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass"
value="com.svw.wehr.wechat.message.timer.GetWxMsgTask" />
</bean>
<!-- 数据拉取同步 -->
<bean id="getWXMsgTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="getMsg" />
<!-- <property name="cronExpression" value="0 */1 * * * ?" /> 1分钟-->
<!-- 每日凌晨两点 -->
<property name="cronExpression" value="0 0 2 * * ?" />
</bean>
<!-- 触发器 -->
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="getWXMsgTrigger" />
</list>
</property>
</bean>
</beans>
package com.wechat.message.timer;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.scheduling.quartz.QuartzJobBean;
public class GetWxMsgTask extends QuartzJobBean {
/**
* 日志
*/
private static final Log log = LogFactory.getLog(GetWxMsgTask.class);
@Override
protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
log.info("定时任务开始跑啦!!!!");
}
}