在OpenJWeb平台中使用Spring框架配置定时器的实例
2008-03-16 11:15
597 查看
由于OpenJWeb平台集成了Spring框架,所以定时作业的开发非常方便,首先一个被定时器调用的类:
package org.openjweb.common.timer;
import org.apache.log4j.Logger;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;
public class JobSchedule extends QuartzJobBean
{
static Logger logger = Logger.getLogger(JobSchedule.class);
static long counter = 0; //计数器
public synchronized void doTimerSchedule()
{
System.out.println("测试定时器........");
}
protected void executeInternal(JobExecutionContext arg0) throws JobExecutionException {
// TODO Auto-generated method stub
}
}
上面的doTimerSchedule是被定时调用的方法,下文讲述此方法在spring配置文件中如何配置为定时调用:
<?xml version="1.0" encoding="GB2312"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<!-- Timer Schedule config start -->
<!-- defime a business timer object-->
<bean id="EAITimerBean" class="org.openjweb.common.timer.JobSchedule"/>
<!--defime which method of class to be called in timer schedule -->
<bean id="EAITimerMethod" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="EAITimerBean" />
<property name="targetMethod" value="doTimerSchedule" />
<property name="concurrent" value="false" /> <!--将并发设置为false-->
</bean>
<!-- 设置定时器 -->
<bean id="eaiTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="EAITimerMethod" />
<!--每两分钟触发-->
<property name="cronExpression" value="0 1/2 * * * ?" />
</bean>
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<!--EAI调度器,list下可加入其他的调度器-->
<ref bean="eaiTrigger"/>
</list>
</property>
</bean>
<!-- Timer Schedule config end -->
</beans>
如果你的开发环境中集成了spring后,将上面的代码和xml配置放到你自己应用对应的位置编译启动tomcat后可以看到后台每两分钟输出一次调试信息.1/2表示每两分钟的第一分钟.
package org.openjweb.common.timer;
import org.apache.log4j.Logger;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;
public class JobSchedule extends QuartzJobBean
{
static Logger logger = Logger.getLogger(JobSchedule.class);
static long counter = 0; //计数器
public synchronized void doTimerSchedule()
{
System.out.println("测试定时器........");
}
protected void executeInternal(JobExecutionContext arg0) throws JobExecutionException {
// TODO Auto-generated method stub
}
}
上面的doTimerSchedule是被定时调用的方法,下文讲述此方法在spring配置文件中如何配置为定时调用:
<?xml version="1.0" encoding="GB2312"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<!-- Timer Schedule config start -->
<!-- defime a business timer object-->
<bean id="EAITimerBean" class="org.openjweb.common.timer.JobSchedule"/>
<!--defime which method of class to be called in timer schedule -->
<bean id="EAITimerMethod" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="EAITimerBean" />
<property name="targetMethod" value="doTimerSchedule" />
<property name="concurrent" value="false" /> <!--将并发设置为false-->
</bean>
<!-- 设置定时器 -->
<bean id="eaiTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="EAITimerMethod" />
<!--每两分钟触发-->
<property name="cronExpression" value="0 1/2 * * * ?" />
</bean>
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<!--EAI调度器,list下可加入其他的调度器-->
<ref bean="eaiTrigger"/>
</list>
</property>
</bean>
<!-- Timer Schedule config end -->
</beans>
如果你的开发环境中集成了spring后,将上面的代码和xml配置放到你自己应用对应的位置编译启动tomcat后可以看到后台每两分钟输出一次调试信息.1/2表示每两分钟的第一分钟.
相关文章推荐
- 在OpenJWeb平台中使用Spring框架配置定时器的实例
- Spring框架的定时器(Quartz)的配置和使用
- Spring框架中的定时器 使用和配置
- Spring框架中的定时器 如何使用和配置
- Spring框架中的定时器 如何使用和配置
- Spring框架中的定时器quartzquartz如何使用和配置
- Spring框架中的定时器 如何使用和配置
- Spring框架中的定时器 如何使用和配置
- Spring框架的定时器的配置及使用
- Spring框架中的定时器 如何使用和配置
- spring框架中定时器的使用和配置
- CREELINKS平台_处理器CeAd资源使用说明(CeAd的配置与使用)
- 使用iptables配置NAT的实例
- 使用Eclipse连接SAP云平台上的HANA数据库实例
- ARM平台上蓝牙协议栈Bluez的移植使用和配置 收藏
- 使用反射创建Bean、Spring中是如何根据类名配置创建Bean实例、Java提供了Class类获取类别的字段和方法,包括构造方法
- 使用Gitblit在Windows平台上安装与配置Git Server
- 使用spring配置定时器
- 从零开发校园商铺平台(SSM到SpringBoot)三.Logback配置与使用
- spring框架中多数据源创建加载并且实现动态切换的配置实例代码