Revision history [back]

click to hide/show revision 1
initial version

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

cor3000 gravatar image cor3000

ZK Team

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));
    }
}

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

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

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

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