4

I have a simple Infinispan local-cache (also tried distributed cache) on Wildfly 8.2. Everything is working fine until I do redeployment of my .WAR. After redeployment of my .WAR I get the following error:

Caused by: java.lang.ClassCastException: my.package.MyClass cannot be cast to my.package.MyClass

Full stacktrace: https://gist.github.com/bagges/07af1842a874f7c99ef3

I lookup the Cache in a CDI Bean like this:

@Path("/mypath")
@Stateless
public class MyServiceClass {

    @Resource(lookup = "java:jboss/infinispan/myContainer")
    private CacheContainer container;

    private Cache<Integer, MyCacheObject> myCache;

    @PostConstruct
    public void start() {
        myCache = container.getCache("myCache");
    }

    @GET
    public String get() {
        if(!myCache.containsKey(1)) {
            myCache.put(1, new MyCacheObject(1, "Hello Cache"));
        }
        return myCache.get(1).getName();
    }
}

Wildfly-Config:

<cache-container name="myContainer" jndi-name="java:jboss/infinispan/myContainer" start="EAGER">
    <local-cache name="myCache"/>
</cache-container>

I know that the error occured because off different class loaders. Infinispan tries to cast the entity stored with the previous classloader which cannot work. But how to avoid this?

markus
  • 602
  • 4
  • 13

2 Answers2

2

Don't use start="EAGER". That will fix your problem. We've removed this from WildFly 9, since its misuse has been the source of many user headaches.

Also, I recommend injecting your cache directly (instead of just the cache container). That way the cache lifecycle will be bound to the lifecycle of your deployment. e.g.

@Resource(lookup = "java:jboss/infinispan/cache/myContainer/myCache")
private Cache<Integer, MyCacheObject> myCache;

Lastly, feel free to use a resource-ref to avoid referencing a vendor-specific jndi namespace in your application.

Paul Ferraro
  • 326
  • 1
  • 3
  • Thanks for this hint. If I use start="LAZY", the redeployment problem is gone. But only because the cache is evicted during redeployment. The previously stored objects are gone. I can still not access the cache from another EAR/WAR. It will still throw the ClassCastException. – markus Jun 10 '15 at 07:50
0

You should be able to share the cache if you enable store-as-binary in the Infinispan configuration and you force the cache to use the application's classloader instead of the one in the GlobalConfiguration:

Cache appSpecificCache = cacheFromJndi.getAdvancedCache().with(applicationClassLoader)
Dan Berindei
  • 7,054
  • 3
  • 41
  • 48