일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 |
- ES6
- ajax
- spring
- mysql
- topologySpreadConstraints
- jsp
- AWS RDS
- MySQL Error
- mongodb
- sessionStorage
- 인생이재밌다
- javascript
- Bootstrap
- AWS
- 영화예매
- git
- Java
- zombie-hit apartment
- html
- spread operator
- terminationGracePeriodSeconds
- json
- AWS Route53
- post
- Get
- ssh
- chartjs
- Kubernetes
- 예매로직
- node.js
- Today
- Total
jongviet
July 1, 2021 - DB : local 파일 정리(quartz-scheduler) 본문
*7월1일
-org.quartz-scheduler를 활용하여 DB와 local 상의 물리적인 파일 불일치를 정리해보자.
-첨부파일 수정 시, 새로운 파일과 기존 파일을 일일이 비교해서 매칭 시킨 후 삭제할 수 없기에, 일반적으로 기존 파일을 모두 제거하고 새로운 파일을 대체하는 식의 로직을 구성한다. 그에 따라 DB상에 최신으로 업데이트된 파일 내역과 실제 local(또는 어떠한 저장소)에 있는 물리적 파일상의 불일치가 생기는데, 이를 scheduler를 활용하여 일정 주기로 제거해줄 수 있다.
-scheduler는 그 외에도 서비스 중인 프로젝트의 정기적인 로그 점검, 오류 메시지 전송 등 다양한 일을 자바 단 내에서 할 수 있으니 추후에 다른 목적으로 사용해보도록 하자.
@Component //cpnt 등록 필수;
public class FileSweeper {
private static Logger logger = LoggerFactory.getLogger(FileSweeper.class);
private final String BASE_PATH = "C:\\Spring\\workspace\\upload\\";
@Inject
private FilesDAORule fdao;
//초 분 시 일 월 요일 연도(optional)
@Scheduled(cron = "00 50 23 * * *") // 1년 365일 내내 23:50:00에 실행
public void fileSweep() throws Exception {
List<FilesVO> dbFileList = fdao.selectList(); //DB내 전체 파일 리스트
ArrayList<String> currFiles = new ArrayList<String>();
for (FilesVO fvo : dbFileList) {
String file_path = fvo.getSavedir() + "\\" + fvo.getUuid() + "_";
String file_name = fvo.getFname();
currFiles.add(BASE_PATH + file_path + file_name); //DB에 등록되어 있는 경로+파일명
if(fvo.getFtype() > 0) {
// 이미지 파일 썸네일 경로
currFiles.add(BASE_PATH + file_path + "th_" +file_name);
}
}
LocalDate now = LocalDate.now();
String today = now.toString();
today = today.replace("-", File.separator);
File dir = Paths.get(BASE_PATH + today).toFile();
File[] allFileObjs = dir.listFiles(); //오늘 날짜 경로 기준 실제 파일 리스트
for (File file : allFileObjs) {
String storedFileName = file.toPath().toString();
if(!currFiles.contains(storedFileName)) { //DB 파일 리스트에 포함되지 않은 파일은 제거
file.delete();
}
}
}
}
'Spring legacy' 카테고리의 다른 글
July 4, 2021 - Spring transaction (0) | 2021.07.04 |
---|---|
July 1, 2021 - paging 기본 (0) | 2021.07.01 |
June 30, 2021 - Spring 서칭 (0) | 2021.06.30 |
June 30, 2021 - Spring 파일업로드 (0) | 2021.06.30 |
June 25, 2021 - Spring 개인 필기 (1) | 2021.06.25 |