0

ZK + Spring boot + Spring session

asked 2017-10-08 09:51:33 +0800

estuardoumg gravatar image estuardoumg
15 1

Does anyone already try ZK with Spring boot and Spring session using Redis as storage for session information? I´m trying do something with these 3 components, but it seems the status of every component in a page is not persisted, i think i´m not doing something but i really don´t now what thing is, if some has an example or knows where i can find info about this i will appreciate too much.

delete flag offensive retag edit

2 Answers

Sort by » oldest newest most voted
1

answered 2017-10-12 13:08:36 +0800

cor3000 gravatar image cor3000
6280 2 7

updated 2017-10-13 17:37:33 +0800

Hi Estuardo,

yes when using Redis there's at least one catch ... by default it only persists changes to session attributes by calculating a delta.

ZK is storing the ZK-Session (SerializableSession) in a session attribute called javax.zkoss.zk.ui.Session This session attribute will never change during the lifetime of the session (only nested attributes such as the desktop cache containing the component trees change). Hence Redis doesn't know about changes in the internal fields of the ZK-Session object and doesn't update the cached value. The Solution is to somehow tell Spring Redis that this session attribute needs to be persisted. As of the current implementation I found that setting the session attribute again with the same value does the trick. To automate this after each ajax request one can implement an ExecutionCleanup listener as follows.

package zk.example;

import java.util.List;

import org.zkoss.zk.ui.Execution;
import org.zkoss.zk.ui.Session;
import org.zkoss.zk.ui.sys.Attributes;
import org.zkoss.zk.ui.util.ExecutionCleanup;

public class RedisSessionDirty implements ExecutionCleanup {
    public void cleanup(Execution exec, Execution parent, List<Throwable> errs)
            throws Exception {
        Session session = exec.getSession();
        session.setAttribute(Attributes.ZK_SESSION, session.getAttribute(Attributes.ZK_SESSION));
    }
}

and configure it in zk.xml:

<listener>
    <listener-class>zk.example.RedisSessionDirty</listener-class>
</listener>

In my case this was enough to make Redis save the updated ZK-Session and make it available in subsequent ajax requests.

BTW: I used the following maven dependencies: for spring-boot-redis

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.7.RELEASE</version>
</parent>
...
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.session</groupId>
        <artifactId>spring-session</artifactId>
        <version>1.3.1.RELEASE</version>
    </dependency>

and @EnableRedisHttpSession to enable the default configuration.

Robert

link publish delete flag offensive edit
0

answered 2017-10-13 13:07:11 +0800

estuardoumg gravatar image estuardoumg
15 1

It works !! Nice trick, thank you very much for your help, your time and explanation.

link publish delete flag offensive edit

Comments

You're welcome, I am glad to hear that. If you come across a less tricky/hacky way to achieve the same don't hesitate to drop the information here. ;)

cor3000 ( 2017-10-13 13:49:05 +0800 )edit

Side Note: in case you intent to setup clustering -> make sure to use STICKY-sessions. Without them ZK can't synchronize concurrent updates within a session and information gets lost when session updates are written into the Redis cache from 2 different nodes.

cor3000 ( 2017-10-13 13:53:22 +0800 )edit

Yes, you are right, thanks again :)

estuardoumg ( 2017-10-14 00:41:05 +0800 )edit
Your answer
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow

RSS

Stats

Asked: 2017-10-08 09:51:33 +0800

Seen: 46 times

Last updated: Oct 13 '17

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More