在Spring中石英调度和定时任务使用说明(个人学习资料)

Spring中定时任务三种方式使用说明:

有关定时任务在Github上的资料地址

Spring中Quartz调度器的使用

在pom.xml中配置项目依赖;
<!-- 使用石英调度需要使用 -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context-support</artifactId>
    <version>4.3.8.RELEASE</version>
</dependency>

<dependency>
    <groupId>org.quartz-scheduler</groupId>
    <artifactId>quartz</artifactId>
    <version>2.2.3</version>
</dependency>

<dependency>
    <groupId>org.quartz-scheduler</groupId>
    <artifactId>quartz-jobs</artifactId>
    <version>2.2.3</version>
</dependency>
  • Spring定时任务1
1、自定义类,继承自QuartzJobBean
/**
 * 作业类继承自特定的基类QuartzJobBean
 */
public class MyQuartz extends QuartzJobBean {

    @Override
    protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
        System.out.println("作业类继承自特定的基类QuartzJobBean");
    }

}

2、在spring-quartzjobbean.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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
                            http://www.springframework.org/schema/beans/spring-beans.xsd
                            http://www.springframework.org/schema/context
                            http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- 配置JobDetailBean -->
    <bean id="job" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
        <property name="jobClass" value="com.security.quartz.MyQuartz"></property>
    </bean>

    <!-- 配置触发器 -->
    <bean id="trigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
        <!-- 注入任务详情对象 -->
        <property name="jobDetail">
            <ref bean="job" />
        </property>
        <!-- 注入cron表达式,通过这个表达式指定触发的时间点 -->
        <property name="cronExpression">
            <value>0/5 * * * * ?</value>
        </property>
    </bean>

    <!-- 配置调度工厂 -->
    <bean id="sheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <!-- 注入触发器,可配置多个 -->
        <property name="triggers">
            <list>
                <ref bean="trigger" />
            </list>
        </property>
    </bean>

</beans>

3、将spring-quartzjobbean.xml配置文件加入applicationContext.xml中;
<!--定时任务,继承自QuartzJobBean-->
<import resource="spring-quartzjobbean.xml"/>
  • Spring定时任务2
1、自定义定时任务;
    /**
     * 石英调度任务;
     */
    public class ScheduleQuartz {

        public void run(){
            /*System.out.println("定时任务执行......");*/
        }
    }

    2、在spring-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"
        xsi:schemaLocation="http://www.springframework.org/schema/beans 
                                http://www.springframework.org/schema/beans/spring-beans.xsd
                                http://www.springframework.org/schema/context
                                http://www.springframework.org/schema/context/spring-context.xsd">

        <!-- 注册自定义作业类 -->
        <bean id="myJob" class="com.security.quartz.ScheduleQuartz"/>

        <!-- 配置JobDetail -->
        <bean id="myJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
            <!-- 注入目标对象 -->
            <property name="targetObject">
                <ref bean="myJob" />
            </property>
            <!-- 注入目标方法 -->
            <property name="targetMethod">
                <value>run</value>
            </property>
        </bean>

        <!-- 配置触发器 -->
        <bean id="trigger"
            class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
            <!-- 注入任务详情对象 -->
            <property name="jobDetail">
                <ref bean="myJobDetail" />
            </property>
            <!-- 注入cron表达式,通过这个表达式指定触发的时间点 -->
            <property name="cronExpression">
                <value>0/5 * * * * ?</value>
            </property>
        </bean>

        <!-- 配置调度工厂 -->
        <bean id="sheduler"
            class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
            <!-- 注入触发器,可配置多个 -->
            <property name="triggers">
                <list>
                    <ref bean="trigger" />
                </list>
            </property>
        </bean>

    </beans>

3、将spring-quartz.xml配置文件加入applicationContext.xml中;
<!--自定义定时任务-->
<import resource="spring-quartz.xml"/>
  • Spring定时任务3

    ①注解方式:

    1、自定义定时任务,注解方式
    /**
     * Spring中简单的定时任务,可以看做一个简单的Quartz;
     */
    @Component
    public class TaskJob {
    
        @Scheduled(cron="0/5 * * * * ?")
        public void job(){
            System.out.println("Spring中简单的定时任务,可以看做一个简单的Quartz;");
        }
    }   
    
    2、在spring-task.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:task="http://www.springframework.org/schema/task" 
        xsi:schemaLocation="http://www.springframework.org/schema/beans 
                                http://www.springframework.org/schema/beans/spring-beans.xsd
                                http://www.springframework.org/schema/context
                                http://www.springframework.org/schema/context/spring-context.xsd
                                http://www.springframework.org/schema/task 
                                http://www.springframework.org/schema/task/spring-task.xsd">
    
    
        <!-- annotation注解方式 -->
        <!-- 开启这个配置,spring才能识别@Scheduled注解   -->  
        <task:annotation-driven scheduler="scheduler" mode="proxy"/>  
        <task:scheduler id="scheduler" pool-size="10"/>
    
    </beans>
    
    3、将spring-task.xml配置文件加入applicationcontext.xml中
    <!-- Spring中的定时任务,相当于一个简单的quartz; -->
    <import resource="spring-task.xml"/>

    ②配置文件方式:

    1、自定义定时任务,配置文件方式
    /**
     * Spring中简单的定时任务,可以看做一个简单的Quartz;
     */
    public class TaskJob {
    
        public void job(){
            System.out.println("Spring中简单的定时任务,可以看做一个简单的Quartz;");
        }
    }   
    
    2、在spring-task.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:task="http://www.springframework.org/schema/task" 
        xsi:schemaLocation="http://www.springframework.org/schema/beans 
                                http://www.springframework.org/schema/beans/spring-beans.xsd
                                http://www.springframework.org/schema/context
                                http://www.springframework.org/schema/context/spring-context.xsd
                                http://www.springframework.org/schema/task 
                                http://www.springframework.org/schema/task/spring-task.xsd">
    
    
        <!-- xml配置方式 -->
        <bean id="taskjob" class="com.security.quartz.TaskJob"></bean>
    
        <task:scheduled-tasks>
            <task:scheduled ref="taskjob" method="job" cron="0/5 * * * * ?"/>
        </task:scheduled-tasks>
    
    </beans>
    
    3、将spring-task.xml配置文件加入applicationcontext.xml中
    <!-- Spring中的定时任务,相当于一个简单的quartz; -->
    <import resource="spring-task.xml"/>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值