package com.njhq.bagl.task;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;
import com.njhq.bagl.pojo.helper.IpZybh;
@Component
@PropertySource("classpath:cron.properties")
public class ClearYwlxMapJob {
@Scheduled(cron="${jobs.schedule}")
public void clear(){
WebApplicationContext webApplicationContext = ContextLoader.getCurrentWebApplicationContext();
ServletContext servletContext = webApplicationContext.getServletContext();
Map<String, List<IpZybh>> ywlxMap = (Map)servletContext.getAttribute("ywlxMap");
List<IpZybh> txxxbs = ywlxMap.get("txxxbs");
List<IpZybh> tjcsdjbs = ywlxMap.get("tjcsdjbs");
List<IpZybh> lshjxxbs = ywlxMap.get("lshjxxbs");
if(txxxbs == null || txxxbs.size() == 0){
System.out.println("txxxbs中沒有數據");
}else{
txxxbs.clear();
List<IpZybh> ipZybhs = ywlxMap.get("txxxbs");
if(ipZybhs == null || ipZybhs.size() == 0){
System.out.println("清除成功");
}
}
if(tjcsdjbs == null || tjcsdjbs.size() == 0){
System.out.println("tjcsdjbs中沒有數據");
}else{
tjcsdjbs.clear();
List<IpZybh> ipZybhs = ywlxMap.get("tjcsdjbs");
if(ipZybhs == null || ipZybhs.size() == 0){
System.out.println("清除成功");
}
}
if(lshjxxbs == null || lshjxxbs.size() == 0){
System.out.println("lshjxxbs中沒有數據");
}else{
lshjxxbs.clear();
List<IpZybh> ipZybhs = ywlxMap.get("lshjxxbs");
if(ipZybhs == null || ipZybhs.size() == 0){
System.out.println("清除成功");
}
}
}
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
}
通過ContextLoader獲得WebApplicationContext,然後在通過webApplicationContext獲得ServletContext
這邊文章裏面的重點還有兩個:
1.spring定時任務處理,通過註解的形式實現。
application-quratz.xml配置文件,代碼如下:
<?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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
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/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd">
<!-- 自動掃描-->
<context:component-scan base-package="com.njhq.bagl.task" />
<!--TaskExecutor提供的線程池支持也是基於jdk自帶的Executor的-->
<task:executor id="executor" pool-size="5" />
<!--調度器-->
<task:scheduler id="scheduler" pool-size="10" />
<!-- 開啓定時任務註解 -->
<task:annotation-driven executor="executor" scheduler="scheduler" />
</beans>
cron.properties屬性文件,代碼如下:
jobs.schedule = 0 0 1 * * ?
2.properties文件的解析注入。
使用@Component將該類注入到spring容器中,然後再用@PropertySource("classpath:cron.properties")註解解析文件cron.properties,如果想使用${jobs.schedule}這種方式獲取值的,就必須在該類下面添加如下代碼:
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}