美文网首页
spring Quartz基于配置文件和注解的实现

spring Quartz基于配置文件和注解的实现

作者: whoismy8023 | 来源:发表于2017-10-26 15:17 被阅读0次

一、基于配置文件的实现

①编写需要调度的类

package com.study;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
//@Component
public class QuartzJob {
    public QuartzJob(){
        System.out.println("Quartzjob创建成功");
    }
    //@Scheduled(cron = "0/1 * *  * * ? ")
    public void run(){
        System.out.println("Quartz执行的任务调度");
    }
}

②设置配置文件spring-quartz.xml

<!-- 定义导出交易商基础信息表任务调度-->
<!-- 1、实例化需要进行任务调度的对象 -->
<bean id="queryFirmJob" class="gnnt.MEBS.pushSystem.job.QueryFirmJob"></bean>
 
<!-- 2、定义调用对象和调用对象的方法 -->
<bean id="queryFirmJobTask" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
    <!-- 调用的类 -->
    <property name="targetObject" ref="queryFirmJob"></property>
    <!-- 调用类中的方法 -->
    <property name="targetMethod" value="run"></property>
    <!-- 是否并发执行 -->
    <property name="concurrent" value="false"></property>
</bean>
<!-- 3、定义触发时间 -->
<bean id="queryFirmJobCronTriggerBean" class="org.springframework.scheduling.quartz.CronTriggerBean">
    <property name="jobDetail" ref="queryFirmJobTask"></property>
    <!-- cron表达式 -->
    <property name="cronExpression" value="0 0 20 * * ?"></property>
</bean>
<!--定义任务调度-->
 
 
<!-- 总管理类 如果将lazy-init='false'那么容器启动就会执行调度程序 -->
<bean id="startQuertz" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
    <property name="triggers">
        <list>
            <ref bean="queryFirmJobCronTriggerBean"/>
        </list>
    </property>
</bean>

③启动spring容器

package com.study;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
    public static void main(String[] args) {
        System.out.println("启动spring容器");
        ApplicationContext ac = newClassPathXmlApplicationContext("classpath:spring-quartz.xml");
    }
}

二、基于注解的实现

①配置需要调度的类,并添加注解

package com.study;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
public class QuartzJob {
    public QuartzJob(){
        System.out.println("Quartzjob创建成功");
    }
    @Scheduled(cron = "0/1 * *  * * ? ")
    public voidrun(){
        System.out.println("Quartz执行的任务调度");
    }
}

②添加配置文件

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:task="http://www.springframework.org/schema/task" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/task
       http://www.springframework.org/schema/task/spring-task-3.0.xsd   
       http://www.springframework.org/schema/context 
       http://www.springframework.org/schema/context/spring-context-3.0.xsd
       http://www.springframework.org/schema/tx 
       http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
       http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
    <context:component-scan base-package="gnnt.MEBS.pushSystem.job" />
    <task:executor id="executor" pool-size="5" />
    <task:scheduler id="scheduler" pool-size="10" />
    <task:annotation-driven executor="executor" scheduler="scheduler" />
</beans>

③启动容器,这里通过配置web.xml启动

<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring-quartz2.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
</web-app>

相关文章

网友评论

      本文标题:spring Quartz基于配置文件和注解的实现

      本文链接:https://www.haomeiwen.com/subject/lfdkpxtx.html