@@ -22,9 +22,10 @@ import org.springframework.session.SessionRepository;
|
||||
* Specifies when to write to the backing Hazelcast instance.
|
||||
*
|
||||
* @author Aleksandar Stojsavljevic
|
||||
* @since 1.3
|
||||
* @since 1.3.0
|
||||
*/
|
||||
public enum HazelcastFlushMode {
|
||||
|
||||
/**
|
||||
* Only writes to Hazelcast when
|
||||
* {@link SessionRepository#save(org.springframework.session.Session)} is invoked. In
|
||||
@@ -34,9 +35,10 @@ public enum HazelcastFlushMode {
|
||||
|
||||
/**
|
||||
* Writes to Hazelcast as soon as possible. For example
|
||||
* {@link SessionRepository#createSession()} will write the session to Hazelcast. Another
|
||||
* example is that setting an attribute on the session will also write to Hazelcast
|
||||
* immediately.
|
||||
* {@link SessionRepository#createSession()} will write the session to Hazelcast.
|
||||
* Another example is that setting an attribute on the session will also write to
|
||||
* Hazelcast immediately.
|
||||
*/
|
||||
IMMEDIATE
|
||||
|
||||
}
|
||||
|
||||
@@ -182,8 +182,8 @@ public class HazelcastSessionRepository implements
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the Hazelcast flush mode. Default flush mode is {@link HazelcastFlushMode#ON_SAVE}.
|
||||
*
|
||||
* Sets the Hazelcast flush mode. Default flush mode is
|
||||
* {@link HazelcastFlushMode#ON_SAVE}.
|
||||
* @param hazelcastFlushMode the new Hazelcast flush mode
|
||||
*/
|
||||
public void setHazelcastFlushMode(HazelcastFlushMode hazelcastFlushMode) {
|
||||
@@ -266,9 +266,9 @@ public class HazelcastSessionRepository implements
|
||||
* basis for its mapping. It keeps track if changes have been made since last save.
|
||||
*
|
||||
* @author Aleksandar Stojsavljevic
|
||||
* @since 1.3
|
||||
*/
|
||||
final class HazelcastSession implements ExpiringSession {
|
||||
|
||||
private final MapSession delegate;
|
||||
private boolean changed;
|
||||
|
||||
@@ -284,9 +284,8 @@ public class HazelcastSessionRepository implements
|
||||
|
||||
/**
|
||||
* Creates a new instance from the provided {@link MapSession}.
|
||||
*
|
||||
* @param cached the {@link MapSession} that represents the persisted session that was
|
||||
* retrieved. Cannot be null.
|
||||
* @param cached the {@link MapSession} that represents the persisted session that
|
||||
* was retrieved. Cannot be {@code null}.
|
||||
*/
|
||||
HazelcastSession(MapSession cached) {
|
||||
Assert.notNull(cached, "MapSession cannot be null");
|
||||
@@ -362,5 +361,7 @@ public class HazelcastSessionRepository implements
|
||||
HazelcastSessionRepository.this.save(this);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -77,19 +77,15 @@ public @interface EnableHazelcastHttpSession {
|
||||
String sessionMapName() default HazelcastHttpSessionConfiguration.DEFAULT_SESSION_MAP_NAME;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* Sets the flush mode for the Hazelcast sessions. The default is ON_SAVE which only
|
||||
* Flush mode for the Hazelcast sessions. The default is {@code ON_SAVE} which only
|
||||
* updates the backing Hazelcast when
|
||||
* {@link SessionRepository#save(org.springframework.session.Session)} is invoked. In
|
||||
* a web environment this happens just before the HTTP response is committed.
|
||||
* </p>
|
||||
* <p>
|
||||
* Setting the value to IMMEDIATE will ensure that the any updates to the Session are
|
||||
* immediately written to the Hazelcast instance.
|
||||
* </p>
|
||||
*
|
||||
* Setting the value to {@code IMMEDIATE} will ensure that the any updates to the
|
||||
* Session are immediately written to the Hazelcast instance.
|
||||
* @return the {@link HazelcastFlushMode} to use
|
||||
* @since 1.3
|
||||
* @since 1.3.0
|
||||
*/
|
||||
HazelcastFlushMode hazelcastFlushMode() default HazelcastFlushMode.ON_SAVE;
|
||||
|
||||
|
||||
@@ -77,7 +77,8 @@ public class HazelcastHttpSessionConfiguration extends SpringHttpSessionConfigur
|
||||
setMaxInactiveIntervalInSeconds(
|
||||
(Integer) enableAttrs.getNumber("maxInactiveIntervalInSeconds"));
|
||||
setSessionMapName(enableAttrs.getString("sessionMapName"));
|
||||
setHazelcastFlushMode((HazelcastFlushMode) enableAttrs.getEnum("hazelcastFlushMode"));
|
||||
setHazelcastFlushMode(
|
||||
(HazelcastFlushMode) enableAttrs.getEnum("hazelcastFlushMode"));
|
||||
}
|
||||
|
||||
public void setMaxInactiveIntervalInSeconds(int maxInactiveIntervalInSeconds) {
|
||||
@@ -91,4 +92,5 @@ public class HazelcastHttpSessionConfiguration extends SpringHttpSessionConfigur
|
||||
public void setHazelcastFlushMode(HazelcastFlushMode hazelcastFlushMode) {
|
||||
this.hazelcastFlushMode = hazelcastFlushMode;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -42,7 +42,6 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.Matchers.eq;
|
||||
import static org.mockito.Matchers.isA;
|
||||
import static org.mockito.Mockito.reset;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.verifyZeroInteractions;
|
||||
@@ -129,7 +128,6 @@ public class HazelcastSessionRepositoryTests {
|
||||
isA(Long.class), eq(TimeUnit.SECONDS));
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
public void saveUpdatedAttributeFlushModeImmediate() {
|
||||
this.repository.setHazelcastFlushMode(HazelcastFlushMode.IMMEDIATE);
|
||||
@@ -138,7 +136,6 @@ public class HazelcastSessionRepositoryTests {
|
||||
session.setAttribute("testName", "testValue");
|
||||
verify(this.sessions, times(2)).put(eq(session.getId()), eq(session.getDelegate()),
|
||||
isA(Long.class), eq(TimeUnit.SECONDS));
|
||||
reset(this.sessions);
|
||||
|
||||
this.repository.save(session);
|
||||
verifyZeroInteractions(this.sessions);
|
||||
@@ -155,7 +152,6 @@ public class HazelcastSessionRepositoryTests {
|
||||
isA(Long.class), eq(TimeUnit.SECONDS));
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
public void removeAttributeFlushModeImmediate() {
|
||||
this.repository.setHazelcastFlushMode(HazelcastFlushMode.IMMEDIATE);
|
||||
@@ -164,7 +160,6 @@ public class HazelcastSessionRepositoryTests {
|
||||
session.removeAttribute("testName");
|
||||
verify(this.sessions, times(2)).put(eq(session.getId()), eq(session.getDelegate()),
|
||||
isA(Long.class), eq(TimeUnit.SECONDS));
|
||||
reset(this.sessions);
|
||||
|
||||
this.repository.save(session);
|
||||
verifyZeroInteractions(this.sessions);
|
||||
@@ -181,7 +176,6 @@ public class HazelcastSessionRepositoryTests {
|
||||
isA(Long.class), eq(TimeUnit.SECONDS));
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
public void saveUpdatedLastAccessedTimeFlushModeImmediate() {
|
||||
this.repository.setHazelcastFlushMode(HazelcastFlushMode.IMMEDIATE);
|
||||
@@ -190,7 +184,6 @@ public class HazelcastSessionRepositoryTests {
|
||||
session.setLastAccessedTime(System.currentTimeMillis());
|
||||
verify(this.sessions, times(2)).put(eq(session.getId()), eq(session.getDelegate()),
|
||||
isA(Long.class), eq(TimeUnit.SECONDS));
|
||||
reset(this.sessions);
|
||||
|
||||
this.repository.save(session);
|
||||
verifyZeroInteractions(this.sessions);
|
||||
@@ -207,7 +200,6 @@ public class HazelcastSessionRepositoryTests {
|
||||
isA(Long.class), eq(TimeUnit.SECONDS));
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
public void saveUpdatedMaxInactiveIntervalInSecondsFlushModeImmediate() {
|
||||
this.repository.setHazelcastFlushMode(HazelcastFlushMode.IMMEDIATE);
|
||||
@@ -216,26 +208,22 @@ public class HazelcastSessionRepositoryTests {
|
||||
session.setMaxInactiveIntervalInSeconds(1);
|
||||
verify(this.sessions, times(2)).put(eq(session.getId()), eq(session.getDelegate()),
|
||||
isA(Long.class), eq(TimeUnit.SECONDS));
|
||||
reset(this.sessions);
|
||||
|
||||
this.repository.save(session);
|
||||
verifyZeroInteractions(this.sessions);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
public void saveUnchangedFlushModeOnSave() {
|
||||
HazelcastSession session = this.repository.createSession();
|
||||
this.repository.save(session);
|
||||
verify(this.sessions, times(1)).put(eq(session.getId()), eq(session.getDelegate()),
|
||||
isA(Long.class), eq(TimeUnit.SECONDS));
|
||||
reset(this.sessions);
|
||||
|
||||
this.repository.save(session);
|
||||
verifyZeroInteractions(this.sessions);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
public void saveUnchangedFlushModeImmediate() {
|
||||
this.repository.setHazelcastFlushMode(HazelcastFlushMode.IMMEDIATE);
|
||||
@@ -243,7 +231,6 @@ public class HazelcastSessionRepositoryTests {
|
||||
HazelcastSession session = this.repository.createSession();
|
||||
verify(this.sessions, times(1)).put(eq(session.getId()), eq(session.getDelegate()),
|
||||
isA(Long.class), eq(TimeUnit.SECONDS));
|
||||
reset(this.sessions);
|
||||
|
||||
this.repository.save(session);
|
||||
verifyZeroInteractions(this.sessions);
|
||||
|
||||
@@ -150,8 +150,8 @@ public class HazelcastHttpSessionConfigurationTests {
|
||||
HazelcastSessionRepository repository = this.context
|
||||
.getBean(HazelcastSessionRepository.class);
|
||||
assertThat(repository).isNotNull();
|
||||
assertThat(ReflectionTestUtils.getField(repository, "hazelcastFlushMode")).isEqualTo(
|
||||
HazelcastFlushMode.IMMEDIATE);
|
||||
assertThat(ReflectionTestUtils.getField(repository, "hazelcastFlushMode"))
|
||||
.isEqualTo(HazelcastFlushMode.IMMEDIATE);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -162,8 +162,8 @@ public class HazelcastHttpSessionConfigurationTests {
|
||||
HazelcastSessionRepository repository = this.context
|
||||
.getBean(HazelcastSessionRepository.class);
|
||||
assertThat(repository).isNotNull();
|
||||
assertThat(ReflectionTestUtils.getField(repository, "hazelcastFlushMode")).isEqualTo(
|
||||
HazelcastFlushMode.IMMEDIATE);
|
||||
assertThat(ReflectionTestUtils.getField(repository, "hazelcastFlushMode"))
|
||||
.isEqualTo(HazelcastFlushMode.IMMEDIATE);
|
||||
}
|
||||
|
||||
private void registerAndRefresh(Class<?>... annotatedClasses) {
|
||||
|
||||
Reference in New Issue
Block a user