@Scheduled Spring定时任务每次执行两次解决方案
来源:互联网 发布:网络基础设施建设问题 编辑:程序博客网 时间:2024/06/06 16:36
在执行定时任务的时候我遇到了一种情况,一个定时器每次会被执行两次,这就让人有点困惑,猜测肯定是哪里配置出了问题(配了两个当前上下文情况),看了一下网上很多解释,没有找到与自己相匹配的情况,所以自己也不断尝试修改配置文件。终于找到错误原因,这里把错误代码和原因贴出来方便与我有相同困惑的朋友参考。
首先错误的日志输出如下图:
在我们的web.xml文件中,我们有对spring整合配置,比如配置监听器,上下文加载对象,拦截器,会话的管理等等。
<!-- The Bootstrap listener to start up and shut down Spring's root WebApplicationContext. It is registered to Servlet Container --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <listener> <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value> </context-param> <servlet> <servlet-name>mvc-dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> <async-supported>true</async-supported> </servlet> <servlet-mapping> <servlet-name>mvc-dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
上面是我对监听器Listener和上下文对象contextConfigLocation和DispatcherServlet的配置, 出错原因就是没有设置contextConfigLocation为空,导致加载了两次配置文件,所以就有两个操作的上下文的session出现。把上面的代码添加一段即可:
<!-- The Bootstrap listener to start up and shut down Spring's root WebApplicationContext. It is registered to Servlet Container --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <listener> <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value> </context-param> <servlet> <servlet-name>mvc-dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!--加上了这个 定时器就不会同时执行两次了--> <init-param> <param-name>contextConfigLocation</param-name> <param-value></param-value> </init-param> <load-on-startup>1</load-on-startup> <async-supported>true</async-supported> </servlet> <servlet-mapping> <servlet-name>mvc-dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
重新运行程序,log日志输出结果:
阅读全文
0 0
- @Scheduled Spring定时任务每次执行两次解决方案
- Spring的定时任务@Scheduled,部署后执行加载两次
- spring 定时任务执行两次 解决方案
- spring-task定时任务执行两次解决方案
- spring 定时任务执行两次
- spring 定时任务执行两次
- 使用spring @Scheduled注解执行定时任务、
- 使用spring @Scheduled注解执行定时任务、
- 使用spring @Scheduled注解执行定时任务
- 使用spring @Scheduled注解执行定时任务
- 使用spring @Scheduled注解执行定时任务
- 使用spring @Scheduled注解执行定时任务、
- 使用spring @Scheduled注解执行定时任务
- 使用spring @Scheduled注解执行定时任务
- 使用spring @Scheduled注解执行定时任务
- 使用spring @Scheduled注解执行定时任务
- 使用spring @Scheduled注解执行定时任务
- 使用spring @Scheduled注解执行定时任务、
- 认证机构业务管理信息系统、项目流程管理软件
- 不借助第三个变量交换两个整数
- 【每周论文】Graphene: Packing and Dependency-aware Scheduling for Data-Parallel Clusters(OSDI 2016)
- Linux功耗管理(4)_Power Management Interface
- Java笔记:数组
- @Scheduled Spring定时任务每次执行两次解决方案
- [mob]Android手机短信验证(自定义的)
- python数据结构-栈
- ubuntu16.04安装pycharm
- LocalAlloc,VirtualAlloc,malloc,new的异同
- python基础--数据类型之间的转换
- Hadoop2.6.0安装教程
- Java模板引擎之 FreeMarker
- Spring MVC小案例