Change the specification and interface contract of SessionExpirationPolicy.determineExpirationTimeout(:Session):Duration to prevent null return values and return an Optional of Duration instead.
An Optional<Duration> return value, particularly when Optional#EMPTY, may suggest that the Session should not expire or that the expiration determination should be delegated to the next expiration policy in the chain of policies when composing expiration policies using the Composite Software Design Pattern. Alternately, an Optional#EMPTY return value can suggest to the underlying Spring Session data store provider (e.g. Apache Geode or Pivotal GemFire) to use the configured, default Idle Timeout (TTI) Expiration policy for the Region managing Session state. Resolve gh-5.
This commit is contained in:
@@ -20,6 +20,7 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.time.Duration;
|
||||
import java.util.Optional;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
@@ -45,7 +46,8 @@ import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
|
||||
/**
|
||||
* Integration tests for {@link EnableGemFireHttpSession} and {@link GemFireHttpSessionConfiguration}.
|
||||
* Integration tests for {@link EnableGemFireHttpSession} and {@link GemFireHttpSessionConfiguration}
|
||||
* involving {@link Session} expiration configuration.
|
||||
*
|
||||
* @author John Blum
|
||||
* @see org.junit.Test
|
||||
@@ -90,7 +92,8 @@ public class CustomSessionExpirationConfigurationIntegrationTests {
|
||||
public void sessionExpirationPolicyConfigurationIsCorrect() {
|
||||
|
||||
assertThat(this.sessionExpirationPolicy).isInstanceOf(TestSessionExpirationPolicy.class);
|
||||
assertThat(this.sessionExpirationPolicy.determineExpirationTimeout(null)).isEqualTo(Duration.ofMinutes(10));
|
||||
assertThat(this.sessionExpirationPolicy.determineExpirationTimeout(null).orElse(null))
|
||||
.isEqualTo(Duration.ofMinutes(10));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -154,8 +157,8 @@ public class CustomSessionExpirationConfigurationIntegrationTests {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Duration determineExpirationTimeout(Session session) {
|
||||
return this.expirationTimeout;
|
||||
public Optional<Duration> determineExpirationTimeout(Session session) {
|
||||
return Optional.ofNullable(this.expirationTimeout);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,21 +17,22 @@
|
||||
package org.springframework.session.data.gemfire.expiration;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.apache.geode.cache.Region;
|
||||
|
||||
import org.springframework.lang.NonNull;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.session.Session;
|
||||
|
||||
/**
|
||||
* The {@link SessionExpirationPolicy} interface is a Strategy Interface defining a contract for users to implement
|
||||
* custom application expiration policies and rules for {@link Session} management.
|
||||
* custom application expiration policies and rules for {@link Session} state management.
|
||||
*
|
||||
* Examples of different {@link Session} expiration strategies might include, but are not limited to:
|
||||
* idle expiration timeouts, fixed duration expiration timeouts, Time-To-Live (TTL) expiration, and so on.
|
||||
*
|
||||
* @author John Blum
|
||||
* @see java.lang.FunctionalInterface
|
||||
* @see org.apache.geode.cache.Region
|
||||
* @see org.springframework.session.Session
|
||||
* @see org.springframework.session.data.gemfire.expiration.support.FixedTimeoutSessionExpirationPolicy
|
||||
@@ -42,21 +43,26 @@ import org.springframework.session.Session;
|
||||
public interface SessionExpirationPolicy {
|
||||
|
||||
/**
|
||||
* Determines the {@link Duration length of time} until the given {@link Session} will expire.
|
||||
* Determines an {@link Optional} {@link Duration length of time} until the given {@link Session} will expire.
|
||||
* A {@link Duration#ZERO Zero} or {@link Duration#isNegative() Negative Duration} indicates that
|
||||
* the {@link Session} has expired.
|
||||
*
|
||||
* May return {@literal null}, which indicates to Apache Geode or Pivotal GemFire that it should default to
|
||||
* the configured Idle Timeout (TTI) Expiration Policy for the {@link Session} {@link Region} to determine when
|
||||
* the {@link Session} will expire.
|
||||
* May return {@link Optional#EMPTY} as a "suggestion" that the Session should not expire or that the expiration
|
||||
* determination should be handled by the next expiration policy in a chain of policies. Implementors are free
|
||||
* to compose 2 or more expiration policies using Composite Software Design Pattern as necessary.
|
||||
*
|
||||
* @param session {@link Session} to evaluate. A {@link Session} object is required.
|
||||
* @return a {@link Duration} specifying the length of time until the {@link Session} will expire.
|
||||
* May return {@literal null} to indicate that the default, configured Idle Timeout (TTI) Expiration Policy
|
||||
* for the {@link Session} {@link Region} should be used to determine when the {@link Session} will expire.
|
||||
* In Apache Geode or Pivotal GemFire's case, an {@link Optional#EMPTY} return value will indicate that it
|
||||
* should default to the configured Entry Idle Timeout (TTI) Expiration Policy of the {@link Region} managing
|
||||
* {@link Session} state to determine exactly when the {@link Session} will expire.
|
||||
*
|
||||
* @param session {@link Session} to evaluate. {@link Session} is required.
|
||||
* @return an {@link Optional} {@link Duration} specifying the length of time until the {@link Session} will expire.
|
||||
* @see <a href="https://en.wikipedia.org/wiki/Composite_pattern">Composite Software Design Pattern</a>
|
||||
* @see org.springframework.session.Session
|
||||
* @see java.time.Duration
|
||||
* @see java.util.Optional
|
||||
*/
|
||||
@Nullable
|
||||
Duration determineExpirationTimeout(@NonNull Session session);
|
||||
Optional<Duration> determineExpirationTimeout(@NonNull Session session);
|
||||
|
||||
/**
|
||||
* Specifies the {@link ExpirationAction action} to take when the {@link Session} expires.
|
||||
@@ -71,7 +77,7 @@ public interface SessionExpirationPolicy {
|
||||
}
|
||||
|
||||
/**
|
||||
* Enumeration of different actions to take when the {@link Session} expires.
|
||||
* Enumeration of different actions to take when a {@link Session} expires.
|
||||
*/
|
||||
enum ExpirationAction {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user