java定时器任务中获取request对象 @Scheduled 获取request 对象 quartz中获取request对象
java定时器任务中获取request对象 @Scheduled 获取request 对象 quartz中获取request对象
一、问题描述
1、在java定时任务中,使用 @Scheduled 注解来实现;遇到一个需要通过 request 对象获取相对路径的问题,尝试过过 静态获取 和 使用 直接注入HttpServletRequest对象 的办法,都没有解决问题,会导致抛出异常如下:
ERROR [org.springframework.scheduling.support.TaskUtils$LoggingErrorHandler] - Unexpected error occurred in scheduled task.
2、使用 @Autowired 注入 HttpServletRequest ,会抛出如下异常:
ava.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.at org.springframework.web.context.request.RequestContextHolder.currentRequestAttributes(RequestContextHolder.java:131)at org.springframework.web.context.support.WebApplicationContextUtils.currentRequestAttributes(WebApplicationContextUtils.java:273)
二、问题解决
1、通过 ContextLoader 获取 getCurrentWebApplicationContext 方法解决。
(SSM 架构的,非 Spring Boot的 2022年9月21日22:36:03)
WebApplicationContext webApplicationContext = ContextLoader.getCurrentWebApplicationContext();ServletContext servletContext = webApplicationContext.getServletContext();String realPath = servletContext.getRealPath(File.separator);
( org.springframework.web.context.ContextLoader )
三、补充说明
1、评论中,大家反馈的 空指针 情况, 获取到 WebApplicationContext 对象为空情况,经过测试,是在Spring Boot项目中,会出现这种情况,经常测试,简单解决办法是,直接 注入ServletContext 对象即可。
@Slf4j
@Component
public class Task {@Autowiredprivate ServletContext servletContext2;@Autowiredprivate HttpServletRequest request;@Scheduled(cron = "0/10 * * * * ? ")public void task(){Thread th = Thread.currentThread();log.warn("start==={}",th.getName());/*** spring boot 项目下: WebApplicationContext 为空。* 需要手动注入:servletContext2 即可*/
// WebApplicationContext web = ContextLoader.getCurrentWebApplicationContext();
// ServletContext servletContext = web.getServletContext();String realPath = servletContext2.getRealPath(File.separator);log.warn("end === realPath {}",realPath);}
}
2、原项目环境是:SSM 架构的,非 Spring Boot的, 使用的 Spring 版本为 4.3 。
3、本文表述有一点误解, 定时任务中,无法获取到 request 对象的, 是可以获取到 servlet的上下文对象ServletContext 的。
因未描述清楚项目环境,导致影响了大家解决问题的速度, 本人深表歉意 。
2022年9月21日22:35:43
java定时器任务中获取request对象 @Scheduled 获取request 对象 quartz中获取request对象相关推荐
- java定时器的使用
java定时器的使用(Timer) 1.在应用开发中,经常需要一些周期性的操作,比如每5分钟执行某一操作等. 对于这样的操作最方便.高效的实现方式就是使用java.util.Timer工具类. pri ...
- java 从一个容器获取对象,如何从 Spring IoC 容器中获取对象?
前面几篇文章主要分析了 Spring IoC 容器如何初始化,以及解析和注册我们定义的 bean 信息. 其中,「Spring 中的 IoC 容器」对 Spring 中的容器做了一个概述,「Sprin ...
- springboot中的拦截器interceptor和过滤器filter,多次获取request参数
大家好,我是烤鸭: 这是一篇关于springboot的拦截器(interceptor)和过滤器(Filter). 先说一下过滤器和拦截器. 区别: 1. servlet请求,顺序:Filter ...
- JSP九大内置对象的分析,对应SERVLET中如何获取、使用
首先,JSP是javaweb服务器端的动态资源,可动态显示网页,本质上其实就是SERVLET. 一个JSP文件,在第一次访问时,服务器会先将jsp翻译成java文件,也就是一个servlet,然后该j ...
- java定时器停止_java 定时器中任务的启动、停止、再启动
package com.cvicse.ump.timer.service; import java.util.Date; import java.util.Timer; import com.cvic ...
- java获取列族的列_在cassandra-cli中如何获取表中的所有列名以及如何在java中使用hector获取它?...
我正在尝试获取列名,但无法获得仅列名称. 在cli我执行命令描述表节点,它返回结果: CREATE TABLE nodes ( key text PRIMARY KEY, id text, score ...
- 如何优雅的创建一个Java不可变对象类,JDK源码中也是这么干的!
前面有篇文章当介绍了Java的不可变对象的一些特性,以及它的一些好处,但是并没有介绍如何实现一个不可变对象类.今天就来看看如何实现一个不可变对象类. Java中常用的不可变对象类 String类应该是 ...
- Java中创建对象的六个步骤 细分后(new关键字)对象头详细介绍
要看的懂对象的创建过程,首先你得有对Java虚拟机和Java基础以及JUC很是熟悉, 比如类的加载过程.CAS.多线程.JVM的GC等等 文章目录 一.创建对象的六个步骤: 二.对象头的内部结构 首先 ...
- Java取对象中年龄最大的那个_Java8怎么从流中获取最大值、最小值
继续来写Java8开始的流式编码. 今天要写的是怎么从流中获取最大值.最小值. 为了方便没有看过前面文章的朋友,用到的类和数据,还是贴出来一下. 上面准备了一组Student对象组合的List集合. ...
最新文章
- 网页中如何启用QQ交谈
- statemodels 笔记: lowess
- LeetCode-二分查找-35. 搜索插入位置
- Java基础day11
- 跟益达学Solr5之solrconfig.xml配置详解
- Scala-2.13.0 安装及配置
- React开发(204):react代码分割之路由懒加载
- HDU 4857 逃生(拓扑排序)
- html5怎么设置li向下移,请问如何实现鼠标移到li上改变另一个div的样式
- JavaScript字符集编码与解码
- Webix JavaScript UI 新版得心应手
- 最大流算法之Dinic
- 深度可分离卷积(Depthwise seperable convolution)
- 漏洞扫描工具Nessus的下载与安装教程
- 云计算服务包括哪三种服务?怎么定义?
- iOS:开放平台引用(二)--微信分享
- python你好怎么写_Python基础01之“你好世界”
- 异常:java.lang.LinkageError: loader constraint violation: when resolving interface method javax.servl
- 深度学习--深度信念网络(Deep Belief Network)
- C++:C++编程语言学习之数学运算运算符及其优先级的简介、案例应用之详细攻略