5 Replies Latest reply on Feb 18, 2009 8:27 AM by canik

    Writing custom Async Loader

    canik

      Hello folks,

      First a little intro to my persistence problem:
      I'm writing data to a couple of different nodes in JBoss Cache. I'm doing this in a batch (afaik this is interpreted as a transaction by JBoss) since the different cache nodes in fact contain a memory snapshot of a parsing process.
      The parsing process is fast, that is, there can be lots of put operations per second. Thus I use async. replication, to replicate the data only every ~500ms. The problem is, I do not need the database to be hit that often. It would be absolutely sufficient to write the last snapshot (without the intermediate stages) to the DB every 5 seconds. I cannot see any possibility to do this without custom coding.

      First I tried "cache.getInvocationContext().getOptionOverrides().setSuppressPersistence(true);" but this does not work for transactions. (Even if it worked, I still would have the little issue that if I suppress persistence for 5 seconds, and the parsing stopped for a while, the last message would not be persisted (because suppression is on), even if 10 hours had passed. But I could live with that.)

      Then I tried to write a custom cache loader based on the async loader. I tried both: extending AbstractDelegatingCacheLoader and extending AsyncCacheLoader.
      But start() e.g. never gets called in my implementation. I inserted log outputs in multiple methods. When the constructor is invoked, log output appears. Also, for setConfig(), and setCache() I can produce log output. But start() is never invoked. Nor are any of the put() methods invoked. Persistence via JDBC works, though.

      This is a cache configuration xml fragment:

      <loaders passivation="false" shared="true">
       <loader class="mypackage.CustomJDBCCacheLoader" async="false" fetchPersistentState="false"
       ignoreModifications="false" purgeOnStartup="false">
       <properties>
       cache.jdbc.driver=org.postgresql.Driver
       cache.jdbc.url=jdbc:postgresql://localhost:5432/...
       cache.jdbc.user=...
       cache.jdbc.password=...
       cache.jdbc.table.name=...
       cache.jdbc.table.drop=false
       cache.jdbc.node.type=bytea
       </properties>
       </loader>
       </loaders>
      


      So, does anybody know why start() isn't invoked by the framework?

      Best regards,
      Can Özdemir




      import org.apache.commons.logging.Log;
      import org.apache.commons.logging.LogFactory;
      
      public class CustomJDBCCacheLoader extends org.jboss.cache.loader.AsyncCacheLoader
      {
       private static final Log log = LogFactory.getLog(CustomJDBCCacheLoader.class);
      
       public CustomJDBCCacheLoader()
       {
       super(new org.jboss.cache.loader.JDBCCacheLoader());
       log.info("ASYNC JDBC LOADER Constructor");
       }
      
       @Override
       public void start() throws Exception
       {
       log.info("-----------------Async cache loader starting: " + this);
       System.out.println("-----------------Async cache loader starting: " + this);
      
       super.start();
       }
      }