Commit 300885c3 by zt

优化代码

parent 50e73237
package com.foc;
import com.foc.service.CheckBlackIpService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @Author: lisu
* @Date: 2019/4/29 15:35
* @Description: 安科录音同步任务及数据源监控
* 1.查看定时任务最后同步时间
* 2.查看是否有录音文件记录
* 3.查看安科数据库最后一条数据
*/
public class DbCheckBlackIpTask {
private static final Logger log = LoggerFactory.getLogger(DbCheckBlackIpTask.class);
public static void main(String[] args) {
CheckBlackIpService.init();
CheckBlackIpService.checkBlackIp();
log.info("检查黑名单结束");
}
}
package com.foc.dao;
import org.apache.ibatis.annotations.Param;
import java.util.List;
/**
* @Author: lisu
* @Date: 2019/4/29 16:23
* @Description: java类作用描述
*/
public interface BlackIpDao {
/**
* 查询IP锁列表
*
* @return
*/
List<String> getBlackIpList();
/**
* 删除ip锁
*
* @param blackIp
*/
void deleteBlackIp(@Param("blackIp") String blackIp);
}
package com.foc.service;
import com.foc.dao.BlackIpDao;
import com.foc.dao.SoundsMaxIdDao;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.CollectionUtils;
import java.io.IOException;
import java.io.Reader;
import java.util.List;
/**
* @author zangtao
* @create 2019 - 09 -25 18:10
*/
public class CheckBlackIpService {
private static final Logger log = LoggerFactory.getLogger(CheckBlackIpService.class);
private static Reader reader;
private static SqlSessionFactory sqlSessionFactory;
private static SqlSession session;
public static void init() {
try {
reader = Resources.getResourceAsReader("mybatis-config.xml");
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
session = sqlSessionFactory.openSession();
} catch (IOException e) {
log.error("连接数据库异常");
e.printStackTrace();
}
}
public static void checkBlackIp() {
//检查ip锁数据库
BlackIpDao blackIpDao = session.getMapper(BlackIpDao.class);
List<String> blackIpList = blackIpDao.getBlackIpList();
if(!CollectionUtils.isEmpty(blackIpList)){
blackIpList.forEach( (blackIp) ->{
blackIpDao.deleteBlackIp(blackIp);
log.info("删除的ip地址有:{}",blackIp);
});
session.commit();
}
}
}
...@@ -15,7 +15,6 @@ import org.slf4j.LoggerFactory; ...@@ -15,7 +15,6 @@ import org.slf4j.LoggerFactory;
import java.io.IOException; import java.io.IOException;
import java.io.Reader; import java.io.Reader;
import java.text.SimpleDateFormat;
import java.time.Instant; import java.time.Instant;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
...@@ -58,17 +57,9 @@ public class SoundMonitorService { ...@@ -58,17 +57,9 @@ public class SoundMonitorService {
*/ */
public static boolean test() { public static boolean test() {
Properties properties = PropertiesUtils.getProperties(); Properties properties = PropertiesUtils.getProperties();
//间隔时间
String interval = properties.getProperty("interval"); String interval = properties.getProperty("interval");
SoundsMaxIdDao soundsMaxIdDao = session.getMapper(SoundsMaxIdDao.class); SoundsMaxIdDao soundsMaxIdDao = session.getMapper(SoundsMaxIdDao.class);
//检查ip锁数据库
List<String> blackIpList = soundsMaxIdDao.getBlackIpList();
if(blackIpList!=null){
blackIpList.forEach( (blackIp) ->{
soundsMaxIdDao.deleteBlackIp(blackIp);
log.info("删除的ip地址有:{}",blackIp);
session.commit();
});
}
SoundsMaxId maxId = soundsMaxIdDao.getMaxId(); SoundsMaxId maxId = soundsMaxIdDao.getMaxId();
String updateTime = maxId.getUpdateDate(); String updateTime = maxId.getUpdateDate();
Date date = new Date(); Date date = new Date();
...@@ -122,7 +113,6 @@ public class SoundMonitorService { ...@@ -122,7 +113,6 @@ public class SoundMonitorService {
//当前时间 //当前时间
Instant now = Instant.now().plusMillis(TimeUnit.HOURS.toMillis(8)); Instant now = Instant.now().plusMillis(TimeUnit.HOURS.toMillis(8));
Date date = new Date(); Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//判断是否要发送短信 间隔一小时 //判断是否要发送短信 间隔一小时
FocSmsEmailRecord focSendSmsRecord = soundsMaxIdDao.selectFocSendSmsRecord(); FocSmsEmailRecord focSendSmsRecord = soundsMaxIdDao.selectFocSendSmsRecord();
FocSmsEmailRecord focSendEmailRecord = soundsMaxIdDao.selectFocSendEmailRecord(); FocSmsEmailRecord focSendEmailRecord = soundsMaxIdDao.selectFocSendEmailRecord();
......
<?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.foc.dao.BlackIpDao">
<select id="getBlackIpList" resultType="string">
select black_ip from sys_login_blacklist where black_ip like '10.%'
</select>
<delete id="deleteBlackIp">
delete from sys_login_blacklist where black_ip = #{blackIp}
</delete>
</mapper>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment