rockyroad
rockyroad

Reputation: 2957

Apache Ignite Caching and PeerClassLoading

1. Is it possible to put non-POJO class instances as the value of a cache?

For example, I have a QueryThread class which is a subclass of java.lang.Thread and I am trying to put this instance in a cache. It looks like the put operation is failing because this cache is always empty.

Consider the following class:

public class QueryThread extends Thread {

private IgniteCache<?, ?> cache;
private String queryId;
private String query;
private long timeIntervalinMillis;
private volatile boolean running = false;

public QueryThread(IgniteCache<?, ?> dataCache, String queryId, String query, long timeIntervalinMillis) {
    this.queryId = queryId;
    this.cache = dataCache;
    this.query = query;
    this.timeIntervalinMillis = timeIntervalinMillis;
}

public void exec() throws Throwable {
    SqlFieldsQuery qry = new SqlFieldsQuery(query, false);
    while (running) {
        List<List<?>> queryResult = cache.query(qry).getAll();
        for (List<?> list : queryResult) {
            System.out.println("result : "+list);
        }
        System.out.println("..... ");
        Thread.sleep(timeIntervalinMillis);
    }
}
}

This class is not a POJO. How do I store an instance of this class in the cache? I tried implementing Serializable (didn't help). I need to be able to do this:

queryCache.put(queryId, queryThread);

Next I tried broadcasting the class using the IgniteCallable interface. But my class takes multiple arguments in the constructor. I feel PeerClassLoading is easy if the class takes a no-arg constructor:

 IgniteCompute compute = ignite.compute(ignite.cluster().forServers());
     compute.broadcast(new IgniteCallable<MyServiceImpl>() {

        @Override
        public MyServiceImpl call() throws Exception {
            MyServiceImpl myService = new MyServiceImpl();
            return myService;
        }

    });

2. How do I do PeerClassLoading in the case of a class with multi-arg constructor?

Upvotes: 0

Views: 263

Answers (1)

Evgenii Zhuravlev
Evgenii Zhuravlev

Reputation: 3007

  1. It's restricted to put Thread instances to the cache, Thread instance cannot be serialized due to call to Native Methods. Thats why you always get empty value.

  2. PeerClassLoading is a special distributed ClassLoader in Ignite for inter-node byte-code exchange. So, it's only about sharing classes between nodes. It doesn't make sense how many arguments in constructor class have. But, on the other hand, object, that you created, will be serialised and sent to other nodes and for deserialisation it will need a default(non-arg) constructor.

Upvotes: 2

Related Questions