While enabling JCache, JCacheMetrics is throwing NullPointerException in getCacheManager with spring boot

classic Classic list List threaded Threaded
13 messages Options
daya airody daya airody
Reply | Threaded
Open this post in threaded view
|

While enabling JCache, JCacheMetrics is throwing NullPointerException in getCacheManager with spring boot

I am using ignite within a spring application using JCache API.  Application
is throwing NPE when starting up.  

--  exception ----
Caused by: java.lang.NullPointerException: null
        at
io.micrometer.core.instrument.binder.cache.JCacheMetrics.<init>(JCacheMetrics.java:80)

Spring is loading ignite correctly as you can see from below log:
-------------------

[15:08:08] Ignite node started OK (id=66e75cae)
[15:08:08] Topology snapshot [ver=9, servers=2, clients=1, CPUs=4,
offheap=6.4GB, heap=2.8GB]
[15:08:08]   ^-- Node [id=66E75CAE-FB54-401F-9330-CEBDA3A7EB84,
clusterState=ACTIVE]

-------------
Adding an entry into the cache as part of a service bean's postconstruct
method is working. So the CacheManager is correctly autowired. I am able to
successfully add an entry into a cache.
-------------- Service bean ---
        @Autowired
        private CacheManager cacheManager;
       
        @PostConstruct
        public void setup() {
   
    Cache cache = cacheManager.getCache("users");  
    cache.put("user","daya");
    logger.info("****** added user to cache ***********");

        }

----  application.properties -------------
spring.cache.jcache.config=classpath:example-cache.xml

---- example-cache.xml -------------------
    <bean id="ignite.cfg"
class="org.apache.ignite.configuration.IgniteConfiguration">
        <property name="clientMode" value="true"/>
        <property name="cacheConfiguration">
            <list>
               
                <bean
class="org.apache.ignite.configuration.CacheConfiguration">
                    <property name="name" value="default"/>
                    <property name="atomicityMode" value="ATOMIC"/>
                    <property name="backups" value="1"/>
                </bean>
            </list>
        </property>

       
        <property name="discoverySpi">
            <bean
class="org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi">
                <property name="ipFinder">

                    <bean
class="org.apache.ignite.spi.discovery.tcp.ipfinder.multicast.TcpDiscoveryMulticastIpFinder">
                        <property name="addresses">
                            <list>
                               
                                <value>127.0.0.1:47500..47509</value>
                            </list>
                        </property>
                    </bean>
                </property>
            </bean>
        </property>
    </bean>

---  JCacheMetrics code where NPE is getting thrown --------------
   public JCacheMetrics(Cache<?, ?> cache, Iterable<Tag> tags) {
        super(cache, cache.getName(), tags);
        try {
            String cacheManagerUri =
cache.getCacheManager().getURI().toString()
                    .replace(':', '.'); // ehcache's uri is prefixed with
'urn:'

            this.objectName = new
ObjectName("javax.cache:type=CacheStatistics"
                    + ",CacheManager=" + cacheManagerUri
                    + ",Cache=" + cache.getName());
        } catch (MalformedObjectNameException ignored) {
            throw new InvalidConfigurationException("Cache name '" +
cache.getName() + "' results in an invalid JMX name");
        }
    }
---
 cache.getCacheManager() is returning null.

Please help as we are not able to use ignite. JCache integration is failing
for us because of this.
This is strange as I was able to use the autowired cacheManager to retrieve
a Cache and add an entry.





--
Sent from: http://apache-ignite-users.70518.x6.nabble.com/
daya airody daya airody
Reply | Threaded
Open this post in threaded view
|

Re: While enabling JCache, JCacheMetrics is throwing NullPointerException in getCacheManager with spring boot

I saw that if cache is created using ignite API, getCacheManager() would be
null. So, I had added below line to spring's application.properties

spring.cache.cache-names=users,cannedReports

I am assuming it should be created using JCache API. However,
getCacheManager() is still null. How should a cache be created so that
JCacheMetrics does not throw NPE?




--
Sent from: http://apache-ignite-users.70518.x6.nabble.com/
daya airody daya airody
Reply | Threaded
Open this post in threaded view
|

Re: While enabling JCache, JCacheMetrics is throwing NullPointerException in getCacheManager with spring boot

I had to remove all the caches from ignite config file:

        <property name="cacheConfiguration">
            <list>
 
            </list>
        </property>

This ensures that none of the caches loaded by JCache are created using
ignite API.
If I programmatically create cache and then add items to those caches, I
don't get JCacheMetrics error.
       cacheManager.createCache( "myCache", new MutableConfiguration<>());

But, this means I can't configure these caches using Ignite's cache settings
from the ignite config file.
I am missing something here. Could you please help?

