Java Timer触发定时器
XML:
<!-- Java Timer定时 --> <!-- <bean id="shortUrlTask" class=" com.spring.common.ShortUrlTask"> </bean> <bean id="scheduleReportTask" class="org.springframework.scheduling.timer.ScheduledTimerTask"> <property name="timerTask" ref="shortUrlTask" /> <property name="period"> <value>500</value> </property> </bean> <bean class="org.springframework.scheduling.timer.TimerFactoryBean"> <property name="scheduledTimerTasks"> <list> <ref bean="scheduleReportTask"/> </list> </property> </bean> --> <!-- <bean id="SchedulerTask" class="org.springframework.scheduling.quartz.JobDetailBean"> <property name="jobClass"> <value>com.spring.common.QuatzTime</value> </property> </bean> <bean id="SchedulerTaskTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean"> <property name="jobDetail" ref="SchedulerTask" /> <property name="cronExpression"> <value>0/10 * * * * ?</value> </property> </bean> --> <bean id="jobDetail" class="org.springframework.scheduling.quartz.JobDetailFactoryBean"> <!-- durability 表示任务完成之后是否依然保留到数据库,默认false --> <property name="durability" value="true" /> <!-- 目标类 /wmuitp/src/test/SpringQuartz.java--> <property name="jobClass" value="com.spring.common.QuatzTime"></property> </bean> <!-- 另一种触发器是CornTrigger --> <bean id="cornTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"> <property name="jobDetail" ref="jobDetail"/> <!-- 每个10秒触发 --> <property name="cronExpression" value="0/10 27 10 * * ?"/> </bean> <!-- 定义核心调度器 --> <bean id="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <ref bean="cornTrigger"/> </property> </bean>
java类中:
public class QuatzTime extends QuartzJobBean { @Override protected void executeInternal(JobExecutionContext arg0) throws JobExecutionException { System.out.println("现在的时间是:" + nowTime()); } public String nowTime() { SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String time = sf.format(new Date()); return time; } }
posted on 2016-06-13 20:12 peng.zhang 阅读(2383) 评论(0) 编辑 收藏 举报