注 :spring 和quartz 整合时,版本有点问题。
spring 3.1 以上 最好用 quartz 2.x 以上
spring 3.1 以下 最好用 quartz 1.x
我的版本:spring 3 quartz 1.8
文件中注释很详细。
这是applicationContext.xml 文件
<?xml version="1.0" encoding="UTF-8"?>
<!-- 要想在xml中配置aop 必须导入命名空间 xmlns:aop="http://www.springframework.org/schema/aop"
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd -->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!-- 业务对象 -->
<bean id="testJobTask" class="com.vic.quartzSpring.job.TestJob" />
<bean id="serviceJob" class="com.vic.quartzSpring.job.ServiceJob"/>
<!-- 这是 JobDateMap -->
<bean id="testDataMap" class="org.quartz.JobDataMap">
<constructor-arg>
<map>
<entry key="PARAM1" value="参数1"></entry>
</map>
</constructor-arg>
</bean>
<!-- 第一种 jobDetail 这种的话 jobClass 要实现 job 接口并且会自动执行execute方法
-->
<bean id="jobDetail" class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass">
<value>
com.vic.quartzSpring.job.TestJob
</value>
</property>
<property name="jobDataMap">
<ref bean="testDataMap" />
</property>
</bean>
<!-- 第二中JobDetail 随便一个普通类和普通方法-->
<!-- 调度业务 -->
<bean id="servicejobDetail"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="serviceJob" />
<property name="targetMethod" value="service" />
<property name="concurrent" value="false" /><!-- 作业不并发调度 -->
</bean>
<!-- 因为是 spring3.1以下和quartz2.x 以下
所以 Class 为 CronTriggerBean
如果是高版本的那就是
需将CronTriggerBean修改为CronTriggerFactoryBean
-->
<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="jobDetail" />
<property name="cronExpression" value="0/5 * * * * ?" />
</bean>
<bean id="cronTriggerone" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="jobDetailOne" />
<property name="cronExpression" value="0/5 * * * * ?" />
</bean>
<!-- 线程执行器配置,用于任务注册 ThreadPoolTaskExecutor,线程池,用来并行执行每个对应的job,提高效率
-->
<bean id="executor"
class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
<property name="corePoolSize" value="10" />
<property name="maxPoolSize" value="100" />
<property name="queueCapacity" value="500" />
</bean>
<!-- 设置调度 -->
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="cronTrigger" />
<ref bean="cronTriggerone" />
</list>
</property>
<property name="taskExecutor" ref="executor" />
</bean>
</beans>
这个是我的TestJob 类 主要类执任务的。这个类中没有调用数据库的东西。
public class TestJob implements Job {
@Override
public void execute(JobExecutionContext arg0) throws JobExecutionException {
String jobName = arg0.getJobDetail().getFullName();
System.out.println(jobName+" is job by trigger "+arg0.getTrigger().getName()+" time "+new Date());
System.out.println(arg0.getJobDetail().getJobDataMap().get("PARAM1"));
}
}
这个 Job中是定时 从数据库中取到数据
public class ServiceJob {
public void service() throws Exception{
System.out.println("这是service方法 is job by trigger time: "+new Date());
StudentServiceimpl studentService = new StudentServiceimpl();
List<String> ls = studentService.getCourseList();
if(ls!=null&&ls.size()>0){
for(String s:ls){
System.out.println(s);
}
}else{
System.out.println("沒有查到,从数据库");
}
}
}
这个StudentServiceimpl 类打开连接从数据库去数据
我用的是c3p0。
public class StudentServiceimpl {
public List<String> getCourseList() throws SQLException{
List<String> cl = new ArrayList<String>();
Connection conn = DataSourceUtils.getConnection();
String sql = "select * from course";
java.sql.PreparedStatement preparedStatement = conn.prepareStatement(sql);
ResultSet result = preparedStatement.executeQuery();
while(result.next()){
cl.add(result.getString("CNAME"));
}
return cl;
}
}
下一篇 用maven 和 spring 4和 quartz 2.2.