概述
这几天因为一个细节导致甚至浪费了我几天几夜的功夫,差点崩溃了(崩溃的极限越来越久)。
背景:Spring 4.0.4 和Quartz 2.2.1
在Spring中使用定时器Quartz开发步骤
- 定义一个Job(任务类),有两种:实现Quartz的Job接口;或自己写个普通的类,含有方法;
- 定义触发器(理论上,一个任务一个触发器)
- 把所有触发器放入到org.springframework.scheduling.quartz.SchedulerFactoryBean
- 在appliaction.xml 中import quartz.xml
定义任务类
/**
* HelloWorld.java 功能:(一句话描述类的功能)
*
* 修订历史:(格式为:"@since 日期 作者 修订内容简介")
*
* @since 2017年7月17日 http://blog.csdn.net/u014229282 初版
*
* @author http://blog.csdn.net/u014229282, 1406721322@qq.com
* @version 1.0
*/
public class HelloWorld implements Job {
public void execute(JobExecutionContext context)
throws JobExecutionException {
System.out.println("Hello, Quartz! - executing its JOB at "+
new Date() + " by1 " + context.getTrigger());
}
public void runTask(){
System.out.println("Hello, Quartz! - executing its JOB at "+
new Date() + " by runTask");
}
}
注意:这个类实现了Job,且有runTask方法。
在Quartz.xml配置
<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-4.0.xsd"
default-lazy-init="false">
<!-- Created by http://blog.csdn.net/u014229282 -->
<bean id="schedulerFactory"
class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="startupDelay" value="3"/>
<property name="triggers">
<list>
<ref bean="abcTrigger"/>
<ref bean="aTrigger"/>
</list>
</property>
<!-- <property name="configLocation" value="classpath:quartz.properties" /> -->
</bean>
<!-- 定义触发时间 -->
<bean id="abcTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" >
<bean class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<!-- 调用的类 -->
<property name="targetObject">
<bean class="com.yd.ssh.quartz.HelloWorld" />
</property>
<!-- 调用类中的方法 -->
<property name="targetMethod">
<value>runTask</value>
</property>
<!-- 如果前一个任务还没有结束第二个任务不会启动 false -->
<!-- <property name="concurrent" value="false" /> -->
</bean>
</property>
<property name="cronExpression">
<value>1/2 * * * * ?</value>
</property>
</bean>
<!-- 定义触发时间 -->
<bean id="aTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="cronExpression">
<value>2/5 * * * * ?</value>
</property>
<property name="jobDetail">
<bean class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
<property name="jobClass" value="com.yd.ssh.quartz.HelloWorld"/>
<property name="durability" value="true"/>
</bean>
</property>
</bean>
</beans>
在这xml可以知道有两个触发器,它们的任务类都是com.yd.ssh.quartz.HelloWorld.java;接下来直接测试下。
测试定时器运行
/**
* BeanTest.java
*
* Copyright 1406721322@qq.com || ZYD
*/
package com.yd.ssh.service;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.yd.ssh.annotation.Bike;
import com.yd.ssh.annotation.People;
/**
* BeanTest.java 功能:(一句话描述类的功能)
*
* 修订历史:(格式为:"@since 日期 作者 修订内容简介")
*
* @since 2017年7月16日 Created by http://blog.csdn.net/u014229282 初版
*
* @author Created by http://blog.csdn.net/u014229282, 1406721322@qq.com
* @version 1.0
*/
public class BeanTest {
static final Log log = LogFactory.getLog(BeanTest.class); // 日志
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("quartz.xml");
// Person p = (Person) ctx.getBean("chinese",Person.class);
// p.say();
//通过BeanFactory管理
// Resource isr = new ClassPathResource("SSH-public.xml");
// DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
// new XmlBeanDefinitionReader(bf).loadBeanDefinitions(isr);
// Person p = (Person) bf.getBean("chinese",Person.class);
// p.say();
}
}
控制台输出(不想贴图,贴图太麻烦了,以下为黑色输出部分)
Hello, Quartz! - executing its JOB at Sat Jul 22 18:49:20 CST 2017 by runTask
Hello, Quartz! - executing its JOB at Sat Jul 22 18:49:20 CST 2017 by runTask
Hello, Quartz! - executing its JOB at Sat Jul 22 18:49:20 CST 2017 by1 Trigger 'DEFAULT.aTrigger': triggerClass: 'org.quartz.impl.triggers.CronTriggerImpl calendar: 'null' misfireInstruction: 0 nextFireTime: Sat Jul 22 18:49:22 CST 2017
Hello, Quartz! - executing its JOB at Sat Jul 22 18:49:21 CST 2017 by runTask
Hello, Quartz! - executing its JOB at Sat Jul 22 18:49:22 CST 2017 by1 Trigger 'DEFAULT.aTrigger': triggerClass: 'org.quartz.impl.triggers.CronTriggerImpl calendar: 'null' misfireInstruction: 0 nextFireTime: Sat Jul 22 18:49:27 CST 2017
Hello, Quartz! - executing its JOB at Sat Jul 22 18:49:23 CST 2017 by runTask
注意
刚刚得知任务器能运行,但接下来就有点模糊了,不相信试试,
在application.xml中import quartz.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd"
default-lazy-init="false">
<import resource="classpath:quartz.xml"/>
</beans>
就是简单的导入其他xml文件。
在测试类中该为:
/**
* BeanTest.java
*
* Copyright 1406721322@qq.com || ZYD
*/
package com.yd.ssh.service;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.yd.ssh.annotation.Bike;
import com.yd.ssh.annotation.People;
/**
* BeanTest.java 功能:(一句话描述类的功能)
*
* 修订历史:(格式为:"@since 日期 作者 修订内容简介")
*
* @since 2017年7月16日 Created by http://blog.csdn.net/u014229282 初版
*
* @author Created by http://blog.csdn.net/u014229282, 1406721322@qq.com
* @version 1.0
*/
public class BeanTest {
static final Log log = LogFactory.getLog(BeanTest.class); // 日志
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("application.xml");
// Person p = (Person) ctx.getBean("chinese",Person.class);
// p.say();
//通过BeanFactory管理
// Resource isr = new ClassPathResource("application.xml");
// DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
// new XmlBeanDefinitionReader(bf).loadBeanDefinitions(isr);
// Person p = (Person) bf.getBean("chinese",Person.class);
// p.say();
}
}
异常出来了
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'schedulerFactory' defined in class path resource [SSH-scheduling.xml]: Cannot resolve reference to bean 'abcTrigger' while setting bean property 'triggers' with key [0]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'abcTrigger' defined in class path resource [SSH-scheduling.xml]: Cannot create inner bean 'org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean#48503868' of type [org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean] while setting bean property 'jobDetail'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean#48503868' defined in class path resource [SSH-scheduling.xml]: Invocation of init method failed; nested exception is java.lang.NoSuchMethodException: com.sun.proxy.$Proxy13.runTask()
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:336)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:108)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveManagedList(BeanDefinitionValueResolver.java:359)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:157)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1456)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1197)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:684)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at com.yd.ssh.service.BeanTest.main(BeanTest.java:30)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'abcTrigger' defined in class path resource [SSH-scheduling.xml]: Cannot create inner bean 'org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean#48503868' of type [org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean] while setting bean property 'jobDetail'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean#48503868' defined in class path resource [SSH-scheduling.xml]: Invocation of init method failed; nested exception is java.lang.NoSuchMethodException: com.sun.proxy.$Proxy13.runTask()
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveInnerBean(BeanDefinitionValueResolver.java:290)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:122)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1456)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1197)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:328)
... 17 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean#48503868' defined in class path resource [SSH-scheduling.xml]: Invocation of init method failed; nested exception is java.lang.NoSuchMethodException: com.sun.proxy.$Proxy13.runTask()
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1553)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveInnerBean(BeanDefinitionValueResolver.java:276)
... 27 more
Caused by: java.lang.NoSuchMethodException: com.sun.proxy.$Proxy13.runTask()
at java.lang.Class.getMethod(Class.java:1773)
at org.springframework.util.MethodInvoker.prepare(MethodInvoker.java:174)
at org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean.afterPropertiesSet(MethodInvokingJobDetailFactoryBean.java:205)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1612)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1549)
... 30 more
就是动态创建任务在反射失败了,经过我有五天时间不断的发现与研究,就是一个小小的地方,就是在helloWorld.java 不能一边实现Job,又一边在org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean这里使用这个类。
注意:有时候想偷懒,但会适得其反。
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.yd</groupId>
<artifactId>SSH-Service</artifactId>
<version>0.0.1</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<spring.version>4.0.4.RELEASE</spring.version>
<aspect.version>1.6.6</aspect.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency> -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.0.1.RELEASE</version>
</dependency>
<!-- <dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.2</version>
</dependency> -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-mock</artifactId>
<version>2.0.8</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${aspect.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspect.version}</version>
</dependency>
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.1</version>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.2.1</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<!-- Created by http://blog.csdn.net/u014229282, 1406721322@qq.com -->
</project>
May-The-Good-Luck-Be-With-You
最后
以上就是精明鸡为你收集整理的Spring、Quartz——整合注意事项的全部内容,希望文章能够帮你解决Spring、Quartz——整合注意事项所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复