增加定时器功能

This commit is contained in:
zhangwei
2019-01-26 18:02:44 +06:00
parent 0fb4bf08a4
commit f90ef5ab85
47 changed files with 5923 additions and 356 deletions

View File

@@ -0,0 +1,76 @@
package com.nis.web.dao;
import java.util.List;
import org.apache.ibatis.annotations.Param;
import com.nis.domain.ScheduleCfg;
import com.nis.domain.ScheduleExceInfo;
import com.nis.domain.configuration.BaseCfg;
@MyBatisDao
public interface SchedulerDao extends CrudDao<ScheduleCfg> {
List<ScheduleCfg> findScheduleList(ScheduleCfg cfg);
/**
* 查找最新的修改数据
* @param id
* @param size
* @return
*/
List<ScheduleCfg> findNewlyCfg(@Param("id")Long id,@Param("limit")Long limit);
/**
* 更新 del_flag 字段为删除标识
* @param cfg
* @return
*/
int deleteByCompileId(ScheduleCfg cfg);
/**
* 查找 配置 下发 最新记录
* @param compileId
* @param isValid
* @return
*/
ScheduleExceInfo findScheduleExceNew(ScheduleExceInfo se);
/**
* 修改配置表状态
* @param tableName
* @param compileId
* @param isValid
* @return
*/
int updateCfgTableStatus(@Param("tableName")String tableName,@Param("compileId")Integer compileId,@Param("isValid")Integer isValid);
/**
* 查询 配置信息
* @param compileId
* @return
*/
BaseCfg getCfgTableInfo(@Param("tableName")String tableName,@Param("compileId")Integer compileId);
/**
* 保存执行记录
* @param exceInfo
* @return
*/
int insertScheduleExceLog(ScheduleExceInfo exceInfo);
/**
* 保存最新记录表
* @param exceInfo
* @return
*/
int insertScheduleExceNew(ScheduleExceInfo exceInfo);
/**
* 更新最新记录表
* @param exceInfo
* @return
*/
int updateScheduleExceNew(ScheduleExceInfo exceInfo);
}

View File

