Skip to content
切换导航条
切换导航条
当前项目
正在载入...
登录
孙克
/
smf-core
转到一个项目
切换导航栏
切换导航栏固定状态
项目
群组
代码片段
帮助
项目
活动
版本库
流水线
图表
问题
0
合并请求
0
维基
网络
创建新的问题
作业
提交
问题看板
文件
提交
网络
比较
分支
标签
Commit e0b83d4d
由
sunke
编写于
2023-02-17 14:21:12 +0800
浏览文件
选项
浏览文件
标签
下载
电子邮件补丁
差异文件
数据存档处理
1 个父辈
59765f11
显示空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
155 行增加
和
0 行删除
src/main/java/com/neotel/smfcore/core/system/util/DbBackupService.java
src/main/java/com/neotel/smfcore/core/system/util/DbBackupService.java
0 → 100644
查看文件 @
e0b83d4
package
com
.
neotel
.
smfcore
.
core
.
system
.
util
;
import
com.google.common.base.Strings
;
import
com.neotel.smfcore.common.utils.DateUtil
;
import
com.neotel.smfcore.core.device.util.DataCache
;
import
com.neotel.smfcore.core.inList.service.po.InList
;
import
com.neotel.smfcore.core.inList.service.po.InListItem
;
import
com.neotel.smfcore.core.message.service.po.Message
;
import
com.neotel.smfcore.core.order.service.po.LiteOrder
;
import
com.neotel.smfcore.core.order.service.po.LiteOrderItem
;
import
com.neotel.smfcore.core.system.service.dao.IAlarmInfoDao
;
import
com.neotel.smfcore.core.system.service.po.AlarmInfo
;
import
com.neotel.smfcore.core.system.service.po.DataLog
;
import
com.neotel.smfcore.core.system.service.po.Humiture
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.data.domain.PageRequest
;
import
org.springframework.data.domain.Sort
;
import
org.springframework.data.mongodb.core.MongoTemplate
;
import
org.springframework.data.mongodb.core.query.Criteria
;
import
org.springframework.data.mongodb.core.query.Query
;
import
org.springframework.stereotype.Service
;
import
javax.annotation.PostConstruct
;
import
java.util.Date
;
import
java.util.List
;
import
java.util.concurrent.ExecutorService
;
import
java.util.concurrent.Executors
;
import
java.util.concurrent.TimeUnit
;
/**
* @author sunke
* @date 2023/1/31 10:02 AM
*/
@Slf4j
@Service
public
class
DbBackupService
{
/**
* 自动存档间隔Key
*/
private
String
BACKUP_MONTH_KEY
=
"db.backup.month"
;
/**
* 上次自动存档时间
*/
private
String
LAST_BACKUP_TIME_KEY
=
"db.backup.lastTime"
;
@Autowired
DataCache
dataCache
;
@Autowired
private
IAlarmInfoDao
alarmInfoDao
;
boolean
processing
=
false
;
@PostConstruct
public
void
init
(){
ExecutorService
executorService
=
Executors
.
newSingleThreadExecutor
();
executorService
.
execute
(
new
Runnable
()
{
@Override
public
void
run
()
{
log
.
info
(
"开启数据库定时存档任务"
);
while
(
true
){
try
{
Integer
dbBackupMonth
=
dataCache
.
getCache
(
BACKUP_MONTH_KEY
);
dbBackupMonth
=
1
;
if
(
dbBackupMonth
!=
null
&&
dbBackupMonth
>
0
){
Date
lastBackupTime
=
dataCache
.
getCache
(
LAST_BACKUP_TIME_KEY
);
boolean
needBackup
=
false
;
if
(
lastBackupTime
==
null
){
needBackup
=
true
;
}
else
{
long
monthNum
=
cn
.
hutool
.
core
.
date
.
DateUtil
.
betweenMonth
(
lastBackupTime
,
new
Date
(),
true
);
if
(
monthNum
>=
1
){
//未备份过的或上次备份时间距当前一个月的,进行备份
needBackup
=
true
;
}
}
if
(
needBackup
){
backup
();
}
}
TimeUnit
.
HOURS
.
sleep
(
1
);
}
catch
(
Exception
e
){
log
.
error
(
"数据库定时存档任务出错"
+
e
.
getMessage
());
}
}
}
});
}
/**
* 备份数据库数据比较多的表
* @return
*/
public
boolean
backup
(){
if
(
processing
){
log
.
info
(
"上一次存档任务未完成,忽略本次存档"
);
return
false
;
}
processing
=
true
;
Integer
dbBackupMonth
=
dataCache
.
getCache
(
BACKUP_MONTH_KEY
);
if
(
dbBackupMonth
==
null
){
dbBackupMonth
=
12
;
//默认备份12个月前的数据
dataCache
.
updateCache
(
BACKUP_MONTH_KEY
,
dbBackupMonth
);
}
dataCache
.
updateCache
(
LAST_BACKUP_TIME_KEY
,
new
Date
());
backupTable
(
dbBackupMonth
,
DataLog
.
class
,
Humiture
.
class
,
LiteOrder
.
class
,
LiteOrderItem
.
class
,
InList
.
class
,
InListItem
.
class
,
Message
.
class
,
AlarmInfo
.
class
);
processing
=
false
;
return
true
;
}
private
void
backupTable
(
int
monthsBefore
,
Class
<?>...
entityClassList
){
for
(
Class
<?>
entityClass
:
entityClassList
)
{
try
{
MongoTemplate
mongoTemplate
=
alarmInfoDao
.
getMongoTemplate
();
Date
monthsBeforeToday
=
DateUtil
.
addMonthsFromToday
(-
monthsBefore
);
Criteria
c
=
Criteria
.
where
(
"createDate"
).
lt
(
monthsBeforeToday
);
Query
query
=
new
Query
(
c
);
PageRequest
page
=
PageRequest
.
ofSize
(
20000
);
String
collectionName
=
mongoTemplate
.
getCollectionName
(
entityClass
);
collectionName
=
collectionName
;
log
.
info
(
"开始存档["
+
collectionName
+
"]的数据"
);
String
backupCollectionName
=
collectionName
+
"_bak_"
+
DateUtil
.
toDateString
(
new
Date
(),
"yyyyMMddHHmmss"
);
while
(
true
){
query
.
with
(
page
);
log
.
info
(
"开始存档["
+
collectionName
+
"]的第["
+
page
.
getOffset
()+
"]条数据"
);
List
<?>
data
=
mongoTemplate
.
find
(
query
,
entityClass
,
collectionName
);
log
.
info
(
"查询到["
+
data
.
size
()+
"]条数据"
);
if
(
data
==
null
||
data
.
isEmpty
()){
break
;
}
mongoTemplate
.
insert
(
data
,
backupCollectionName
);
page
=
page
.
next
();
}
log
.
info
(
"存档["
+
collectionName
+
"]的数据完成,开始清理原始数据"
);
Query
removeQuery
=
new
Query
(
c
);
mongoTemplate
.
remove
(
removeQuery
,
entityClass
,
collectionName
);
log
.
info
(
"["
+
collectionName
+
"]的数据清理完成"
);
}
catch
(
Exception
e
){
log
.
error
(
"数据存档失败"
,
e
);
}
}
}
}
编写
预览
支持
Markdown
格式
附加文件
你添加了
0
人
到此讨论。请谨慎行事。
Finish editing this message first!
Cancel
请
注册
或
登录
后发表评论