Thanks.




--
Sent from: http://apache-ignite-users.70518.x6.nabble.com/
daya airody daya airody
Reply | Threaded
Open this post in threaded view
|

Re: While enabling JCache, JCacheMetrics is throwing NullPointerException in getCacheManager with spring boot

There is a problem with using
spring.cache.cache-names=users,cannedReports

When the SpringBoot application is restarted, it will throw below error:


Caused by: org.apache.ignite.cache.CacheExistsException: Failed to start
cache (a cache with the same name is already started): users
        at
org.apache.ignite.internal.processors.cache.GridCacheProcessor.prepareCacheChangeRequest(GridCacheProcessor.java:4358)
        at
org.apache.ignite.internal.processors.cache.GridCacheProcessor.dynamicStartCache(GridCacheProcessor.java:2945)
        at
org.apache.ignite.internal.processors.cache.GridCacheProcessor.dynamicStartCache(GridCacheProcessor.java:2884)
        at
org.apache.ignite.internal.IgniteKernal.createCache(IgniteKernal.java:2724)



--
Sent from: http://apache-ignite-users.70518.x6.nabble.com/
slava.koptilin slava.koptilin
Reply | Threaded
Open this post in threaded view
|

Re: While enabling JCache, JCacheMetrics is throwing NullPointerException in getCacheManager with spring boot

Hello,

>  org.apache.ignite.cache.CacheExistsException: Failed to start cache (a cache with the same name is already started): users
This exception means that the cache is already created in the cluster.
Please consider using Ignite#getOrCreateCache() method instead of Ignite#createCache() [1]
This method allows to get an existing cache with the given name or creates a new one.

By the way, please take a look at this page: https://apacheignite-mix.readme.io/docs/spring-caching
this page provides information about Spring Caching.

perhaps, the following GitHub project will be useful as well https://github.com/Romeh/spring-boot-ignite

Thanks!
daya airody daya airody
Reply | Threaded
Open this post in threaded view
|

Re: While enabling JCache, JCacheMetrics is throwing NullPointerException in getCacheManager with spring boot

Hi slava,

thanks for your comments. I am creating the cache directly using JCache API
below:
-------------------
    @Bean
    public JCacheManagerCustomizer cacheManagerCustomizer() {
        return cm -> {
            Configuration<Object, Object> cacheConfiguration =
createCacheConfiguration();
           
            if (cm.getCache("users") == null)
                cm.createCache("users", cacheConfiguration);
            if (cm.getCache("cannedReports") == null)
                cm.createCache("cannedReports",
createCustomCacheConfiguration());
        };
    }
----------------------
Even though I have created these caches using JCache API ( and not through
Ignite API), when I restart my application, cache.getCacheManager() is
returning null within JCacheMetrics constructor. This is a blocker.

Any help is appreciated.



--
Sent from: http://apache-ignite-users.70518.x6.nabble.com/
slava.koptilin slava.koptilin
Reply | Threaded
Open this post in threaded view
|

Re: While enabling JCache, JCacheMetrics is throwing NullPointerException in getCacheManager with spring boot

Hi,

You can create a cache via CacheManager instance using Ignite CacheConfiguration or MutableConfiguration:

public class ExampleNodeStartup {
    public static void main(String[] args) throws Exception {
        CachingProvider provider = Caching.getCachingProvider();
        CacheManager manager = provider.getCacheManager();

        Cache c1 = manager.createCache("my-test-cache-1", new MutableConfiguration<>());

        CacheConfiguration cfg = new CacheConfiguration<>("my-test-cache-2");
        cfg.setCacheMode(CacheMode.REPLICATED);
        cfg.setBackups(2);

        Cache c2 = manager.createCache(cfg.getName(), cfg);
    }
}

Moreover, you can provide your own Ignite configuration as well:

public class ExampleNodeStartup {
    public static void main(String[] args) throws Exception {
        URI igniteCfg = "//projects//ignite//examples//config//example-cache.xml";

        CachingProvider provider = Caching.getCachingProvider();
        CacheManager manager = provider.getCacheManager(igniteCfg, ExampleNodeStartup.class.getClassLoader());

        Cache default = manager.getCache("default");
    }
}

All these examples work as expected, without any errors/exceptions.

So, I owuld suggest creating a small project, that reproduces the issue you mentioned, and upload it on GitHub
so that the community can take a look at this.

Thanks.

вт, 7 авг. 2018 г. в 20:45, daya airody <[hidden email]>:
Hi slava,

