首页 技术 正文
技术 2022年11月19日
0 收藏 404 点赞 4,857 浏览 4837 个字

简单例子可参考

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;
}}

  

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,031
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,520
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,368
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,148
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,781
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,860