Polish Hazelcast

* This commit moves the hazelcast support into a parent package
so that it no longer impies a Spring Data dependency.
* Add guards on SessionEntryListener logger
* Remove getSessionMapName on HazelcastHttpSessionConfiguration
* Use setSessionMapName on HazelcastHttpSessionConfiguration
rather than field access
* Formatting polish
* Fix Javadoc

Issue gh-276
This commit is contained in:
Rob Winch
2015-11-05 08:43:53 -06:00
parent d1c00c6080
commit 76d341d6ca
7 changed files with 48 additions and 45 deletions

View File

@@ -17,7 +17,7 @@ package docs.http;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.session.data.hazelcast.config.annotation.web.http.EnableHazelcastHttpSession;
import org.springframework.session.hazelcast.config.annotation.web.http.EnableHazelcastHttpSession;
import com.hazelcast.config.Config;
import com.hazelcast.core.Hazelcast;

View File

@@ -17,7 +17,7 @@ package sample;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.session.data.hazelcast.config.annotation.web.http.EnableHazelcastHttpSession;
import org.springframework.session.hazelcast.config.annotation.web.http.EnableHazelcastHttpSession;
import org.springframework.util.SocketUtils;
import com.hazelcast.config.NetworkConfig;

View File

@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.session.data.hazelcast;
package org.springframework.session.hazelcast;
import static org.fest.assertions.Assertions.assertThat;
@@ -24,7 +24,7 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.session.ExpiringSession;
import org.springframework.session.SessionRepository;
import org.springframework.session.data.hazelcast.config.annotation.web.http.EnableHazelcastHttpSession;
import org.springframework.session.hazelcast.config.annotation.web.http.EnableHazelcastHttpSession;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;

View File

@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.session.data.hazelcast.config.annotation.web.http;
package org.springframework.session.hazelcast.config.annotation.web.http;
import static org.fest.assertions.Assertions.assertThat;
@@ -35,6 +35,7 @@ import org.springframework.session.data.SessionEventRegistry;
import org.springframework.session.events.SessionCreatedEvent;
import org.springframework.session.events.SessionDeletedEvent;
import org.springframework.session.events.SessionExpiredEvent;
import org.springframework.session.hazelcast.config.annotation.web.http.EnableHazelcastHttpSession;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;

View File

