Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 메일 템플릿 의존성 추가 및 설정 #4

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package kr.seok.common.mail;

public interface EmailService {
void sendSimpleMessage(String subject, String template, String... to);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package kr.seok.common.mail;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.MailException;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.mail.javamail.MimeMessagePreparator;
import org.springframework.stereotype.Component;

@Slf4j
@Component
@RequiredArgsConstructor
public class EmailServiceImpl implements EmailService {

public static final String ENCODING_UTF8 = "UTF-8";
private final JavaMailSender javaMailSender;

@Value(value = "${spring.mail.username}")
private String from;

public void sendSimpleMessage(String subject, String template, String... to) {
MimeMessagePreparator mimeMessagePreparator = makeMassage(subject, template, to);
try {
javaMailSender.send(mimeMessagePreparator);
log.info("[LOG] [SEND] EXECUTE");
} catch(MailException e) {
log.debug("[LOG] [MailException] : {}", e.getMessage());
}
}

private MimeMessagePreparator makeMassage(final String subject, final String template, final String[] to) {
return mimeMessage -> {
MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, false, ENCODING_UTF8);
mimeMessageHelper.setSubject(subject);
mimeMessageHelper.setFrom(from);
mimeMessageHelper.setText(template, true);
mimeMessageHelper.setTo(to);
};
}
}
17,480 changes: 0 additions & 17,480 deletions batch-hospital/src/main/resources/files/seoul_hospital_position_info.csv

This file was deleted.

3,319 changes: 0 additions & 3,319 deletions batch-library-jpa/src/main/resources/files/전국도서관표준데이터.csv

This file was deleted.

3,319 changes: 0 additions & 3,319 deletions batch-library-test/src/main/resources/file/전국도서관표준데이터.csv

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.example.mail.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.spring5.SpringTemplateEngine;
import org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver;
import org.thymeleaf.templatemode.TemplateMode;

