使用terracotta服务器

时间:2017-07-12 14:55:04

标签: ehcache terracotta

我正在尝试配置terracotta服务器以使用spring / mybatis应用程序,我收到以下错误。我不确定这是否意味着密钥本身或密钥返回的值无法序列化。缓存作为本地缓存工作正常,但现在有问题尝试使用服务器。我需要一个线索,为什么这不能被序列化。谢谢。

所以我从这个How do you serialize a Spring Bean (spring 3)得到了一个线索,它可能与缺乏会话范围有关。当我启动Tomcat并加载第一个网页时会发生这些错误。我将实现java.io.Serializable添加到Site bean类中,这使我将该错误移到了下一个被调用的bean中。将此添加到许多bean之后,我想知道是否A,这是正确的做法,并且会在这些spring类上强制实现java.io.Serializable会产生任何副作用吗? B:有没有更好的方法呢,因为我在这个应用程序中有很多bean?

SEVERE: Servlet.service() for servlet [ezreg] in context with path [] threw exception [Request processing failed; nested exception is net.sf.ehcache.CacheException: The value com.trifecta.src.ezreg.beans.Site@655ad5d5 for key getSiteByHostname127.0.0.1 is not Serializable. Consider using Element.getObjectValue()] with root cause
net.sf.ehcache.CacheException: The value com.trifecta.src.ezreg.beans.Site@655ad5d5 for key getSiteByHostname127.0.0.1 is not Serializable. Consider using Element.getObjectValue()
    at net.sf.ehcache.Element.getValue(Element.java:326)
    at net.sf.ehcache.ElementData.<init>(ElementData.java:35)
    at net.sf.ehcache.EternalElementData.<init>(EternalElementData.java:19)
    at org.terracotta.modules.ehcache.store.ValueModeHandlerSerialization.createElementData(ValueModeHandlerSerialization.java:48)
    at org.terracotta.modules.ehcache.store.ClusteredStore.doPut(ClusteredStore.java:745)
    at org.terracotta.modules.ehcache.store.ClusteredStore.putInternal(ClusteredStore.java:291)
    at org.terracotta.modules.ehcache.store.ClusteredStore.put(ClusteredStore.java:263)
    at org.terracotta.modules.ehcache.store.ClusteredSafeStore.put(ClusteredSafeStore.java:247)
    at org.terracotta.modules.ehcache.store.nonstop.NonStopStoreWrapper.put(NonStopStoreWrapper.java:820)
    at net.sf.ehcache.Cache.putInternal(Cache.java:1617)
    at net.sf.ehcache.Cache.put(Cache.java:1543)
    at net.sf.ehcache.Cache.put(Cache.java:1508)
    at org.springframework.cache.ehcache.EhCacheCache.put(EhCacheCache.java:121)
    at org.springframework.cache.interceptor.AbstractCacheInvoker.doPut(AbstractCacheInvoker.java:85)
    at org.springframework.cache.interceptor.CacheAspectSupport$CachePutRequest.apply(CacheAspectSupport.java:784)
    at org.springframework.cache.interceptor.CacheAspectSupport.execute(CacheAspectSupport.java:417)
    at org.springframework.cache.interceptor.CacheAspectSupport.execute(CacheAspectSupport.java:327)
    at org.springframework.cache.interceptor.CacheInterceptor.invoke(CacheInterceptor.java:61)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213)
    at com.sun.proxy.$Proxy56.getSiteByHostname(Unknown Source)
    at com.trifecta.src.ezreg.daos.SiteDaoImpl.getSiteByHostname(SiteDaoImpl.java:35)


doa method:
    public Site getSiteByHostname(String hostname) {        
        return getSiteMapper().getSiteByHostname(hostname);
    }

mapper方法:

@Cacheable(cacheNames="siteCache", key="#root.methodName.concat(#root.args)")
Site getSiteByHostname(String hostname);

返回Site bean:

package com.trifecta.src.ezreg.beans;

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;

@XmlRootElement
public class Site {
    public static String ADMIN_CURRENT_SITE = "adminCurrentSite";
    public static String _CURRENT_SITE = "currentSite";

    @XmlAttribute
    private Long id;

    @XmlAttribute
    private String name;

    @XmlAttribute
    private String supportphonenumber;

    @XmlElement
    private SitePreference sitePreference;

    @XmlElement
    private SiteInterfaceDevice siteInterfaceDevice;

    @XmlElement
    private SitePdfFormat sitePdfFormat;

    @XmlAttribute
    private boolean ecum; 

    @XmlTransient
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    @XmlTransient
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    @XmlTransient
    public SiteInterfaceDevice getSiteInterfaceDevice() {
        return siteInterfaceDevice;
    }
    public void setSiteInterfaceDevice(SiteInterfaceDevice siteInterfaceDevice) {
        this.siteInterfaceDevice = siteInterfaceDevice;
    }
    @XmlTransient
    public SitePdfFormat getSitePdfFormat() {
        return sitePdfFormat;
    }
    public void setSitePdfFormat(SitePdfFormat sitePdfFormat) {
        this.sitePdfFormat = sitePdfFormat;
    }
    @XmlTransient
    public SitePreference getSitePreference() {
        return sitePreference;
    }
    public void setSitePreference(SitePreference sitePreference) {
        this.sitePreference = sitePreference;
    }
    @XmlTransient
    public String getSupportphonenumber() {
        return supportphonenumber;
    }
    public void setSupportphonenumber(String supportphonenumber) {
        this.supportphonenumber = supportphonenumber;
    }
    @XmlTransient
    public boolean getEcum() {
        return ecum;
    }
    public void setEcum(boolean ecum) {
        this.ecum = ecum;
    }
}


public class Site implements java.io.Serializable{
    public static String ADMIN_CURRENT_SITE = "adminCurrentSite";
    public static String _CURRENT_SITE = "currentSite";

1 个答案:

答案 0 :(得分:1)

在本地缓存时,您可以仅使用堆内存进行设置,因此不需要存储在缓存中的键/值。

但是,当您转移到群集或实际上除堆之外的任何其他缓存层时,您的密钥和值必须实现Serializable,因为这是我们可以通过线路存储/传送映射的唯一方法。 / p>

因此,在您的情况下,com.trifecta.src.ezreg.beans.Site类型不会implement Serializable

您有两种选择:

  1. 将类型定义更新为implement Serializable并确保其为真 - 即它只有Serializable个字段
  2. 将非序列化值转换为可序列化的值。
  3. 请注意,使用Ehcache 3,您可以选择为您的类型指定自定义序列化程序,而不会限制您使用Java序列化。