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,用到了线程池
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | 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(); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | 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 ; } } |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何打造一个高并发系统?
· .NET Core GC压缩(compact_phase)底层原理浅谈
· 现代计算机视觉入门之:什么是图片特征编码
· .NET 9 new features-C#13新的锁类型和语义
· Linux系统下SQL Server数据库镜像配置全流程详解
· Sdcb Chats 技术博客:数据库 ID 选型的曲折之路 - 从 Guid 到自增 ID,再到
· 语音处理 开源项目 EchoSharp
· 《HelloGitHub》第 106 期
· Huawei LiteOS基于Cortex-M4 GD32F4平台移植
· mysql8.0无备份通过idb文件恢复数据过程、idb文件修复和tablespace id不一致处