Spring与Quartz集成实现定时调度任务的简单使用
2017-09-04 21:59
881 查看
在前面一篇文章中,简单的讲解了一下Quartz的基本用法,这篇文章继续上篇文章未完成的任务,Spring与Quartz集成。
主要涉及的知识点有Spring,Quartz,ServletContextListener
举一个最最最简单的入门例子:
一、maven引入相关依赖
二、Spring配置文件
三、在web.xml配置文件中初始化Spring
四、定义任务类
五、定义一个类实现ServletContextListenner方法
同时记得在web.xml中配置该类
上面的例子利用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实现定时任务的最基本用户,后续将继续研究更高级的使用。
相关文章推荐
- Spring与Quartz集成配置实现简单定时任务调度
- Spring与Quartz集成配置实现简单定时任务调度
- 使用Spring + quartz实现定时任务调度
- 使用quartz+spring实现简单的可配置化的定时任务(可和数据库配套使用)
- 如何使用spring 定时调度 【 Spring+Quartz实现定时任务 】
- Quartz 集成Spring实现简单的任务调度Demo
- 使用Spring + quartz实现定时任务调度
- 使用Spring来实现任务计划服务三:集成quartz任务调度框架
- 使用Quartz2.1.6+Spring3.0实现任务调度简单示例
- spring使用Quartz实现定时任务
- Spring整合Quartz实现定时任务调度
- Spring和Quartz集成实现定时任务
- Spring--简单使用quartz实现定时作业
- 简单介绍Spring+Quartz实现定时任务的配置方法
- Spring--简单使用quartz实现定时作业
- Quartz 框架和cron表达式任务调度的例子(spring下实现定时发送邮件)
- Spring和Quartz实现的定时任务调度
- Spring--简单使用quartz实现定时作业
- 使用Quartz2.1.6+Spring3.0实现任务调度简单示例
- Quartz + Spring 简单实现定时任务