Skip to content

Commit

Permalink
ATS-12
Browse files Browse the repository at this point in the history
  • Loading branch information
madness-inc committed Aug 16, 2019
1 parent a3f13d9 commit bc60bf7
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 12 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Expand Up @@ -226,11 +226,11 @@
<artifactId>appng-api</artifactId>
<version>1.18.0</version>
<scope>provided</scope>
</dependency>
</dependency>
<dependency>
<groupId>com.hazelcast</groupId>
<artifactId>hazelcast-client</artifactId>
<version>3.12</version>
<version>3.12.2</version>
</dependency>
</dependencies>
</project>
Expand Up @@ -20,10 +20,13 @@
import org.apache.catalina.LifecycleException;
import org.apache.catalina.Session;
import org.apache.catalina.session.PersistentManagerBase;
import org.apache.juli.logging.Log;
import org.apache.juli.logging.LogFactory;
import org.appng.tomcat.session.Utils;

public class HazelcastPersistentManager extends PersistentManagerBase {

private static final Log log = LogFactory.getLog(HazelcastPersistentManager.class);
private String name;

protected void destroyInternal() throws LifecycleException {
Expand All @@ -47,6 +50,18 @@ public void remove(Session session, boolean update) {
removeSession(session.getId());
}

@Override
public Session createSession(String sessionId) {
Session session = super.createSession(sessionId);
try {
getStore().save(session);
} catch (IOException e) {
log.warn(String.format("Error creating session: %s", session.getIdInternal()));
session = null;
}
return session;
}

@Override
public Session findSession(String id) throws IOException {
// do not call super, instead load the session directly from the store
Expand Down
Expand Up @@ -28,8 +28,7 @@
import org.appng.tomcat.session.Utils;

/**
* A {@link Valve} that uses {@link HazelcastPersistentManager} to store a
* {@link Session}
* A {@link Valve} that uses {@link HazelcastPersistentManager} to store a {@link Session}
*/
public class HazelcastSessionTrackerValve extends PersistentValve {

Expand All @@ -44,16 +43,15 @@ public void invoke(Request request, Response response) throws IOException, Servl
HazelcastPersistentManager manager = (HazelcastPersistentManager) request.getContext().getManager();
Session session = request.getSessionInternal(false);
if (session != null) {
if (!Utils.isTemplateRequest(request)) {
if (session.isValid()) {
log.debug(String.format("Request with session completed, saving session %s", session.getId()));
manager.getStore().save(session);
} else {
log.debug(String.format("HTTP Session has been invalidated, removing %s", session.getId()));
manager.remove(session);
}
if (session.isValid()) {
log.debug(String.format("Request with session completed, saving session %s", session.getId()));
manager.getStore().save(session);
} else {
log.debug(String.format("HTTP Session has been invalidated, removing %s", session.getId()));
manager.remove(session);
}
}

long duration = System.currentTimeMillis() - start;
if (log.isDebugEnabled() && duration > 0) {
log.debug(String.format("handling session for %s took %sms", request.getServletPath(), duration));
Expand Down

0 comments on commit bc60bf7

Please sign in to comment.