Spring--quartzJob配置

Posted 向东1991

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring--quartzJob配置相关的知识,希望对你有一定的参考价值。

maven依赖:

    <!-- quartz job start-->
        <dependency>
            <groupId>org.quartz-scheduler</groupId>
            <artifactId>quartz</artifactId>
            <version>2.2.1</version>
        </dependency>
        <dependency>
            <groupId>org.quartz-scheduler</groupId>
            <artifactId>quartz-jobs</artifactId>
            <version>2.2.1</version>
        </dependency>
        <!-- quartz job end-->

使用时要注意spring版本,其中3.2.x之前只支持1.8.x之前的版本,对应spring配置也会有不同。

自定义job:

public class MyQuartzJobPojo {

    private static Logger logger = Logger.getLogger(MyQuartzJobPojo.class);
    public void doMyQuartzJobPojo(){
        
        
        System.out.println("run in my quartz job pojo ..."+(new Date()).getTime());
    }
    
}

spring-quartz.xml配置:

<!-- quartz job pojo start -->
    <bean id="myJob" class="com.copsec.railway.im.quartzJob.MyQuartzJobPojo"/>
    <!-- job detail start -->
    <bean id="myJobDetail"
        class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <property name="targetObject" ref="myJob"/>
        <property name="targetMethod" value="doMyQuartzJobPojo" />
    </bean>
    <!-- job detail stop -->
    <!-- job trigger start -->
    <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
        <property name="jobDetail" ref="myJobDetail" />
        <property name="startDelay" value="0" /> <!-- 调度工厂实例化后,经过0秒开始执行调度 -->
        <property name="cronExpression"><!-- 分时日月周  0 0 1 * * ? -->  
            <value>0/10 * * * * ?</value>  
        </property>  
    </bean>
    <!-- job trigger end -->
    <!-- schedule start -->
    <bean id="myScheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="triggers">
            <list>
                <ref bean="cronTrigger" />
            </list>
        </property>
    </bean>
    <!-- schedule stop -->
    <!-- quartz job pojo end -->

web.xml当中配置,不要把这个配置信息写入applicationContext.xml,会出现重复执行的情况。

<context-param>  
        <param-name>contextConfigLocation</param-name>  
        <param-value>classpath:applicationContext.xml,classpath:spring-quartzJob.xml</param-value>  
    </context-param>
<!-- 防止spring内存溢出监听器 -->
    <listener>
        <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>DispatchServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
          <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:applicationContext.xml</param-value>
        </init-param> 
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>DispatchServlet</servlet-name>
        <!-- 默认匹配所有请求 -->
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>

 

以上是关于Spring--quartzJob配置的主要内容,如果未能解决你的问题,请参考以下文章

VSCode自定义代码片段11——vue路由的配置

VSCode自定义代码片段11——vue路由的配置

VS Code配置markdown代码片段

VS Code配置markdown代码片段

从零开始配置vim(27)——代码片段

从零开始配置vim(27)——代码片段