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人,主要业务是旅游和酒店相关的,应用迭代更新周期比较快,因此,开发人员花费了更多的时间去更=跟上迭代的步伐,而缺乏了对整个系统的把控 没有集群之前 ...
随机推荐
- Oracle版本发布规划 (文档 ID 742060.1)
Oracle Database Release Schedule of Current Database Releases (文档 ID 742060.1) Oracle Database RoadM ...
- antd踩坑:value.locale is not a function
这个问题来源于日期选择器 RangerPicker 的特殊情况. <Col span={7} key={9}> <FormItem label="投运时间" {. ...
- Win7安装Python失败 提示Setup failed
一.安装报错 如图所示,双击Python安装包后进行安装显示Setup failed 安装失败: 二.错误排除 1.首先查看自己的计算机是否已经安装了 Win7 Service Pack 1大补丁,没 ...
- MySQL数据管理
3.MySQL数据管理 3.1外键 方式一: create table `grade`( `gradeid` int(10) not null auto_increment comment '年纪 ...
- kafka笔记——kafka启动
1. 准备 阿里云Linux服务器一个(Centos7) 腾讯云Linux服务器一个(CentOs7) zookeeper,kafka压缩包 Java环境配置好 要死....脚本之家 2. 安装 zo ...
- IO—》Properties类&序列化流与反序列化流
Properties类 介绍:Properties 类表示了一个持久的属性集.Properties 可保存在流中或从流中加载.属性列表中每个键及其对应值都是一个字符串. Properties类特点: ...
- animation动画汇总(一阶段项目)
animation 属性 动画属性: 1.animation-name:规定需要绑定到选择器的 keyframe 名称. 2.animation-duration:规定完成动画所花费的时间,以秒或毫秒 ...
- 萌新学渗透系列之Hack The Box_Devel
我将我的walkthrough过程用视频解说的形式记载 视频地址https://www.bilibili.com/video/BV1Ck4y1B7DB 一是因为看我视频的后来者应该都是刚入门的新手,视 ...
- Redis之NoSql入门和概述(二)
2. 什么是NoSQL? 2.1 NoSQL 概述 NoSQL(NoSQL = Not Only SQL ),意即“不仅仅是SQL”,泛指非关系型的数据库.随着互联网web2.0网站的兴起, ...
- Django开发之Ajax POST提交403报错
问题现象 Django开发时,前端post提交数据时,由于csrf机制,如果不做处理会报403报错 问题解决 通过在data字段中添加 csrfmiddlewaretoken: '{{ csrf_to ...