Summary of problems encountered in spring+quartz implementing timing tasks (beans cannot be injected)

Summary of problems encountered in spring+quartz implementation of timed tasks

Scheduled tasks are required for recent projects. Query the transaction status of the order at regular intervals. After the query, it is decided to use spring+quartz to realize it.

Mainly divided into three steps:

1. Write the execution class and execution method of the scheduled task

2. Configure the scheduled task class and execution method

3. Start the server to perform scheduled tasks

1. Write timing classes and execution methods:

public class TestTask {
    
    private static Logger logger = Logger.getLogger(TestTask.class);
    
    @Autowired
    public OrderService orderService;
    
    public void execute(){
        logger.debug("执行了------TestTask execute log--------");
    }
    
}

2. Configure the scheduled task class and execution method: Configuration file: spring-quartz.xml is as follows:

<?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:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">

     <!-- Please ignore the following line-->
    <context:component-scan base-package="com.xxx.rechargems.app" />
     
    <!-- Configure the scheduled task class-->
    <bean id=" testTask" class="com.xxx.rechargems.app.task.TestTask" />
    <bean id="testJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <!-- targetObject is used to specify timing tasks The class of -->
        <property name="targetObject" ref="testTask"/>
        <!--Specify that the target is encapsulated as a stateful task, a stateful task cannot be executed concurrently, and a stateless task can be executed concurrently-->
        <property name="concurrent" value="false" />
        <!-- targetMethod is used to specify the method to be executed in the class of the scheduled task-->
        <property name="targetMethod"value="execute"/>
    </bean>
    <bean id="testCronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">  
        <property name="jobDetail" ref="testJob" />
        <!-- 延时启动时间,单位ms -->
        <property name="startDelay" value="0" />
        <!-- cron表达式,此处是每分钟执行一次 -->
        <property name="cronExpression" value="0 * * * * ?" />
    </bean>
    
    <bean id="testTask2" class="com.xxx.rechargems.app.task.TestTask2" />
    <bean id="testJob2" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <property name="targetObject"ref="testTask2"/>
        <property name="concurrent" value="false" />
        <property name="targetMethod" value="execute"/>
    </bean>
    
    <bean id="testCronTrigger2" class="org.springframework.scheduling.quartz.SimpleTriggerBean">  
        <property name="jobDetail" ref="testJob2" />  
        <!-- 调度工厂实例化后,经过0秒开始执行调度   -->
        <property name="startDelay" value="0" />
        <!-- 每2秒调度一次   -->
        <property name="repeatInterval" value="2000" />
    </bean>
   
    <bean id="SpringJobSchedulerFactoryBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">  
        
        <property name="triggers">  
            <list>
                <ref bean="testCronTrigger"/>
                <ref bean="testCronTrigger2"/>
            </list>  
        </property>
    </bean>

</beans>

3. Start the server to perform timed tasks.

At this point, it should be the timed task to start, everyone is happy, but the excitement is coming, and the startup reports an error! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! !

After reading the error, the bean is not injected! ! !

     @Autowired
    public OrderService orderService;

This is what caused this, so find the reason:

1.jar package: the quartz-all.jar package has been configured (pom has the corresponding dependency configuration in the field)

2. Whether the configuration file has been read: spring-quartz.xml has been added to web.xml:

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

</context-param>

3. tomcat and jdk version: the original is jdk1.7 and tomcat7, but spring is version 4.0. In order to check whether it is a version problem, it is changed to jdk1.8 and tomcat-8.0, but an error is still reported.

Helpless. . . . . . . . . . . . . . . . . . . . . . . . .

The final reason is that no scan package has been added! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! !

Just add the scan package path in: spring-mybatis.xml or spring-quartz.xml: <context:component-scan base-package="com.xxx.rechargems.app" />

So far the problem is solved.

Annotation scanning is configured in springmvc, but spring annotations and spring annotation scanning should be configured separately. Only springmvc scanning is configured. Spring annotations cannot be used. This is probably the reason why they cannot be injected! ! ! Whether this is the reason remains to be verified! ! ! !


Earlier problems: When quartz was configured, and no service injection was added, the scheduled task was killed and not executed. Later, it was found that the timing-related configuration was not found when spring started, that is, quartz was not initialized, and spring-quartz.xml should be

Add to the spring initialization configuration, which is the configuration in reason 2,

The solution was finally found by Mr. You, thank you! ! ! !


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325562367&siteId=291194637