@Configuration
public class ThymeleafConfig {

private static final String CHARACTER_ENCODING_UTF8 = "UTF-8";
public static final String PREFIX_LOCATION = "classpath:templates/";
public static final String SUFFIX_HTML = ".html";

@Bean
public TemplateEngine htmlTemplateEngine() {
TemplateEngine templateEngine = new SpringTemplateEngine();
templateEngine.addTemplateResolver(springResourceTemplateResolver());
return templateEngine;
}

@Bean
public SpringResourceTemplateResolver springResourceTemplateResolver() {
SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver();
resolver.setOrder(1);
resolver.setPrefix(PREFIX_LOCATION);
resolver.setSuffix(SUFFIX_HTML);
resolver.setTemplateMode(TemplateMode.HTML);
resolver.setCharacterEncoding(CHARACTER_ENCODING_UTF8);
resolver.setCacheable(false);
return resolver;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.example.mail.job;


import com.example.mail.template.MailTemplateBuilder;
import kr.seok.common.mail.EmailService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.JobScope;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.batch.item.database.JpaPagingItemReader;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Slf4j
@Configuration
@RequiredArgsConstructor
public class MailJobConfiguration {

private static final String JOB_NAME = "mailJob";
public static final int CHUNK_SIZE = 1000;

private final JobBuilderFactory jobBuilderFactory;
private final StepBuilderFactory stepBuilderFactory;

private final EmailService emailService;
private final MailTemplateBuilder mailTemplateBuilder;

@Bean(value = JOB_NAME)
public Job mailJob() {
log.info("[LOG] [JOB] [START] {}", JOB_NAME);
return jobBuilderFactory.get(JOB_NAME)
.incrementer(new RunIdIncrementer())
.start(dataReader())
.build();
}

@Bean
@JobScope
public Step dataReader() {
log.info("[LOG] [STEP] [START] {}", "mailStep");
return stepBuilderFactory.get("mailStep")
.chunk(CHUNK_SIZE)
// .reader(mybatisReader())
// .writer(mailSender())
.build();
}
//
// @Bean
// public JpaPagingItemReader<?> mybatisReader() {
// log.info("[LOG] [READER] [START] {}", "mybatisReader");
// String queryId = "com.example.jobs.mapper.UserMapper.findAllData";
// return new MyBatisCursorItemReaderBuilder<>()
// .sqlSessionFactory(sqlSessionFactory)
// .queryId(queryId)
// .build();
// }
//
// public ItemWriter<? super User> mailSender() {
// log.info("[LOG] [WRITER] [START] {}", "mailSender");
// return items -> {
// String[] toRecipients = items.stream()
// .map(User::getEmail)
// .toArray(String[]::new);
// String subject = "제목";
// String templateName = "mail_template";
// String template = mailTemplateBuilder.makeTemplate(templateName, items);
// emailService.sendSimpleMessage(subject, template, toRecipients);
// log.info("[LOG] [WRITER] [END] : SEND");
// };
// }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.example.mail.template;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import java.util.List;
import java.util.Locale;
import java.util.Map;

@Component
@RequiredArgsConstructor
public class MailTemplateBuilder {

public static final String TEMPLATE_PATH = "mail/";
private final TemplateEngine templateEngine;

public String makeTemplate(String templateName, List<? extends Map<String, Object>> users) {
Context context = new Context(Locale.getDefault());
context.setVariable("list", users);
return templateEngine.process(TEMPLATE_PATH + templateName, context);
}
}
35 changes: 21 additions & 14 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,10 @@ subprojects {
implementation "org.springframework.boot:spring-boot-starter-data-jpa"
implementation "javax.validation:validation-api"

// 컴파일 단계에서 이미 롬복의 기능을 제공받고, 런타임 단계에서는 해당 라이브러리가 필요하지 않기 때문에 Compile Only로 설정
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'

/* Excel*/
implementation "org.apache.poi:poi:5.1.0" // .xlsx
implementation "org.apache.poi:poi-ooxml:5.1.0" // .xls

runtimeOnly 'com.h2database:h2'
runtimeOnly 'mysql:mysql-connector-java'

Expand Down Expand Up @@ -108,6 +105,12 @@ configure(querydslProjects) {
}

project(":batch-common") {
dependencies {
implementation "org.apache.poi:poi:5.1.0" // .xlsx
implementation "org.apache.poi:poi-ooxml:5.1.0" // .xls

implementation 'org.springframework.boot:spring-boot-starter-mail'
}
/* DTO, Util 클래스 등등 */
bootJar { enabled = false }
jar { enabled = true }
Expand Down Expand Up @@ -139,16 +142,16 @@ project(":batch-admin") {
project(":batch-hospital") {
dependencies {
implementation project(":batch-common")
implementation 'org.apache.httpcomponents:httpclient:4.5.13'
/* https://mvnrepository.com/artifact/org.modelmapper.extensions/modelmapper-spring */
implementation 'org.modelmapper.extensions:modelmapper-spring:1.1.0'

implementation 'org.springframework.boot:spring-boot-starter-data-elasticsearch'

// es 의존성 추가시 배치가 끝나지 않는 문제 발생
implementation 'org.elasticsearch:elasticsearch:7.6.2'
implementation 'org.elasticsearch.client:elasticsearch-rest-client:7.6.2'
implementation 'org.elasticsearch.client:elasticsearch-rest-high-level-client:7.6.2'
// implementation 'org.apache.httpcomponents:httpclient:4.5.13'
// /* https://mvnrepository.com/artifact/org.modelmapper.extensions/modelmapper-spring */
// implementation 'org.modelmapper.extensions:modelmapper-spring:1.1.0'
//
// implementation 'org.springframework.boot:spring-boot-starter-data-elasticsearch'
//
// // es 의존성 추가시 배치가 끝나지 않는 문제 발생
// implementation 'org.elasticsearch:elasticsearch:7.6.2'
// implementation 'org.elasticsearch.client:elasticsearch-rest-client:7.6.2'
// implementation 'org.elasticsearch.client:elasticsearch-rest-high-level-client:7.6.2'
}
}

Expand All @@ -175,18 +178,22 @@ project(":batch-library-jpa") {
implementation project(":batch-common")
}
}

project(":batch-library-test") {
dependencies {
implementation project(":batch-common")
}
}

project(":batch-real-estate") {
dependencies {
implementation project(":batch-common")
}
}

project(":batch-mail-service") {
dependencies {
implementation project(":batch-common")
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
}
}