[Practice] 스케쥴러 활용 (2)
2021. 4. 20. 17:11ㆍSpring/Practice
1. 문제
- 회원중 가입일이 1년이 도래한 회원들에 대해서 서비스 이용내역 통지 메일을 보내는 기능
2. 풀이
- data.sql
INSERT INTO MAIL_TEMPLATE(ID, TEMPLATE_ID, TITLE, CONTENTS, SEND_EMAIL, SEND_USER_NAME, REG_DATE) VALUES
(1, 'USER_RESET_PASSWORD', '{USER_NAME}님의 비밀번호 초기화 요청입니다.', '<div><p>{USER_NAME}님 안녕하세요.</p><p>아래 링크를 클릭하여, 비밀번호를 초기화해주세요.</p><p><a href="{SERVER_URL}/reset?key={RESET_PASSWORD_KEY}">초기화</a></p></div>',
'ozofweird@gmail.com', '관리자', '2021-01-01 01:01:01.000000'),
(2, 'BOARD_ADD', '{USER_NAME}님이 글을 게시하였습니다.', '<div><p>제목: {BOARD_TITLE}</p><p>내용</p><div>{BOARD_CONTENTS}</div></div>',
'ozofweird@gmail.com', '관리자', '2021-01-01 01:01:01.000000'),
(3, 'BOARD_REPLY', '{USER_NAME}님이 글에 답변하였습니다.', '<p>제목: {BOARD_TITLE}</p><p>내용</p><div>{BOARD_CONTENTS}</div><p>답변</p><div>{BOARD_REPLY_CONTENTS}</div></div>',
'ozofweird@gmail.com', '관리자', '2021-01-01 01:01:01.000000'),
(4, 'USER_SERVICE_NOTICE', '{USER_NAME}님 안녕하세요.', '<div><p>개인정보 이용내역 안내</p><p>서비스를 잘 이용하고 계십니다.</p></div>',
'ozofweird@gmail.com', '관리자', '2021-01-01 01:01:01.000000');
- UserService.java
package com.example.jpa.sample.user.service;
import com.example.jpa.sample.board.model.ServiceResult;
import com.example.jpa.sample.user.entity.User;
import com.example.jpa.sample.user.model.*;
import java.util.List;
public interface UserService {
...
void sendServiceNotice();
}
- UserServiceImpl.java
package com.example.jpa.sample.user.service;
import com.example.jpa.sample.board.model.ServiceResult;
import com.example.jpa.sample.common.MailComponent;
import com.example.jpa.sample.common.exception.BizException;
import com.example.jpa.sample.mail.entity.MailTemplate;
import com.example.jpa.sample.mail.repository.MailTemplateRepository;
import com.example.jpa.sample.user.entity.User;
import com.example.jpa.sample.user.entity.UserInterest;
import com.example.jpa.sample.user.model.*;
import com.example.jpa.sample.user.repository.UserCustomRepository;
import com.example.jpa.sample.user.repository.UserInterestRepository;
import com.example.jpa.sample.user.repository.UserRepository;
import com.example.jpa.sample.util.PasswordUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
@RequiredArgsConstructor
@Service
public class UserServiceImpl implements UserService {
private final UserRepository userRepository;
private final UserCustomRepository userCustomRepository;
private final UserInterestRepository userInterestRepository;
private final MailComponent mailComponent;
private final MailTemplateRepository mailTemplateRepository;
...
@Override
public void sendServiceNotice() {
// 전체 회원에 대해 전송
Optional<MailTemplate> optionalMailTemplate = mailTemplateRepository.findByTemplateId("USER_SERVICE_NOTICE");
optionalMailTemplate.ifPresent(e -> {
String fromEmail = e.getSendEmail();
String fromName = e.getSendUserName();
String contents = e.getContents();
userRepository.findAll().stream().forEach((u) -> {
String title = e.getTitle().replaceAll("\\{USER_NAME\\}", u.getName());
mailComponent.send(fromEmail, fromName, u.getEmail(), u.getName(), title, contents);
});
});
}
}
- user_service_notice.html (resource - mail-template)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>{USER_NAME}님 안녕하세요.</title>
</head>
<body>
<div>
<p>개인정보 이용내역 안내</p>
<p>서비스를 잘 이용하고 계십니다.</p>
</div>
</body>
</html>
- Scheduler.java
package com.example.jpa.sample.common.schedule;
import com.example.jpa.sample.logs.service.LogService;
import com.example.jpa.sample.user.service.UserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Slf4j
@RequiredArgsConstructor
@Component
public class Scheduler {
private final LogService logService;
private final UserService userService;
// @Scheduled(cron = "0 0 4 * * *") // 새벽 4시
@Scheduled(fixedDelay = 1000 * 60) // 1분에 한번씩 실행
public void deleteLog() {
log.info("스케쥴 실행");
logService.deleteLog();
}
// @Scheduled(cron = "0 0 4 * * *") // 새벽 4시
@Scheduled(fixedDelay = 1000 * 60) // 1분에 한번씩 실행
public void sendServiceNotice() {
log.info("스케쥴 실행");
userService.sendServiceNotice();
}
}
728x90
'Spring > Practice' 카테고리의 다른 글
[Practice] 스케쥴러 활용 (1) (0) | 2021.04.20 |
---|---|
[Practice] 메일 전송 (4) (0) | 2021.04.20 |
[Practice] 메일 전송 (3) (0) | 2021.04.20 |
[Practice] 메일 전송 (2) (0) | 2021.04.20 |