Skip to content

Commit

Permalink
Renaming send mail task
Browse files Browse the repository at this point in the history
  • Loading branch information
nschwalbe committed Apr 18, 2017
1 parent 90816f4 commit dca651c
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 22 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>de.nschwalbe</groupId>
<artifactId>postoffice</artifactId>
<version>0.1.2</version>
<version>0.1.3</version>

<parent>
<groupId>org.springframework.boot</groupId>
Expand Down
17 changes: 10 additions & 7 deletions src/main/java/de/nschwalbe/postoffice/PostOfficeConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
Expand All @@ -21,28 +22,30 @@ public class PostOfficeConfiguration {
@Autowired
private JavaMailSender javaMailSender;

@Autowired
private Environment env;

@Bean
public PostOffice mailService(MailStorage mailStorage) {
public PostOffice postOffice(MailStorage mailStorage) {
return new PostOffice(mailStorage, javaMailSender);
}

@Bean("mailSendingTriggerTask")
public MailSendingTriggerTaskFactory mailSendingTriggerTaskFactory(MailStorage mailStorage) {
return new MailSendingTriggerTaskFactory(mailStorage, javaMailSender);
@Bean("sendMailTask")
public SendMailTaskFactory sendMailTaskFactory(MailStorage mailStorage) {
return new SendMailTaskFactory(mailStorage, javaMailSender, env);
}

@EnableScheduling
@Configuration
static class SchedulerConfiguration implements SchedulingConfigurer {

@Autowired
private TriggerTask mailSendingTriggerTask;
private TriggerTask sendMailTask;

// TODO not for test
@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
try {
taskRegistrar.addTriggerTask(mailSendingTriggerTask);
taskRegistrar.addTriggerTask(sendMailTask);
} catch (Exception e) {
throw new RuntimeException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.config.AbstractFactoryBean;
import org.springframework.core.env.Environment;
import org.springframework.mail.MailAuthenticationException;
import org.springframework.mail.MailException;
import org.springframework.mail.MailSendException;
Expand All @@ -27,14 +28,16 @@
* @author Nathanael Schwalbe
* @since 05.04.2017
*/
class MailSendingTriggerTaskFactory extends AbstractFactoryBean<TriggerTask> {
class SendMailTaskFactory extends AbstractFactoryBean<TriggerTask> {

private final MailStorage mailStorage;
private final JavaMailSender javaMailSender;
private final Environment env;

MailSendingTriggerTaskFactory(MailStorage mailStorage, JavaMailSender javaMailSender) {
SendMailTaskFactory(MailStorage mailStorage, JavaMailSender javaMailSender, Environment env) {
this.mailStorage = mailStorage;
this.javaMailSender = javaMailSender;
this.env = env;
}

@Override
Expand All @@ -44,18 +47,20 @@ public Class<?> getObjectType() {

@Override
protected TriggerTask createInstance() throws Exception {
MailSendingTrigger trigger = new MailSendingTrigger();
MailSendingTask task = new MailSendingTask(mailStorage, javaMailSender, trigger);
SendMailTrigger trigger = new SendMailTrigger(env.getProperty("postoffice.worker.delay", Integer.class, 10));
SendMailTask task = new SendMailTask(mailStorage, javaMailSender, trigger);
return new TriggerTask(task, trigger);
}

static class MailSendingTrigger implements Trigger {
static class SendMailTrigger implements Trigger {

// seconds
private int delay = 30;
private int defaultDelay;
private int delay;

int getDelay() {
return delay;

SendMailTrigger(int delay) {
this.defaultDelay = delay;
this.delay = delay;
}

// try again in 30s, then wait 60s, then wait 90s ...
Expand All @@ -70,7 +75,7 @@ void increaseDelay() {


void resetDelay() {
delay = 30;
delay = defaultDelay;
}

@Override
Expand All @@ -90,15 +95,15 @@ public Date nextExecutionTime(TriggerContext triggerContext) {
}
}

static class MailSendingTask implements Runnable {
static class SendMailTask implements Runnable {

private Logger log = LoggerFactory.getLogger(MailSendingTask.class);
private Logger log = LoggerFactory.getLogger(SendMailTask.class);

private final MailStorage mailStorage;
private final JavaMailSender javaMailSender;
private final MailSendingTrigger trigger;
private final SendMailTrigger trigger;

MailSendingTask(MailStorage mailStorage, JavaMailSender javaMailSender, MailSendingTrigger trigger) {
SendMailTask(MailStorage mailStorage, JavaMailSender javaMailSender, SendMailTrigger trigger) {
this.mailStorage = mailStorage;
this.javaMailSender = javaMailSender;
this.trigger = trigger;
Expand Down

0 comments on commit dca651c

Please sign in to comment.