diff --git a/src/main/java/com/nis/quartz/ScheduleStatisticASNIPNumJob.java b/src/main/java/com/nis/quartz/ScheduleStatisticASNIPNumJob.java new file mode 100644 index 000000000..c28a67328 --- /dev/null +++ b/src/main/java/com/nis/quartz/ScheduleStatisticASNIPNumJob.java @@ -0,0 +1,137 @@ +package com.nis.quartz; + + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; + +import org.apache.ibatis.session.ExecutorType; +import org.apache.ibatis.session.SqlSession; +import org.apache.ibatis.session.SqlSessionFactory; +import org.apache.log4j.Logger; +import org.quartz.DisallowConcurrentExecution; +import org.quartz.Job; +import org.quartz.JobExecutionContext; +import org.quartz.JobExecutionException; +import org.quartz.PersistJobDataAfterExecution; + +import com.nis.util.IPUtil; +import com.nis.util.StringUtil; +import com.nis.web.dao.basics.AsnGroupInfoDao; +import com.nis.web.dao.basics.AsnIpCfgDao; +import com.nis.web.service.SpringContextHolder; + +/** + * 定时任务: 定时统计ASN 下IPv4和IPV6的个数 + * 1、每 n s 执行一次(两个小时执行一次) + * 2、单线程执行 + * @author ddm + * + */ +@DisallowConcurrentExecution +@PersistJobDataAfterExecution +public class ScheduleStatisticASNIPNumJob implements Job { + private static final Logger logger = Logger.getLogger(ScheduleStatisticASNIPNumJob.class); + + @Override + public void execute(JobExecutionContext context) throws JobExecutionException { + AsnIpCfgDao asnIpCfgDao = SpringContextHolder.getBean(AsnIpCfgDao.class); + logger.info("定时统计ASN IP个数开始。。。。。。。。。。。。。。"); + long start=System.currentTimeMillis(); + Map> asnNumerMap=new HashMap<>(); + List asnIpList=new ArrayList<>(); + asnIpList=asnIpCfgDao.findAllAsnIpCfgList(); + + getAllASNIPNumber(asnIpList, asnNumerMap); + + updateAllASNIPNumber(asnNumerMap); + + long end=System.currentTimeMillis(); + logger.info("定时统计ASN IP个数结束:耗时("+(end-start)+"/毫秒)。。。。。。。。。。。。。。"); + } + + public void getAllASNIPNumber(List asnIpList,Map> asnNumerMap){ + logger.info("计算ASN IP开始:asn ip size:"+asnIpList.size()+"。。。。。。。。。。。。。。"); + long start=System.currentTimeMillis(); + if(!StringUtil.isEmpty(asnIpList)){ + try { + for (Iterator iterator = asnIpList.iterator(); iterator.hasNext();) { + Map asnIpMap=(Map) iterator.next(); + //groupId + Integer asnGroupId = (Integer)asnIpMap.get("asn_ip_group"); + //ipType(4:v4,6:v6) + Integer ipType= (Integer)asnIpMap.get("ip_type"); + //(1:mask,2:range,3:ip) + //Integer ipPattern= (Integer)asnIpMap.get("ip_pattern"); + //dest_ip_address + String ipAddress= (String)asnIpMap.get("dest_ip_address"); + long IPNumber=0; + if(ipType.equals(4)){ + if(ipAddress.indexOf("/") > -1){ + Integer mask=Integer.parseInt(ipAddress.split("/")[1]); + ipAddress=ipAddress.split("/")[0]; + IPNumber=IPUtil.getIpNum(ipAddress, mask); + }else{ + IPNumber=1; + } + //判断组是否已经存在 + if(asnNumerMap.keySet().contains(asnGroupId)){ + asnNumerMap.get(asnGroupId).put("v4", asnNumerMap.get(asnGroupId).get("v4")+IPNumber); + asnNumerMap.put(asnGroupId, asnNumerMap.get(asnGroupId)); + }else{ + Map map=new HashMap<>(); + map.put("v4", IPNumber); + map.put("v6", 0l); + asnNumerMap.put(asnGroupId, map); + } + }else{ + IPNumber=1; + //判断组是否已经存在 + if(asnNumerMap.keySet().contains(asnGroupId)){ + asnNumerMap.get(asnGroupId).put("v6", asnNumerMap.get(asnGroupId).get("v6")+IPNumber); + asnNumerMap.put(asnGroupId, asnNumerMap.get(asnGroupId)); + }else{ + Map map=new HashMap<>(); + map.put("v4", 0l); + map.put("v6", IPNumber); + asnNumerMap.put(asnGroupId, map); + } + } + } + } catch (Exception e) { + logger.error("计算ASN IP个数失败",e); + } + } + + long end=System.currentTimeMillis(); + logger.info("计算ASN IP个数结束:耗时("+(end-start)+"/毫秒)。。。。。。。。。。。。。。"); + } + public void updateAllASNIPNumber(Map> asnNumerMap) { + logger.info("修改ASN IP个数开始:asn size:"+asnNumerMap.size()+"。。。。。。。。。。。。。。"); + long start=System.currentTimeMillis(); + if(!StringUtil.isEmpty(asnNumerMap)){ + int index=0; + SqlSessionFactory sqlSessionFactory=SpringContextHolder.getBean(SqlSessionFactory.class); + SqlSession batchSqlSession = null; + try { + batchSqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, true); + for(Entry> e: asnNumerMap.entrySet()) { + ((AsnGroupInfoDao) batchSqlSession.getMapper(AsnGroupInfoDao.class)).updateIpNum(e.getValue().get("v4"),e.getValue().get("v6"),e.getKey()); + batchSqlSession.commit(); + index++; + } + }catch (Exception e) { + logger.error("修改ASN IP个数失败", e); + } finally { + if(batchSqlSession != null){ + batchSqlSession.close(); + } + } + } + long end=System.currentTimeMillis(); + logger.info("修改ASN IP个数结束:耗时("+(end-start)+"/毫秒)。。。。。。。。。。。。。。"); + } +} diff --git a/src/main/resources/applicationContext-quartz.xml b/src/main/resources/applicationContext-quartz.xml index e597748a0..00485961a 100644 --- a/src/main/resources/applicationContext-quartz.xml +++ b/src/main/resources/applicationContext-quartz.xml @@ -16,7 +16,7 @@ http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd" default-lazy-init="true"> - + @@ -35,7 +35,7 @@ - + @@ -51,8 +51,22 @@ + - + + + + + + + + + + + + + + @@ -64,6 +78,7 @@ + diff --git a/src/main/resources/spring-mvc.xml b/src/main/resources/spring-mvc.xml index 83926c54b..8e1808b75 100644 --- a/src/main/resources/spring-mvc.xml +++ b/src/main/resources/spring-mvc.xml @@ -104,10 +104,6 @@ - - - -