由於工作尚需要,最近又開始搞spring的東西了
其中有一項是要讓排程的時間可以設在資料庫裡
一般來說用spring+quartz就可以很簡單的設定好排程
可是大部分都是直接把排程設定檔設在xml檔裡
這樣就不彈性了,要動態的調整時間可以說是不可能
網路上找了找方法有個MethodInvokingFactoryBean的class可以使用
不囉嗦 直接來code吧
quartz-jobs.xml:
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="org.gjt.mm.mysql.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/testjob?characterEncoding=utf-8" /> <property name="username" value="testjob" /> <property name="password" value="testjob" /> </bean> <bean id="JobConstantDaoBean" class="tw.cse.moa.dao.JobConstantDao"> <property name="jdbcTemplate" ref="dataSource" /> </bean> <bean id="workBean" class="tw.cse.moa.job.Work"> </bean> <bean id="scheduledJobDef" name="scheduledJobDef" class="tw.cse.moa.job.ScheduledJobDef"> <property name="jobConstantDao" ref="JobConstantDaoBean"/> </bean> <bean id="rescheduler" name="rescheduler" class="tw.cse.moa.job.Rescheduler"> <property name="schedulerFactory" ref="schedulerFactory"></property> <property name="scheduledJobDef" ref="scheduledJobDef"></property> </bean> <bean id="copyJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject"> <ref bean="workBean" /> </property> <property name="targetMethod"> <value>doWork</value> </property> </bean> <bean id="triggerCopyBean" class="org.springframework.scheduling.quartz.SimpleTriggerBean"> <property name="jobDetail" ref="copyJobDetail" /> <property name="startDelay" value="1000" /> <property name="repeatInterval" ref="repeatIntervalMethod" /> </bean> <bean id="schedulerFactory" name="schedulerFactory" class="org.springframework.scheduling.quartz.SchedulerFactoryBean" scope="singleton"> <property name="triggers"> <list> <ref bean="triggerCopyBean" /> </list> </property> </bean> <bean id="repeatIntervalMethod" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean"> <property name="targetObject"> <ref local="scheduledJobDef" /> </property> <property name="targetMethod"> <value>getRepeatInterval</value> </property> <property name="arguments"> <list /> </property> </bean>
Rescheduler.java:
public class Rescheduler { StdScheduler schedulerFactory; ScheduledJobDef scheduledJobDef; public void reschedule() { try { SimpleTriggerBean t = (SimpleTriggerBean) schedulerFactory.getTrigger("triggerCopyBean", Scheduler.DEFAULT_GROUP); schedulerFactory.getJobDetail(t.getJobDetail().getName(), t.getJobDetail().getGroup()); t.setRepeatInterval(scheduledJobDef.getRepeatInterval()); t.setStartTime(new Date(System.currentTimeMillis())); schedulerFactory.rescheduleJob(t.getName(), Scheduler.DEFAULT_GROUP, t); } catch (Exception e) { e.printStackTrace(); } } public StdScheduler getSchedulerFactory() { return schedulerFactory; } public void setSchedulerFactory(StdScheduler schedulerFactory) { this.schedulerFactory = schedulerFactory; } public ScheduledJobDef getScheduledJobDef() { return scheduledJobDef; } public void setScheduledJobDef(ScheduledJobDef scheduledJobDef) { this.scheduledJobDef = scheduledJobDef; } }
ScheduledJobDef.java:
public class ScheduledJobDef { private JobConstantDao jobConstantDao; public void setJobConstantDao(JobConstantDao jobConstantDao) { this.jobConstantDao = jobConstantDao; } public long getRepeatInterval() { Pattern p = Pattern.compile("([0-9]+)"); String v1 = jobConstantDao.getContant("RepeatInterval").getValue(); System.out.println("get RepeatInterval: "+v1); Matcher m = p.matcher(v1); if(m.find()){ return Long.parseLong(m.group(0)); } return 5; } }
這邊懶得把main分開到別的class所以寫在一起
Work.java:
public static void main(String... args) throws InterruptedException { ApplicationContext qj = new ClassPathXmlApplicationContext("quartz-jobs.xml"); Thread.sleep(5000); JobConstantDao jobConstantDao = (JobConstantDao) qj.getBean("JobConstantDaoBean"); System.out.println("Setting to 2000"); jobConstantDao.setContant("RepeatInterval", "2000"); System.out.println("Rescheduling..."); Rescheduler rescheduler = (Rescheduler) qj.getBean("rescheduler"); rescheduler.reschedule(); } public void doWork() { SimpleDateFormat format = new SimpleDateFormat("hh:mm:ss"); System.out.println("Scheduled job - Current Millis : " + format.format(new Date(System.currentTimeMillis()))); }執行結果:
完整source下載:
testjob.zip
沒有留言:
張貼留言