使用Spring进行Infinispan,从缓存投射失败 [英] Infinispan with Spring, casting from cache failing

查看:104
本文介绍了使用Spring进行Infinispan,从缓存投射失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有已部署到WildFly 10的Spring 1.4应用程序,它使用的是WildFly内置的Infinispan 8.1.

I have Spring 1.4 app which is deployed to WildFly 10, and it is using Infinispan 8.1 built in with WildFly.

我设法正确部署了应用程序,这是Infinispan的配置: 1)CacheManager

I have managed to deploy the app properly, and this is the configuration for the Infinispan: 1) CacheManager

@Bean
public CacheManager cacheManager() throws Exception {
    JndiTemplate jndiTemplate = new JndiTemplate();
    EmbeddedCacheManager embededCacheManager = (EmbeddedCacheManager) jndiTemplate.lookup("java:jboss/infinispan/container/CONTAINER");
    SpringEmbeddedCacheManager cacheManager = new SpringEmbeddedCacheManager(embededCacheManager);
}

2)pom.xml

2) pom.xml

<dependency>
    <groupId>org.infinispan</groupId>
    <artifactId>infinispan-spring</artifactId>
    <version>8.1.0.Final</version>
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-logging</artifactId>
        </exclusion>
        <exclusion>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
        </exclusion>
        <exclusion>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
        </exclusion>
        <exclusion>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-jcl</artifactId>
        </exclusion>
        <exclusion>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-slf4j-impl</artifactId>
        </exclusion>
    </exclusions>
</dependency>

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <configuration>
        <archive>
            <manifestEntries>
                <Dependencies>org.infinispan, org.infinispan.commons, org.jboss.as.clustering.infinispan export</Dependencies>
            </manifestEntries>
        </archive>
    </configuration>
</plugin>

当我第一次部署应用程序时,一切正常.但是,在启动缓存后以及重新部署应用程序时,使用缓存时出现以下错误:

When I deploy the app for the first time everything works fine. However, after the cache has been started, and when the application is redeployed I get the following error when using cache:

java.lang.ClassCastException: com.dplesa.Class cannot be cast to com.dplesa.Class

我尝试使用不同的类进行此操作,但没有做任何事情,错误是相同的.但是,我没有从缓存了简单字符串的缓存中得到此错误.什么原因可能导致此问题?

I tried this with different classes and no mater what I do, the error is the same. However, I don't get this error from caches where simple Strings are cached. What could cause this problem?

推荐答案

这听起来很吓人,但在这种情况下,行为是正确的.

This may sound scary but in this case, the behavior is correct.

一个类总是绑定到一个特定的类加载器. Wildfly使用模块化的类加载器,每个部署使用不同的类加载器实例.现在,假设您使用类加载器'A'放置了类的某些实例,进行了重新部署(正在使用类加载器'A'部署新的部署,并使用类加载器'B'加载了新部署),并尝试使用类加载器'B'从缓存中读取数据.这些类加载器不匹配,并且结果与您的异常不同-类com.dplesa.Class无法强制转换为com.dplesa.Class.

A class is always binded to a particular classloader. Wildfly uses modular classloaders and each deployment uses different classloader instance. Now imagine that you put some instances of your class using classloader 'A', do redeployment (classloader 'A' is being disposed and new deployment is loaded using classloader 'B') and try to read data from cache using classloader 'B'. Those classloaders don't match and this results with your exception - Class com.dplesa.Class cannot be cast to com.dplesa.Class.

有几种方法可以解决此问题:

There are several ways to fix this:

  1. 在您的应用程序中嵌入Infinispan(例如,使用infinispan-embedded工件).使用此技巧,Infinispan将使用与您的域类相同的类加载器进行加载.

  1. Embed Infinispan with your application (using infinispan-embedded artifact for example). With this trick, Infinispan will be loaded with the same classloader as your domain classes.

将域类放入Wildfly模块中.

Put your domain classes in Wildfly modules.

分别部署Infinispan集群,并使用HotRod Client(例如,使用infinispan-remote工件)连接到它.

Deploy Infinispan cluster separately and connect to it using HotRod Client (using infinispan-remote artifact for example).

解决方案#1最简单,但是您需要注意重新部署期间集群的状况(确保节点正确地加入/离开集群,并根据需要复制数据等).

Solution #1 is the simplest but you need to pay attention what's happening to your cluster during redeployments (make sure your nodes join/leave the cluster properly, that your data replicates as you desire etc).

这篇关于使用Spring进行Infinispan,从缓存投射失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