@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.session.data.hazelcast;
package org.springframework.session.hazelcast;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@@ -34,11 +34,11 @@ import com.hazelcast.map.listener.EntryRemovedListener;
* translate those events into the corresponding Spring Session events.
* Publish the Spring Session events with the given {@link ApplicationEventPublisher}.
* <ul>
* <li>entryAdded --> {@link SessionCreatedEvent}</li>
* <li>entryEvicted --> {@link SessionExpiredEvent}</li>
* <li>entryRemoved --> {@link SessionDeletedEvent}</li>
* <li>entryAdded - {@link SessionCreatedEvent}</li>
* <li>entryEvicted - {@link SessionExpiredEvent}</li>
* <li>entryRemoved - {@link SessionDeletedEvent}</li>
* </ul>
*
*
* @author Tommy Ludwig
* @author Mark Anderson
* @since 1.1
@@ -46,7 +46,7 @@ import com.hazelcast.map.listener.EntryRemovedListener;
public class SessionEntryListener implements EntryAddedListener<String, ExpiringSession>,
EntryEvictedListener<String, ExpiringSession>, EntryRemovedListener<String, ExpiringSession> {
private static final Log logger = LogFactory.getLog(SessionEntryListener.class);
private ApplicationEventPublisher eventPublisher;
public SessionEntryListener(ApplicationEventPublisher eventPublisher) {
@@ -55,17 +55,23 @@ public class SessionEntryListener implements EntryAddedListener<String, Expiring
}
public void entryAdded(EntryEvent<String, ExpiringSession> event) {
logger.debug("Session created with id: " + event.getValue().getId());
if(logger.isDebugEnabled()) {
logger.debug("Session created with id: " + event.getValue().getId());
}
this.eventPublisher.publishEvent(new SessionCreatedEvent(this, event.getValue()));
}
public void entryEvicted(EntryEvent<String, ExpiringSession> event) {
logger.debug("Session expired with id: " + event.getOldValue().getId());
if(logger.isDebugEnabled()) {
logger.debug("Session expired with id: " + event.getOldValue().getId());
}
this.eventPublisher.publishEvent(new SessionExpiredEvent(this, event.getOldValue()));
}
public void entryRemoved(EntryEvent<String, ExpiringSession> event) {
logger.debug("Session deleted with id: " + event.getOldValue().getId());
if(logger.isDebugEnabled()) {
logger.debug("Session deleted with id: " + event.getOldValue().getId());
}
this.eventPublisher.publishEvent(new SessionDeletedEvent(this, event.getOldValue()));
}

View File

@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.session.data.hazelcast.config.annotation.web.http;
package org.springframework.session.hazelcast.config.annotation.web.http;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
@@ -26,7 +26,7 @@ import org.springframework.session.config.annotation.web.http.EnableSpringHttpSe
/**
* Add this annotation to a {@code @Configuration} class to expose the
* SessionRepositoryFilter as a bean named "springSessionRepositoryFilter" and
* backed by Hazelcast. In order to leverage the annotation, a single {@link HazelcastInstance}
* backed by Hazelcast. In order to leverage the annotation, a single HazelcastInstance
* must be provided. For example:
* <pre>
* <code>
@@ -45,7 +45,7 @@ import org.springframework.session.config.annotation.web.http.EnableSpringHttpSe
* </pre>
*
* More advanced configurations can extend {@link HazelcastHttpSessionConfiguration} instead.
*
*
* @author Tommy Ludwig
* @since 1.1
* @see EnableSpringHttpSession
@@ -62,11 +62,11 @@ public @interface EnableHazelcastHttpSession {
* <p>If you wish to use external configuration (outside of this annotation) to set this value, you can
* set this to "" (an empty String), which will prevent this configuration from overriding
* the external configuration for this value.</p>
*
*
* @return the seconds a session can be inactive before expiring
*/
String maxInactiveIntervalInSeconds() default "1800";
/**
* This is the name of the Map that will be used in Hazelcast to store the session data.
* Default is "spring:session:sessions".

View File

@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.session.data.hazelcast.config.annotation.web.http;
package org.springframework.session.hazelcast.config.annotation.web.http;
import java.util.Map;
@@ -31,7 +31,7 @@ import org.springframework.session.ExpiringSession;
import org.springframework.session.MapSessionRepository;
import org.springframework.session.SessionRepository;
import org.springframework.session.config.annotation.web.http.SpringHttpSessionConfiguration;
import org.springframework.session.data.hazelcast.SessionEntryListener;
import org.springframework.session.hazelcast.SessionEntryListener;
import org.springframework.session.web.http.SessionRepositoryFilter;
import org.springframework.util.ClassUtils;
@@ -43,26 +43,26 @@ import com.hazelcast.core.IMap;
* Exposes the {@link SessionRepositoryFilter} as a bean named
* "springSessionRepositoryFilter". In order to use this a single
* {@link HazelcastInstance} must be exposed as a Bean.
*
*
* @author Tommy Ludwig
* @since 1.1
* @see EnableHazelcastHttpSession
*/
@Configuration
public class HazelcastHttpSessionConfiguration extends SpringHttpSessionConfiguration implements ImportAware, BeanClassLoaderAware {
/** This is the magic value to use if you do not want this configuration
/** This is the magic value to use if you do not want this configuration
* overriding the maxIdleSeconds value for the Map backing the session data. */
private static final String DO_NOT_CONFIGURE_INACTIVE_INTERVAL_STRING = "";
private ClassLoader beanClassLoader;
private Integer maxInactiveIntervalInSeconds = 1800;
private String sessionMapName = "spring:session:sessions";
private String sessionListenerUid;
private IMap<String, ExpiringSession> sessionsMap;
@Bean
@@ -73,24 +73,24 @@ public class HazelcastHttpSessionConfiguration extends SpringHttpSessionConfigur
MapSessionRepository sessionRepository = new MapSessionRepository(this.sessionsMap);
sessionRepository.setDefaultMaxInactiveInterval(maxInactiveIntervalInSeconds);
return sessionRepository;
}
@PreDestroy
private void removeSessionListener() {
this.sessionsMap.removeEntryListener(this.sessionListenerUid);
}
@Bean
public SessionEntryListener sessionListener(ApplicationEventPublisher eventPublisher) {
return new SessionEntryListener(eventPublisher);
}
/**
* Make a {@link MapConfig} for the given sessionMapName if one does not exist.
* Ensure that maxIdleSeconds is set to maxInactiveIntervalInSeconds for proper session expiration.
*
*
* @param hazelcastInstance the {@link HazelcastInstance} to configure
*/
private void configureSessionMap(HazelcastInstance hazelcastInstance) {
@@ -116,13 +116,13 @@ public class HazelcastHttpSessionConfiguration extends SpringHttpSessionConfigur
enableAttrs = AnnotationAttributes.fromMap(enableAttrMap);
}
}
transferAnnotationAttributes(enableAttrs);
}
private void transferAnnotationAttributes(AnnotationAttributes enableAttrs) {
String maxInactiveIntervalString = enableAttrs.getString("maxInactiveIntervalInSeconds");
if (DO_NOT_CONFIGURE_INACTIVE_INTERVAL_STRING.equals(maxInactiveIntervalString)) {
this.maxInactiveIntervalInSeconds = null;
} else {
@@ -134,17 +134,13 @@ public class HazelcastHttpSessionConfiguration extends SpringHttpSessionConfigur
+ maxInactiveIntervalString + "' instead.", nfe);
}
}
this.sessionMapName = enableAttrs.getString("sessionMapName");
setSessionMapName(enableAttrs.getString("sessionMapName"));
}
public void setMaxInactiveIntervalInSeconds(int maxInactiveIntervalInSeconds) {
this.maxInactiveIntervalInSeconds = maxInactiveIntervalInSeconds;
}
public String getSessionMapName() {
return this.sessionMapName;
}
public void setSessionMapName(String sessionMapName) {
this.sessionMapName = sessionMapName;
}
@@ -152,5 +148,5 @@ public class HazelcastHttpSessionConfiguration extends SpringHttpSessionConfigur
public void setBeanClassLoader(ClassLoader classLoader) {
this.beanClassLoader = classLoader;
}
}