[java-support] branch master updated: JSPT-79 - Review date and time handling for Java 8

Scott Cantor cantor.2 at osu.edu
Mon Mar 18 17:11:45 EDT 2019


This is an automated email from the git hooks/post-receive script.

scantor pushed a commit to branch master
in repository java-support.

View the commit online:
http://git.shibboleth.net/view/?p=java-support.git;a=commit;h=8feeba8488b483d9cdea43d658acb179c4f1938f

The following commit(s) were added to refs/heads/master by this push:
       new  8feeba8   JSPT-79 - Review date and time handling for Java 8
8feeba8 is described below

commit 8feeba8488b483d9cdea43d658acb179c4f1938f
Author: Scott Cantor <cantor.2 at osu.edu>
AuthorDate: Mon Mar 18 17:11:43 2019 -0400

    JSPT-79 - Review date and time handling for Java 8
    
    https://issues.shibboleth.net/jira/browse/JSPT-79
    
    More long to Duration conversion.
---
 .../support/httpclient/IdleConnectionSweeper.java  | 35 ++++++++++++----------
 .../httpclient/IdleConectionSweeperTest.java       | 12 ++++----
 2 files changed, 26 insertions(+), 21 deletions(-)

diff --git a/src/main/java/net/shibboleth/utilities/java/support/httpclient/IdleConnectionSweeper.java b/src/main/java/net/shibboleth/utilities/java/support/httpclient/IdleConnectionSweeper.java
index dd383f1..2acd53e 100644
--- a/src/main/java/net/shibboleth/utilities/java/support/httpclient/IdleConnectionSweeper.java
+++ b/src/main/java/net/shibboleth/utilities/java/support/httpclient/IdleConnectionSweeper.java
@@ -17,6 +17,8 @@
 
 package net.shibboleth.utilities.java.support.httpclient;
 
+import java.time.Duration;
+import java.time.Instant;
 import java.util.Timer;
 import java.util.TimerTask;
 import java.util.concurrent.TimeUnit;
