Spring quartz中取得ServletContext
在开发javaWeb定时任务的时候,有些处理要取得应用的相对路径,这就需要用到ServletContext取得到这个路径
解决思路是在web应用启动时,把ServletContext提前注入到Scheduler的Context中
注册一个应用启动的listener
- <listener>
- <listener-class>com.krm.slsint.common.web.listener.StartupListener</listener-class>
- </listener>
在listener中给scheduler加入ServletContext
需要引入:
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.impl.StdSchedulerFactory;
- public class StartupListener implements ServletContextListener,HttpSessionAttributeListener,HttpSessionListener
- {
- public void contextInitialized(ServletContextEvent event)
- {
- try{
- Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
- scheduler.getContext().put("ServletContextForScheduler", event.getServletContext());
- event.getServletContext().setAttribute(Constants.SCHEDULER_KEY,
- scheduler);
- catch (SchedulerException e){
- e.printStackTrace();
- }
- }
- }
- public void contextDestroyed(ServletContextEvent event) {
- Scheduler scheduler = (Scheduler) event.getServletContext().getAttribute(
- Constants.SCHEDULER_KEY);
- if (scheduler != null) {
- try {
- scheduler.shutdown(true);
- } catch (SchedulerException e) {
- if (log.isDebugEnabled()) {
- log.debug(e.getMessage());
- }
- }
- }
- servletContext = null;
- }
从JobExecutionContext中读出ServletContext,可以取得中间件路径
- public class BatchMailSendJob extends QuartzJobBean {
- @Override
- protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
- try {
- ServletContext servletContext = (ServletContext) context.getScheduler()
- .getContext().get("ServletContextForScheduler");
- String dir = servletContext.getRealPath(File.separator);
- } catch (FileNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (SchedulerException se) {
- // TODO Auto-generated catch block
- se.printStackTrace();
- }
- }
- }
下面是quartz的配置applicationContext-timertask.xml
- <beans>
- <bean name="mailJob" class="org.springframework.scheduling.quartz.JobDetailBean">
- <property name="jobClass">
- <value>com.krm.slsint.mail.task.BatchMailSendJob</value>
- </property>
- <property name="jobDataAsMap">
- <map>
- <entry key="timeout">
- <value>5</value>
- </entry>
- </map>
- </property>
- </bean>
- <bean id="mailCronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
- <property name="jobDetail">
- <ref bean="mailJob"/>
- </property>
- <property name="cronExpression">
- <value>0 0/10 9-23 * * ? </value>
- </property>
- </bean>
- <bean id="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
- <property name="triggers">
- <list>
- <ref local="mailCronTrigger"/>
- </list>
- </property>
- </bean>
- </beans>
Spring quartz中取得ServletContext的更多相关文章
- 分析解决 spring quartz 中出现的执行两次问题
1. 问题描述 在开发询盘功能时,遇到一个需求,就是后台定时任务执行用电施工业务的工单下发. 使用的技术是 spring quartz,因为其他应用有先例,配置quartz 完成后,先写了一个 hel ...
- 解决Spring+Quartz无法自动注入bean问题
问题 我们有时需要执行一些定时任务(如数据批处理),比较常用的技术框架有Spring + Quartz中.无奈此方式有个问题:Spring Bean无法自动注入. 环境:Spring3.2.2 + Q ...
- 【定时任务】Spring Boot 中如何使用 Quartz
这篇文章将介绍如何在Spring Boot 中使用Quartz. 一.首先在 pom.xml 中添加 Quartz 依赖. <!-- quartz依赖 --> <dependency ...
- spring定时器中如何获取servletcontext
spring定时器中如何获取servletcontext 学习了:https://zhidao.baidu.com/question/406212574.html @Scheduled(cron = ...
- spring Quartz 调度
Quartz 是开源任务调度框架中的翘首,它提供了强大任务调度机制,同时保持了使用的简单性.Quartz 允许开发人员灵活地定义触发器的调度时间表,并可以对触发器和任务进行关联映射.此外,Quartz ...
- 【微信】微信获取TOKEN,以及储存TOKEN方法,Spring quartz让Token永只是期
官网说明 access_token是公众号的全局唯一票据,公众号调用各接口时都需使用access_token.开发人员须要进行妥善保存. access_token的存储至少要保留512个字符空间.ac ...
- spring quartz 配置实现定时任务 详解
一. 编写定时任务JAVA类 比如: public class QuartzJob { public QuartzJob(){ System.out.println(" ...
- spring quartz分布式任务计划
spring quartz分布式任务计划 环境: 通过maven管理的spring mvc工程,且已经成功连接数据库. 数据库表结构 /*Table structure for table `qrtz ...
- 基于spring+quartz的分布式定时任务框架
问题背景 我公司是一个快速发展的创业公司,目前有200人,主要业务是旅游和酒店相关的,应用迭代更新周期比较快,因此,开发人员花费了更多的时间去更=跟上迭代的步伐,而缺乏了对整个系统的把控 没有集群之前 ...
随机推荐
- three.js 曲线
上几篇说了three.js的曲线,这篇来郭先生来说说three.js曲线,在线案例点击郭先生的博客查看. 1. 了解three.js曲线 之前已经说了一些three.js的几何体,这篇说一说three ...
- 全栈的自我修养: 003Axios 的简单使用
全栈的自我修养: Axios 的简单使用 You should never judge something you don't understand. 你不应该去评判你不了解的事物. 全栈的自我修养: ...
- 六十来行python代码完成一个文件分类器
你的桌面是否像这样的一样被各种文件给堆满了,但是每一个文件又不清楚是否后面还有作用,也不敢删除,自己一个一个转移又太麻烦了.没关系,今天我带大家用python一起来做一个文件归类器,一键进行 ...
- easyUI传递参数
#======================JSP===================================== <div class="l ...
- 从连接器组件看Tomcat的线程模型——NIO模式
Tomcat8之后,针对Http协议默认使用org.apache.coyote.http11.Http11NioProtocol,也就是NIO模式.通过之前的博客分析,我们知道Connector组件在 ...
- JVM——内存区域:运行时数据区域详解
关注微信公众号:CodingTechWork,一起学习进步. 引言 我们经常会被问到一个问题是Java和C++有何区别?我们除了能回答一个是面向对象.一个是面向过程编程以外,我们还会从底层内存管理 ...
- InceptionV4
目录 1. inception v4 2. Inception-resnet-v1 & Inception-resnet-v2 2.1 Inception-resnet-v1的组成模块 2.2 ...
- 使用iOS网络请求
https://github.com/yuantiku/YTKNetwork/blob/master/Docs/2.0_MigrationGuide_cn.md
- 一个startforresult的例子
https://blog.csdn.net/qq_32521313/article/details/52451364
- jquery判断radio是否选中
微交易-实体系统 微交易-虚拟系统 <div class="system"> <div class="systemt"> <l ...