From dca651c4270ca5f1e85a40cb27e5a63a449f4b93 Mon Sep 17 00:00:00 2001 From: Nathanael Schwalbe Date: Tue, 18 Apr 2017 11:55:59 +0200 Subject: [PATCH] Renaming send mail task --- pom.xml | 2 +- .../postoffice/PostOfficeConfiguration.java | 17 ++++++---- ...kFactory.java => SendMailTaskFactory.java} | 33 +++++++++++-------- 3 files changed, 30 insertions(+), 22 deletions(-) rename src/main/java/de/nschwalbe/postoffice/{MailSendingTriggerTaskFactory.java => SendMailTaskFactory.java} (83%) diff --git a/pom.xml b/pom.xml index e9b39aa..c731112 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ de.nschwalbe postoffice - 0.1.2 + 0.1.3 org.springframework.boot diff --git a/src/main/java/de/nschwalbe/postoffice/PostOfficeConfiguration.java b/src/main/java/de/nschwalbe/postoffice/PostOfficeConfiguration.java index f23778e..9040e51 100644 --- a/src/main/java/de/nschwalbe/postoffice/PostOfficeConfiguration.java +++ b/src/main/java/de/nschwalbe/postoffice/PostOfficeConfiguration.java @@ -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; @@ -21,14 +22,17 @@ 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 @@ -36,13 +40,12 @@ public MailSendingTriggerTaskFactory mailSendingTriggerTaskFactory(MailStorage m 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); } diff --git a/src/main/java/de/nschwalbe/postoffice/MailSendingTriggerTaskFactory.java b/src/main/java/de/nschwalbe/postoffice/SendMailTaskFactory.java similarity index 83% rename from src/main/java/de/nschwalbe/postoffice/MailSendingTriggerTaskFactory.java rename to src/main/java/de/nschwalbe/postoffice/SendMailTaskFactory.java index ae2a973..970fc08 100644 --- a/src/main/java/de/nschwalbe/postoffice/MailSendingTriggerTaskFactory.java +++ b/src/main/java/de/nschwalbe/postoffice/SendMailTaskFactory.java @@ -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; @@ -27,14 +28,16 @@ * @author Nathanael Schwalbe * @since 05.04.2017 */ -class MailSendingTriggerTaskFactory extends AbstractFactoryBean { +class SendMailTaskFactory extends AbstractFactoryBean { 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 @@ -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 ... @@ -70,7 +75,7 @@ void increaseDelay() { void resetDelay() { - delay = 30; + delay = defaultDelay; } @Override @@ -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;