在spring中手动执行定时任务:
1.spring配置文件片段:
<!-- 更新定时任务 -->
<bean id="job" class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass">
<value>cn.test.Job</value>
</property>
</bean>
<bean id="jobTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="job" />
<property name="startDelay">
<value>3000</value>
</property>
<property name="cronExpression">
<value>0 0 12 * * ?</value>
</property>
</bean>
<!-- 定时任务触发器列表,定时器配置原则:需要申请相同资源的定时任务不要存在同时启动的情况 -->
<bean id="scheduler"
class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="jobTrigger"/>
</list>
</property>
</bean>
springController写法
@Controller
@RequestMapping("/admin/district")
public class DistrictController {
@Resource
private StdScheduler scheduler;
static Logger logger = LoggerFactory.getLogger(DistrictController.class);
//手动同步
@RequestMapping(value="/run")
@ResponseBody
public String run(){
ViewResult view = ViewResult.newInstance();
try {
scheduler.triggerJob("job", Scheduler.DEFAULT_GROUP);
view.state(0, "成功");
}
catch (SchedulerException e) {
view.state(1, "失败");
logger.error("####手动更新区信息-出现异常!", e);
}
return ViewResult.newInstance().json();
}
}