spring定时器详解

时间:2022-01-15 00:09:56

1,配置spring定时器,

  第一步: 引入jar

<!--quartz配置 -->
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.2.1</version>
</dependency>

第二部: 配置web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-base.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--加上了这个 定时器就不会同时执行两次了 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value></param-value>
</init-param>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
<async-supported>true</async-supported>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>


<!-- 配置监听器:保证在web应用关闭的时候释放与掉这个web应用相关的class loader和由它管理的类 -->
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
<listener>
<listener-class>com.init.StartInit</listener-class>
</listener>

<welcome-file-list>

<welcome-file>index.jsp</welcome-file>

</welcome-file-list>
</web-app>

第三步: 配置spring-mvc.xml,必须在spring-mvc.xml中加上此配置

<!-- 开启这个配置,spring才能识别@Scheduled注解 -->
<task:annotation-driven scheduler="qbScheduler" mode="proxy" />
<task:scheduler id="qbScheduler" pool-size="10" />

第四步: 在spring可以扫描的包下,创建定时器类

@Component
public class DataQuartz {

@Autowired
private DataService dataServise;

private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
private SimpleDateFormat sdfDay = new SimpleDateFormat("d");

// "0 0 12 * * ?" 每天中午12点触发
// "0 15 10 ? * *" 每天上午10:15触发
// "0 15 10 * * ?" 每天上午10:15触发
// "0 15 10 * * ? *" 每天上午10:15触发
// "0 15 10 * * ? 2005" 2005年的每天上午10:15触发
// "0 * 14 * * ?" 在每天下午2点到下午2:59期间的每1分钟触发
// "0 0/5 14 * * ?" 在每天下午2点到下午2:55期间的每5分钟触发
// "0 0/5 14,18 * * ?" 在每天下午2点到2:55期间和下午6点到6:55期间的每5分钟触发
// "0 0-5 14 * * ?" 在每天下午2点到下午2:05期间的每1分钟触发
// "0 10,44 14 ? 3 WED" 每年三月的星期三的下午2:10和2:44触发
// "0 15 10 ? * MON-FRI" 周一至周五的上午10:15触发
// "0 15 10 15 * ?" 每月15日上午10:15触发
// "0 15 10 L * ?" 每月最后一日的上午10:15触发
// "0 15 10 ? * 6L" 每月的最后一个星期五上午10:15触发
// "0 15 10 ? * 6L 2002-2005" 2002年至2005年的每月的最后一个星期五上午10:15触发
// "0 15 10 ? * 6#3" 每月的第三个星期五上午10:15触发
// 每隔5秒执行一次:*/5 * * * * ?
// 每隔1分钟执行一次:0 */1 * * * ?
// 每天23点执行一次:0 0 23 * * ?
// 每天凌晨1点执行一次:0 0 1 * * ?
// 每月1号凌晨1点执行一次:0 0 1 1 * ?
// 每月最后一天23点执行一次:0 0 23 L * ?
// 每周星期天凌晨1点实行一次:0 0 1 ? * L

@Scheduled(cron="0 0 1 * * ?") //每天凌晨1点执行一次
public void writAllDataExcel() {
        //在这里面就可以常见自己的定时代码
       }
 }     
 
 
 
 

 

注:

   1,  Scheduled 是一步起线程来处理自己的业务逻辑的,所以在定时器方法中,不可以使用HttpServletRequest request和HttpServletResponse response,并且在参数中都不可以

        使用

    2, 在webxml中必须添加定时器空配置,否则会导致两次进入定时方法(在引入spring配置文件时,不要使用spring-*.xml的方式,这样会吧spring-mvc.xml重复引入,这是造成两次进 

        入定时任务的原因)