锁定老帖子 主题:spring+quartz调度的问题
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2008-03-14
我的 applicationContext 配置文件是这样的 <bean id="testQuarz" class="com.TestQuarz" />
<bean id="cronTrigger" <value>0 * 14 * * ?</value> <bean autowire="no" 我想 把那个 cronExpression 表达式 变成动态的,就是传入什么值,就可以在这个值进行调度,但是又不想在传入新值之后重新启动服务器,不知道有什么好的解决办法? 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2008-03-14
你不要在xml里面配置,在类里面set就好了~~~
|
|
返回顶楼 | |
发表时间:2008-03-15
这是我的处理办法
1.继承spring SchedulerFactoryBean的工厂类,主要是传入数据源 package cn.com.huangjiej.timer; import javax.sql.DataSource; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.scheduling.quartz.SchedulerFactoryBean; import java.util.Iterator; import java.util.List; /** * 定时器创建工厂 * @author huangjj * */ public class IESchedulerFactoryBean extends SchedulerFactoryBean { private DataSource datasource; public DataSource getDatasource() { return datasource; } public void setDatasource(DataSource datasource) { this.datasource = datasource; } public void init() { //TODO 这部分以后可以用于从数据库中取出数据并自动创建trigger // if(datasource==null) // { // throw new RuntimeException("不能创建定时器,没有指定dataSource"); // } // JdbcTemplate temp = new JdbcTemplate(datasource); // String sql = "select id,name,startDelay,repeatInterval,cornString,userStatus from iecm_alertTimer"; // // List list = temp.queryForList(sql); // if(list!=null) // { // for (Iterator iterator = list.iterator(); iterator.hasNext(); ) { // Object[] values = (Object[]) iterator.next(); // for (int i = 0; i < values.length; i++) { // String object = (String)values[i]; // // } // } // } } } 2。 继承triggerbean ,主要是改成从数据库中取出cron 表达式 package cn.com.huangjiej.timer; import java.text.ParseException; import javax.sql.DataSource; import org.apache.commons.logging.LogFactory; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.scheduling.quartz.CronTriggerBean; import java.util.List; import java.util.Map; /** * 复杂定时指令控制器,从数据库中取出规则 * @author huangjj * */ public class IECronTriggerBean extends CronTriggerBean { private org.apache.commons.logging.Log log = LogFactory.getLog(this.getClass()); IECronTriggerBean() { } private String timeid;//定时器ID private DataSource datasource;//数据库,用于查询数据库 public DataSource getDatasource() { return datasource; } public void setDatasource(DataSource datasource) { this.datasource = datasource; } public void init() { if(datasource==null) { log.error("不能创建定时器,没有指定dataSource"); throw new RuntimeException("不能创建定时器,没有指定dataSource"); } JdbcTemplate temp = new JdbcTemplate(datasource); String sql = "select id,name,startDelay,repeatInterval,cronString,userStatus from iecm_alertTimer where userStatus=1 and id='"+timeid+"'"; List list = temp.queryForList(sql); if(list!=null&&!list.isEmpty()) { Map values = (Map) list.get(0); String id = (String) values.get("id"); String name = (String) values.get("name"); String cronString = (String) values.get("cronString"); try { this.setCronExpression(cronString); } catch (ParseException e) { log.error("不能创建定时器,复杂定时指令(crontab字符串)不正确",e); throw new RuntimeException("不能创建定时器,复杂定时指令(crontab字符串)不正确"); } } else { log.error("不能创建定时器,没有指定复杂定时指令(crontab字符串)或数据查询出错"); throw new RuntimeException("不能创建定时器,没有指定复杂定时指令(crontab字符串)或数据查询出错"); } } public String getTimeid() { return timeid; } public void setTimeid(String timeid) { this.timeid = timeid; } } 3.写业务方法处理的类 package cn.com.huangjiej.timer; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; import org.springframework.scheduling.quartz.QuartzJobBean; import java.util.Date; public class AlertRunningJob extends QuartzJobBean { private TimerTigger timeExecutor;//处理类的业务方法 public TimerTigger getTimeExecutor() { return timeExecutor; } public void setTimeExecutor(TimerTigger timeExecutor) { this.timeExecutor = timeExecutor; } //业务执行 protected void executeInternal(JobExecutionContext jobexecutioncontext) throws JobExecutionException { Date date = jobexecutioncontext.getFireTime(); //getTigger().execute(date); System.out.println(date); } } 4. 定义spring <?xml version="1.0" encoding="gbk"?> <!DOCTYPE beans SYSTEM "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <!-- 定时器部分 --> <!-- 定时器工厂,主要是生成定时器 --> <!-- <bean id="schedulerFactory" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref local="cronTrigger" /> <ref local="simpleTrigger" /> </list> </property> </bean> --> <!-- 定时器,规定在什么时候执行,简单定时器,指定执行的首次执行时间和再次执行时间,该时间从数据库中取出 --> <!-- <bean id="simpleTrigger" class="cn.com.huangjiej.timer.IESimpleTriggerBean" init-method="init"> <property name="jobDetail" ref="checkExecutor" /> <property name="datasource" ref="jidiDataSource" /> <property name="timeid" value="1" /> --数据据中的主键 </bean> --> <!-- 定时器,规定在什么时候执行,cron定时器,指定执行周期,该时间从数据库中取出 --> <!-- <bean id="cronTrigger" class="cn.com.huangjiej.timer.IECronTriggerBean" init-method="init"> <property name="jobDetail" ref="sendExecutor" /> <property name="datasource" ref="jidiDataSource" /> <property name="timeid" value="2" /> </bean> <bean id="checkExecutor" class="org.springframework.scheduling.quartz.JobDetailBean"> <property name="jobClass" value="cn.com.huangjiej.timer.AlertRunningJob" /> <property name="jobDataAsMap"> <map> <entry key="timeExecutor"><null/></entry> </map> </property> </bean> --> <!-- 具体执行的业务方法 --> <bean id="sendExecutor" class="org.springframework.scheduling.quartz.JobDetailBean"> <property name="jobClass" value="cn.com.huangjiej.timer.AlertRunningJob" /> <property name="jobDataAsMap"> <map> <entry key="timeExecutor"><null/></entry>//jobDataAsMap 可以传入参数 </map> </property> </bean> </beans> 其思想是很简单的是,把cron expression保存在数据库中,在spring中创建从数据库读出来。至于读出来的方法是多种多样的,不限于我这样,这要发挥大家的想象力了。 至于改cron后要不重启而更新trigger ,我现在还未做。 请参考 http://www.iteye.com/topic/21147 动态改变quartz的触发器策略? 如果楼主弄好了,是否也可以与我分享一下经验呢 |
|
返回顶楼 | |
浏览 5629 次