Search code examples
ignite

About apache ignite IgnitePredicate<EventAdapter> interface


I am using IgnitePredicate interface to get to know when the cache had modified(put or remove).

@Override
    public boolean apply(EventAdapter e) {
        if (e instanceof CacheEvent) {
            CacheEvent cacheEvent = (CacheEvent)e;
            if (e.type() == EventType.EVT_CACHE_OBJECT_PUT) {
                MyClass obj = (MyClass)cacheEvent.newValue();//line 1
                MyClass obj = ((BinaryObject)cacheEvent.newValue()).deserialize()//line 2
            }
        }
        return true;
    }

I wanna get the new data put into the cache by the codes above.

At first, I am using the code line 1, and it works.

Afterward, I found it cause an error "java.lang.ClassCastException: org.apache.ignite.internal.binary.BinaryObjectImpl cannot be cast to MyClass".So I change the code to line 2.But it cause another exception "MyClass cannot be cast to o.a.i.i.b.BinaryObjectImpl".And the newValue is a MyClass object.

I wanna know why sometimes it is a BinaryOjbectImpl and sometimes it is a MyClass object. Or it depended on something I do not know. Or something wrong in my code.

Thanks. Best regards.


Solution

  • In general events are being triggered not only for one exact cache but for multiple caches. CacheEvent contains cacheName as a property. As far as I can see you don't filter them according cacheName. So it's possible that some cache is being used with binary storage enabled. You can get more information here.

    For example cache A is created as:

    IgniteCache<Integer, BinaryObject> binaryCache = ignite.cache("A").withKeepBinary();
    

    Another one (cache B) is created as:

    IgniteCache<Integer, SomeClass> cache = ignite.cache("B");
    

    By the way I suppose that the right way to achieve the desired behavior is to use continuous queries.