在开发javaWeb定时任务的时候,有些处理要取得应用的相对路径,这就需要用到ServletContext取得到这个路径

解决思路是在web应用启动时,把ServletContext提前注入到Scheduler的Context中

注册一个应用启动的listener

  1. <listener>
  2. <listener-class>com.krm.slsint.common.web.listener.StartupListener</listener-class>
  3. </listener>

在listener中给scheduler加入ServletContext

需要引入:

import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.impl.StdSchedulerFactory;

  1. public class StartupListener implements ServletContextListener,HttpSessionAttributeListener,HttpSessionListener
  2. {
  3. public void contextInitialized(ServletContextEvent event)
  4. {
  5. try{
  6. Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
  7. scheduler.getContext().put("ServletContextForScheduler", event.getServletContext());
  8. event.getServletContext().setAttribute(Constants.SCHEDULER_KEY,
  9. scheduler);
  10. catch (SchedulerException e){
  11. e.printStackTrace();
  12. }
  13. }
  14. }
  15.  
  16. public void contextDestroyed(ServletContextEvent event) {
  17. Scheduler scheduler = (Scheduler) event.getServletContext().getAttribute(
  18. Constants.SCHEDULER_KEY);
  19. if (scheduler != null) {
  20. try {
  21. scheduler.shutdown(true);
  22. } catch (SchedulerException e) {
  23. if (log.isDebugEnabled()) {
  24. log.debug(e.getMessage());
  25. }
  26. }
  27. }
  28. servletContext = null;
  29. }

从JobExecutionContext中读出ServletContext,可以取得中间件路径

  1. public class BatchMailSendJob extends QuartzJobBean {
  2. @Override
  3. protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
  4. try {
  5. ServletContext servletContext = (ServletContext) context.getScheduler()
  6. .getContext().get("ServletContextForScheduler");
  7. String dir = servletContext.getRealPath(File.separator);
  8. } catch (FileNotFoundException e) {
  9. // TODO Auto-generated catch block
  10. e.printStackTrace();
  11. } catch (SchedulerException se) {
  12. // TODO Auto-generated catch block
  13. se.printStackTrace();
  14. }
  15. }
  16. }

下面是quartz的配置applicationContext-timertask.xml

  1. <beans>
  2. <bean name="mailJob" class="org.springframework.scheduling.quartz.JobDetailBean">
  3. <property name="jobClass">
  4. <value>com.krm.slsint.mail.task.BatchMailSendJob</value>
  5. </property>
  6. <property name="jobDataAsMap">
  7. <map>
  8. <entry key="timeout">
  9. <value>5</value>
  10. </entry>
  11. </map>
  12. </property>
  13. </bean>
  14. <bean id="mailCronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
  15. <property name="jobDetail">
  16. <ref bean="mailJob"/>
  17. </property>
  18. <property name="cronExpression">
  19. <value>0 0/10 9-23 * * ? </value>
  20. </property>
  21. </bean>
  22. <bean id="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
  23. <property name="triggers">
  24. <list>
  25. <ref local="mailCronTrigger"/>
  26. </list>
  27. </property>
  28. </bean>
  29. </beans>

Spring quartz中取得ServletContext的更多相关文章

  1. 分析解决 spring quartz 中出现的执行两次问题

    1. 问题描述 在开发询盘功能时,遇到一个需求,就是后台定时任务执行用电施工业务的工单下发. 使用的技术是 spring quartz,因为其他应用有先例,配置quartz 完成后,先写了一个 hel ...

  2. 解决Spring+Quartz无法自动注入bean问题

    问题 我们有时需要执行一些定时任务(如数据批处理),比较常用的技术框架有Spring + Quartz中.无奈此方式有个问题:Spring Bean无法自动注入. 环境:Spring3.2.2 + Q ...

  3. 【定时任务】Spring Boot 中如何使用 Quartz

    这篇文章将介绍如何在Spring Boot 中使用Quartz. 一.首先在 pom.xml 中添加 Quartz 依赖. <!-- quartz依赖 --> <dependency ...

  4. spring定时器中如何获取servletcontext

    spring定时器中如何获取servletcontext 学习了:https://zhidao.baidu.com/question/406212574.html @Scheduled(cron = ...

  5. spring Quartz 调度

    Quartz 是开源任务调度框架中的翘首,它提供了强大任务调度机制,同时保持了使用的简单性.Quartz 允许开发人员灵活地定义触发器的调度时间表,并可以对触发器和任务进行关联映射.此外,Quartz ...

  6. 【微信】微信获取TOKEN,以及储存TOKEN方法,Spring quartz让Token永只是期

    官网说明 access_token是公众号的全局唯一票据,公众号调用各接口时都需使用access_token.开发人员须要进行妥善保存. access_token的存储至少要保留512个字符空间.ac ...

  7. spring quartz 配置实现定时任务 详解

    一. 编写定时任务JAVA类 比如: public class QuartzJob {     public QuartzJob(){         System.out.println(" ...

  8. spring quartz分布式任务计划

    spring quartz分布式任务计划 环境: 通过maven管理的spring mvc工程,且已经成功连接数据库. 数据库表结构 /*Table structure for table `qrtz ...

  9. 基于spring+quartz的分布式定时任务框架

    问题背景 我公司是一个快速发展的创业公司,目前有200人,主要业务是旅游和酒店相关的,应用迭代更新周期比较快,因此,开发人员花费了更多的时间去更=跟上迭代的步伐,而缺乏了对整个系统的把控 没有集群之前 ...

随机推荐

  1. three.js 曲线

    上几篇说了three.js的曲线,这篇来郭先生来说说three.js曲线,在线案例点击郭先生的博客查看. 1. 了解three.js曲线 之前已经说了一些three.js的几何体,这篇说一说three ...

  2. 全栈的自我修养: 003Axios 的简单使用

    全栈的自我修养: Axios 的简单使用 You should never judge something you don't understand. 你不应该去评判你不了解的事物. 全栈的自我修养: ...

  3. 六十来行python代码完成一个文件分类器

    ​    你的桌面是否像这样的一样被各种文件给堆满了,但是每一个文件又不清楚是否后面还有作用,也不敢删除,自己一个一个转移又太麻烦了.没关系,今天我带大家用python一起来做一个文件归类器,一键进行 ...

  4. easyUI传递参数

    #======================JSP=====================================                <div class="l ...

  5. 从连接器组件看Tomcat的线程模型——NIO模式

    Tomcat8之后,针对Http协议默认使用org.apache.coyote.http11.Http11NioProtocol,也就是NIO模式.通过之前的博客分析,我们知道Connector组件在 ...

  6. JVM——内存区域:运行时数据区域详解

    关注微信公众号:CodingTechWork,一起学习进步. 引言   我们经常会被问到一个问题是Java和C++有何区别?我们除了能回答一个是面向对象.一个是面向过程编程以外,我们还会从底层内存管理 ...

  7. InceptionV4

    目录 1. inception v4 2. Inception-resnet-v1 & Inception-resnet-v2 2.1 Inception-resnet-v1的组成模块 2.2 ...

  8. 使用iOS网络请求

    https://github.com/yuantiku/YTKNetwork/blob/master/Docs/2.0_MigrationGuide_cn.md

  9. 一个startforresult的例子

    https://blog.csdn.net/qq_32521313/article/details/52451364

  10. jquery判断radio是否选中

    微交易-实体系统 微交易-虚拟系统   <div class="system"> <div class="systemt"> <l ...