@@ -43,23 +45,23 @@ public class IdleConnectionSweeper implements DestructableComponent {
     private boolean createdTimer;
 
     /** HttpClientConnectionManager whose connections will be swept. */
-    private final HttpClientConnectionManager connectionManager;
+    @Nonnull private final HttpClientConnectionManager connectionManager;
 
     /** Timer used to schedule and execute the sweeping task. */
-    private final Timer taskTimer;
+    @Nonnull private final Timer taskTimer;
 
     /** Sweeping task executed by the timer. */
-    private final TimerTask sweeper;
+    @Nonnull private final TimerTask sweeper;
 
     /**
      * Constructor. This method will create a daemon {@link Timer} and use it to periodically sweep connections.
      * 
      * @param manager HTTP client connection manager whose connections will be swept
-     * @param idleTimeout length of time, in milliseconds, connection may be idle before being closed down
-     * @param sweepInterval length of time, in milliseconds, between sweeps
+     * @param idleTimeout length of time connection may be idle before being closed down
+     * @param sweepInterval length of time between sweeps
      */
-    public IdleConnectionSweeper(@Nonnull final HttpClientConnectionManager manager, final long idleTimeout,
-            final long sweepInterval) {
+    public IdleConnectionSweeper(@Nonnull final HttpClientConnectionManager manager,
+            @Nonnull final Duration idleTimeout, @Nonnull final Duration sweepInterval) {
         this(manager, idleTimeout, sweepInterval,
                 new Timer(TimerSupport.getTimerName(IdleConnectionSweeper.class.getName(), null), true));
         createdTimer = true;
@@ -69,36 +71,37 @@ public class IdleConnectionSweeper implements DestructableComponent {
      * Constructor.
      * 
      * @param manager HTTP client connection manager whose connections will be swept
-     * @param idleTimeout length of time, in milliseconds, connection may be idle before being closed down
-     * @param sweepInterval length of time, in milliseconds, between sweeps
+     * @param idleTimeout length of time connection may be idle before being closed down
+     * @param sweepInterval length of time between sweeps
      * @param backgroundTimer timer used to schedule the background sweeping task
      */
-    public IdleConnectionSweeper(@Nonnull final HttpClientConnectionManager manager, final long idleTimeout,
-            final long sweepInterval, @Nonnull final Timer backgroundTimer) {
+    public IdleConnectionSweeper(@Nonnull final HttpClientConnectionManager manager,
+            @Nonnull final Duration idleTimeout, @Nonnull final Duration sweepInterval,
+            @Nonnull final Timer backgroundTimer) {
         connectionManager = Constraint.isNotNull(manager, "HttpClientConnectionManager can not be null");
         taskTimer = Constraint.isNotNull(backgroundTimer, "Sweeper task timer can not be null");
 
         sweeper = new TimerTask() {
             public void run() {
-                connectionManager.closeIdleConnections(idleTimeout, TimeUnit.MILLISECONDS);
+                connectionManager.closeIdleConnections(idleTimeout.toMillis(), TimeUnit.MILLISECONDS);
             }
         };
 
-        taskTimer.schedule(sweeper, sweepInterval, sweepInterval);
+        taskTimer.schedule(sweeper, sweepInterval.toMillis(), sweepInterval.toMillis());
     }
 
     /**
-     * Gets the time, in milliseconds since the epoch, when the sweeper last executed or, if it has not yet executed,
+     * Gets the time when the sweeper last executed or, if it has not yet executed,
      * when it was first scheduled to run.
      * 
      * @return the time when the sweeper last executed or when it was first scheduled to run
      */
-    public long scheduledExecutionTime() {
+    @Nonnull public Instant scheduledExecutionTime() {
         if (isDestroyed()) {
             throw new DestroyedComponentException();
         }
 
-        return sweeper.scheduledExecutionTime();
+        return Instant.ofEpochMilli(sweeper.scheduledExecutionTime());
     }
 
     /** {@inheritDoc} */
diff --git a/src/test/java/net/shibboleth/utilities/java/support/httpclient/IdleConectionSweeperTest.java b/src/test/java/net/shibboleth/utilities/java/support/httpclient/IdleConectionSweeperTest.java
index 037a2cc..b02ead8 100644
--- a/src/test/java/net/shibboleth/utilities/java/support/httpclient/IdleConectionSweeperTest.java
+++ b/src/test/java/net/shibboleth/utilities/java/support/httpclient/IdleConectionSweeperTest.java
@@ -17,6 +17,8 @@
 
 package net.shibboleth.utilities.java.support.httpclient;
 
+import java.time.Duration;
+import java.time.Instant;
 import java.util.Timer;
 
 import net.shibboleth.utilities.java.support.component.DestroyedComponentException;
@@ -28,14 +30,14 @@ import org.testng.annotations.Test;
 /** {@link IdleConnectionSweeper} unit test. */
 public class IdleConectionSweeperTest {
 
-    private final long SWEEP_INTERVAL = 50;
+    private final Duration SWEEP_INTERVAL = Duration.ofMillis(50);
 
     @Test public void test() throws Exception {
         PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
 
-        IdleConnectionSweeper sweeper = new IdleConnectionSweeper(connectionManager, 30, SWEEP_INTERVAL);
+        IdleConnectionSweeper sweeper = new IdleConnectionSweeper(connectionManager, Duration.ofMillis(30), SWEEP_INTERVAL);
         Thread.sleep(75);
-        Assert.assertTrue(sweeper.scheduledExecutionTime() + SWEEP_INTERVAL > System.currentTimeMillis());
+        Assert.assertTrue(sweeper.scheduledExecutionTime().plus(SWEEP_INTERVAL).isAfter(Instant.now()));
 
         sweeper.destroy();
         Assert.assertTrue(sweeper.isDestroyed());
@@ -48,9 +50,9 @@ public class IdleConectionSweeperTest {
         }
 
         Timer timer = new Timer(true);
-        sweeper = new IdleConnectionSweeper(connectionManager, 30, SWEEP_INTERVAL, timer);
+        sweeper = new IdleConnectionSweeper(connectionManager, Duration.ofMillis(30), SWEEP_INTERVAL, timer);
         Thread.sleep(10);
-        Assert.assertTrue(sweeper.scheduledExecutionTime() + SWEEP_INTERVAL > System.currentTimeMillis());
+        Assert.assertTrue(sweeper.scheduledExecutionTime().plus(SWEEP_INTERVAL).isAfter(Instant.now()));
 
         sweeper.destroy();
         Assert.assertTrue(sweeper.isDestroyed());

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.


More information about the commits mailing list