@@ -0,0 +1,315 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.nis.web.dao.SchedulerDao" >
<resultMap type="com.nis.domain.ScheduleCfg" id="scheduleCfgMap">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="cronValid" column="cron_valid"/>
<result property="cronInvalid" column="cron_invalid"/>
<result property="cfgId" column="cfg_id"/>
<result property="compileId" column="compile_id"/>
<result property="serviceId" column="service_id"/>
<result property="isValid" column="is_valid"/>
<result property="isAudit" column="is_audit"/>
<result property="functionId" column="function_id"/>
<result property="creatorId" column="creator_id"/>
<result property="createTime" column="create_time"/>
<result property="editorId" column="editor_id"/>
<result property="editTime" column="edit_time"/>
<result property="tableName" column="table_name"/>
<result property="userRegion1" column="user_region1"/>
<result property="userRegion2" column="user_region2"/>
<result property="userRegion3" column="user_region3"/>
<result property="userRegion4" column="user_region4"/>
<result property="userRegion5" column="user_region5"/>
<result property="delFlag" column="del_flag"/>
</resultMap>
<resultMap type="com.nis.domain.ScheduleExceInfo" id="scheduleExceInfoMap">
<id property="id" column="id"/>
<result property="scheduleId" column="schedule_id"/>
<result property="exceTime" column="exce_time"/>
<result property="issueStatus" column="issue_status"/>
<result property="issueResult" column="issue_result"/>
<result property="errorInfo" column="error_info"/>
<result property="compileId" column="compile_id"/>
<result property="isIssue" column="is_issue"/>
</resultMap>
<resultMap id="baseCfgMap" type="com.nis.domain.configuration.BaseCfg" >
<id column="cfg_id" property="cfgId" jdbcType="BIGINT" />
<result column="cfg_desc" property="cfgDesc" jdbcType="VARCHAR" />
<result column="action" property="action" jdbcType="INTEGER" />
<result column="is_valid" property="isValid" jdbcType="INTEGER" />
<result column="is_audit" property="isAudit" jdbcType="INTEGER" />
<result column="creator_id" property="creatorId" jdbcType="INTEGER" />
<result column="create_time" property="createTime" jdbcType="TIMESTAMP" />
<result column="editor_id" property="editorId" jdbcType="INTEGER" />
<result column="edit_time" property="editTime" jdbcType="TIMESTAMP" />
<result column="auditor_id" property="auditorId" jdbcType="INTEGER" />
<result column="audit_time" property="auditTime" jdbcType="TIMESTAMP" />
<result column="service_id" property="serviceId" jdbcType="INTEGER" />
<result column="request_id" property="requestId" jdbcType="INTEGER" />
<result column="compile_id" property="compileId" jdbcType="INTEGER" />
<result column="is_area_effective" property="isAreaEffective" jdbcType="INTEGER" />
<result column="classify" property="classify" jdbcType="VARCHAR" />
<result column="attribute" property="attribute" jdbcType="VARCHAR" />
<result column="lable" property="lable" jdbcType="VARCHAR" />
<result column="area_effective_ids" property="areaEffectiveIds" jdbcType="VARCHAR" />
<result column="function_id" property="functionId" jdbcType="INTEGER" />
<result column="do_log" property="doLog" jdbcType="INTEGER" />
<result column="do_blacklist" property="doBlackList" jdbcType="INTEGER" />
</resultMap>
<sql id="scheduleCfgColumns">
a.ID,
a.NAME,
a.cron_valid,
a.cron_invalid,
a.SERVICE_ID,
a.COMPILE_ID,
a.CFG_ID,
a.IS_VALID,
a.IS_AUDIT,
a.function_id,
a.CREATOR_ID,
a.CREATE_TIME,
a.EDITOR_ID,
a.EDIT_TIME,
a.table_name,
a.user_region1,
a.user_region2,
a.user_region3,
a.user_region4,
a.user_region5
</sql>
<select id="findScheduleList" resultMap="scheduleCfgMap">
select
<include refid="scheduleCfgColumns"/>
from schedule_cfg a
<where>
del_Flag = #{DEL_FLAG_NORMAL}
<if test="id != null">
and id = #{id}
</if>
<if test="compileId != null">
and compile_id = #{compileId}
</if>
<if test="isValid != null">
and IS_VALID = #{isValid}
</if>
<if test="isAudit != null">
and IS_AUDIT = #{isAudit}
</if>
<if test="compileId != null">
and CFG_ID = #{cfgId}
</if>
<if test="functionId != null">
and function_id = #{functionId}
</if>
<if test="tableName != null and tableName != ''">
and table_name = #{tableName}
</if>
<!-- 动态where条件 -->
<if test=" whereStr != null and whereStr !=''">
${whereStr}
</if>
</where>
order by a.id
</select>
<!-- 查找最新的更新数据 -->
<select id="findNewlyCfg" resultMap="scheduleCfgMap">
select
a.ID,
a.NAME,
a.cron_valid,
a.cron_invalid,
a.SERVICE_ID,
a.COMPILE_ID,
a.CFG_ID,
a.IS_VALID,
a.IS_AUDIT,
a.CREATOR_ID,
a.CREATE_TIME,
a.EDITOR_ID,
a.EDIT_TIME,
a.table_name,
a.user_region1,
a.user_region2,
a.user_region3,
a.user_region4,
a.user_region5
from schedule_cfg a
<where>
del_Flag = 1
and id > #{id}
</where>
order by a.id
limit #{limit}
</select>
<insert id="insert" parameterType="com.nis.domain.ScheduleCfg" useGeneratedKeys="true" keyProperty="id" >
insert into schedule_cfg (
NAME,
cron_valid,
cron_invalid,
SERVICE_ID,
COMPILE_ID,
CFG_ID,
IS_VALID,
IS_AUDIT,
function_id,
CREATOR_ID,
CREATE_TIME,
EDITOR_ID,
EDIT_TIME,
table_name,
user_region1,
user_region2,
user_region3,
user_region4,
user_region5
) values (
#{name,jdbcType=VARCHAR},
#{cronValid,jdbcType=VARCHAR},
#{cronInvalid,jdbcType=VARCHAR},
#{serviceId,jdbcType=INTEGER},
#{compileId,jdbcType=INTEGER},
#{cfgId,jdbcType=INTEGER},
#{isValid,jdbcType=INTEGER},
#{isAudit,jdbcType=INTEGER},
#{functionId,jdbcType=INTEGER},
#{creatorId,jdbcType=INTEGER},
#{createTime,jdbcType=TIMESTAMP},
#{editorId,jdbcType=INTEGER},
#{editTime,jdbcType=TIMESTAMP},
#{tableName,jdbcType=VARCHAR},
#{userRegion1,jdbcType=VARCHAR},
#{userRegion2,jdbcType=VARCHAR},
#{userRegion3,jdbcType=VARCHAR},
#{userRegion4,jdbcType=VARCHAR},
#{userRegion5,jdbcType=VARCHAR}
)
</insert>
<!-- 根据 compileId 将定时任务失效,定时任务的修改策略为:删除之前的所有配置,新增 -->
<update id="deleteByCompileId" parameterType="com.nis.domain.ScheduleCfg">
update schedule_cfg
<set>
<if test="editorId != null ">
editor_Id = #{editorId,jdbcType=VARCHAR},
</if>
<if test="editTime != null ">
edit_Time = #{editTime,jdbcType=TIMESTAMP},
</if>
del_flag = #{DEL_FLAG_DELETE}
</set>
WHERE compile_Id = #{compileId} and del_flag =1
</update>
<!-- 查找 配置 下发 最新记录 -->
<select id="findScheduleExceNew" resultMap="scheduleExceInfoMap" >
SELECT
id,
schedule_id,
exce_time,
issue_status,
issue_result,
error_info,
compile_id,
is_issue
FROM
schedule_exce_new
WHERE
compile_id = #{compileId} and issue_status = #{isValid}
</select>
<!-- 修改配置表状态 -->
<update id="updateCfgTableStatus">
update ${tableName} set is_valid = #{isValid} ,is_audit = 1 where compile_id = #{compileId}
</update>
<!-- 查询最新的配置状态 -->
<select id="getCfgTableInfo" resultMap="baseCfgMap">
select * from ${tableName} where compile_id = #{compileId};
</select>
<!-- 保存执行记录 -->
<insert id="insertScheduleExceLog" parameterType="com.nis.domain.ScheduleExceInfo">
INSERT INTO schedule_exce_log (
schedule_id,
exce_time,
issue_status,
issue_result,
error_info,
compile_id,
is_issue
) VALUES (
#{scheduleId},
#{exceTime},
#{issueStatus},
#{issueResult},
#{errorInfo},
#{compileId},
0
);
</insert>
<!-- 保存最新记录表 -->
<insert id="insertScheduleExceNew" parameterType="com.nis.domain.ScheduleExceInfo">
INSERT INTO schedule_exce_new (
schedule_id,
exce_time,
issue_status,
issue_result,
error_info,
compile_id,
is_issue
) VALUES (
#{scheduleId},
#{exceTime},
#{issueStatus},
#{issueResult},
#{errorInfo},
#{compileId},
0
);
</insert>
<!-- 更新最新记录表 -->
<update id="updateScheduleExceNew" parameterType="com.nis.domain.ScheduleExceInfo">
UPDATE schedule_exce_new
<set>
<if test="scheduleId != null">
schedule_id = #{scheduleId},
</if>
<if test=" exceTime != null">
exce_time = #{exceTime},
</if>
<if test=" issueResult != null">
issue_result = #{issueResult},
</if>
<if test=" errorInfo != null">
error_info = #{errorInfo},
</if>
<if test=" isIssue != null">
is_issue = #{isIssue},
</if>
</set>
WHERE
compile_Id = #{compileId} and issue_status = #{issueStatus}
</update>
</mapper>

