Quartz + spring的开发步骤
1.需要jar包:
commons-logging-1.0.4.jar (日记所需要的包)
log4j-1.2.11.jar (日记所需要的包)
quartz-1.6.1.jar (Quartz所需要的包)
spring-2.5.6.jar(spring所需要的包)
commons-collections-3.1.jar (这个包是解决以下问题的:
java.lang.NoSuchMethodError: org.apache.commons.collections.SetUtils: method
orderedSet(Ljava/util/Set;)Ljava/util/Set; not found)
2.步骤
a.编写Quartz执行的工作的类
b.创建spring的配置文件xxx-applicationContext.xml
c.在xxx-applicationContext.xml对Quartz进行配置
d.配置完成后,编写测试类
f.完成
3.需要创建的文件(以本人工程为案例)
a.QuartzJob.java (Quartz调度的java类)
b.time-applicationContext.xml (Quartz与spring结合)
c.MainTest.java (测试类)
d.log4j.properties (log所需要的配置文件,放在src目录下)
4.具体代码如下
QuartzJob.java
_____________________________________
package com.flashdream.quartz;
public class QuartzJob {
public void work() {
System.out.println("这里是Quartz的任务调度");
}
}
______________________________________
time-applicationContext.xml
______________________________________
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!-- 要调用的工作类 -->
<bean id="quartzJob" class="com.flashdream.quartz.QuartzJob"/>
<!-- 定义调用对象和调用对象的方法 -->
<bean id="jobtask"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryB
ean">
<!-- 调用的类 -->
<property name="targetObject">
<ref bean="quartzJob" />
</property>
<!-- 调用类中的方法 -->
<property name="targetMethod">
<value>work</value>
</property>
</bean>
<!-- 定义触发时间 -->
<bean id="doTime"
class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail">
<ref bean="jobtask" />
</property>
<!-- cron表达式 -->
<property name="cronExpression">
<value>0/10 * * * * ?</value>
</property>
</bean>
<!-- 总管理类 如果将lazy-init='false'那么容器启动就会执行调度程序
-->
<bean id="startQuertz"
class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="doTime" />
</list>
</property>
</bean>
</beans>
______________________________________
MainTest.java
______________________________________
package com.flashdream8.quartz;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainTest {
/**
* @param args
*/
public static void main(String[] args) {
System.out.println("Test start.");
ApplicationContext context = new
ClassPathXmlApplicationContext(
"time-applicationContext.xml");
context.getBean("startQuertz");
System.out.print("Test end..");
}
}
______________________________________
log4j.properties
______________________________________
log4j.rootLogger=INFO,A1,R
#log4j.category.com.laoer=INFO,A1,R
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss,SSS} [%
c]-[%p] %m%n
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=${bbt.root}/clip.log
log4j.appender.R.MaxFileSize=500KB
log4j.appender.R.MaxBackupIndex=1
#log4j.appender.R.layout=org.apache.log4j.HTMLLayout
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} [%t]
[%c] [%p] - %m%n
log4j.logger.org.directwebremoting = ERROR
______________________________________
Quartz + spring的开发步骤
最新推荐文章于 2021-02-19 15:07:13 发布