quartz spring
简单例子可参考
http://yangpanwww.iteye.com/blog/797563
http://liuzidong.iteye.com/blog/1118992
关于时间配置可参考另一篇http://www.cnblogs.com/stit/p/4013398.html
我的项目的应用
1 knowledge-schedule.xml 不要忘了在spring总配置文件中引入
调度工厂没有加lazy-init="false"
<!-- 总管理类如果将lazy-init='false'那么容器启动就会执行调度程序 -->
<bean id="startQuertz" class="org.springframework.scheduling.quartz.SchedulerFactoryBean" lazy-init="false" >
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="threadPoolTaskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor"> <property name="corePoolSize" value="5"/> <property name="keepAliveSeconds" value="200"/> <property name="maxPoolSize" value="50"/> <property name="queueCapacity" value="60"/> </bean> <!-- 排行统计start --> <bean id="methodSchedulerFactory_StatisticsInfoHotReply" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject" ref="rankTask" /> <property name="targetMethod" value="startStatistics" /> <property name="arguments" value="info" /> </bean> <bean id="cronTriggerBean_StatisticsInfoHotReply" class="org.springframework.scheduling.quartz.CronTriggerBean"> <property name="jobDetail" ref="methodSchedulerFactory_StatisticsInfoHotReply" /> <property name="cronExpression" value="* * 2 * * ?" /> <!--每晚2点一次 --> </bean> <!-- 排行统计end --> <!-- 栏目定时统计订阅数开始 --> <bean id="columnJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject" ref="columnTimerTasker" /> <property name="targetMethod" value="execute" /> <!--将并发设置为false--> <property name="concurrent" value="false" /> </bean> <bean id="columnJobTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean"> <property name="jobDetail" ref="columnJobDetail" /> <!--表达式,每30分钟 执行一次 --> <property name="cronExpression" value="0 7/30 * * * ?" /> </bean> <!-- 栏目定时统计订阅数结束 --> <!--调度工厂 --> <bean id="SpringJobSchedulerFactoryBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref bean="cronTriggerBean_StatisticsInfoHotReply" /> <ref bean="columnJobTrigger" /> </list> </property> </bean> </beans>
2 ColumnTimerTasker
package com.ginkgocap.ywxt.knowledge.util; import java.text.SimpleDateFormat; import java.util.Date; import java.util.List; import javax.annotation.Resource; import org.quartz.JobExecutionException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import com.ginkgocap.ywxt.knowledge.entity.Column; import com.ginkgocap.ywxt.knowledge.mapper.ColumnMapper; import com.ginkgocap.ywxt.knowledge.service.ColumnService; import com.ginkgocap.ywxt.knowledge.service.ColumnSubscribeService; @Component("columnTimerTasker") public class ColumnTimerTasker { @Resource ColumnService cs; @Resource ColumnSubscribeService subcs; @Autowired ColumnMapper columnMapper; boolean b=true; public void execute() throws JobExecutionException { SimpleDateFormat f=new SimpleDateFormat("E yyyy-MM-dd HH:mm:ss"); Date date=new Date(); System.out.println("ColumnTimerTasker.execute() "+f.format(date)+" "); // if (!b) { // return; // } List<Column> list= cs.queryAll(); for (int i = 0; i < list.size(); i++) { Column c=list.get(i); long count=subcs.countByKC(c.getId()); // if (count>0) { // System.out.print(c.getId()+"---"); // System.out.println(count); // } Column cc=new Column(); cc.setId(c.getId()); cc.setSubscribeCount(count); // if (b) { // columnMapper.updateByPrimaryKeySelective(cc); // }else { // if (count>0) { // columnMapper.updateByPrimaryKeySelective(cc); // } // } if (b||count>0) { columnMapper.updateByPrimaryKeySelective(cc); } } if (b) { b=false; } } }
3另一哥们写的RankTask,用到了线程池
package com.ginkgocap.ywxt.knowledge.util; import java.util.concurrent.Future; import javax.annotation.Resource; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; import org.springframework.stereotype.Component; /** * 排行任务 * <p>于2014-9-11 由 创建 </p> * @author <p>当前负责人 </p> * */ @Component("rankTask") public class RankTask { @Resource ThreadPoolTaskExecutor threadPoolTaskExecutor; @Autowired RankSchedule schedule; public void startStatistics(String[] obj) { schedule.setObj(obj); Future future = threadPoolTaskExecutor.submit(schedule); // future.get(); } }
package com.ginkgocap.ywxt.knowledge.util; import java.util.concurrent.Callable; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; /** * 排行计划 * <p>于2014-9-11 由 创建 </p> * @author <p>当前负责人 </p> */ @Component public class RankSchedule implements Callable<String> { @Autowired private RankStatistic rs; private String[] obj; public String[] getObj() { return obj; } public void setObj(String[] obj) { this.obj = obj; } @Override public String call() throws Exception { rs.run(obj[0]); return null; } }