View File

@@ -99,4 +99,6 @@ public interface ConfigSynchronizationDao {
public List<AppIpCfg> getAppIpFeatureList(BaseCfg entity);
public void updateCfgStatus(BaseCfg entity);
public PxyObjSpoofingIpPool getPxyObjSpoofingIpPool(Long cfgId);
}

View File

@@ -733,6 +733,39 @@
<result column="dns_strategy_id" property="dnsStrategyId" jdbcType="INTEGER" />
<result column="dns_strategy_name" property="dnsStrategyName" jdbcType="VARCHAR" />
</resultMap>
<resultMap id="PxyObjSpoofingPoolMap" type="com.nis.domain.configuration.PxyObjSpoofingIpPool" >
<id column="cfg_id" property="cfgId" jdbcType="BIGINT" />
<result column="cfg_desc" property="cfgDesc" jdbcType="VARCHAR" />
<result column="cfg_region_code" property="cfgRegionCode" jdbcType="INTEGER" />
<result column="ip_type" property="ipType" jdbcType="INTEGER" />
<result column="ip_address" property="ipAddress" jdbcType="VARCHAR" />
<result column="direction" property="direction" jdbcType="INTEGER" />
<result column="protocol" property="protocol" jdbcType="INTEGER" />
<result column="location" property="location" jdbcType="INTEGER" />
<result column="port" property="port" jdbcType="VARCHAR" />
<!-- <result column="action" property="action" jdbcType="INTEGER" /> -->
<result column="is_valid" property="isValid" jdbcType="INTEGER" />
<result column="is_audit" property="isAudit" jdbcType="INTEGER" />
<result column="creator_id" property="creatorId" jdbcType="INTEGER" />
<result column="create_time" property="createTime" jdbcType="TIMESTAMP" />
<result column="editor_id" property="editorId" jdbcType="INTEGER" />
<result column="edit_time" property="editTime" jdbcType="TIMESTAMP" />
<result column="auditor_id" property="auditorId" jdbcType="INTEGER" />
<result column="audit_time" property="auditTime" jdbcType="TIMESTAMP" />
<result column="service_id" property="serviceId" jdbcType="INTEGER" />
<result column="request_id" property="requestId" jdbcType="INTEGER" />
<result column="compile_id" property="compileId" jdbcType="INTEGER" />
<result column="location" property="location" jdbcType="INTEGER"/>
<result column="is_area_effective" property="isAreaEffective" jdbcType="INTEGER" />
<result column="classify" property="classify" jdbcType="VARCHAR" />
<result column="attribute" property="attribute" jdbcType="VARCHAR" />
<result column="lable" property="lable" jdbcType="VARCHAR" />
<result column="area_effective_ids" property="areaEffectiveIds" jdbcType="VARCHAR" />
<result column="function_id" property="functionId" jdbcType="INTEGER" />
<result column="group_id" property="groupId" jdbcType="INTEGER" />
<result column="group_name" property="groupName" jdbcType="INTEGER" />
<result column="user_region" property="userRegion" jdbcType="VARCHAR" />
</resultMap>
<sql id="BaseCfg_Column" >
a.cfg_id,a.cfg_desc,a.action,a.is_valid,a.is_audit,a.creator_id,a.audit_time,
a.service_id,a.request_id,a.compile_id,a.is_area_effective,a.classify,a.attribute,a.lable,
@@ -877,12 +910,13 @@
a.ip_type,a.src_ip_address,a.ip_pattern,a.port_pattern,a.src_port
,a.protocol,a.direction,a.dest_port,a.dest_ip_address,a.cfg_type,a.compile_id
</sql>
<sql id="PxyObjSpoofingIpPoolColumns">
a.CFG_ID, a.CFG_DESC,a.CFG_REGION_CODE, a.IP_TYPE, a.IP_ADDRESS,
a.DIRECTION,a.PROTOCOL,a.IS_VALID,a.IS_AUDIT,a.location,a.port,
a.CREATOR_ID,a.CREATE_TIME,a.EDITOR_ID,a.EDIT_TIME,a.AUDITOR_ID,a.AUDIT_TIME,
a.SERVICE_ID,a.REQUEST_ID,a.COMPILE_ID,a.IS_AREA_EFFECTIVE,a.CLASSIFY,
a.ATTRIBUTE,a.LABLE,a.AREA_EFFECTIVE_IDS,a.FUNCTION_ID,a.GROUP_ID,a.user_region
<sql id="PxyObjSpoofingIpPoolColumns">
r.CFG_ID, r.CFG_DESC,r.CFG_REGION_CODE, r.IP_TYPE, r.IP_ADDRESS,
r.DIRECTION,r.PROTOCOL,r.IS_VALID,r.IS_AUDIT,r.location,r.port,
r.CREATOR_ID,r.CREATE_TIME,r.EDITOR_ID,r.EDIT_TIME,r.AUDITOR_ID,r.AUDIT_TIME,
r.SERVICE_ID,r.REQUEST_ID,r.COMPILE_ID,r.IS_AREA_EFFECTIVE,r.CLASSIFY,
r.ATTRIBUTE,r.LABLE,r.AREA_EFFECTIVE_IDS,r.FUNCTION_ID,r.GROUP_ID,r.user_region
</sql>
<!-- <sql id="WebsiteDomainTopic_Column">
id,website_service_id websiteServiceId,domain,topic_id topicId,create_time createTime,creator_id creatorId,is_valid isValid
@@ -938,7 +972,16 @@
<if test="action != null">
AND a.action=#{action,jdbcType=INTEGER}
</if>
and a.is_valid=#{isValid} and a.is_audit=#{isAudit} and a.is_valid!=-1
<if test="isValid != null">
AND a.is_valid=#{isValid,jdbcType=INTEGER}
</if>
<if test="isAudit != null">
AND a.is_audit=#{isAudit,jdbcType=INTEGER}
</if>
<if test="compileId != null">
AND a.compile_id=#{compileId,jdbcType=INTEGER}
</if>
and a.is_valid!=-1
</trim>
ORDER BY a.CFG_ID
</select>
@@ -961,7 +1004,16 @@
<if test="action != null">
AND a.action=#{action,jdbcType=INTEGER}
</if>
and a.is_valid=#{isValid} and a.is_audit=#{isAudit} and a.is_valid!=-1
<if test="isValid != null">
AND a.is_valid=#{isValid,jdbcType=INTEGER}
</if>
<if test="isAudit != null">
AND a.is_audit=#{isAudit,jdbcType=INTEGER}
</if>
<if test="compileId != null">
AND a.compile_id=#{compileId,jdbcType=INTEGER}
</if>
and a.is_valid!=-1
</trim>
ORDER BY a.CFG_ID
</select>
@@ -1602,6 +1654,13 @@
and function_id=#{functionId,jdbcType=INTEGER}
</trim>
</update>
<select id="getPxyObjSpoofingIpPool" resultMap="PxyObjSpoofingPoolMap" >
SELECT
<include refid="PxyObjSpoofingIpPoolColumns"/>
FROM pxy_obj_spoofing_ip_pool r
where r.CFG_ID=#{cfgId,jdbcType=BIGINT}
</select>
<!-- <select id="findAppPolicyList" resultMap="AppPolicyCfgMap" parameterType="com.nis.domain.configuration.AppPolicyCfg" >
select
<include refid="AppPolicyCfg_Column"/>

