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();
}