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;
} }
quartz spring的更多相关文章
- Quartz —— Spring 环境下的使用
一.在 Spring 环境下 Quartz 的使用超级简单. 二.具体使用 1.添加对应的 spring-quartz 的配置文件. 2.新建要执行定时任务的目标类和目标方法,不需要继承 Job 接口 ...
- Quartz Spring与Spring Task总结
Spring对Quartz作了一个封装,同时,Spring自己也提供了一个任务定时器(spring-task),现把它总结一下. 对于Quartz,我们使用的时候主要是注重两个方面,一个是定时任 ...
- 【59】Quartz+Spring框架详解
什么是Quartz Quartz是一个作业调度系统(a job scheduling system),Quartz不但可以集成到其他的软件系统中,而且也可以独立运行的:在本文中"job sc ...
- 分布式任务调度——quartz + spring + 数据库
项目中使用分布式并发部署定时任务,多台跨JVM,按照常理逻辑每个JVM的定时任务会各自运行,这样就会存在问题,多台分布式JVM机器的应用服务同时干活,一个是加重服务负担,另外一个是存在严重的逻 ...
- Quartz Spring分布式集群搭建Demo
注:关于单节点的Quartz使用在这里不做详细介绍,直接进阶为分布式集群版的 1.准备工作: 使用环境Spring4.3.5,Quartz2.2.3,持久化框架JDBCTemplate pom文件如下 ...
- quartz spring 实现动态定时任务
在实际项目应用中经常会用到定时任务,可以通过quartz和spring的简单配置即可完成,但如果要改变任务的执行时间.频率,废弃任务等就需要改变配置甚至代码需要重启服务器,这里介绍一下如何通过quar ...
- quartz+spring 实现多任务动态定时器问题
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w ...
- quartz spring 时间配置
关于时间配置, 1前面带0和不带0的区别是??? (开始时间,带0以整点整分整秒开始,不带的以启动时间定时循环??) 比如 0 7/37 * * * ? 表示每个小时的第7分钟开始执行,然后隔三 ...
- quartz + spring 配置示例
<!-- 配置job定时任务类 --> <bean id="triggerCalculateLecturerProfitJob" class="com. ...
随机推荐
- 关于iOS上的对象映射公用方法-备
具体的使用方法,请见下面说明,或者见工程里的单元测试代码.或者,参考原始文档: https://github.com/mystcolor/JTObjectMapping 使用方法 ======== 绝 ...
- C语言学习笔记--指针与字符串
字符类型 char(character)是一种整数,也是一种特殊的类型:字符.这是因为 ① 用单引号表示的字符字符字面量:‘a’,'1' ②‘’也是一个字符 ③printf和scanf里用%c来输入. ...
- Entity Framework with MySQL 学习笔记一(复杂类型 Complex Types)
有时候我们希望在sql一个表里面的column, 一部分被分化成另一个class 典型的例子是 Address 直接看代码: [Table("member")] public cl ...
- Android隐藏标题栏
打开程序,在onCreate()方法中添加如下代码: protected void onCreate(Bundle savedInstanceState) { super.onCreate(saved ...
- Building Web Apps with SignalR, Part 1
Building Web Apps with SignalR, Part 1 In the first installment of app-building with SignalR, learn ...
- JavaScript 实现数组的foreach
Array.prototype.forEach = function (action) { for (var i = 0; i < this.length; i++) { action(this ...
- HDOJ 1076 An Easy Task(闰年计算)
Problem Description Ignatius was born in a leap year, so he want to know when he could hold his birt ...
- HDU-5532(LIS-nlogn)
思路: 解法一: 新的认识get+1,对于一个数组,可以通过记录他'<'和'>'来判断数组的升降序状态,这种方法只需要n的复杂度就可以解决问题,需要注意的一点是,每次删除一个结点在消失两个 ...
- [置顶] 宏途_LCD调试流程.
今天在调试宏途的LCD屏时,开始是开机屏幕不亮,背光都不亮,可能板子已经损坏,一般通过测试电流电压简单验证,(注:硬件引脚没焊好也会引起读lcd id出现错误!!!)出现这个问题一般是因为引脚没焊好, ...
- android开发常用组件(库)推荐
版本兼容:官方 support 全家桶 网络请求:Android-Async-Http.Retrofit.OkHttp.Volley图片加载:Glide 和 Universal-Image-Loade ...