Skip to content

Commit

Permalink
Avoid hardcoded emails on notification listener.
Browse files Browse the repository at this point in the history
  • Loading branch information
gortazar committed May 26, 2016
1 parent 425cefa commit ec67c91
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ public class BatchConfiguration {
@Value("${codeurjc.batch.attachment}")
private String attachment;

@Value("${codeurjc.batch.notifications.email}")
private String email;

// tag::readerwriterprocessor[]
@Bean
public FlatFileItemReader<Student> reader() {
Expand Down Expand Up @@ -73,7 +76,7 @@ public MailBatchItemWriter writer() {

@Bean
public JobExecutionListener listener() {
return new JobCompletionNotificationListener();
return new JobCompletionNotificationListener(email);
}

// end::listener[]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.listener.JobExecutionListenerSupport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;

Expand All @@ -11,14 +12,20 @@ public class JobCompletionNotificationListener extends JobExecutionListenerSuppo
@Autowired
private JavaMailSender mailSender;

private String notificationEmail;

public JobCompletionNotificationListener(String notificationEmail) {
this.notificationEmail = notificationEmail;
}

@Override
public void afterJob(JobExecution jobExecution) {

SimpleMailMessage message = new SimpleMailMessage();

message.setSubject("Job completion");
message.setFrom("[email protected]");
message.setTo("[email protected]");
message.setFrom(notificationEmail);
message.setTo(notificationEmail);
message.setText("Job completed with " + jobExecution.getExitStatus());

mailSender.send(message);
Expand All @@ -30,8 +37,8 @@ public void beforeJob(JobExecution jobExecution) {
SimpleMailMessage message = new SimpleMailMessage();

message.setSubject("Job started");
message.setFrom("[email protected]");
message.setTo("[email protected]");
message.setFrom(notificationEmail);
message.setTo(notificationEmail);
message.setText("Job started");

mailSender.send(message);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public MimeMessage process(Student student) throws Exception {
model.put("code", student.getCode());
helper.setFrom(sender);
helper.setTo(student.getEmail());
helper.setCc(sender);
helper.setSubject(VelocityEngineUtils.mergeTemplateIntoString(engine, "email-subject.vm", "UTF-8", model));
helper.setText(VelocityEngineUtils.mergeTemplateIntoString(engine, "email-body.vm", "UTF-8", model));

Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,6 @@ spring.mail.properties.mail.smtp.starttls.enable=true
#codeurjc.batch.data=
# Path to attachment data (this file will be attached to all emails sent)
#codeurjc.batch.attachment=

# email for job notifications
# codeurjc.batch.notifications.email=

0 comments on commit ec67c91

Please sign in to comment.