Spring 使用+;弹簧&x2B;Struts应用程序

Spring 使用+;弹簧&x2B;Struts应用程序,spring,caching,ehcache,spring-4,Spring,Caching,Ehcache,Spring 4,在我的应用程序中,ehcache的配置如下所示 AppDataRegion.java //import statements. public class AppDataRegion{ //Variable for region identifier. private String appRegionId; // constructor sets the appRegionId; //obtained from the system current time. public AppDataR

在我的应用程序中,ehcache的配置如下所示

AppDataRegion.java

//import statements.

public class AppDataRegion{

//Variable for region identifier.
private String appRegionId;

// constructor sets the appRegionId;
//obtained from the system current time.
public AppDataRegion(){
appRegionId = String.valueOf(System.currentTimeMillis());
}

//Variable for cachemanager
// injected by spring DI from context xml
Private CacheManager appCacheMngr;

//necessary getter / setter methods for variable cachemanager
//necessary getter / setter methods for variable appRegionId

//method to create a region in cache using the appRegionId value
private createRegion(){
  if (!appCacheMngr.cacheExists(appRegionId)){
    try {
        appCacheMngr.addCache(appRegionId);
    catch (exc){
    //exception handled
    }
   }
}

public Cache getRegion(){
if(appCacheMngr == null || !appCacheMngr.cacheExists(appRegionId)){
createRegion();
}
return appCacheMangr.getCache(appRegionId);
}
private createCustRegion(){
    try{
    Cache custCache = appCacheMngr.getCache(“custdiskCache”);
    If(null == custCache.addCache(custCache);
    }catch (exp){
    //handled the exceptions
}
retrun appCacheMngr.getCache(“custdiskCache”);
}
}
弹簧配置

<bean id="appDataStoreService" class="com.app.cache.AppDataStoreService" >      
        <property name="appDataStoreRegion" ref="appDataStoreRegion"/>
     </bean>

     <bean id="appDataStoreRegion" class="com.app.cache.AppDataStoreRegion">        
        <property name="appcacheManager" ref="cacheManager"/>
     </bean>

<bean id="cacheManager" class="net.sf.ehcache.CacheManager" factory-method="create">       
        <constructor-arg index="0" type="java.net.URL" value="classpath:ehcache-app.xml" />
    </bean>
在应用程序中,在DTO对象中填充数据后,我调用appDataStoreService类的storeCustObjInCache()方法将内容写入磁盘

ehcache配置为单例而不是实例

web应用程序使用struts(2.3.20)框架进行web流,使用Spring框架(4.1.2)进行DI对象管理

同样,对于JSP for UI,我们迭代使用bean对象,该对象具有显示内容的列表

我们从1.2.2迁移到Ehcache2.9.0,只是在SpringXML上下文中更改了jar和配置。迁移之后,我们开始频繁地出现以下异常

net.sf.ehcache.CacheException:由于ConcurrentModificationException,未能序列化元素。这通常是线程之间不适当地共享线程不安全对象(如ArrayList、HashMap等)的结果 位于net.sf.ehcache.store.disk.DiskStorageFactory.serializeElement(DiskStorageFactory.java:405) 位于net.sf.ehcache.store.disk.DiskStorageFactory.write(DiskStorageFactory.java:385) 在net.sf.ehcache.store.disk.DiskStorageFactory$DiskWriteTask.call(DiskStorageFactory.java:477) 位于net.sf.ehcache.store.disk.DiskStorageFactory$PersistentDiskWriteTask.call(DiskStorageFactory.java:1071) 位于net.sf.ehcache.store.disk.DiskStorageFactory$PersistentDiskWriteTask.call(DiskStorageFactory.java:1055) 在java.util.concurrent.FutureTask.run(FutureTask.java:266)处 位于java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180) 位于java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293) 位于java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) 位于java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) 运行(Thread.java:745) 原因:java.util.ConcurrentModificationException 位于java.util.ArrayList.writeObject(ArrayList.java:766) 在sun.reflect.GeneratedMethodAccessor8008.invoke处(未知源) 在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)中 位于java.lang.reflect.Method.invoke(Method.java:497) 位于java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:988) 位于java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1496) 位于java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1432) 位于java.io.ObjectOutputStream.WriteObject 0(ObjectOutputStream.java:1178) 位于java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1548) 位于java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1509) 位于java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1432) 位于java.io.ObjectOutputStream.WriteObject 0(ObjectOutputStream.java:1178) 位于java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1548) 位于java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:441) 位于net.sf.ehcache.Element.writeObject(Element.java:875)

我的理解是,当ehcache试图写入磁盘时,另一个线程正在修改序列化列表。我无法找到哪个线程以及它的可能性

有没有一个例子说明如何使用ehcache,在spring中以编程方式使用它,而不使用注释

关于如何识别导致此问题的线程,您有两种选择:

  • 在缓存任何对象后查找其所有代码路径
  • 确保缓存的内容是不可变的
  • 如果你能以这种方式重构你的应用程序,我倾向于选择2

    这个问题绝对不是由连接不同参与者对象的方式引起的,顺便说一句,在提供的示例中没有使用注释

    public class AppDataStoreService{
    
    //datastoreregion variable declaration
    private AppDataStoreRegion appDataStoreRegion;
    
    
    public void storeCustObjInCache(String id, Object obj){
        Cache region = appDataStoreRegion.getCustRegion();
        If(region != null && id !=null && region.isElementInMemory(id)){
        region.remove(id);
    }
    Element ele = new Element(id, obj);
    If(region != null) region.put(ele);
    }
    }