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人,主要业务是旅游和酒店相关的,应用迭代更新周期比较快,因此,开发人员花费了更多的时间去更=跟上迭代的步伐,而缺乏了对整个系统的把控 没有集群之前 ...
随机推荐
- Spring Boot 2.3.0正式发布:优雅停机、配置文件位置通配符新特性一览
当大潮退去,才知道谁在裸泳..关注公众号[BAT的乌托邦]开启专栏式学习,拒绝浅尝辄止.本文 https://www.yourbatman.cn 已收录,里面一并有Spring技术栈.MyBatis. ...
- golang第一天--安装
先上吉祥物 安装 下载链接:https://studygolang.com/dl 下载好之后开始安装 next.next.next,选择好目录.next.等待.finish. 成了!! 配置环境变量: ...
- ICP备案业务中取消接入和注销网站是什么
ICP备案业务中取消接入和注销网站是什么 之前给大家介绍了ICP备案业务中的<什么是ICP备案>.<ICP备案类型>.<ICP备案信息基本标准>.<已备案域名 ...
- Ethical Hacking - GAINING ACCESS(22)
CLIENT SIDE ATTACKS - BeEf Framework Browser Exploitation Framework allowing us to launch a number o ...
- 食用Win系统自带的PowerShell登录服务器
运行powershell输入ssh root@你的服务器ip -p你的端口 切换rm ~/.ssh/known_hosts cmd 运行 ping 你的ip -t一直ping ctrl+c停止 tra ...
- 04 CMD规范基础使用详解
CMD模块规范 1.1 CMD规范说明 专门用于浏览器端,并且模块的加载是异步的,而且只有模块使用时才会加载执行: CMD规范的语法类似于Commonjs + AMD --定义模块使用AMD语法,暴露 ...
- 小书MybatisPlus第7篇-代码生成器的原理精讲及使用方法
本文是本系列文章的第七篇,前6篇访问地址如下: 小书MybatisPlus第1篇-整合SpringBoot快速开始增删改查 小书MybatisPlus第2篇-条件构造器的应用及总结 小书Mybatis ...
- 数字麦克风PDM信号采集与STM32 I2S接口应用(四)--单片机源码
本文是数字麦克风笔记文章的单片机程序.一些朋友私信我,调试出问题. 我就把源码贴出来吧,可能主要问题是DMA的配置. 尤其双DMA时候,需要手动启动I2S的接收DMA,HAL库没有这个接口,不看dat ...
- C++的vector的使用方法
vector c++的vector的使用方法,创建,初始化,插入,删除等. #include "ex_vector.h" #include <iostream> #in ...
- 常用核心数据库查询sql
一.查询账户信息 -- 查询数据量 /*{"xdb_comment":"1","table":"mb_tran_hist" ...