View File

@@ -0,0 +1,116 @@
package com.nis.web.service;
import java.util.Date;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.nis.domain.ScheduleCfg;
import com.nis.domain.ScheduleExceInfo;
import com.nis.domain.configuration.BaseCfg;
import com.nis.util.SchedulerTaskUtil;
import com.nis.util.ServiceConfigTemplateUtil;
import com.nis.web.dao.SchedulerDao;
import com.nis.web.dao.configuration.ConfigSynchronizationDao;
@Service
public class ScheduleService extends BaseService{
@Autowired
private SchedulerDao dao ;
@Autowired
private ConfigSynchronizationDao configSynchronizationDao;
@SuppressWarnings("rawtypes")
@Transactional(readOnly=false,rollbackFor=RuntimeException.class)
public void issueCompileInfo(ScheduleCfg cfg,Integer isValid) {
Integer compileId = cfg.getCompileId();
BaseCfg baseCfg = dao.getCfgTableInfo(cfg.getTableName(),compileId);//查询当前配置的最新状态
Integer curIsValid = baseCfg.getIsValid();//当前配置的最新 是否有效信息
if(curIsValid == isValid) {
logger.info(String.format("当前isValid状态没有变化,不需执行,compileId:%s,isValid : %s", compileId,isValid));
return;
}
//1、根据 compileId isvalid 查找new 判断是否已经下发过
ScheduleExceInfo se = new ScheduleExceInfo();
se.setCompileId(compileId);
se.setIsValid(isValid);
ScheduleExceInfo exceNew = dao.findScheduleExceNew(se);
//2、如果已经下发直接下发状态否则下发配置
Integer issueResult = 1;
String errorInfo = null;
String tableName = cfg.getTableName();
SchedulerTaskUtil scheduler = new SchedulerTaskUtil();
boolean udpateConfigStatus = false;
try {
if(isValid == 1 && (exceNew == null || exceNew.getIsIssue() == 1)) {//生效配置需要下发
udpateConfigStatus = scheduler.updateConfigByServiceAndCompile(cfg.getServiceId(), compileId, isValid, 1,configSynchronizationDao);
logger.info(String.format("定时器下发配置内容compileId:%s,isValid:%s,issueResult:%s,errorInfo:%s",compileId,isValid,issueResult,errorInfo));
}else{//只需修改状态
udpateConfigStatus = scheduler.updateConfigByServiceAndCompile(cfg.getServiceId(), compileId, isValid, 0,configSynchronizationDao);
logger.info(String.format("定时器修改配置状态compileId:%s,isValid:%s,issueResult:%s,errorInfo:%s",compileId,isValid,issueResult,errorInfo));
}
} catch (NoSuchFieldException e) {
udpateConfigStatus = false;
e.printStackTrace();
} catch (SecurityException e) {
udpateConfigStatus = false;
e.printStackTrace();
} catch (IllegalArgumentException e) {
udpateConfigStatus = false;
e.printStackTrace();
} catch (IllegalAccessException e) {
udpateConfigStatus = false;
e.printStackTrace();
}
// logger.info(String.format("配置状态更新compileId:%s,isValid:%s,issueResult:%s,errorInfo:%s",compileId,isValid,issueResult,errorInfo));
if(udpateConfigStatus){//配置更新成功
if(exceNew == null) {
//新增exce_new 表状态
exceNew = new ScheduleExceInfo();
exceNew.setCompileId(compileId);
exceNew.setIssueStatus(isValid);
exceNew.setExceTime(new Date());
exceNew.setIssueResult(issueResult);
exceNew.setErrorInfo(errorInfo);
exceNew.setIsIssue(0);
exceNew.setScheduleId(cfg.getId());
dao.insertScheduleExceNew(exceNew);
}else {
//修改 exce_new 表状态
exceNew.setExceTime(new Date());
exceNew.setIssueResult(issueResult);
exceNew.setErrorInfo(errorInfo);
exceNew.setScheduleId(cfg.getId());
dao.updateScheduleExceNew(exceNew);
}
ServiceConfigTemplateUtil serviceTemplate = new ServiceConfigTemplateUtil();
List<Map<String,Object>> serviceList = serviceTemplate.getServiceListByServiceId(cfg.getServiceId());
//根据编译ID查询配置表中的配置信息
for(Map<String,Object> service:serviceList){
//获取业务下的配置域
List<Map<String,Object>> cfgList = (List<Map<String, Object>>) service.get("cfgList");
//查询子域配置详情
if(cfgList!=null){
for(Map<String,Object> m:cfgList){
String regionTable = m.get("tableName").toString();
//更新配置域表的isValid字段
dao.updateCfgTableStatus(regionTable, compileId, isValid);
}
}
}
//3、更新 配置表的 isValid 字段,添加 exce_log 记录
dao.insertScheduleExceLog(exceNew);
dao.updateCfgTableStatus(tableName, compileId, isValid);
}
}
}