参考:http://gong1208.iteye.com/blog/1773177
spring配置文件:
<!-- spring定时任务 定时统计设备定检率-->
<bean id="springTimer" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject">
<bean class="com.gf.equipment.controller.EquipmentStatisticsHistoryController" />
</property>
<property name="targetMethod" value="fixedInspectionRate" />
<property name="concurrent" value="false" /><!-- 作业不并发调度 -->
</bean>
<!-- 配置作业调度的触发方式(触发器) -->
<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="springTimer" />
<property name="cronExpression" value="0 39-45 14 * * ?" /><!--每天14:39至14:45每分钟一次触发 -->
</bean>
<!-- 配置调度工厂 -->
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="cronTrigger" />
</list>
</property>
</bean>
相关java类
package com.gf.equipment.controller;
import com.gf.common.BaseController;
import com.gf.equipment.service.EquipmentService;
import com.gf.equipment.service.EquipmentStatisticsService;
import com.gf.pf.model.Flexigrid;
import com.gf.pf.service.LogService;
import com.gf.pf.service.MyBatisService;
import com.gf.util.DateUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.*;
/**
* Description: 设备统计(Spring定时任务,定时统计设备定检率,存入数据库,以供查询)
* Package Name: com.gf.equipment.controller
* User:
* Date: 2017-06-05
* Time: 15:11
*/
@Controller
@RequestMapping("/equipmentStatisticsHistory")
public class EquipmentStatisticsHistoryController extends BaseController {
private static final Logger logger = LoggerFactory.getLogger(EquipmentStatisticsHistoryController.class);
@Autowired
private MyBatisService myBatisService;
@Autowired
private EquipmentService equipmentService;
@Autowired
private LogService logService;
@Autowired
private EquipmentStatisticsService equipmentStatisticsService;
/**
* 定时统计定检率存入数据库
*
* @return
*/
public void fixedInspectionRate() {
logger.info("定时统计定检率存入数据库");
System.out.println(DateUtil.date_string(new Date()));
}
}
启动项目测试一下,看控制台输出