JavaWEB springmvc 使用定时任务
1.配置web.xml
在web.xml配置使用springmvc框架,其他配置略。
<display-name>xxx.com</display-name>
<!-- 配置Spring MVC DispatcherServlet -->
<servlet>
<servlet-name>MVC DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 初始化参数 -->
<init-param>
<!-- 加载SpringMVC的xml到 spring的上下文容器中 -->
<param-name>contextConfigLocation</param-name>
<!-- 下面这个参数是指定springmvc的配置文件所在 -->
<param-value>classpath:com/jieli/config/springmvc-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- 配置DispatcherServlet所需要拦截的 url -->
<servlet-mapping>
<servlet-name>MVC DispatcherServlet</servlet-name>
<!-- <url-pattern>/</url-pattern> 这样配置的话,所有页面都会进入拦截器
这个在springmvc 的配置文件里还有对其进行再次配置-->
<url-pattern>/</url-pattern>
</servlet-mapping>
2.配置springmvc-context.xml
根据web.xml里面的指定配置springmvc 核心代码在第18-19行和第85-92行进行配置。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.0.xsd" > <!-- ==============基础配置 开始============= -->
<!-- 开启controller注解支持 -->
<!-- use-default-filters="false" 只扫描指定的注解 -->
<context:component-scan base-package="com.jieli.controller" use-default-filters="false">
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
</context:component-scan> <!-- 视图解析器 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="contentType" value="text/html"/>
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean> <!-- 如果当前请求为"/"时,则转发到"index" -->
<mvc:view-controller path="/" view-name="forward:index"/> <!-- 默认的注解映射的支持 -->
<mvc:annotation-driven /> <!-- 静态资源映射 不经过controller-->
<mvc:resources mapping="/js/**" location="/WEB-INF/js/" />
<mvc:resources mapping="/css/**" location="/WEB-INF/css/" />
<mvc:resources mapping="/fonts/**" location="/WEB-INF/fonts/" />
<mvc:resources mapping="/plugins/**" location="/WEB-INF/plugins/" />
<mvc:resources mapping="/images/**" location="/WEB-INF/images/" />
<mvc:resources mapping="/html/**" location="/WEB-INF/html/" />
<mvc:resources mapping="/jsp/**" location="/WEB-INF/jsp/" />
<!-- 当上面要访问的静态资源不包括在上面的配置中时,则根据此配置来访问 -->
<mvc:default-servlet-handler/> <!-- 自定义拦截器 准备用于权限管理 -->
<!-- <mvc:interceptors> -->
<!-- 如果不定义 mvc:mapping path 将拦截所有的URL请求 -->
<!-- <bean class="com.demo.web.auth.AuthInterceptor"></bean> -->
<!-- <mvc:interceptor> -->
<!-- <mvc:mapping path="/secure/*"/> -->
<!-- <bean class="*****.***Interceptor"></bean> -->
<!-- </mvc:interceptor> -->
<!-- </mvc:interceptors> --> <!-- ==============基础配置 结束============= --> <!-- =========下面的配置是一些插件化配置====== -->
<!-- 支持上传文件 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 设置上传文件的最大尺寸为1MB -->
<property name="maxUploadSize">
<value>2048576</value>
</property>
<property name="defaultEncoding">
<value>UTF-8</value>
</property>
</bean> <!-- 权限控制, 作用是让所有的请求动通过拦截器 -->
<mvc:interceptors>
<!-- 如果不定义 mvc:mapping path 将拦截所有的URL请求 -->
<bean class="com.jieli.interceptor.AuthInterceptor"></bean>
</mvc:interceptors> <!-- 增加定时任务插件 -->
<context:annotation-config></context:annotation-config>
<!-- spring 扫描注解配置 -->
<context:component-scan base-package="com.jieli.plugins.timetask">
</context:component-scan>
<!-- 开启这个配置 spring才能识别@Scheduled注解 -->
<task:annotation-driven scheduler="qbScheduler" mode="proxy"/>
<task:scheduler id="qbScheduler" pool-size="10"/> <!-- 增加邮件 -->
</beans>
3.使用注解方式
package com.jieli.plugins.timetask; import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component; @Component("taskJob")
public class TestEverySecond {
@Scheduled(cron = "1/5 * * * * ?")
public void testTask(){
System.out.println(System.currentTimeMillis());
}
}
这样就可以了,就会每5秒进行一次任务。一般做一些定时通知,定时备份,定时清理等任务。
4.出现问题及解决
这样配置完成后,发现启动服务后,这个testTask任务,都是执行两次的。查看tomcat打印的日志,发现定时任务的启动两次的,或者说整个web服务是启动两次的。以前没有这个定时任务,由于启动时日志太多,也没有报错,所以一直没有太注意。web容器启动了两次服务,就产生了两个定时器实例。经上网查询,是有两种可能,一个是spring配置出错,一个是tomcat配置出错。关于配置出错,请参看这篇文章: http://blog.csdn.net/chaijunkun/article/details/6925889 。
我是tomcat配置出错。具体出现问题如下:
默认tomcat配置文件conf/server.xml 里面Host段是这个样子的
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true"
xmlValidation="false" xmlNamespaceAware="false">
</Host>
我们把在eclipse下的项目部署到webapps文件目录下后,启动服务后,在浏览器中访问的地址是 Http://127.0.0.1:8080/<项目名称>/ 这样子进行访问。当我们不要项目名称作为访问路径,想通过ip、端口直接进行访问时,配置如下:
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true"
xmlValidation="false" xmlNamespaceAware="false"> <Context path="" docBase="/JieLiERP" debug="0" reloadable="true" /> </Host>
增加context段,进行配置。就这样一直无事,直到最近增加定时任务功能,才发现原来这样配置会出现项目启动两次的情况。也是导致定时任务被调用两次的原因。我的解决办法是 配置如下:
<Host name="localhost" appBase=""
unpackWARs="true" autoDeploy="true"
xmlValidation="false" xmlNamespaceAware="false"> <Context path="" docBase="/webapps/JieLiERP" debug="0" reloadable="true" /> </Host>
就这样解决问题。 还有Context段,tomcat 6.0 和 tomcat 7.0及以上的配置有点区别
<!-- tomcat 6.0 -->
<Context path="" docBase="/webapps/JieLiERP" debug="0" reloadable="true" /> <!-- tomcat 7.0及以上 -->
<Context path="/" docBase="webapps/JieLiERP" debug="0" reloadable="true" />
本文地址: http://www.cnblogs.com/wunaozai/p/5026765.html
JavaWEB springmvc 使用定时任务的更多相关文章
- SpringMVC中定时任务配置
在项目中使用定时任务是常有的事,比如每天定时进行数据同步或者备份等等. 以前在从事C语言开发的时候,定时任务都是通过写个shell脚本,然后添加到linux定时任务中进行调度的. 现在使用Spring ...
- springmvc添加定时任务
springmvc.xml文件中添加如下配置 <bean id="ClearTempRoomLogTask" class="com.test.listener.St ...
- springMvc 添加定时任务
1.创建定时类 import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stere ...
- Springmvc配置定时任务注解开发
1.添加命名空间和xsd约束 xmlns:task="http://www.springframework.org/schema/task" http://www.springfr ...
- springMVC定时任务总是执行两次
情况: springmvc的定时任务在本机上测试的时候没有问题,但是放到测试服务器上的时候总是执行两次: 探索:(网上搜索) 一.spring注入的时候实例化了多次,说是spring-servlet. ...
- springmvc 学习笔记
@Autowired,@RequestMapping,@RequestParam 使用该注解,引入对象时, 可以省略setter getter.减少代码显示. @AutowiredSimService ...
- spring计划任务,springMvc计划任务,Spring@Scheduled,spring定时任务
spring计划任务,springMvc计划任务,Spring@Scheduled,spring定时任务 >>>>>>>>>>>> ...
- javaweb项目-医者天下 (Spring+SpringMVC+MyBatis)
项目下载地址:http://download.csdn.net/detail/qq_33599520/9826683 项目完整结构图: 项目简介: 医者天下项目是一个基于Spring+SpringMV ...
- Java SpringMVC 定时任务
1.web.xml 2.spring-mvc.xml <?xml version="1.0" encoding="UTF-8"?> <bean ...
随机推荐
- 各个版本VS编译好的GDAL库下载
简单说明 自己编译GDAL比较繁琐且时间较长,而且很多 Driver 需要自己去编译.如果不是要学习怎么编译GDAL,可以直接使用已经编译好的库. OSGeo官方没有提供编译好的GDAL,但是它有给出 ...
- 转 Linux定时执行任务命令at和crontab
本文介绍在Linux下的两种定时执行任务的方法:at命令,以及crontab服务. (1)at命令 假如我们只是想要让特定任务运行一次,那么,这时候就要用到at监控程序了. 设置at命令很简单,指示定 ...
- UVM:8.4.3 用factory 机制创建实例的接口
1.create_object_by_name,依据类名字创建object,原型: 一般仅仅用第一个: 2.create_object_by_type.依据类型创建一个object,原型: 一般仅仅用 ...
- EntityFramework 5.0 CodeFirst 教程01-搭建环境和快速上手
----------------------------目录------------------------------ EntityFramework 5.0 CodeFirst 教程03-数据结构 ...
- 阿里云k8s服务springboot项目应用升级时出现502错误
背景 随着小步快跑.快速迭代的开发模式被越来越多的互联网企业认同和采用,应用的变更.升级频率变得越来越频繁.为了应对不同的升级需求,保证升级过程平稳顺利地进行,诞生了一系列的部署发布模式. 停机发布 ...
- [转]极不和谐的 fork 多线程程序
极不和谐的 fork 多线程程序 继续前几天的话题.做梦幻西游服务器优化的事情.以往的代码,定期存盘的工作分两个步骤,把 VM 里的动态数据序列化,然后把序列化后的数据写盘.这两个步骤,序列化工作并没 ...
- google开发新人入职100天,聊聊自己的经验&教训 个人对编程和开发的理解 技术发展路线
新人入职100天,聊聊自己的经验&教训 这篇文章讲了什么? 如题,本屌入职100天之后的经验和教训,具体包含: 对开发的一点感悟. 对如何提问的一点见解. 对Google开发流程的吐槽. 如果 ...
- ceph iscsi (SCST)
ceph结合iscsi iscsi Target 安装 1.安装SCST tar -jxf scst-3.0.1.tar.bz2 cd scst-3.0.1 make && make ...
- int和Integer之间的区别和联系
在工作中使用==埋下的坑这篇博文中,我们看到当使用基本类型的时候==是完全没有问题的,部分或者混合使用基本类型和装箱基本类型的时候,就可能出现问题了,那么我们可能会想基本类型和装箱基本类型 ...
- 【Algorithm】自顶向下的归并排序
一. 算法描述 自顶向下的归并排序:采用分治法进行自顶向下的程序设计方式,分治法的核心思想就是分解.求解.合并. 先将长度为N的无序序列分割平均分割为两段 然后分别对前半段进行归并排序.后半段进行归并 ...