读书人

施用quartz调度的一个java例子

发布时间: 2012-08-15 16:57:16 作者: rapoo

使用quartz调度的一个java例子
1, 加入 jar

2, 在web.xml 中

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:webpointContext.xml,
classpath:spring-quartz.xml
</param-value>
</context-param>

3, class 路径下建立spring-quartz.xml,内容如下



<!-- 一个调度任务 -->
<bean name="reportTask"
value="com.tydic.metadata.util.CsaNoticeJob" />
</bean>

<!-- 触发器 -->
<bean id="cronTrigger"
ref="reportTask" />

<!-- 每天每10分钟运行一次 -->
<property name="cronExpression" value="0 0/10 * * * ?" />
</bean>


<!-- 调度器 -->
<bean
/>
</list>
</property>

</bean>



4,写个java类,

public class CsaNoticeJob implements Job {

private static final long serialVersionUID = 1L;

ApplicationContext ctx= new ClassPathXmlApplicationContext("webpointContext.xml");


private SqlMapClient sqlMapClient ;

//必须实现的方法
public void execute(JobExecutionContext arg0) throws JobExecutionException {

perform();
}

// 自己定义的具体方法,执行调度任务
public void perform(){
try {
sqlMapClient= (SqlMapClient) ctx.getBean("sqlMapClient");
if(sqlMapClient==null){
System.err.println("sqlMapClient 是 null ");
}
int i=sqlMapClient.update("updateStateNoticeKF");

} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();

}


}

}

读书人网 >软件架构设计

热点推荐