JAMES-1877 Extract helper for counting delivery retries of a mail
Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/b5633492 Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/b5633492 Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/b5633492 Branch: refs/heads/master Commit: b5633492b792ed4c86560e598dce492b033549b6 Parents: a62e460 Author: Benoit Tellier <btell...@linagora.com> Authored: Thu Dec 1 15:57:56 2016 +0700 Committer: Benoit Tellier <btell...@linagora.com> Committed: Tue Jan 10 15:12:49 2017 +0700 ---------------------------------------------------------------------- .../remoteDelivery/DeliveryRetriesHelper.java | 43 ++++++++++ .../remoteDelivery/DeliveryRunnable.java | 31 ++------ .../remoteDelivery/DeliveryRetryHelperTest.java | 83 ++++++++++++++++++++ 3 files changed, 131 insertions(+), 26 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/b5633492/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/remoteDelivery/DeliveryRetriesHelper.java ---------------------------------------------------------------------- diff --git a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/remoteDelivery/DeliveryRetriesHelper.java b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/remoteDelivery/DeliveryRetriesHelper.java new file mode 100644 index 0000000..485127e --- /dev/null +++ b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/remoteDelivery/DeliveryRetriesHelper.java @@ -0,0 +1,43 @@ +/**************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one * + * or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information * + * regarding copyright ownership. The ASF licenses this file * + * to you under the Apache License, Version 2.0 (the * + * "License"); you may not use this file except in compliance * + * with the License. You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, * + * software distributed under the License is distributed on an * + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * + * KIND, either express or implied. See the License for the * + * specific language governing permissions and limitations * + * under the License. * + ****************************************************************/ + +package org.apache.james.transport.mailets.remoteDelivery; + +import org.apache.mailet.Mail; + +public class DeliveryRetriesHelper { + + public static int retrieveRetries(Mail mail) { + try { + return Integer.parseInt(mail.getErrorMessage()); + } catch (NumberFormatException e) { + // Something strange was happen with the errorMessage.. + return 0; + } + } + + public static void initRetries(Mail mail) { + mail.setErrorMessage("0"); + } + + public static void incrementRetries(Mail mail) { + mail.setErrorMessage(String.valueOf(retrieveRetries(mail) + 1)); + } + +} http://git-wip-us.apache.org/repos/asf/james-project/blob/b5633492/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/remoteDelivery/DeliveryRunnable.java ---------------------------------------------------------------------- diff --git a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/remoteDelivery/DeliveryRunnable.java b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/remoteDelivery/DeliveryRunnable.java index d9db12c..75fb3e2 100644 --- a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/remoteDelivery/DeliveryRunnable.java +++ b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/remoteDelivery/DeliveryRunnable.java @@ -136,14 +136,7 @@ public class DeliveryRunnable implements Runnable { LifecycleUtil.dispose(mail); } else { // Something happened that will delay delivery. Store it back in the retry repository. - int retries = 0; - try { - retries = Integer.parseInt(mail.getErrorMessage()); - } catch (NumberFormatException e) { - // Something strange was happen with the errorMessage.. - } - - long delay = getNextDelay(retries); + long delay = getNextDelay(DeliveryRetriesHelper.retrieveRetries(mail)); if (configuration.isUsePriority()) { // Use lowest priority for retries. See JAMES-1311 @@ -154,7 +147,6 @@ public class DeliveryRunnable implements Runnable { } } - /** * We can assume that the recipients of this message are all going to the * same mail server. We will now rely on the DNS server to do DNS MX record @@ -558,12 +550,7 @@ public class DeliveryRunnable implements Runnable { logger.info("No mail server found for: " + host); String exceptionBuffer = "There are no DNS entries for the hostname " + host + ". I cannot determine where to send this message."; - int retry = 0; - try { - retry = Integer.parseInt(mail.getErrorMessage()); - } catch (NumberFormatException e) { - // Unable to parse retryCount - } + int retry = DeliveryRetriesHelper.retrieveRetries(mail); if (retry == 0 || retry > configuration.getDnsProblemRetry()) { // The domain has no dns entry.. Return a permanent error return failMessage(mail, new MessagingException(exceptionBuffer), true); @@ -647,8 +634,6 @@ public class DeliveryRunnable implements Runnable { } /** - * Insert the method's description here. - * * @param mail org.apache.james.core.MailImpl * @param ex javax.mail.MessagingException * @param permanent @@ -659,21 +644,15 @@ public class DeliveryRunnable implements Runnable { if (!permanent) { if (!mail.getState().equals(Mail.ERROR)) { mail.setState(Mail.ERROR); - mail.setErrorMessage("0"); + DeliveryRetriesHelper.initRetries(mail); mail.setLastUpdated(new Date()); } - int retries = 0; - try { - retries = Integer.parseInt(mail.getErrorMessage()); - } catch (NumberFormatException e) { - // Something strange was happen with the errorMessage.. - } + int retries = DeliveryRetriesHelper.retrieveRetries(mail); if (retries < configuration.getMaxRetries()) { logger.debug("Storing message " + mail.getName() + " into outgoing after " + retries + " retries"); - ++retries; - mail.setErrorMessage(retries + ""); + DeliveryRetriesHelper.incrementRetries(mail); mail.setLastUpdated(new Date()); return false; } else { http://git-wip-us.apache.org/repos/asf/james-project/blob/b5633492/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/remoteDelivery/DeliveryRetryHelperTest.java ---------------------------------------------------------------------- diff --git a/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/remoteDelivery/DeliveryRetryHelperTest.java b/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/remoteDelivery/DeliveryRetryHelperTest.java new file mode 100644 index 0000000..72ad294 --- /dev/null +++ b/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/remoteDelivery/DeliveryRetryHelperTest.java @@ -0,0 +1,83 @@ +/**************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one * + * or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information * + * regarding copyright ownership. The ASF licenses this file * + * to you under the Apache License, Version 2.0 (the * + * "License"); you may not use this file except in compliance * + * with the License. You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, * + * software distributed under the License is distributed on an * + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * + * KIND, either express or implied. See the License for the * + * specific language governing permissions and limitations * + * under the License. * + ****************************************************************/ + +package org.apache.james.transport.mailets.remoteDelivery; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.apache.mailet.base.test.FakeMail; +import org.junit.Test; + +public class DeliveryRetryHelperTest { + + @Test + public void retrieveRetriesShouldBeZeroByDefault() throws Exception { + assertThat(DeliveryRetriesHelper.retrieveRetries(FakeMail.defaultFakeMail())) + .isEqualTo(0); + } + + @Test + public void retrieveRetriesShouldBeZeroAfterInit() throws Exception { + FakeMail mail = FakeMail.defaultFakeMail(); + + DeliveryRetriesHelper.initRetries(mail); + + assertThat(DeliveryRetriesHelper.retrieveRetries(mail)) + .isEqualTo(0); + } + + @Test + public void retrieveRetriesShouldBeOneAfterIncrement() throws Exception { + FakeMail mail = FakeMail.defaultFakeMail(); + + DeliveryRetriesHelper.initRetries(mail); + DeliveryRetriesHelper.incrementRetries(mail); + + assertThat(DeliveryRetriesHelper.retrieveRetries(mail)) + .isEqualTo(1); + } + + @Test + public void incrementRetriesShouldWorkOnNonInitializedMails() throws Exception { + FakeMail mail = FakeMail.defaultFakeMail(); + + DeliveryRetriesHelper.incrementRetries(mail); + + assertThat(DeliveryRetriesHelper.retrieveRetries(mail)) + .isEqualTo(1); + } + + @Test + public void retrieveRetriesShouldBeZeroOnInvalidValue() throws Exception { + FakeMail mail = FakeMail.builder().errorMessage("invalid").build(); + + assertThat(DeliveryRetriesHelper.retrieveRetries(mail)) + .isEqualTo(0); + } + + @Test + public void incrementRetriesShouldWorkOnInvalidMails() throws Exception { + FakeMail mail = FakeMail.builder().errorMessage("invalid").build(); + + DeliveryRetriesHelper.incrementRetries(mail); + + assertThat(DeliveryRetriesHelper.retrieveRetries(mail)) + .isEqualTo(1); + } +} --------------------------------------------------------------------- To unsubscribe, e-mail: server-dev-unsubscr...@james.apache.org For additional commands, e-mail: server-dev-h...@james.apache.org