thanks for your comments. I am creating the cache directly using JCache API
below:
-------------------
    @Bean
    public JCacheManagerCustomizer cacheManagerCustomizer() {
        return cm -> {
            Configuration<Object, Object> cacheConfiguration =
createCacheConfiguration();

            if (cm.getCache("users") == null)
                cm.createCache("users", cacheConfiguration);
            if (cm.getCache("cannedReports") == null)
                cm.createCache("cannedReports",
createCustomCacheConfiguration());
        };
    }
----------------------
Even though I have created these caches using JCache API ( and not through
Ignite API), when I restart my application, cache.getCacheManager() is
returning null within JCacheMetrics constructor. This is a blocker.

Any help is appreciated.



--
Sent from: http://apache-ignite-users.70518.x6.nabble.com/
daya airody daya airody
Reply | Threaded
Open this post in threaded view
|

Re: While enabling JCache, JCacheMetrics is throwing NullPointerException in getCacheManager with spring boot

Hi slava,

I have uploaded my code at below link:
https://github.com/daya-airody/ignite-caching

You need uncomment below lines in application.properties before running
startup.sh.

spring.cache.jcache.config=classpath:example-cache.xml
spring.cache.cache-names=users,cannedReports

When you run startup.sh, it throws below error:

org.apache.ignite.cache.CacheExistsException: Failed to start cache (a cache
with the same name is already started): users

Please review my code and help me debug this issue.
Thanks in advance,
--daya--



--
Sent from: http://apache-ignite-users.70518.x6.nabble.com/
slava.koptilin slava.koptilin
Reply | Threaded
Open this post in threaded view
|

Re: While enabling JCache, JCacheMetrics is throwing NullPointerException in getCacheManager with spring boot


пн, 13 авг. 2018 г. в 15:14, daya airody <[hidden email]>:
Hi slava,

I have uploaded my code at below link:
https://github.com/daya-airody/ignite-caching

You need uncomment below lines in application.properties before running
startup.sh.

spring.cache.jcache.config=classpath:example-cache.xml
spring.cache.cache-names=users,cannedReports

When you run startup.sh, it throws below error:

org.apache.ignite.cache.CacheExistsException: Failed to start cache (a cache
with the same name is already started): users

Please review my code and help me debug this issue.
Thanks in advance,
--daya--



--
Sent from: http://apache-ignite-users.70518.x6.nabble.com/
daya airody daya airody
Reply | Threaded
Open this post in threaded view
|

Re: While enabling JCache, JCacheMetrics is throwing NullPointerException in getCacheManager with spring boot

Hi Slava,

other reply is about issues in caching Spring proxied objects.

I am also seeing "Cache already exists" issue. Could you please throw some
light on this?

Thanks in advance,
--daya--




--
Sent from: http://apache-ignite-users.70518.x6.nabble.com/
slava.koptilin slava.koptilin
Reply | Threaded
Open this post in threaded view
|

Re: While enabling JCache, JCacheMetrics is throwing NullPointerException in getCacheManager with spring boot

  Hello,

As I mentioned above, this exception indicates that the cache with the given name already exists in the cluster. 

Thanks,
S. 

пт, 17 авг. 2018 г., 10:52 daya airody <[hidden email]>:
Hi Slava,

other reply is about issues in caching Spring proxied objects.

I am also seeing "Cache already exists" issue. Could you please throw some
light on this?

Thanks in advance,
--daya--




--
Sent from: http://apache-ignite-users.70518.x6.nabble.com/
daya airody daya airody
Reply | Threaded
Open this post in threaded view
|

Re: While enabling JCache, JCacheMetrics is throwing NullPointerException in getCacheManager with spring boot

I commented below line:

#spring.cache.cache-names=users,cannedReports

I am creating cache explicitly in CacheConfiguration class:

            if (cm.getCache("users") == null)
                cm.createCache("users", cacheConfiguration);
            if (cm.getCache("cannedReports") == null)
                cm.createCache("cannedReports",
createCustomCacheConfiguration());

This ensures that cache is created according to JCache API.

Now can enable Java method cache using @CacheResult @CacheKey annotations.



--
Sent from: http://apache-ignite-users.70518.x6.nabble.com/
daya airody daya airody
Reply | Threaded
Open this post in threaded view
|

Re: While enabling JCache, JCacheMetrics is throwing NullPointerException in getCacheManager with spring boot

In reply to this post by slava.koptilin
I commented below line:

#spring.cache.cache-names=users,cannedReports

I am creating cache explicitly in CacheConfiguration class:

            if (cm.getCache("users") == null)
                cm.createCache("users", cacheConfiguration);
            if (cm.getCache("cannedReports") == null)
                cm.createCache("cannedReports",
createCustomCacheConfiguration());

This ensures that cache is created according to JCache API.

Now you can enable Java method cache using @CacheResult @CacheKey
annotations.



--
Sent from: http://apache-ignite-users.70518.x6.nabble.com/