I am currently using the GroupDocs.Watermark for Java library in a Java 8 environment, and I have encountered an issue where the CPU usage exceeds 100% on our EC2 when invoking the watermarking service. Below is a brief overview of the code implementation:
@RequestMapping(value = "{fileSecKey}/link.do")
public ModelAndView link(@PathVariable String fileSecKey
, @RequestParam(value = "ubiYn", required = false, defaultValue = "N") String ubiYn
, @RequestParam(value = "inline", required = false, defaultValue = "false") boolean inline
, @RequestParam(value = "thumbYn", required = false, defaultValue = "N") String thumbYn
, @RequestParam(value = "reqUsrId", required = false, defaultValue = "USR_ID") String reqUsrId
, @RequestParam(value = "reqUsrNm", required = false, defaultValue = "USR_NM") String reqUsrNm
, @RequestParam(value = "reqCorpNm", required = false, defaultValue = "CORP_NM") String reqCorpNm
, @RequestParam(value = "reqBrchNm", required = false, defaultValue = "BRCH_NM") String reqBrchNm
, @RequestParam(value = "watermarkYn", required = false, defaultValue = "N") String watermarkYn
, @RequestParam(value = "useCookieYn", required = false, defaultValue = "N") String useCookieYn
, HttpServletRequest request
, HttpServletResponse response) throws Exception {
String transactionAId = (String) request.getAttribute(TRANSACTION_ID);
FileItemVO fileItem = fileMgrService.selectFileItemBySecKey(fileSecKey);
log.info("fileUpload.link.do -info " + transactionAId + " fileSecKey :" + fileSecKey + " FileItemVO is null :" + (fileItem == null));
String filePath = "";
if (StringUtils.hasText(fileItem.getRemark())) {
if (fileItem.getRemark().equals("sal") || fileItem.getRemark().equals("crm")) {
//ubiReport๋ก ๋ง๋ค์ด์ง PDF์ธ ๊ฒฝ์ฐ
if (ubiYn.equals("Y")) {
filePath = UploadPathType.valueOf(fileItem.getRemark()).getUploadPath() + fileItem.getFileData() + "." + fileItem.getFileTp();
} else {
filePath = UploadPathType.valueOf(fileItem.getRemark()).getUploadPath() + fileItem.getFileData();
}
} else {
filePath = UploadPathType.valueOf(fileItem.getRemark()).getUploadPath() + fileItem.getFileData();
}
} else {
filePath = fileItem.getFileData();
}
log.info("file requested : " + filePath);
DownloadVO downloadVO = null;
File file = new File(filePath);
//ํ์ผ์ด ๋ฏธ์กด์ฌ ํ๋ฉด(๋ฐ์ดํฐ๋ง ์กด์ฌ)
if (!file.exists()) {
downloadVO = FileUtils.getWdmsImageResource(fileItem, resourceLoader, FileUtils.ResourceFilePathType.DEL_FILE_IMG_PATH);
}else {
//์ธ๋ค์ผ ํธ์ถ ์ด๋ฉด ์ธ๋ค์ผ ์ด๋ฏธ์ง ๋์ ํ์ธ ํ ์์ฒญ
if (ThumbnailGeneratorUtil.IS_OK_IMG_CONTENT_TYPE.contains(fileItem.getFileTp())//์ด๋ฏธ์ง ๋์
&& "Y".equals(thumbYn)) {
downloadVO = thumbnailGeneratorUtil.reImgThumbFile(fileItem, filePath, resourceLoader);
} else {
// watermarked file outputFilePath
String outputFilePath = filePath;
// ์ํฐ๋งํฌ ์ฌ๋ถํ์ธ
if("Y".equals(watermarkYn)) {
long startTime = System.currentTimeMillis(); // ์์ ์๊ฐ ๊ธฐ๋ก
// PDF ํ์ผ์ผ ๊ฒฝ์ฐ ์ํฐ๋งํฌ ์ถ๊ฐ
if ("application/pdf".equalsIgnoreCase(fileItem.getFileTp())) {
outputFilePath = watermarkService.addWatermarkToPdf(filePath, fileItem, reqUsrNm, reqUsrId, reqCorpNm, reqBrchNm);
}
// IMAGE ํ์ผ์ผ ๊ฒฝ์ฐ ์ํฐ๋งํฌ ์ถ๊ฐ
else if (fileItem.getFileTp().startsWith("image/")) {
outputFilePath = watermarkService.addWatermarkToImage(filePath, fileItem, reqUsrNm, reqUsrId, reqCorpNm, reqBrchNm);
}
long endTime = System.currentTimeMillis(); // ์ข
๋ฃ ์๊ฐ ๊ธฐ๋ก
long duration = endTime - startTime; // ์คํ ์๊ฐ ๊ณ์ฐ
log.info("์ํฐ๋งํฌ ์ถ๊ฐ ์์
์คํ ์๊ฐ: " + duration + "ms");
// // PPT ํ์ผ์ผ ๊ฒฝ์ฐ ์ํฐ๋งํฌ ์ถ๊ฐ
// if ("application/vnd.ms-powerpoint".equalsIgnoreCase(fileItem.getFileTp()) ||
// "application/vnd.openxmlformats-officedocument.presentationml.presentation".equalsIgnoreCase(fileItem.getFileTp())) {
// outputFilePath = watermarkService.addWatermarkToPpt(filePath, fileItem, reqUsrNm, reqUsrId, reqCorpNm, reqBrchNm);
// }
}
//์ธ๋ค์ผ ์์ฒญ์ด ์๋ ํ์ผ
downloadVO = new DownloadVO();
// ๋ง์ด๊ทธ๋ ์ด์
ํ ํ์ผ์ ํฌ๊ธฐ๋ฅผ ์ ๋ขฐํ ์ ์์ด ํด๋น ํ์ผ์ ์ฌ์ด์ฆ๋ฅผ ์ง์ ํ์ธํ๋๋ก ์์
long fileSize = (new File(outputFilePath)).length();
downloadVO.setFileName(fileItem.getFileNm());
downloadVO.setFilePath(outputFilePath);
downloadVO.setFileSize(fileSize);
downloadVO.setContentType(fileItem.getFileTp());
}
}
// ํ์ผ์ด ์กด์ฌํ๋ ๊ฒฝ์ฐ์๋ง ๋ค์ด๋ก๋ ์ฒ๋ฆฌ
if (downloadVO != null && "Y".equals(useCookieYn)) {
// Set-Cookie ํค๋ ์ถ๊ฐ
response.setHeader("Set-Cookie", "fileDownload=true; path=/; Domain="+getDomain()+"; SameSite=None; Secure");
}
ModelAndView mav = new ModelAndView("downloadView");
mav.addObject("FILE_INFO", downloadVO);
mav.addObject("INLINE", inline);
response.setHeader("Content-Type", fileItem.getFileTp() + "; charset=UTF-8;");
return mav;
}
public String addWatermarkToImage(String filePath, FileItemVO fileItem, String userName, String userId, String corpNm, String brchNm) {
String outputFilePath = filePath;
ImageLoadOptions loadOptions = new ImageLoadOptions();
Watermarker watermarker = null;
try {
watermarker = new Watermarker(filePath, loadOptions);
String dateFormat = new SimpleDateFormat("yy-MM-dd").format(new Date());
String baseText = String.format("%s_%s_%s_%s", corpNm, brchNm, userName, dateFormat);
TextWatermark footerTextWatermark = new TextWatermark(baseText, new Font("BMWTypeNext Kr TT Regular", 15));
footerTextWatermark.setForegroundColor(Color.getGray());
footerTextWatermark.setHorizontalAlignment(HorizontalAlignment.Left);
footerTextWatermark.setVerticalAlignment(VerticalAlignment.Bottom);
footerTextWatermark.setOpacity(0.8);
watermarker.add(footerTextWatermark);
// ํ์ผ ํ์ฅ์ ์ถ์ถ
String extension = "";
int i = fileItem.getFileTp().lastIndexOf('/');
if (i > 0) {
extension = fileItem.getFileTp().substring(i + 1).toLowerCase();
}
outputFilePath = filePath + "_watermarked." + extension;
watermarker.save(outputFilePath);
} finally {
if (watermarker != null) {
watermarker.close();
}
}
return outputFilePath;
}
I would appreciate any guidance or recommendations you could provide to help reduce the CPU usage during the watermarking process. Are there any specific optimizations or configurations within the GroupDocs.Watermark library that could help mitigate this issue?
Thank you for your assistance.