6 Replies Latest reply on May 21, 2012 6:45 AM by dcarniel

    Add a CacheLoader programmaticaly with the ConfigurationBuilder (5.1.0.CR2)

    David Regnier Newbie

      I am trying to update the configuration of the Cache from the deprecated org.infinispan.config.Configuration to org.infinispan.configuration.cache.Configuration:

       

      • deprecated example:

       

      final Configuration configuration =

                                    new Configuration()

                                              .fluent()

                                              .loaders()

        .addCacheLoader(

                 new FileCacheStoreConfig().location("location")

        )

        .build();

       

      • with the new API:

       

      final CacheLoader cacheLoader = new FileCacheStore();

       

      final Configuration configuration =

                                    new ConfigurationBuilder()

        .loaders()

        .addCacheLoader()

        .cacheLoader(cacheLoader).build();

       

      Issue with that

       

      .cacheLoader(CacheLoader cacheLoader) is expecting  a CacheLoader.

       

      It seems we cannot anymore configure a CacheLoader with a CacheLoaderConfig

       

      Does anyone know how to configure a CacheLoader with a CacheLoaderConfig? (using the new org.infinispan.configuration.cache.Configuration)

        • 1. Re: Add a CacheLoader programmaticaly with the ConfigurationBuilder (5.1.0.CR2)
          Galder Zamarreño Master

          In the case of the file cache store, you can do:

           

          Configuration builder = new ConfigurationBuilder()
          FileCacheStoreConfigurationBuilder fcscb = builder.loaders().addFileCacheStore();
          

           

          And use the file cache store config builder to set the config options you want.

           

          With any other cache store, addCacheLoader() returns a LoaderConfigurationBuilder which you use to pass in the configuration options as before.

          • 2. Re: Add a CacheLoader programmaticaly with the ConfigurationBuilder (5.1.0.CR2)
            dcarniel Newbie

            Hi,

             

            I'm facing a similar question, but my cache loader is not related with the FileCacheLoader. My problem is that I would like to instantiate a CacheLoader which requires some initialization parameters.

             

            I thought I could instantiate my CacheLoader myself and pass it to the configuration builder doing something like this:

             

            MyCacheLoader cacheLoader = new MyCacheLoader( ... );

            Configuration configuration = new ConfigurationBuilder()

              .loaders()

              .addCacheLoader()

              .cacheLoader(cacheLoader).build();

             

            Unfortunately it seems that infinispan actually tries to instantiates itself the CacheLoader even though an instance is provided (which in my case fails as the required constructor isn't there).

             

            I could pass necessary informations via a CacheLoaderConfig subclass, but I fail to see how I can instantiate a CacheLoader based on this with the new API.

             

            Could someone shed some light on this ? Maybe there's a trick using the "addFileCacheStore()" but I don't see which one.

             

            Thanks in advance.

            • 3. Re: Add a CacheLoader programmaticaly with the ConfigurationBuilder (5.1.0.CR2)
              Galder Zamarreño Master

              For any other cache loader, until we sort out the new config for it, you have to do something like this:

               

              Properties loaderProps = new Properties();
              // For each property to configure, set it here - just like you do with XML
              loaderProps.setProperty("remoteCallTimeout", replTimeout.toString);
              builder.loaders().addCacheLoader().cacheLoader(new ClusterCacheLoader())
                  .withProperties(loaderProps);
              

               

              The cache loader instance passed is only used to determine the type of cache loader. Infinispan internals will generate a new instance out of it. So, no point in setting anything there. This is already explained in the cacheLoader() method javadoc.

              • 4. Re: Add a CacheLoader programmaticaly with the ConfigurationBuilder (5.1.0.CR2)
                dcarniel Newbie

                Hi,

                 

                Thanks I actually came across the functions that allow setting properties, which I though would be good enought for my use case. Trying it though caused and issue, it complains that setters aren't found on the AbstractCacheLoader class whereas I'm trying to assign value to properties of my own class.

                 

                I get the following exception:

                 

                Caused by: org.infinispan.config.ConfigurationException: Couldn't find a setter named [setWrappedClassName] which takes a single parameter, for parameter wrappedClassName on class [class org.infinispan.loaders.AbstractCacheStoreConfig]

                  at org.infinispan.config.parsing.XmlConfigHelper.setValues(XmlConfigHelper.java:470)

                  at org.infinispan.configuration.cache.LegacyConfigurationAdaptor.adapt(LegacyConfigurationAdaptor.java:217)

                  at org.infinispan.manager.DefaultCacheManager.defineConfiguration(DefaultCacheManager.java:461)

                 

                Looking at the code in LegacyConfigurationAdaptor, it seems that when entering the "setValues" method of "XmlConfigHelper", the target object is seen as an AbstractCacheLoaderConfig, which is surprising since the CacheLoader I pass in configuration overrides the "getConfigurationClass" to point to my own configuration file (public inner class of MyCacheLoader).

                 

                Not sure why I see that AbstractCacheLoaderConfig instead of my own class.

                • 5. Re: Add a CacheLoader programmaticaly with the ConfigurationBuilder (5.1.0.CR2)
                  Galder Zamarreño Master

                  Can you attach a test case (zipped) so that we can have a look?

                  • 6. Re: Add a CacheLoader programmaticaly with the ConfigurationBuilder (5.1.0.CR2)
                    dcarniel Newbie

                    As the topic actually derived from the original question and I couldn't find a way to attach a file in my reply, I created a new thread with the example:

                     

                    https://community.jboss.org/thread/199957