You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by bt...@apache.org on 2019/03/27 03:01:35 UTC
[james-project] branch master updated (15aa698 -> 28360f8)
This is an automated email from the ASF dual-hosted git repository.
btellier pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/james-project.git.
from 15aa698 JAMES-2666 Rename SwarmGenericContainer into DockerGenericContainer
new 32f0972 TimeConverter should not convert duration to seconds but use java.time types
new 59bd643 MIME4J-0.8.3 Upgrade to mime4J 0.8.3
new 28360f8 JAMES-2699 Add documentation for Mailet API deprecation
The 3 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
Summary of changes:
.../custom/mailets/IsDelayedForMoreThan.java | 5 +-
.../mailing/QuotaMailingListenerConfiguration.java | 4 +-
pom.xml | 2 +-
.../core/fastfail/AbstractGreylistHandler.java | 38 +-
.../modules/mailbox/TikaConfigurationReader.java | 4 +-
.../java/org/apache/james/util/TimeConverter.java | 72 ++--
.../org/apache/james/util/TimeConverterTest.java | 398 +++------------------
.../transport/mailets/remote/delivery/Delay.java | 28 +-
.../mailets/remote/delivery/DelaysAndMaxRetry.java | 9 +-
.../mailets/remote/delivery/DeliveryRunnable.java | 8 +-
.../delivery/RemoteDeliveryConfiguration.java | 5 +-
.../mailets/remote/delivery/DelayTest.java | 19 +-
.../remote/delivery/DelaysAndMaxRetryTest.java | 24 +-
.../remote/delivery/DeliveryRunnableTest.java | 13 +-
.../delivery/RemoteDeliveryConfigurationTest.java | 3 +-
.../SetMessagesOutboxFlagUpdateTest.java | 3 +-
.../james/protocols/lib/POP3BeforeSMTPHelper.java | 19 +-
.../james/smtpserver/POP3BeforeSMTPHandler.java | 6 +-
.../smtpserver/fastfail/JDBCGreylistHandler.java | 23 +-
.../smtpserver/POP3BeforeSMTPHandlerTest.java | 3 +-
.../queue/activemq/ActiveMQMailQueueBlobTest.java | 6 +-
server/queue/queue-api/pom.xml | 4 +
.../java/org/apache/james/queue/api/MailQueue.java | 17 +-
.../james/queue/api/DelayedMailQueueContract.java | 5 +-
.../org/apache/james/queue/file/FileMailQueue.java | 19 +-
.../org/apache/james/queue/jms/JMSMailQueue.java | 19 +-
.../james/queue/memory/MemoryMailQueueFactory.java | 14 +-
.../james/queue/rabbitmq/RabbitMQMailQueue.java | 8 +-
.../CassandraMailQueueViewConfiguration.java | 3 +-
upgrade-instructions.md | 18 +
30 files changed, 274 insertions(+), 525 deletions(-)
---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org
[james-project] 02/03: MIME4J-0.8.3 Upgrade to mime4J 0.8.3
Posted by bt...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
btellier pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/james-project.git
commit 59bd643157475d1ceecebd6cc37c73d2c2bb609c
Author: Benoit Tellier <bt...@linagora.com>
AuthorDate: Fri Mar 22 14:58:17 2019 +0700
MIME4J-0.8.3 Upgrade to mime4J 0.8.3
---
pom.xml | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/pom.xml b/pom.xml
index bed7ab4..23f959f 100644
--- a/pom.xml
+++ b/pom.xml
@@ -586,7 +586,7 @@
<james.groupId>org.apache.james</james.groupId>
<james.protocols.groupId>${james.groupId}.protocols</james.protocols.groupId>
<activemq.version>5.15.5</activemq.version>
- <apache-mime4j.version>0.8.2</apache-mime4j.version>
+ <apache-mime4j.version>0.8.3</apache-mime4j.version>
<apache.openjpa.version>3.0.0</apache.openjpa.version>
<camel.version>2.22.1</camel.version>
<derby.version>10.14.2.0</derby.version>
---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org
[james-project] 01/03: TimeConverter should not convert duration to
seconds but use java.time types
Posted by bt...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
btellier pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/james-project.git
commit 32f097272bda172dfab80602725048e1350f612c
Author: Matthieu Baechler <ma...@apache.org>
AuthorDate: Tue Mar 26 09:08:04 2019 +0100
TimeConverter should not convert duration to seconds but use java.time types
---
.../custom/mailets/IsDelayedForMoreThan.java | 5 +-
.../mailing/QuotaMailingListenerConfiguration.java | 4 +-
.../core/fastfail/AbstractGreylistHandler.java | 38 +-
.../modules/mailbox/TikaConfigurationReader.java | 4 +-
.../java/org/apache/james/util/TimeConverter.java | 72 ++--
.../org/apache/james/util/TimeConverterTest.java | 398 +++------------------
.../transport/mailets/remote/delivery/Delay.java | 28 +-
.../mailets/remote/delivery/DelaysAndMaxRetry.java | 9 +-
.../mailets/remote/delivery/DeliveryRunnable.java | 8 +-
.../delivery/RemoteDeliveryConfiguration.java | 5 +-
.../mailets/remote/delivery/DelayTest.java | 19 +-
.../remote/delivery/DelaysAndMaxRetryTest.java | 24 +-
.../remote/delivery/DeliveryRunnableTest.java | 13 +-
.../delivery/RemoteDeliveryConfigurationTest.java | 3 +-
.../SetMessagesOutboxFlagUpdateTest.java | 3 +-
.../james/protocols/lib/POP3BeforeSMTPHelper.java | 19 +-
.../james/smtpserver/POP3BeforeSMTPHandler.java | 6 +-
.../smtpserver/fastfail/JDBCGreylistHandler.java | 23 +-
.../smtpserver/POP3BeforeSMTPHandlerTest.java | 3 +-
.../queue/activemq/ActiveMQMailQueueBlobTest.java | 6 +-
server/queue/queue-api/pom.xml | 4 +
.../java/org/apache/james/queue/api/MailQueue.java | 17 +-
.../james/queue/api/DelayedMailQueueContract.java | 5 +-
.../org/apache/james/queue/file/FileMailQueue.java | 19 +-
.../org/apache/james/queue/jms/JMSMailQueue.java | 19 +-
.../james/queue/memory/MemoryMailQueueFactory.java | 14 +-
.../james/queue/rabbitmq/RabbitMQMailQueue.java | 8 +-
.../CassandraMailQueueViewConfiguration.java | 3 +-
28 files changed, 255 insertions(+), 524 deletions(-)
diff --git a/examples/custom-mailets/src/main/java/org/apache/james/examples/custom/mailets/IsDelayedForMoreThan.java b/examples/custom-mailets/src/main/java/org/apache/james/examples/custom/mailets/IsDelayedForMoreThan.java
index 2c40d80..1c5c72b 100644
--- a/examples/custom-mailets/src/main/java/org/apache/james/examples/custom/mailets/IsDelayedForMoreThan.java
+++ b/examples/custom-mailets/src/main/java/org/apache/james/examples/custom/mailets/IsDelayedForMoreThan.java
@@ -21,6 +21,7 @@ package org.apache.james.examples.custom.mailets;
import java.time.Clock;
import java.time.Duration;
+import java.time.temporal.ChronoUnit;
import java.util.Collection;
import java.util.Date;
@@ -35,7 +36,7 @@ import com.google.common.collect.ImmutableList;
public class IsDelayedForMoreThan extends GenericMatcher {
- public static final TimeConverter.Unit DEFAULT_UNIT = TimeConverter.Unit.HOURS;
+ public static final ChronoUnit DEFAULT_UNIT = ChronoUnit.HOURS;
private final Clock clock;
private Duration maxDelay;
@@ -60,7 +61,7 @@ public class IsDelayedForMoreThan extends GenericMatcher {
@Override
public void init() {
String condition = getCondition();
- maxDelay = Duration.ofMillis(TimeConverter.getMilliSeconds(condition, DEFAULT_UNIT));
+ maxDelay = TimeConverter.parseDuration(condition, DEFAULT_UNIT);
}
@Override
diff --git a/mailbox/plugin/quota-mailing/src/main/java/org/apache/james/mailbox/quota/mailing/QuotaMailingListenerConfiguration.java b/mailbox/plugin/quota-mailing/src/main/java/org/apache/james/mailbox/quota/mailing/QuotaMailingListenerConfiguration.java
index fce7d35..ab29dd3 100644
--- a/mailbox/plugin/quota-mailing/src/main/java/org/apache/james/mailbox/quota/mailing/QuotaMailingListenerConfiguration.java
+++ b/mailbox/plugin/quota-mailing/src/main/java/org/apache/james/mailbox/quota/mailing/QuotaMailingListenerConfiguration.java
@@ -20,6 +20,7 @@
package org.apache.james.mailbox.quota.mailing;
import java.time.Duration;
+import java.time.temporal.ChronoUnit;
import java.util.Arrays;
import java.util.Collection;
import java.util.Objects;
@@ -75,8 +76,7 @@ public class QuotaMailingListenerConfiguration {
private static Optional<Duration> readGracePeriod(HierarchicalConfiguration config) {
return Optional.ofNullable(config.getString(XmlKeys.GRACE_PERIOD, null))
- .map(string -> TimeConverter.getMilliSeconds(string, TimeConverter.Unit.DAYS))
- .map(Duration::ofMillis);
+ .map(string -> TimeConverter.parseDuration(string, ChronoUnit.DAYS));
}
private static ImmutableMap<QuotaThreshold, RenderingInformation> readThresholds(HierarchicalConfiguration config) {
diff --git a/protocols/smtp/src/main/java/org/apache/james/protocols/smtp/core/fastfail/AbstractGreylistHandler.java b/protocols/smtp/src/main/java/org/apache/james/protocols/smtp/core/fastfail/AbstractGreylistHandler.java
index 9ccff46..2095f3d 100644
--- a/protocols/smtp/src/main/java/org/apache/james/protocols/smtp/core/fastfail/AbstractGreylistHandler.java
+++ b/protocols/smtp/src/main/java/org/apache/james/protocols/smtp/core/fastfail/AbstractGreylistHandler.java
@@ -19,6 +19,8 @@
package org.apache.james.protocols.smtp.core.fastfail;
+import java.time.Duration;
+import java.time.Instant;
import java.util.Iterator;
import org.apache.james.core.MailAddress;
@@ -41,14 +43,9 @@ import org.slf4j.LoggerFactory;
public abstract class AbstractGreylistHandler implements RcptHook {
private static final Logger LOGGER = LoggerFactory.getLogger(AbstractGreylistHandler.class);
- /** 1 hour */
- private long tempBlockTime = 3600000;
-
- /** 36 days */
- private long autoWhiteListLifeTime = 3110400000L;
-
- /** 4 hours */
- private long unseenLifeTime = 14400000;
+ private Duration tempBlockTime = Duration.ofHours(1);
+ private Duration autoWhiteListLifeTime = Duration.ofDays(36);
+ private Duration unseenLifeTime = Duration.ofHours(4);
private static final HookResult TO_FAST = HookResult.builder()
@@ -64,15 +61,15 @@ public abstract class AbstractGreylistHandler implements RcptHook {
+ " Temporary rejected: Please try again later")
.build();
- public void setUnseenLifeTime(long unseenLifeTime) {
+ public void setUnseenLifeTime(Duration unseenLifeTime) {
this.unseenLifeTime = unseenLifeTime;
}
- public void setAutoWhiteListLifeTime(long autoWhiteListLifeTime) {
+ public void setAutoWhiteListLifeTime(Duration autoWhiteListLifeTime) {
this.autoWhiteListLifeTime = autoWhiteListLifeTime;
}
- public void setTempBlockTime(long tempBlockTime) {
+ public void setTempBlockTime(Duration tempBlockTime) {
this.tempBlockTime = tempBlockTime;
}
@@ -85,7 +82,7 @@ public abstract class AbstractGreylistHandler implements RcptHook {
recip = recipAddress.toString();
}
- long time = System.currentTimeMillis();
+ Instant time = Instant.now();
String ipAddress = session.getRemoteAddress().getAddress().getHostAddress();
try {
@@ -105,9 +102,9 @@ public abstract class AbstractGreylistHandler implements RcptHook {
// if the timestamp is bigger as 0 we have allready a triplet stored
if (createTimeStamp > 0) {
- long acceptTime = createTimeStamp + tempBlockTime;
+ Instant acceptTime = Instant.ofEpochMilli(createTimeStamp).plus(tempBlockTime);
- if ((time < acceptTime) && (count == 0)) {
+ if ((time.isBefore(acceptTime)) && (count == 0)) {
return TO_FAST;
} else {
@@ -133,8 +130,8 @@ public abstract class AbstractGreylistHandler implements RcptHook {
LOGGER.debug("Delete old entries");
- cleanupAutoWhiteListGreyList(time - autoWhiteListLifeTime);
- cleanupGreyList(time - unseenLifeTime);
+ cleanupAutoWhiteListGreyList(time.minus(autoWhiteListLifeTime));
+ cleanupGreyList(time.minus(unseenLifeTime));
}
} catch (Exception e) {
@@ -172,9 +169,8 @@ public abstract class AbstractGreylistHandler implements RcptHook {
* The count
* @param createTime
* The createTime
- * @throws SQLException
*/
- protected abstract void insertTriplet(String ipAddress, String sender, String recip, int count, long createTime)
+ protected abstract void insertTriplet(String ipAddress, String sender, String recip, int count, Instant createTime)
throws Exception;
/**
@@ -193,7 +189,7 @@ public abstract class AbstractGreylistHandler implements RcptHook {
* the current time in ms
* @throws Exception
*/
- protected abstract void updateTriplet(String ipAddress, String sender, String recip, int count, long time) throws Exception;
+ protected abstract void updateTriplet(String ipAddress, String sender, String recip, int count, Instant time) throws Exception;
/**
@@ -203,7 +199,7 @@ public abstract class AbstractGreylistHandler implements RcptHook {
* The time which must be reached before delete the records
* @throws Exception
*/
- protected abstract void cleanupAutoWhiteListGreyList(long time)throws Exception;
+ protected abstract void cleanupAutoWhiteListGreyList(Instant time)throws Exception;
/**
* Delete old entries from the Greylist datarecord
@@ -212,7 +208,7 @@ public abstract class AbstractGreylistHandler implements RcptHook {
* The time which must be reached before delete the records
* @throws Exception
*/
- protected abstract void cleanupGreyList(long time) throws Exception;
+ protected abstract void cleanupGreyList(Instant time) throws Exception;
diff --git a/server/container/guice/cassandra-guice/src/main/java/org/apache/james/modules/mailbox/TikaConfigurationReader.java b/server/container/guice/cassandra-guice/src/main/java/org/apache/james/modules/mailbox/TikaConfigurationReader.java
index fa7438a..07d1936 100644
--- a/server/container/guice/cassandra-guice/src/main/java/org/apache/james/modules/mailbox/TikaConfigurationReader.java
+++ b/server/container/guice/cassandra-guice/src/main/java/org/apache/james/modules/mailbox/TikaConfigurationReader.java
@@ -20,6 +20,7 @@
package org.apache.james.modules.mailbox;
import java.time.Duration;
+import java.time.temporal.ChronoUnit;
import java.util.Optional;
import java.util.Set;
@@ -63,8 +64,7 @@ public class TikaConfigurationReader {
Optional<Duration> cacheEvictionPeriod = Optional.ofNullable(
configuration.getString(TIKA_CACHE_EVICTION_PERIOD,
null))
- .map(rawString -> TimeConverter.getMilliSeconds(rawString, TimeConverter.Unit.SECONDS))
- .map(Duration::ofMillis);
+ .map(rawString -> TimeConverter.parseDuration(rawString, ChronoUnit.SECONDS));
Optional<Long> cacheWeight = Optional.ofNullable(
configuration.getString(TIKA_CACHE_WEIGHT_MAX, null))
diff --git a/server/container/util/src/main/java/org/apache/james/util/TimeConverter.java b/server/container/util/src/main/java/org/apache/james/util/TimeConverter.java
index 6927802..bc7c3cb 100644
--- a/server/container/util/src/main/java/org/apache/james/util/TimeConverter.java
+++ b/server/container/util/src/main/java/org/apache/james/util/TimeConverter.java
@@ -18,9 +18,12 @@
****************************************************************/
package org.apache.james.util;
+import java.time.Duration;
+import java.time.temporal.ChronoUnit;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
+import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -30,29 +33,32 @@ import com.google.common.collect.ImmutableList;
public class TimeConverter {
private static final String PATTERN_STRING = "\\s*([0-9]+)\\s*([a-z,A-Z]*)\\s*";
+ private static final int AMOUNT = 1;
+ private static final int UNIT = 2;
private static Pattern PATTERN = Pattern.compile(PATTERN_STRING);
- public enum Unit {
- MILLI_SECONDS(ImmutableList.of("ms", "msec", "msecs"), 1),
- SECONDS(ImmutableList.of("s", "sec", "secs"), 1000),
- MINUTES(ImmutableList.of("m", "min", "mins", "minute", "minutes"), 1000 * 60),
- HOURS(ImmutableList.of("h", "hour", "hours"), 1000 * 60 * 60),
- DAYS(ImmutableList.of("d", "day", "days"), 1000 * 60 * 60 * 24);
+ private enum Unit {
+ MILLI_SECONDS(ImmutableList.of("ms", "msec", "msecs"), ChronoUnit.MILLIS),
+ SECONDS(ImmutableList.of("s", "sec", "secs"), ChronoUnit.SECONDS),
+ MINUTES(ImmutableList.of("m", "min", "mins", "minute", "minutes"), ChronoUnit.MINUTES),
+ HOURS(ImmutableList.of("h", "hour", "hours"), ChronoUnit.HOURS),
+ DAYS(ImmutableList.of("d", "day", "days"), ChronoUnit.DAYS);
- public static Unit parse(String string) {
+ public static ChronoUnit parse(String string) {
return Arrays.stream(values())
.filter(value -> value.validPatterns.contains(string.toLowerCase(Locale.US)))
.findFirst()
+ .map(entry -> entry.unit)
.orElseThrow(() -> new NumberFormatException("Unknown unit: " + string));
}
private final List<String> validPatterns;
- private final int multiplier;
+ private final ChronoUnit unit;
- Unit(List<String> validPatterns, int multiplier) {
+ Unit(List<String> validPatterns, ChronoUnit unit) {
this.validPatterns = validPatterns;
- this.multiplier = multiplier;
+ this.unit = unit;
}
}
@@ -62,50 +68,28 @@ public class TimeConverter {
}
/**
- * Helper method to get the milliseconds for the given amount and unit
- *
- * @param amount
- * The amount for use with the unit
- * @param unit
- * The unit
- * @return The time in milliseconds
- * @throws NumberFormatException
- * Get thrown if an illegal unit was used
- */
- public static long getMilliSeconds(long amount, String unit) throws NumberFormatException {
- return getMilliSeconds(amount, Unit.parse(unit));
- }
-
- public static long getMilliSeconds(long amount, Unit unit) throws NumberFormatException {
- int multiplier = unit.multiplier;
- return (amount * multiplier);
- }
-
- /**
* Helper method to get the milliseconds for the given rawstring. Allowed
* rawstrings must match pattern: "\\s*([0-9]+)\\s*([a-z,A-Z]+)\\s*"
*
* @param rawString
* The rawstring which we use to extract the amount and unit
- * @return The time in milliseconds
+ * @return The duration
* @throws NumberFormatException
* Get thrown if an illegal rawString was used
*/
- public static long getMilliSeconds(String rawString) throws NumberFormatException {
- return getMilliSeconds(rawString, Unit.MILLI_SECONDS);
+ public static Duration parseDuration(String rawString) throws NumberFormatException {
+ return parseDuration(rawString, ChronoUnit.MILLIS);
}
- public static long getMilliSeconds(String rawString, Unit defaultUnit) throws NumberFormatException {
+ public static Duration parseDuration(String rawString, ChronoUnit defaultUnit) throws NumberFormatException {
Matcher res = PATTERN.matcher(rawString);
if (res.matches()) {
- if (res.group(1) != null && res.group(2) != null) {
- long time = Integer.parseInt(res.group(1).trim());
- String unit = res.group(2);
- if (Strings.isNullOrEmpty(unit)) {
- return getMilliSeconds(time, defaultUnit);
- }
- return getMilliSeconds(time, Unit.parse(unit));
+ if (res.group(AMOUNT) != null && res.group(UNIT) != null) {
+ long time = Integer.parseInt(res.group(AMOUNT).trim());
+ return parseUnitAsDuration(res.group(UNIT))
+ .orElse(defaultUnit.getDuration())
+ .multipliedBy(time);
} else {
// This should never Happen anyway throw an exception
throw new NumberFormatException("The supplied String is not a supported format " + rawString);
@@ -116,4 +100,10 @@ public class TimeConverter {
}
}
+ private static Optional<Duration> parseUnitAsDuration(String unit) {
+ if (Strings.isNullOrEmpty(unit)) {
+ return Optional.empty();
+ }
+ return Optional.of(Unit.parse(unit).getDuration());
+ }
}
diff --git a/server/container/util/src/test/java/org/apache/james/util/TimeConverterTest.java b/server/container/util/src/test/java/org/apache/james/util/TimeConverterTest.java
index 2d64018..ca9fe83 100644
--- a/server/container/util/src/test/java/org/apache/james/util/TimeConverterTest.java
+++ b/server/container/util/src/test/java/org/apache/james/util/TimeConverterTest.java
@@ -21,456 +21,160 @@ package org.apache.james.util;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
-import java.util.concurrent.TimeUnit;
+import java.time.Duration;
+import java.time.temporal.ChronoUnit;
import org.junit.jupiter.api.Test;
-public class TimeConverterTest {
+class TimeConverterTest {
@Test
void getMilliSecondsShouldConvertValueWhenNoUnitAmountAsString() {
- //Given
- long expected = 2;
- //When
- long actual = TimeConverter.getMilliSeconds("2");
- //Then
- assertThat(actual).isEqualTo(expected);
+ Duration actual = TimeConverter.parseDuration("2");
+ assertThat(actual).isEqualTo(Duration.ofMillis(2));
}
@Test
void getMilliSecondsShouldUseProvidedUnitWhenNoUnitAmountAsString() {
- //Given
- long expected = 2000;
- //When
- long actual = TimeConverter.getMilliSeconds("2", TimeConverter.Unit.SECONDS);
- //Then
- assertThat(actual).isEqualTo(expected);
+ Duration actual = TimeConverter.parseDuration("2", ChronoUnit.SECONDS);
+ assertThat(actual).isEqualTo(Duration.ofSeconds(2));
}
@Test
void getMilliSecondsShouldNotUseProvidedUnitWhenNoUnitAmountAsString() {
- //Given
- long expected = 120000;
- //When
- long actual = TimeConverter.getMilliSeconds("2 minutes", TimeConverter.Unit.SECONDS);
- //Then
- assertThat(actual).isEqualTo(expected);
- }
-
- @Test
- void getMilliSecondsShouldConvertValueWhenMsecUnit() {
- //Given
- long expected = 2;
- //When
- long actual = TimeConverter.getMilliSeconds(2, "msec");
- //Then
- assertThat(actual).isEqualTo(expected);
+ Duration actual = TimeConverter.parseDuration("2 minutes", ChronoUnit.SECONDS);
+ assertThat(actual).isEqualTo(Duration.ofMinutes(2));
}
@Test
void getMilliSecondsShouldConvertValueWhenMsecAmountAsString() {
- //Given
- long expected = 2;
- //When
- long actual = TimeConverter.getMilliSeconds("2 msec");
- //Then
- assertThat(actual).isEqualTo(expected);
- }
-
- @Test
- void getMilliSecondsShouldConvertValueWhenMsUnit() {
- //Given
- long expected = 2;
- //When
- long actual = TimeConverter.getMilliSeconds(2, "ms");
- //Then
- assertThat(actual).isEqualTo(expected);
+ Duration actual = TimeConverter.parseDuration("2 msec");
+ assertThat(actual).isEqualTo(Duration.ofMillis(2));
}
@Test
void getMilliSecondsShouldConvertValueWhenMsAmountAsString() {
- //Given
- long expected = 2;
- //When
- long actual = TimeConverter.getMilliSeconds("2 ms");
- //Then
- assertThat(actual).isEqualTo(expected);
- }
-
- @Test
- void getMilliSecondsShouldConvertValueWhenMsUnitCapital() {
- //Given
- long expected = 2;
- //When
- long actual = TimeConverter.getMilliSeconds(2, "Ms");
- //Then
- assertThat(actual).isEqualTo(expected);
+ Duration actual = TimeConverter.parseDuration("2 ms");
+ assertThat(actual).isEqualTo(Duration.ofMillis(2));
}
@Test
void getMilliSecondsShouldConvertValueWhenMsCapitalAmountAsString() {
- //Given
- long expected = 2;
- //When
- long actual = TimeConverter.getMilliSeconds("2 Ms");
- //Then
- assertThat(actual).isEqualTo(expected);
- }
-
- @Test
- void getMilliSecondsShouldConvertValueWhenMsecsUnit() {
- //Given
- long expected = 2;
- //When
- long actual = TimeConverter.getMilliSeconds(2, "msecs");
- //Then
- assertThat(actual).isEqualTo(expected);
+ Duration actual = TimeConverter.parseDuration("2 Ms");
+ assertThat(actual).isEqualTo(Duration.ofMillis(2));
}
@Test
void getMilliSecondsShouldConvertValueWhenMsecsAmountAsString() {
- //Given
- long expected = 2;
- //When
- long actual = TimeConverter.getMilliSeconds("2 msecs");
- //Then
- assertThat(actual).isEqualTo(expected);
- }
-
- @Test
- void getMilliSecondsShouldConvertValueWhenSUnit() {
- //Given
- long expected = TimeUnit.SECONDS.toMillis(2);
- //When
- long actual = TimeConverter.getMilliSeconds(2, "s");
- //Then
- assertThat(actual).isEqualTo(expected);
+ Duration actual = TimeConverter.parseDuration("2 msecs");
+ assertThat(actual).isEqualTo(Duration.ofMillis(2));
}
@Test
void getMilliSecondsShouldConvertValueWhenSAmountAsString() {
- //Given
- long expected = TimeUnit.SECONDS.toMillis(2);
- //When
- long actual = TimeConverter.getMilliSeconds("2 s");
- //Then
- assertThat(actual).isEqualTo(expected);
- }
-
- @Test
- void getMilliSecondsShouldConvertValueWhenSecUnit() {
- //Given
- long expected = TimeUnit.SECONDS.toMillis(2);
- //When
- long actual = TimeConverter.getMilliSeconds(2, "sec");
- //Then
- assertThat(actual).isEqualTo(expected);
+ Duration actual = TimeConverter.parseDuration("2 s");
+ assertThat(actual).isEqualTo(Duration.ofSeconds(2));
}
@Test
void getMilliSecondsShouldConvertValueWhenSecAmountAsString() {
- //Given
- long expected = TimeUnit.SECONDS.toMillis(2);
- //When
- long actual = TimeConverter.getMilliSeconds("2 sec");
- //Then
- assertThat(actual).isEqualTo(expected);
- }
-
- @Test
- void getMilliSecondsShouldConvertValueWhenSecCapitalUnit() {
- //Given
- long expected = TimeUnit.SECONDS.toMillis(2);
- //When
- long actual = TimeConverter.getMilliSeconds(2, "Sec");
- //Then
- assertThat(actual).isEqualTo(expected);
+ Duration actual = TimeConverter.parseDuration("2 sec");
+ assertThat(actual).isEqualTo(Duration.ofSeconds(2));
}
@Test
void getMilliSecondsShouldConvertValueWhenSecCapitalAmountAsString() {
- //Given
- long expected = TimeUnit.SECONDS.toMillis(2);
- //When
- long actual = TimeConverter.getMilliSeconds("2 Sec");
- //Then
- assertThat(actual).isEqualTo(expected);
- }
-
- @Test
- void getMilliSecondsShouldConvertValueWhenSecsUnit() {
- long expected = TimeUnit.SECONDS.toMillis(2);
- //When
- long actual = TimeConverter.getMilliSeconds(2, "secs");
- //Then
- assertThat(actual).isEqualTo(expected);
+ Duration actual = TimeConverter.parseDuration("2 Sec");
+ assertThat(actual).isEqualTo(Duration.ofSeconds(2));
}
@Test
void getMilliSecondsShouldConvertValueWhenSecsAmountAsString() {
- //Given
- long expected = TimeUnit.SECONDS.toMillis(2);
- //When
- long actual = TimeConverter.getMilliSeconds("2 secs");
- //Then
- assertThat(actual).isEqualTo(expected);
- }
-
- @Test
- void getMilliSecondsShouldConvertValueWhenMUnit() {
- //Given
- long expected = TimeUnit.MINUTES.toMillis(2);
- //When
- long actual = TimeConverter.getMilliSeconds(2, "m");
- //Then
- assertThat(actual).isEqualTo(expected);
+ Duration actual = TimeConverter.parseDuration("2 secs");
+ assertThat(actual).isEqualTo(Duration.ofSeconds(2));
}
@Test
void getMilliSecondsShouldConvertValueWhenMAmountAsString() {
- //Given
- long expected = TimeUnit.MINUTES.toMillis(2);
- //When
- long actual = TimeConverter.getMilliSeconds("2 m");
- //Then
- assertThat(actual).isEqualTo(expected);
- }
-
- @Test
- void getMilliSecondsShouldConvertValueWhenMinuteUnit() {
- //Given
- long expected = TimeUnit.MINUTES.toMillis(2);
- //When
- long actual = TimeConverter.getMilliSeconds(2, "minute");
- //Then
- assertThat(actual).isEqualTo(expected);
+ Duration actual = TimeConverter.parseDuration("2 m");
+ assertThat(actual).isEqualTo(Duration.ofMinutes(2));
}
@Test
void getMilliSecondsShouldConvertValueWhenMinuteAmountAsString() {
- //Given
- long expected = TimeUnit.MINUTES.toMillis(2);
- //When
- long actual = TimeConverter.getMilliSeconds("2 minute");
- //Then
- assertThat(actual).isEqualTo(expected);
- }
-
- @Test
- void getMilliSecondsShouldConvertValueWhenMinuteCapitalUnit() {
- //Given
- long expected = TimeUnit.MINUTES.toMillis(2);
- //When
- long actual = TimeConverter.getMilliSeconds(2, "Minute");
- //Then
- assertThat(actual).isEqualTo(expected);
+ Duration actual = TimeConverter.parseDuration("2 minute");
+ assertThat(actual).isEqualTo(Duration.ofMinutes(2));
}
@Test
void getMilliSecondsShouldConvertValueWhenMinuteCapitalAmountAsString() {
- //Given
- long expected = TimeUnit.MINUTES.toMillis(2);
- //When
- long actual = TimeConverter.getMilliSeconds("2 Minute");
- //Then
- assertThat(actual).isEqualTo(expected);
- }
-
- @Test
- void getMilliSecondsShouldConvertValueWhenMinutesUnit() {
- //Given
- long expected = TimeUnit.MINUTES.toMillis(2);
- //When
- long actual = TimeConverter.getMilliSeconds(2, "minutes");
- //Then
- assertThat(actual).isEqualTo(expected);
+ Duration actual = TimeConverter.parseDuration("2 Minute");
+ assertThat(actual).isEqualTo(Duration.ofMinutes(2));
}
@Test
void getMilliSecondsShouldConvertValueWhenMinutesAmountAsString() {
- //Given
- long expected = TimeUnit.MINUTES.toMillis(2);
- //When
- long actual = TimeConverter.getMilliSeconds("2 minutes");
- //Then
- assertThat(actual).isEqualTo(expected);
- }
-
- @Test
- void getMilliSecondsShouldConvertValueWhenHUnit() {
- //Given
- long expected = TimeUnit.HOURS.toMillis(2);
- //When
- long actual = TimeConverter.getMilliSeconds(2, "h");
- //Then
- assertThat(actual).isEqualTo(expected);
+ Duration actual = TimeConverter.parseDuration("2 minutes");
+ assertThat(actual).isEqualTo(Duration.ofMinutes(2));
}
@Test
void getMilliSecondsShouldConvertValueWhenHAmountAsString() {
- //Given
- long expected = TimeUnit.HOURS.toMillis(2);
- //When
- long actual = TimeConverter.getMilliSeconds("2 h");
- //Then
- assertThat(actual).isEqualTo(expected);
- }
-
- @Test
- void getMilliSecondsShouldConvertValueWhenHourUnit() {
- //Given
- long expected = TimeUnit.HOURS.toMillis(2);
- //When
- long actual = TimeConverter.getMilliSeconds(2, "hour");
- //Then
- assertThat(actual).isEqualTo(expected);
+ Duration actual = TimeConverter.parseDuration("2 h");
+ assertThat(actual).isEqualTo(Duration.ofHours(2));
}
@Test
void getMilliSecondsShouldConvertValueWhenHourAmountAsString() {
- //Given
- long expected = TimeUnit.HOURS.toMillis(2);
- //When
- long actual = TimeConverter.getMilliSeconds("2 hour");
- //Then
- assertThat(actual).isEqualTo(expected);
- }
-
- @Test
- void getMilliSecondsShouldConvertValueWhenHourCapitalUnit() {
- //Given
- long expected = TimeUnit.HOURS.toMillis(2);
- //When
- long actual = TimeConverter.getMilliSeconds(2, "Hour");
- //Then
- assertThat(actual).isEqualTo(expected);
+ Duration actual = TimeConverter.parseDuration("2 hour");
+ assertThat(actual).isEqualTo(Duration.ofHours(2));
}
@Test
void getMilliSecondsShouldConvertValueWhenHourCapitalAmountAsString() {
- //Given
- long expected = TimeUnit.HOURS.toMillis(2);
- //When
- long actual = TimeConverter.getMilliSeconds("2 Hour");
- //Then
- assertThat(actual).isEqualTo(expected);
- }
-
- @Test
- void getMilliSecondsShouldConvertValueWhenHoursUnit() {
- //Given
- long expected = TimeUnit.HOURS.toMillis(2);
- //When
- long actual = TimeConverter.getMilliSeconds(2, "hours");
- //Then
- assertThat(actual).isEqualTo(expected);
+ Duration actual = TimeConverter.parseDuration("2 Hour");
+ assertThat(actual).isEqualTo(Duration.ofHours(2));
}
@Test
void getMilliSecondsShouldConvertValueWhenHoursAmountAsString() {
- //Given
- long expected = TimeUnit.HOURS.toMillis(2);
- //When
- long actual = TimeConverter.getMilliSeconds("2 hours");
- //Then
- assertThat(actual).isEqualTo(expected);
- }
-
- @Test
- void getMilliSecondsShouldConvertValueWhenDUnit() {
- //Given
- long expected = TimeUnit.DAYS.toMillis(2);
- //When
- long actual = TimeConverter.getMilliSeconds(2, "d");
- //Then
- assertThat(actual).isEqualTo(expected);
+ Duration actual = TimeConverter.parseDuration("2 hours");
+ assertThat(actual).isEqualTo(Duration.ofHours(2));
}
@Test
void getMilliSecondsShouldConvertValueWhenDAmountAsString() {
- //Given
- long expected = TimeUnit.DAYS.toMillis(2);
- //When
- long actual = TimeConverter.getMilliSeconds("2 d");
- //Then
- assertThat(actual).isEqualTo(expected);
- }
-
- @Test
- void getMilliSecondsShouldConvertValueWhenDayUnit() {
- //Given
- long expected = TimeUnit.DAYS.toMillis(2);
- //When
- long actual = TimeConverter.getMilliSeconds(2, "day");
- //Then
- assertThat(actual).isEqualTo(expected);
+ Duration actual = TimeConverter.parseDuration("2 d");
+ assertThat(actual).isEqualTo(Duration.ofDays(2));
}
@Test
void getMilliSecondsShouldConvertValueWhenDayAmountAsString() {
- //Given
- long expected = TimeUnit.DAYS.toMillis(2);
- //When
- long actual = TimeConverter.getMilliSeconds("2 day");
- //Then
- assertThat(actual).isEqualTo(expected);
- }
-
- @Test
- void getMilliSecondsShouldConvertValueWhenDayCapitalUnit() {
- //Given
- long expected = TimeUnit.DAYS.toMillis(2);
- //When
- long actual = TimeConverter.getMilliSeconds(2, "Day");
- //Then
- assertThat(actual).isEqualTo(expected);
+ Duration actual = TimeConverter.parseDuration("2 day");
+ assertThat(actual).isEqualTo(Duration.ofDays(2));
}
@Test
void getMilliSecondsShouldConvertValueWhenDayCapitalAmountAsString() {
- //Given
- long expected = TimeUnit.DAYS.toMillis(2);
- //When
- long actual = TimeConverter.getMilliSeconds("2 Day");
- //Then
- assertThat(actual).isEqualTo(expected);
- }
-
- @Test
- void getMilliSecondsShouldConvertValueWhenDaysUnit() {
- //Given
- long expected = TimeUnit.DAYS.toMillis(2);
- //When
- long actual = TimeConverter.getMilliSeconds(2, "days");
- //Then
- assertThat(actual).isEqualTo(expected);
+ Duration actual = TimeConverter.parseDuration("2 Day");
+ assertThat(actual).isEqualTo(Duration.ofDays(2));
}
@Test
void getMilliSecondsShouldConvertValueWhenDaysAmountAsString() {
- //Given
- long expected = TimeUnit.DAYS.toMillis(2);
- //When
- long actual = TimeConverter.getMilliSeconds("2 days");
- //Then
- assertThat(actual).isEqualTo(expected);
- }
-
- @Test
- void getMilliSecondsShouldThrowWhenIllegalUnitInUnit() {
- assertThatThrownBy(() -> TimeConverter.getMilliSeconds(2, "week"))
- .isInstanceOf(NumberFormatException.class);
+ Duration actual = TimeConverter.parseDuration("2 days");
+ assertThat(actual).isEqualTo(Duration.ofDays(2));
}
@Test
void getMilliSecondsShouldThrowWhenIllegalUnitInRawString() {
- assertThatThrownBy(() -> TimeConverter.getMilliSeconds("2 week"))
+ assertThatThrownBy(() -> TimeConverter.parseDuration("2 week"))
.isInstanceOf(NumberFormatException.class);
}
@Test
void getMilliSecondsShouldThrowWhenIllegalPattern() {
- assertThatThrownBy(() -> TimeConverter.getMilliSeconds("illegal pattern"))
+ assertThatThrownBy(() -> TimeConverter.parseDuration("illegal pattern"))
.isInstanceOf(NumberFormatException.class);
}
}
diff --git a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/remote/delivery/Delay.java b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/remote/delivery/Delay.java
index af50fdd..fd04df9 100644
--- a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/remote/delivery/Delay.java
+++ b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/remote/delivery/Delay.java
@@ -19,8 +19,8 @@
package org.apache.james.transport.mailets.remote.delivery;
+import java.time.Duration;
import java.util.List;
-import java.util.concurrent.TimeUnit;
import javax.mail.MessagingException;
@@ -46,51 +46,51 @@ public class Delay {
List<String> parts = Splitter.on('*').trimResults().splitToList(initString);
if (parts.size() == 1) {
- return new Delay(DEFAULT_ATTEMPTS, TimeConverter.getMilliSeconds(parts.get(0)));
+ return new Delay(DEFAULT_ATTEMPTS, TimeConverter.parseDuration(parts.get(0)));
}
if (parts.size() == 2) {
int attempts = Integer.parseInt(parts.get(0));
if (attempts < 0) {
throw new MessagingException("Number of attempts negative in " + initString);
}
- return new Delay(attempts, TimeConverter.getMilliSeconds(parts.get(1)));
+ return new Delay(attempts, TimeConverter.parseDuration(parts.get(1)));
}
throw new MessagingException(initString + " contains too much parts");
}
- public static final long DEFAULT_DELAY_TIME = TimeUnit.HOURS.toMillis(6);
+ public static final Duration DEFAULT_DELAY_TIME = Duration.ofHours(6);
public static final int DEFAULT_ATTEMPTS = 1;
private final int attempts;
- private final long delayTimeInMs;
+ private final Duration delayTime;
public Delay() {
this(DEFAULT_ATTEMPTS, DEFAULT_DELAY_TIME);
}
@VisibleForTesting
- Delay(int attempts, long delayTime) {
+ Delay(int attempts, Duration delayTime) {
this.attempts = attempts;
- this.delayTimeInMs = delayTime;
+ this.delayTime = delayTime;
}
- public long getDelayTimeInMs() {
- return delayTimeInMs;
+ public Duration getDelayTime() {
+ return delayTime;
}
public int getAttempts() {
return attempts;
}
- public List<Long> getExpendendDelays() {
- return Repeat.repeat(delayTimeInMs, attempts);
+ public List<Duration> getExpendendDelays() {
+ return Repeat.repeat(delayTime, attempts);
}
@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("attempts", attempts)
- .add("delayTime", delayTimeInMs)
+ .add("delayTime", delayTime)
.toString();
}
@@ -100,13 +100,13 @@ public class Delay {
Delay that = (Delay) o;
return Objects.equal(this.attempts, that.attempts)
- && Objects.equal(this.delayTimeInMs, that.delayTimeInMs);
+ && Objects.equal(this.delayTime, that.delayTime);
}
return false;
}
@Override
public int hashCode() {
- return Objects.hashCode(attempts, delayTimeInMs);
+ return Objects.hashCode(attempts, delayTime);
}
}
diff --git a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/remote/delivery/DelaysAndMaxRetry.java b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/remote/delivery/DelaysAndMaxRetry.java
index 7936f3d..8279b58 100644
--- a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/remote/delivery/DelaysAndMaxRetry.java
+++ b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/remote/delivery/DelaysAndMaxRetry.java
@@ -19,6 +19,7 @@
package org.apache.james.transport.mailets.remote.delivery;
+import java.time.Duration;
import java.util.List;
import javax.mail.MessagingException;
@@ -63,12 +64,12 @@ public class DelaysAndMaxRetry {
private static DelaysAndMaxRetry addExtraAttemptToLastDelay(int intendedMaxRetries, int extra, List<Delay> delayTimesList) throws MessagingException {
if (delayTimesList.size() != 0) {
Delay lastDelay = delayTimesList.get(delayTimesList.size() - 1);
- LOGGER.warn("Delay of {} msecs is now attempted: {} times", lastDelay.getDelayTimeInMs(), lastDelay.getAttempts());
+ LOGGER.warn("Delay of {} msecs is now attempted: {} times", lastDelay.getDelayTime(), lastDelay.getAttempts());
return new DelaysAndMaxRetry(intendedMaxRetries,
ImmutableList.copyOf(
Iterables.concat(
Iterables.limit(delayTimesList, delayTimesList.size() - 1),
- ImmutableList.of(new Delay(lastDelay.getAttempts() + extra, lastDelay.getDelayTimeInMs())))));
+ ImmutableList.of(new Delay(lastDelay.getAttempts() + extra, lastDelay.getDelayTime())))));
} else {
throw new MessagingException("No delaytimes, cannot continue");
}
@@ -135,8 +136,8 @@ public class DelaysAndMaxRetry {
* @param list the list to expand
* @return the expanded list
*/
- public List<Long> getExpandedDelays() {
- ImmutableList.Builder<Long> builder = ImmutableList.builder();
+ public ImmutableList<Duration> getExpandedDelays() {
+ ImmutableList.Builder<Duration> builder = ImmutableList.builder();
for (Delay delay: delays) {
builder.addAll(delay.getExpendendDelays());
}
diff --git a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/remote/delivery/DeliveryRunnable.java b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/remote/delivery/DeliveryRunnable.java
index ddaa71a..b387c6a 100644
--- a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/remote/delivery/DeliveryRunnable.java
+++ b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/remote/delivery/DeliveryRunnable.java
@@ -19,8 +19,8 @@
package org.apache.james.transport.mailets.remote.delivery;
+import java.time.Duration;
import java.util.Date;
-import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Supplier;
@@ -155,16 +155,16 @@ public class DeliveryRunnable implements Disposable {
DeliveryRetriesHelper.incrementRetries(mail);
mail.setLastUpdated(dateSupplier.get());
// Something happened that will delay delivery. Store it back in the retry repository.
- long delay = getNextDelay(DeliveryRetriesHelper.retrieveRetries(mail));
+ Duration delay = getNextDelay(DeliveryRetriesHelper.retrieveRetries(mail));
if (configuration.isUsePriority()) {
// Use lowest priority for retries. See JAMES-1311
mail.setAttribute(MailPrioritySupport.LOW_PRIORITY_ATTRIBUTE);
}
- queue.enQueue(mail, delay, TimeUnit.MILLISECONDS);
+ queue.enQueue(mail, delay);
}
- private long getNextDelay(int retry_count) {
+ private Duration getNextDelay(int retry_count) {
if (retry_count > configuration.getDelayTimes().size()) {
return Delay.DEFAULT_DELAY_TIME;
}
diff --git a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/remote/delivery/RemoteDeliveryConfiguration.java b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/remote/delivery/RemoteDeliveryConfiguration.java
index 63f5451..aca6f2d 100644
--- a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/remote/delivery/RemoteDeliveryConfiguration.java
+++ b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/remote/delivery/RemoteDeliveryConfiguration.java
@@ -19,6 +19,7 @@
package org.apache.james.transport.mailets.remote.delivery;
+import java.time.Duration;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
@@ -79,7 +80,7 @@ public class RemoteDeliveryConfiguration {
private final int dnsProblemRetry;
private final int connectionTimeout;
private final int workersThreadCount;
- private final List<Long> delayTimes;
+ private final List<Duration> delayTimes;
private final HeloNameProvider heloNameProvider;
private final String outGoingQueueName;
private final String bindAddress;
@@ -242,7 +243,7 @@ public class RemoteDeliveryConfiguration {
return isDebug;
}
- public List<Long> getDelayTimes() {
+ public List<Duration> getDelayTimes() {
return delayTimes;
}
diff --git a/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/remote/delivery/DelayTest.java b/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/remote/delivery/DelayTest.java
index 3d55634..f619d28 100644
--- a/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/remote/delivery/DelayTest.java
+++ b/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/remote/delivery/DelayTest.java
@@ -21,9 +21,10 @@ package org.apache.james.transport.mailets.remote.delivery;
import static org.assertj.core.api.Assertions.assertThat;
+import java.time.Duration;
+
import javax.mail.MessagingException;
-import org.apache.james.transport.mailets.remote.delivery.Delay;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
@@ -41,33 +42,33 @@ public class DelayTest {
@Test
public void stringConstructorShouldWorkForNumbers() throws Exception {
- assertThat(Delay.from("36")).isEqualTo(new Delay(Delay.DEFAULT_ATTEMPTS, 36));
+ assertThat(Delay.from("36")).isEqualTo(new Delay(Delay.DEFAULT_ATTEMPTS, Duration.ofMillis(36)));
}
@Test
public void stringConstructorShouldWorkForZero() throws Exception {
- assertThat(Delay.from("0")).isEqualTo(new Delay(Delay.DEFAULT_ATTEMPTS, 0));
+ assertThat(Delay.from("0")).isEqualTo(new Delay(Delay.DEFAULT_ATTEMPTS, Duration.ofMillis(0)));
}
@Test
public void stringConstructorShouldThrowOnNegativeNumbers() throws Exception {
expectedException.expect(NumberFormatException.class);
- assertThat(Delay.from("-1s")).isEqualTo(new Delay(Delay.DEFAULT_ATTEMPTS, 0));
+ assertThat(Delay.from("-1s")).isEqualTo(new Delay(Delay.DEFAULT_ATTEMPTS, Duration.ofMillis(0)));
}
@Test
public void stringConstructorShouldWorkForNumberAndSecond() throws Exception {
- assertThat(Delay.from("1s")).isEqualTo(new Delay(Delay.DEFAULT_ATTEMPTS, 1000));
+ assertThat(Delay.from("1s")).isEqualTo(new Delay(Delay.DEFAULT_ATTEMPTS, Duration.ofSeconds(1)));
}
@Test
public void stringConstructorShouldWorkForNumberAndAttempts() throws Exception {
- assertThat(Delay.from("2*36")).isEqualTo(new Delay(2, 36));
+ assertThat(Delay.from("2*36")).isEqualTo(new Delay(2, Duration.ofMillis(36)));
}
@Test
public void stringConstructorShouldWorkForNumberAndZeroAttempts() throws Exception {
- assertThat(Delay.from("0*36")).isEqualTo(new Delay(0, 36));
+ assertThat(Delay.from("0*36")).isEqualTo(new Delay(0, Duration.ofMillis(36)));
}
@Test
@@ -93,12 +94,12 @@ public class DelayTest {
@Test
public void stringConstructorShouldWorkForNumberAttemptsAndUnit() throws Exception {
- assertThat(Delay.from("2*36s")).isEqualTo(new Delay(2, 36000));
+ assertThat(Delay.from("2*36s")).isEqualTo(new Delay(2, Duration.ofSeconds(36)));
}
@Test
public void stringConstructorShouldWorkForNumberAttemptsAndUnitWithSpaces() throws Exception {
- assertThat(Delay.from("2 * 36 s")).isEqualTo(new Delay(2, 36000));
+ assertThat(Delay.from("2 * 36 s")).isEqualTo(new Delay(2, Duration.ofSeconds(36)));
}
@Test
diff --git a/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/remote/delivery/DelaysAndMaxRetryTest.java b/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/remote/delivery/DelaysAndMaxRetryTest.java
index 7fb1895..d72ecfc 100644
--- a/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/remote/delivery/DelaysAndMaxRetryTest.java
+++ b/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/remote/delivery/DelaysAndMaxRetryTest.java
@@ -21,10 +21,10 @@ package org.apache.james.transport.mailets.remote.delivery;
import static org.assertj.core.api.Assertions.assertThat;
+import java.time.Duration;
+
import javax.mail.MessagingException;
-import org.apache.james.transport.mailets.remote.delivery.Delay;
-import org.apache.james.transport.mailets.remote.delivery.DelaysAndMaxRetry;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
@@ -40,7 +40,7 @@ public class DelaysAndMaxRetryTest {
public void fromShouldParseSingleDelay() throws Exception {
DelaysAndMaxRetry actual = DelaysAndMaxRetry.from(1, "1s");
- DelaysAndMaxRetry expected = new DelaysAndMaxRetry(1, ImmutableList.of(new Delay(1, 1000)));
+ DelaysAndMaxRetry expected = new DelaysAndMaxRetry(1, ImmutableList.of(new Delay(1, Duration.ofSeconds(1))));
assertThat(actual).isEqualTo(expected);
}
@@ -49,7 +49,7 @@ public class DelaysAndMaxRetryTest {
public void fromShouldParseTwoDelays() throws Exception {
DelaysAndMaxRetry actual = DelaysAndMaxRetry.from(2, "1s,2s");
- DelaysAndMaxRetry expected = new DelaysAndMaxRetry(2, ImmutableList.of(new Delay(1, 1000), new Delay(1, 2000)));
+ DelaysAndMaxRetry expected = new DelaysAndMaxRetry(2, ImmutableList.of(new Delay(1, Duration.ofSeconds(1)), new Delay(1, Duration.ofSeconds(2))));
assertThat(actual).isEqualTo(expected);
}
@@ -58,7 +58,7 @@ public class DelaysAndMaxRetryTest {
public void fromShouldAdaptMaxRetriesWhenUnderAttempts() throws Exception {
DelaysAndMaxRetry actual = DelaysAndMaxRetry.from(1, "1s,2*2s");
- DelaysAndMaxRetry expected = new DelaysAndMaxRetry(3, ImmutableList.of(new Delay(1, 1000), new Delay(2, 2000)));
+ DelaysAndMaxRetry expected = new DelaysAndMaxRetry(3, ImmutableList.of(new Delay(1, Duration.ofSeconds(1)), new Delay(2, Duration.ofSeconds(2))));
assertThat(actual).isEqualTo(expected);
}
@@ -67,7 +67,7 @@ public class DelaysAndMaxRetryTest {
public void fromShouldAdaptDelaysWhenUnderMaxRetries() throws Exception {
DelaysAndMaxRetry actual = DelaysAndMaxRetry.from(4, "1s,2*2s");
- DelaysAndMaxRetry expected = new DelaysAndMaxRetry(4, ImmutableList.of(new Delay(1, 1000), new Delay(3, 2000)));
+ DelaysAndMaxRetry expected = new DelaysAndMaxRetry(4, ImmutableList.of(new Delay(1, Duration.ofSeconds(1)), new Delay(3, Duration.ofSeconds(2))));
assertThat(actual).isEqualTo(expected);
}
@@ -85,7 +85,7 @@ public class DelaysAndMaxRetryTest {
public void fromShouldIgnoreEmptyDelay() throws Exception {
DelaysAndMaxRetry actual = DelaysAndMaxRetry.from(1, "1s,,2s");
- DelaysAndMaxRetry expected = new DelaysAndMaxRetry(2, ImmutableList.of(new Delay(1, 1000), new Delay(1, 2000)));
+ DelaysAndMaxRetry expected = new DelaysAndMaxRetry(2, ImmutableList.of(new Delay(1, Duration.ofSeconds(1)), new Delay(1, Duration.ofSeconds(2))));
assertThat(actual).isEqualTo(expected);
}
@@ -94,7 +94,7 @@ public class DelaysAndMaxRetryTest {
public void fromShouldHandleParsingFailures() throws Exception {
DelaysAndMaxRetry actual = DelaysAndMaxRetry.from(3, "1s,invalid,2s");
- DelaysAndMaxRetry expected = new DelaysAndMaxRetry(3, ImmutableList.of(new Delay(3, 1000)));
+ DelaysAndMaxRetry expected = new DelaysAndMaxRetry(3, ImmutableList.of(new Delay(3, Duration.ofSeconds(1))));
assertThat(actual).isEqualTo(expected);
}
@@ -126,20 +126,22 @@ public class DelaysAndMaxRetryTest {
public void getExpandedDelaysShouldExpandSingleDelays() throws Exception {
DelaysAndMaxRetry testee = DelaysAndMaxRetry.from(3, "1*1S,1*2S,1*5S");
- assertThat(testee.getExpandedDelays()).containsExactly(1000L, 2000L, 5000L);
+ assertThat(testee.getExpandedDelays()).containsExactly(Duration.ofSeconds(1), Duration.ofSeconds(2), Duration.ofSeconds(5));
}
@Test
public void getExpandedDelaysShouldExpandMultipleDelays() throws Exception {
DelaysAndMaxRetry testee = DelaysAndMaxRetry.from(3, "1*1S,2*2S,2*5S");
- assertThat(testee.getExpandedDelays()).containsExactly(1000L, 2000L, 2000L, 5000L, 5000L);
+ assertThat(testee.getExpandedDelays())
+ .containsExactly(Duration.ofSeconds(1), Duration.ofSeconds(2), Duration.ofSeconds(2), Duration.ofSeconds(5), Duration.ofSeconds(5));
}
@Test
public void getExpandedDelaysShouldExpandMultipleDelaysWithSpaces() throws Exception {
DelaysAndMaxRetry testee = DelaysAndMaxRetry.from(3, "1 * 1 S, 2 * 2 S , 2 * 5 S");
- assertThat(testee.getExpandedDelays()).containsExactly(1000L, 2000L, 2000L, 5000L, 5000L);
+ assertThat(testee.getExpandedDelays())
+ .containsExactly(Duration.ofSeconds(1), Duration.ofSeconds(2), Duration.ofSeconds(2), Duration.ofSeconds(5), Duration.ofSeconds(5));
}
}
diff --git a/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/remote/delivery/DeliveryRunnableTest.java b/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/remote/delivery/DeliveryRunnableTest.java
index e2820eb..25487e5 100644
--- a/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/remote/delivery/DeliveryRunnableTest.java
+++ b/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/remote/delivery/DeliveryRunnableTest.java
@@ -27,6 +27,7 @@ import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;
+import java.time.Duration;
import java.util.Date;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;
@@ -147,8 +148,7 @@ public class DeliveryRunnableTest {
.state(Mail.ERROR)
.lastUpdated(FIXED_DATE)
.build(),
- 1000,
- TimeUnit.MILLISECONDS);
+ Duration.ofSeconds(1));
verifyNoMoreInteractions(mailQueue);
}
@@ -170,8 +170,7 @@ public class DeliveryRunnableTest {
.state(Mail.ERROR)
.lastUpdated(FIXED_DATE)
.build(),
- 3000,
- TimeUnit.MILLISECONDS);
+ Duration.ofSeconds(3));
verifyNoMoreInteractions(mailQueue);
}
@@ -193,8 +192,7 @@ public class DeliveryRunnableTest {
.state(Mail.ERROR)
.lastUpdated(FIXED_DATE)
.build(),
- 5000,
- TimeUnit.MILLISECONDS);
+ Duration.ofSeconds(5));
verifyNoMoreInteractions(mailQueue);
}
@@ -247,8 +245,7 @@ public class DeliveryRunnableTest {
.state(Mail.ERROR)
.lastUpdated(FIXED_DATE)
.build(),
- 1000,
- TimeUnit.MILLISECONDS);
+ Duration.ofSeconds(1));
verifyNoMoreInteractions(mailQueue);
}
}
diff --git a/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/remote/delivery/RemoteDeliveryConfigurationTest.java b/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/remote/delivery/RemoteDeliveryConfigurationTest.java
index 3e6e40f..54431e9 100644
--- a/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/remote/delivery/RemoteDeliveryConfigurationTest.java
+++ b/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/remote/delivery/RemoteDeliveryConfigurationTest.java
@@ -23,6 +23,7 @@ import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
+import java.time.Duration;
import java.util.Properties;
import org.apache.james.core.Domain;
@@ -853,7 +854,7 @@ public class RemoteDeliveryConfigurationTest {
.build();
assertThat(new RemoteDeliveryConfiguration(mailetConfig, mock(DomainList.class)).getDelayTimes())
- .containsOnly(5000L, 100000L, 500000L);
+ .containsOnly(Duration.ofSeconds(5), Duration.ofSeconds(100), Duration.ofSeconds(500));
}
@Test
diff --git a/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/methods/integration/SetMessagesOutboxFlagUpdateTest.java b/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/methods/integration/SetMessagesOutboxFlagUpdateTest.java
index 402bf4d..ce2f696 100644
--- a/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/methods/integration/SetMessagesOutboxFlagUpdateTest.java
+++ b/server/protocols/jmap-integration-testing/jmap-integration-testing-common/src/test/java/org/apache/james/jmap/methods/integration/SetMessagesOutboxFlagUpdateTest.java
@@ -33,6 +33,7 @@ import static org.apache.james.jmap.TestingConstants.DOMAIN;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
+import java.time.Duration;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.TimeUnit;
@@ -84,7 +85,7 @@ public abstract class SetMessagesOutboxFlagUpdateTest {
}
@Override
- public void enQueue(Mail mail, long delay, TimeUnit unit) {
+ public void enQueue(Mail mail, Duration delay) {
}
@Override
diff --git a/server/protocols/protocols-library/src/main/java/org/apache/james/protocols/lib/POP3BeforeSMTPHelper.java b/server/protocols/protocols-library/src/main/java/org/apache/james/protocols/lib/POP3BeforeSMTPHelper.java
index 81e6350..2ed8d23 100644
--- a/server/protocols/protocols-library/src/main/java/org/apache/james/protocols/lib/POP3BeforeSMTPHelper.java
+++ b/server/protocols/protocols-library/src/main/java/org/apache/james/protocols/lib/POP3BeforeSMTPHelper.java
@@ -19,6 +19,8 @@
package org.apache.james.protocols.lib;
+import java.time.Duration;
+import java.time.Instant;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
@@ -36,12 +38,12 @@ public class POP3BeforeSMTPHelper {
/**
* The map in which the ipAddresses and timestamp stored
*/
- public static final Map<String, Long> ipMap = Collections.synchronizedMap(new HashMap<String, Long>());
+ public static final Map<String, Instant> ipMap = Collections.synchronizedMap(new HashMap<>());
/**
- * Default expire time in ms (1 hour)
+ * Default expire time in ms (60 hour)
*/
- public static final long EXPIRE_TIME = 216000000;
+ public static final Duration EXPIRE_TIME = Duration.ofHours(60);
/**
* Return true if the ip is authorized to relay
@@ -61,11 +63,11 @@ public class POP3BeforeSMTPHelper {
* The ipAddress
*/
public static void addIPAddress(String ipAddress) {
- ipMap.put(ipAddress, System.currentTimeMillis());
+ ipMap.put(ipAddress, Instant.now());
}
/**
- * @see #removeExpiredIP(long)
+ * @see #removeExpiredIP(Duration)
*/
public static void removeExpiredIP() {
removeExpiredIP(EXPIRE_TIME);
@@ -79,17 +81,16 @@ public class POP3BeforeSMTPHelper {
* The time in milliseconds after which an ipAddress should be
* handled as expired
*/
- public static void removeExpiredIP(long clearTime) {
+ public static void removeExpiredIP(Duration clearTime) {
synchronized (ipMap) {
Iterator<String> storedIP = ipMap.keySet().iterator();
- long currTime = System.currentTimeMillis();
while (storedIP.hasNext()) {
String key = storedIP.next();
- long storedTime = ipMap.get(key);
+ Instant storedTime = ipMap.get(key);
// remove the ip from the map when it is expired
- if ((currTime - clearTime) > storedTime) {
+ if (Instant.now().minus(clearTime).isAfter(storedTime)) {
// remove the entry from the iterator first to get sure that
// we not get
// a ConcurrentModificationException
diff --git a/server/protocols/protocols-smtp/src/main/java/org/apache/james/smtpserver/POP3BeforeSMTPHandler.java b/server/protocols/protocols-smtp/src/main/java/org/apache/james/smtpserver/POP3BeforeSMTPHandler.java
index 21d1028..5558614 100644
--- a/server/protocols/protocols-smtp/src/main/java/org/apache/james/smtpserver/POP3BeforeSMTPHandler.java
+++ b/server/protocols/protocols-smtp/src/main/java/org/apache/james/smtpserver/POP3BeforeSMTPHandler.java
@@ -19,6 +19,8 @@
package org.apache.james.smtpserver;
+import java.time.Duration;
+
import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.HierarchicalConfiguration;
@@ -35,7 +37,7 @@ import org.apache.james.util.TimeConverter;
public class POP3BeforeSMTPHandler implements ConnectHandler<SMTPSession>, Configurable {
/** The time after which ipAddresses should be handled as expired */
- private long expireTime = POP3BeforeSMTPHelper.EXPIRE_TIME;
+ private Duration expireTime = POP3BeforeSMTPHelper.EXPIRE_TIME;
@Override
public void configure(HierarchicalConfiguration config) throws ConfigurationException {
@@ -54,7 +56,7 @@ public class POP3BeforeSMTPHandler implements ConnectHandler<SMTPSession>, Confi
*/
public void setExpireTime(String rawExpireTime) {
if (rawExpireTime != null) {
- this.expireTime = TimeConverter.getMilliSeconds(rawExpireTime);
+ this.expireTime = TimeConverter.parseDuration(rawExpireTime);
}
}
diff --git a/server/protocols/protocols-smtp/src/main/java/org/apache/james/smtpserver/fastfail/JDBCGreylistHandler.java b/server/protocols/protocols-smtp/src/main/java/org/apache/james/smtpserver/fastfail/JDBCGreylistHandler.java
index a1587e8..071958f 100644
--- a/server/protocols/protocols-smtp/src/main/java/org/apache/james/smtpserver/fastfail/JDBCGreylistHandler.java
+++ b/server/protocols/protocols-smtp/src/main/java/org/apache/james/smtpserver/fastfail/JDBCGreylistHandler.java
@@ -25,6 +25,7 @@ import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
+import java.time.Instant;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
@@ -135,7 +136,7 @@ public class JDBCGreylistHandler extends AbstractGreylistHandler implements Prot
* The temporary blocking time
*/
public void setTempBlockTime(String tempBlockTime) {
- setTempBlockTime(TimeConverter.getMilliSeconds(tempBlockTime));
+ setTempBlockTime(TimeConverter.parseDuration(tempBlockTime));
}
/**
@@ -146,7 +147,7 @@ public class JDBCGreylistHandler extends AbstractGreylistHandler implements Prot
* The lifeTime
*/
public void setAutoWhiteListLifeTime(String autoWhiteListLifeTime) {
- setAutoWhiteListLifeTime(TimeConverter.getMilliSeconds(autoWhiteListLifeTime));
+ setAutoWhiteListLifeTime(TimeConverter.parseDuration(autoWhiteListLifeTime));
}
/**
@@ -157,7 +158,7 @@ public class JDBCGreylistHandler extends AbstractGreylistHandler implements Prot
* The lifetime
*/
public void setUnseenLifeTime(String unseenLifeTime) {
- setUnseenLifeTime(TimeConverter.getMilliSeconds(unseenLifeTime));
+ setUnseenLifeTime(TimeConverter.parseDuration(unseenLifeTime));
}
@Inject
@@ -198,7 +199,7 @@ public class JDBCGreylistHandler extends AbstractGreylistHandler implements Prot
}
@Override
- protected void insertTriplet(String ipAddress, String sender, String recip, int count, long createTime) throws SQLException {
+ protected void insertTriplet(String ipAddress, String sender, String recip, int count, Instant createTime) throws SQLException {
Connection conn = datasource.getConnection();
PreparedStatement mappingStmt = null;
@@ -210,7 +211,7 @@ public class JDBCGreylistHandler extends AbstractGreylistHandler implements Prot
mappingStmt.setString(2, sender);
mappingStmt.setString(3, recip);
mappingStmt.setInt(4, count);
- mappingStmt.setTimestamp(5, new Timestamp(createTime));
+ mappingStmt.setTimestamp(5, new Timestamp(createTime.getEpochSecond()));
mappingStmt.executeUpdate();
} finally {
theJDBCUtil.closeJDBCStatement(mappingStmt);
@@ -219,13 +220,13 @@ public class JDBCGreylistHandler extends AbstractGreylistHandler implements Prot
}
@Override
- protected void updateTriplet(String ipAddress, String sender, String recip, int count, long time) throws SQLException {
+ protected void updateTriplet(String ipAddress, String sender, String recip, int count, Instant time) throws SQLException {
Connection conn = datasource.getConnection();
PreparedStatement mappingStmt = null;
try {
mappingStmt = conn.prepareStatement(updateQuery);
- mappingStmt.setTimestamp(1, new Timestamp(time));
+ mappingStmt.setTimestamp(1, new Timestamp(time.getEpochSecond()));
mappingStmt.setInt(2, (count + 1));
mappingStmt.setString(3, ipAddress);
mappingStmt.setString(4, sender);
@@ -238,14 +239,14 @@ public class JDBCGreylistHandler extends AbstractGreylistHandler implements Prot
}
@Override
- protected void cleanupAutoWhiteListGreyList(long time) throws SQLException {
+ protected void cleanupAutoWhiteListGreyList(Instant time) throws SQLException {
PreparedStatement mappingStmt = null;
Connection conn = datasource.getConnection();
try {
mappingStmt = conn.prepareStatement(deleteAutoWhiteListQuery);
- mappingStmt.setTimestamp(1, new Timestamp(time));
+ mappingStmt.setTimestamp(1, new Timestamp(time.getEpochSecond()));
mappingStmt.executeUpdate();
} finally {
@@ -255,7 +256,7 @@ public class JDBCGreylistHandler extends AbstractGreylistHandler implements Prot
}
@Override
- protected void cleanupGreyList(long time) throws SQLException {
+ protected void cleanupGreyList(Instant time) throws SQLException {
Connection conn = datasource.getConnection();
PreparedStatement mappingStmt = null;
@@ -263,7 +264,7 @@ public class JDBCGreylistHandler extends AbstractGreylistHandler implements Prot
try {
mappingStmt = conn.prepareStatement(deleteQuery);
- mappingStmt.setTimestamp(1, new Timestamp(time));
+ mappingStmt.setTimestamp(1, new Timestamp(time.getEpochSecond()));
mappingStmt.executeUpdate();
} finally {
diff --git a/server/protocols/protocols-smtp/src/test/java/org/apache/james/smtpserver/POP3BeforeSMTPHandlerTest.java b/server/protocols/protocols-smtp/src/test/java/org/apache/james/smtpserver/POP3BeforeSMTPHandlerTest.java
index 4fa2de3..66970a2 100644
--- a/server/protocols/protocols-smtp/src/test/java/org/apache/james/smtpserver/POP3BeforeSMTPHandlerTest.java
+++ b/server/protocols/protocols-smtp/src/test/java/org/apache/james/smtpserver/POP3BeforeSMTPHandlerTest.java
@@ -21,6 +21,7 @@ package org.apache.james.smtpserver;
import static org.assertj.core.api.Assertions.assertThat;
import java.net.InetSocketAddress;
+import java.time.Duration;
import org.apache.james.protocols.lib.POP3BeforeSMTPHelper;
import org.apache.james.protocols.smtp.SMTPSession;
@@ -81,7 +82,7 @@ public class POP3BeforeSMTPHandlerTest {
try {
Thread.sleep(sleepTime);
- POP3BeforeSMTPHelper.removeExpiredIP(10);
+ POP3BeforeSMTPHelper.removeExpiredIP(Duration.ofMillis(10));
handler.onConnect(mockedSession);
assertThat(mockedSession.isRelayingAllowed()).isFalse();
diff --git a/server/queue/queue-activemq/src/test/java/org/apache/james/queue/activemq/ActiveMQMailQueueBlobTest.java b/server/queue/queue-activemq/src/test/java/org/apache/james/queue/activemq/ActiveMQMailQueueBlobTest.java
index 078db43..98284d8 100644
--- a/server/queue/queue-activemq/src/test/java/org/apache/james/queue/activemq/ActiveMQMailQueueBlobTest.java
+++ b/server/queue/queue-activemq/src/test/java/org/apache/james/queue/activemq/ActiveMQMailQueueBlobTest.java
@@ -24,7 +24,8 @@ import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
-import java.util.concurrent.TimeUnit;
+import java.time.Duration;
+import java.time.temporal.ChronoUnit;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.broker.BrokerService;
@@ -49,6 +50,7 @@ import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import org.threeten.extra.Years;
@ExtendWith(BrokerExtension.class)
@Tag(BrokerExtension.STATISTICS)
@@ -132,7 +134,7 @@ public class ActiveMQMailQueueBlobTest implements DelayedManageableMailQueueCont
@Test
void computeNextDeliveryTimestampShouldReturnLongMaxWhenOverflow() {
- long deliveryTimestamp = mailQueue.computeNextDeliveryTimestamp(Long.MAX_VALUE, TimeUnit.DAYS);
+ long deliveryTimestamp = mailQueue.computeNextDeliveryTimestamp(ChronoUnit.FOREVER.getDuration());
assertThat(deliveryTimestamp).isEqualTo(Long.MAX_VALUE);
}
diff --git a/server/queue/queue-api/pom.xml b/server/queue/queue-api/pom.xml
index fdbfabb..5b6dbad 100644
--- a/server/queue/queue-api/pom.xml
+++ b/server/queue/queue-api/pom.xml
@@ -96,6 +96,10 @@
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>org.threeten</groupId>
+ <artifactId>threeten-extra</artifactId>
+ </dependency>
</dependencies>
<build>
diff --git a/server/queue/queue-api/src/main/java/org/apache/james/queue/api/MailQueue.java b/server/queue/queue-api/src/main/java/org/apache/james/queue/api/MailQueue.java
index 5de2a47..559d998 100644
--- a/server/queue/queue-api/src/main/java/org/apache/james/queue/api/MailQueue.java
+++ b/server/queue/queue-api/src/main/java/org/apache/james/queue/api/MailQueue.java
@@ -19,12 +19,14 @@
package org.apache.james.queue.api;
+import java.time.Duration;
import java.util.concurrent.TimeUnit;
import javax.mail.MessagingException;
import org.apache.mailet.Mail;
import org.reactivestreams.Publisher;
+import org.threeten.extra.Temporals;
/**
* <p>
@@ -73,13 +75,26 @@ public interface MailQueue {
/**
* Enqueue the Mail to the queue. The given delay and unit are used to
* calculate the time when the Mail will be available for deQueue
+ *
+ * @param mail
+ * @param delay
+ * @throws MailQueueException
+ */
+ void enQueue(Mail mail, Duration delay) throws MailQueueException;
+
+
+ /**
+ * Enqueue the Mail to the queue. The given delay and unit are used to
+ * calculate the time when the Mail will be available for deQueue
*
* @param mail
* @param delay
* @param unit
* @throws MailQueueException
*/
- void enQueue(Mail mail, long delay, TimeUnit unit) throws MailQueueException;
+ default void enQueue(Mail mail, long delay, TimeUnit unit) throws MailQueueException {
+ enQueue(mail, Temporals.chronoUnit(unit).getDuration().multipliedBy(delay));
+ }
/**
* Enqueue the Mail to the queue
diff --git a/server/queue/queue-api/src/test/java/org/apache/james/queue/api/DelayedMailQueueContract.java b/server/queue/queue-api/src/test/java/org/apache/james/queue/api/DelayedMailQueueContract.java
index 5cfc026..e0b51a0 100644
--- a/server/queue/queue-api/src/test/java/org/apache/james/queue/api/DelayedMailQueueContract.java
+++ b/server/queue/queue-api/src/test/java/org/apache/james/queue/api/DelayedMailQueueContract.java
@@ -25,12 +25,12 @@ import static org.assertj.core.api.Assertions.assertThatCode;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import java.time.Duration;
+import java.time.temporal.ChronoUnit;
import java.util.concurrent.TimeUnit;
import org.junit.jupiter.api.Test;
import com.google.common.base.Stopwatch;
-
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.core.scheduler.Schedulers;
@@ -82,8 +82,7 @@ public interface DelayedMailQueueContract {
getMailQueue().enQueue(defaultMail()
.name("name")
.build(),
- Long.MAX_VALUE,
- TimeUnit.DAYS);
+ ChronoUnit.FOREVER.getDuration());
Mono<MailQueue.MailQueueItem> next = Flux.from(getMailQueue().deQueue()).subscribeOn(Schedulers.elastic()).next();
assertThatThrownBy(() -> next.block(Duration.ofSeconds(1)))
diff --git a/server/queue/queue-file/src/main/java/org/apache/james/queue/file/FileMailQueue.java b/server/queue/queue-file/src/main/java/org/apache/james/queue/file/FileMailQueue.java
index 5052efd..3f54528 100644
--- a/server/queue/queue-file/src/main/java/org/apache/james/queue/file/FileMailQueue.java
+++ b/server/queue/queue-file/src/main/java/org/apache/james/queue/file/FileMailQueue.java
@@ -26,6 +26,7 @@ import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.security.SecureRandom;
+import java.time.Duration;
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
@@ -179,7 +180,7 @@ public class FileMailQueue implements ManageableMailQueue {
}
@Override
- public void enQueue(Mail mail, long delay, TimeUnit unit) throws MailQueueException {
+ public void enQueue(Mail mail, Duration delay) throws MailQueueException {
final String key = mail.getName() + "-" + COUNTER.incrementAndGet();
try {
int i = RANDOM.nextInt(SPLITCOUNT) + 1;
@@ -187,8 +188,8 @@ public class FileMailQueue implements ManageableMailQueue {
String name = queueDirName + "/" + i + "/" + key;
final FileItem item = new FileItem(name + OBJECT_EXTENSION, name + MSG_EXTENSION);
- if (delay > 0) {
- mail.setAttribute(new Attribute(NEXT_DELIVERY, AttributeValue.of(System.currentTimeMillis() + unit.toMillis(delay))));
+ if (!delay.isNegative()) {
+ mail.setAttribute(new Attribute(NEXT_DELIVERY, AttributeValue.of(computeNextDelivery(delay))));
}
try (FileOutputStream foout = new FileOutputStream(item.getObjectFile());
ObjectOutputStream oout = new ObjectOutputStream(foout)) {
@@ -208,7 +209,7 @@ public class FileMailQueue implements ManageableMailQueue {
keyMappings.put(key, item);
- if (delay > 0) {
+ if (!delay.isNegative()) {
// The message should get delayed so schedule it for later
scheduler.schedule(() -> {
try {
@@ -218,7 +219,7 @@ public class FileMailQueue implements ManageableMailQueue {
Thread.currentThread().interrupt();
throw new RuntimeException("Unable to init", e);
}
- }, delay, unit);
+ }, delay.getSeconds(), TimeUnit.SECONDS);
} else {
inmemoryQueue.put(key);
@@ -231,6 +232,14 @@ public class FileMailQueue implements ManageableMailQueue {
}
+ private long computeNextDelivery(Duration delay) {
+ try {
+ return Instant.now().plus(delay).getEpochSecond();
+ } catch (ArithmeticException e) {
+ return Long.MAX_VALUE;
+ }
+ }
+
@Override
public void enQueue(Mail mail) throws MailQueueException {
enQueue(mail, 0, TimeUnit.MILLISECONDS);
diff --git a/server/queue/queue-jms/src/main/java/org/apache/james/queue/jms/JMSMailQueue.java b/server/queue/queue-jms/src/main/java/org/apache/james/queue/jms/JMSMailQueue.java
index e552e78..1931cef 100644
--- a/server/queue/queue-jms/src/main/java/org/apache/james/queue/jms/JMSMailQueue.java
+++ b/server/queue/queue-jms/src/main/java/org/apache/james/queue/jms/JMSMailQueue.java
@@ -21,6 +21,8 @@ package org.apache.james.queue.jms;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.Serializable;
+import java.time.DateTimeException;
+import java.time.Duration;
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
@@ -75,7 +77,6 @@ import org.apache.mailet.Mail;
import org.apache.mailet.PerRecipientHeaders;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import org.threeten.extra.Temporals;
import com.github.fge.lambdas.Throwing;
import com.github.steveash.guavate.Guavate;
@@ -253,10 +254,10 @@ public class JMSMailQueue implements ManageableMailQueue, JMSSupport, MailPriori
}
@Override
- public void enQueue(Mail mail, long delay, TimeUnit unit) throws MailQueueException {
+ public void enQueue(Mail mail, Duration delay) throws MailQueueException {
TimeMetric timeMetric = metricFactory.timer(ENQUEUED_TIMER_METRIC_NAME_PREFIX + queueName);
- long nextDeliveryTimestamp = computeNextDeliveryTimestamp(delay, unit);
+ long nextDeliveryTimestamp = computeNextDeliveryTimestamp(delay);
try {
@@ -274,16 +275,16 @@ public class JMSMailQueue implements ManageableMailQueue, JMSSupport, MailPriori
}
}
- public long computeNextDeliveryTimestamp(long delay, TimeUnit unit) {
- if (delay > 0) {
+ public long computeNextDeliveryTimestamp(Duration delay) {
+ if (!delay.isNegative()) {
try {
return ZonedDateTime.now()
- .plus(delay, Temporals.chronoUnit(unit))
+ .plus(delay)
.toInstant()
.toEpochMilli();
- } catch (ArithmeticException e) {
- LOGGER.warn("The {} was caused by conversation {}({}) followed by addition to current timestamp. Falling back to Long.MAX_VALUE.",
- e.getMessage(), delay, unit.name());
+ } catch (DateTimeException | ArithmeticException e) {
+ LOGGER.warn("The {} was caused by conversation {} followed by addition to current timestamp. Falling back to Long.MAX_VALUE.",
+ e.getMessage(), delay);
return Long.MAX_VALUE;
}
diff --git a/server/queue/queue-memory/src/main/java/org/apache/james/queue/memory/MemoryMailQueueFactory.java b/server/queue/queue-memory/src/main/java/org/apache/james/queue/memory/MemoryMailQueueFactory.java
index b534459..803929c 100644
--- a/server/queue/queue-memory/src/main/java/org/apache/james/queue/memory/MemoryMailQueueFactory.java
+++ b/server/queue/queue-memory/src/main/java/org/apache/james/queue/memory/MemoryMailQueueFactory.java
@@ -19,6 +19,8 @@
package org.apache.james.queue.memory;
+import java.time.DateTimeException;
+import java.time.Duration;
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
@@ -104,8 +106,8 @@ public class MemoryMailQueueFactory implements MailQueueFactory<ManageableMailQu
}
@Override
- public void enQueue(Mail mail, long delay, TimeUnit unit) throws MailQueueException {
- ZonedDateTime nextDelivery = calculateNextDelivery(delay, unit);
+ public void enQueue(Mail mail, Duration delay) throws MailQueueException {
+ ZonedDateTime nextDelivery = calculateNextDelivery(delay);
try {
mailItems.put(new MemoryMailQueueItem(cloneMail(mail), this, nextDelivery));
} catch (MessagingException e) {
@@ -113,11 +115,11 @@ public class MemoryMailQueueFactory implements MailQueueFactory<ManageableMailQu
}
}
- private ZonedDateTime calculateNextDelivery(long delay, TimeUnit unit) {
- if (delay > 0) {
+ private ZonedDateTime calculateNextDelivery(Duration delay) {
+ if (!delay.isNegative()) {
try {
- return ZonedDateTime.now().plus(delay, Temporals.chronoUnit(unit));
- } catch (ArithmeticException e) {
+ return ZonedDateTime.now().plus(delay);
+ } catch (DateTimeException | ArithmeticException e) {
return Instant.ofEpochMilli(Long.MAX_VALUE).atZone(ZoneId.of("UTC"));
}
}
diff --git a/server/queue/queue-rabbitmq/src/main/java/org/apache/james/queue/rabbitmq/RabbitMQMailQueue.java b/server/queue/queue-rabbitmq/src/main/java/org/apache/james/queue/rabbitmq/RabbitMQMailQueue.java
index 3f77cf1..8a69787 100644
--- a/server/queue/queue-rabbitmq/src/main/java/org/apache/james/queue/rabbitmq/RabbitMQMailQueue.java
+++ b/server/queue/queue-rabbitmq/src/main/java/org/apache/james/queue/rabbitmq/RabbitMQMailQueue.java
@@ -19,7 +19,7 @@
package org.apache.james.queue.rabbitmq;
-import java.util.concurrent.TimeUnit;
+import java.time.Duration;
import org.apache.james.metrics.api.MetricFactory;
import org.apache.james.queue.api.MailQueueItemDecoratorFactory;
@@ -62,9 +62,9 @@ public class RabbitMQMailQueue implements ManageableMailQueue {
}
@Override
- public void enQueue(Mail mail, long delay, TimeUnit unit) {
- if (delay > 0) {
- LOGGER.info("Ignored delay upon enqueue of {} : {} {}.", mail.getName(), delay, unit);
+ public void enQueue(Mail mail, Duration delay) {
+ if (!delay.isNegative()) {
+ LOGGER.info("Ignored delay upon enqueue of {} : {}.", mail.getName(), delay);
}
enQueue(mail);
}
diff --git a/server/queue/queue-rabbitmq/src/main/java/org/apache/james/queue/rabbitmq/view/cassandra/configuration/CassandraMailQueueViewConfiguration.java b/server/queue/queue-rabbitmq/src/main/java/org/apache/james/queue/rabbitmq/view/cassandra/configuration/CassandraMailQueueViewConfiguration.java
index 9750d34..dc13b23 100644
--- a/server/queue/queue-rabbitmq/src/main/java/org/apache/james/queue/rabbitmq/view/cassandra/configuration/CassandraMailQueueViewConfiguration.java
+++ b/server/queue/queue-rabbitmq/src/main/java/org/apache/james/queue/rabbitmq/view/cassandra/configuration/CassandraMailQueueViewConfiguration.java
@@ -95,8 +95,7 @@ public class CassandraMailQueueViewConfiguration {
.bucketCount(bucketCount)
.updateBrowseStartPace(updateBrowseStartPace)
.sliceWindow(sliceWindowAsString
- .map(TimeConverter::getMilliSeconds)
- .map(Duration::ofMillis)
+ .map(TimeConverter::parseDuration)
.orElse(DEFAULT_SLICE_WINDOW))
.build();
}
---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org
[james-project] 03/03: JAMES-2699 Add documentation for Mailet API
deprecation
Posted by bt...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
btellier pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/james-project.git
commit 28360f856fd3b7c14bf3a9cef05a32d957f6b376
Author: Gautier DI FOLCO <gd...@linagora.com>
AuthorDate: Mon Mar 25 13:27:13 2019 +0100
JAMES-2699 Add documentation for Mailet API deprecation
---
upgrade-instructions.md | 18 ++++++++++++++++++
1 file changed, 18 insertions(+)
diff --git a/upgrade-instructions.md b/upgrade-instructions.md
index a494d53..9bd33ee 100644
--- a/upgrade-instructions.md
+++ b/upgrade-instructions.md
@@ -19,6 +19,7 @@ Change list:
- [Changes to the MailboxListener API](#changes-to-the-mailboxlistener-api)
- [Changes in WebAdmin reIndexing API](#changes-in-webadmin-reindexing-api)
- [Rename KEY column in JAMES_MAILBOX_ANNOTATION table](#james-mailbox-annotation)
+ - [Mailet API changes](#mailet-api-changes)
### Changes to the MailboxListener API
@@ -87,6 +88,23 @@ ALTER TABLE JAMES_MAILBOX_ANNOTATION CHANGE COLUMN KEY ANNOTATION_KEY varchar(20
_or the syntax corresponding to your database._
+### Mailet API changes
+In order to allow safe serialization and strong typing `org.apache.mailet.Mail` have changed.
+
+These methods have been deprecated and replaced:
+
+ * `getSender()` in favor of `getMaybeSender()`
+ * `getAttribute(String)` in favor of `getAttribute(AttributeName)`
+ * `setAttribute(String, Serializable)` in favor of `setAttribute(Attribute)`
+ * `removeAttribute(String)` in favor of `removeAttribute(AttributeName)`
+ * `getAttributeNames()` in favor of `attributeNames()` and `attributesMap()`
+
+Some plain-string `AttributeName` have also been replaced:
+
+ * `SMTP_AUTH_USER_ATTRIBUTE_NAME` in favor of `SMTP_AUTH_USER`
+ * `MAILET_ERROR_ATTRIBUTE_NAME` in favor of `MAILET_ERROR`
+ * `SENT_BY_MAILET` in favor of `SENT_BY_MAILET_ATTRIBUTE`'s name, it is recommended to directly set the `Attribute`.
+
## 3.2.0 version
Changes to apply between 3.1.0 and 3.2.0 had been reported here.
---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org