Spring与Quartz集成实现定时调度任务的简单使用

在前面一篇文章中,简单的讲解了一下Quartz的基本用法,这篇文章继续上篇文章未完成的任务,Spring与Quartz集成。

主要涉及的知识点有Spring,Quartz,ServletContextListener

举一个最最最简单的入门例子:

一、maven引入相关依赖

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>3.2.8.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>3.2.8.RELEASE</version>
        </dependency>


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

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>3.2.8.RELEASE</version>
        </dependency>

二、Spring配置文件

<?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"
    xmlns:task="http://www.springframework.org/schema/task"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-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/task  
	  http://www.springframework.org/schema/task/spring-task-3.2.xsd
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd">
	
	<!-- 引入定时任务工厂类 -->
	<bean id="schedulerFactoryBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"></bean>

</beans> 

三、在web.xml配置文件中初始化Spring

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         version="2.5"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <display-name>core_paltform</display-name>

  <!--初始化spring-->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

  <listener>
    <listener-class>com.jr.test.ProjectInit</listener-class>
  </listener>
  </web-app>

四、定义任务类

@Component("TestJob")
public class TestJob implements Job{
    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        System.out.println("嗯嘛,么么哒…");
    }
}

五、定义一个类实现ServletContextListenner方法

同时记得在web.xml中配置该类

public class ProjectInit implements ServletContextListener {

    private static WebApplicationContext context;

    private Scheduler scheduler;

    public void contextInitialized(ServletContextEvent servletContextEvent) {
        /**
         * 获取上下文
         */
        context = WebApplicationContextUtils.getWebApplicationContext(servletContextEvent.getServletContext());
        scheduler = context.getBean("schedulerFactoryBean", Scheduler.class);
        try {
            JobDetail jobDetail = JobBuilder.newJob(TestJob.class).withIdentity("TestJob", "TestJob").build();
            //表达式调度构建器
            CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(" 0 0/1 * * * ?"); //一分钟执行一次
            CronTrigger trigger = TriggerBuilder.newTrigger().withIdentity("TestJob", "TestJob").withSchedule(scheduleBuilder).build();
            scheduler.scheduleJob(jobDetail, trigger);
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }

    public void contextDestroyed(ServletContextEvent servletContextEvent) {

    }
}


上面的例子利用Spring的定时任务工厂类与Quartz实现定时任务的最基本用户,后续将继续研究更高级的使用。

                 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值