You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@ignite.apache.org by mshah <ms...@gmail.com> on 2018/09/12 21:10:32 UTC

how to configure apache ignite as cache api and as spring cache provider

I want to use ignite as cache api as well as spring cache provider. I am
running following configuration in ignite-config.xml file as below

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:cache="http://www.springframework.org/schema/cache"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
                       
http://www.springframework.org/schema/beans/spring-beans.xsd
                        http://www.springframework.org/schema/cache
                       
http://www.springframework.org/schema/cache/spring-cache.xsd"
                        >




<bean class="org.apache.ignite.configuration.IgniteConfiguration">
  <property name="peerClassLoadingEnabled" value="true"/>
  <property name="igniteInstanceName" value="claimgrid"/>
<property name="cacheConfiguration">
                <list>
                    <bean
class="org.apache.ignite.configuration.CacheConfiguration">

                        <property name="name"
value="T_MST_TEUC_Q_PERCENTAGE"/>
                        <property name="cacheMode" value="REPLICATED"/>
                        <property name="atomicityMode" value="ATOMIC"/>
                        <property name="partitionLossPolicy"
value="READ_WRITE_SAFE"/>
                        <property name="backups" value="0"/>
                        <property name="groupName" value="masterData"/>
                        <property name="cacheStoreFactory">
                            <bean
class="javax.cache.configuration.FactoryBuilder" factory-method="factoryOf">
                                <constructor-arg
value="com.mrm.access.benefits.ms.claim.cache.store.SequesterPercentageModelStore"/>
                            </bean>

                        </property>

                        <property name="readThrough" value="true"/>
                        <property name="writeThrough" value="true"/>

                    </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.vm.TcpDiscoveryVmIpFinder">
                 <property name="addresses">
                        <list>  
                              <value>10.80.211.76</value>
                        </list>
                    </property>
                    </bean>
            </property>
        </bean>
    </property>
</bean>


I want to use ignite as cache api as well as spring cache provider. I am
running following configuration in ignite-config.xml file as below

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:cache="http://www.springframework.org/schema/cache"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
                       
http://www.springframework.org/schema/beans/spring-beans.xsd
                        http://www.springframework.org/schema/cache
                       
http://www.springframework.org/schema/cache/spring-cache.xsd"
                        >




<bean class="org.apache.ignite.configuration.IgniteConfiguration">
  <property name="peerClassLoadingEnabled" value="true"/>
  <property name="igniteInstanceName" value="claimgrid"/>
<property name="cacheConfiguration">
                <list>
                    <bean
class="org.apache.ignite.configuration.CacheConfiguration">

                        <property name="name"
value="T_MST_TEUC_Q_PERCENTAGE"/>
                        <property name="cacheMode" value="REPLICATED"/>
                        <property name="atomicityMode" value="ATOMIC"/>
                        <property name="partitionLossPolicy"
value="READ_WRITE_SAFE"/>
                        <property name="backups" value="0"/>
                        <property name="groupName" value="masterData"/>
                        <property name="cacheStoreFactory">
                            <bean
class="javax.cache.configuration.FactoryBuilder" factory-method="factoryOf">
                                <constructor-arg
value="com.mrm.access.benefits.ms.claim.cache.store.SequesterPercentageModelStore"/>
                            </bean>

                        </property>

                        <property name="readThrough" value="true"/>
                        <property name="writeThrough" value="true"/>

                    </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.vm.TcpDiscoveryVmIpFinder">
                 <property name="addresses">
                        <list>  
                              <value>10.80.211.76</value>
                        </list>
                    </property>
                    </bean>
            </property>
        </bean>
    </property>
</bean>
I am initializing the ignite node as below.

ApplicationContext context= SpringApplication.run(Application.class, args);
Ignite ignite=IgniteSpring.start("ignite-config.xml", context);

The node gets up and running. Now I am using second configuration file for
spring cache manager spring-cache.xml


<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:cache="http://www.springframework.org/schema/cache"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           
http://www.springframework.org/schema/beans/spring-beans.xsd
                            http://www.springframework.org/schema/cache
         					http://www.springframework.org/schema/cache/spring-cache.xsd"
                            >
<bean id="cacheManager"
class="org.apache.ignite.cache.spring.SpringCacheManager">
     	
 
       		<property name="igniteInstanceName" value="claimgrid" />

 
    </bean>
    <cache:annotation-driven/>
</beans>


and I am initializing this file as below

Ignite ignite=Ignition.start("spring-cache.xml");

I am getting the below exception

Caused by: class org.apache.ignite.IgniteCheckedException: Failed to find
configuration in: file:/E:/Workspace/ms.claim/spring-cache.xml at
org.apache.ignite.internal.util.spring.IgniteSpringHelperImpl.loadConfigurations(IgniteSpringHelperImpl.java:116)
at
org.apache.ignite.internal.util.spring.IgniteSpringHelperImpl.loadConfigurations(IgniteSpringHelperImpl.java:98)
at
org.apache.ignite.internal.IgnitionEx.loadConfigurations(IgnitionEx.java:744)
at org.apache.ignite.internal.IgnitionEx.start(IgnitionEx.java:945) at
org.apache.ignite.internal.IgnitionEx.start(IgnitionEx.java:854) at
org.apache.ignite.internal.IgnitionEx.start(IgnitionEx.java:724) at
org.apache.ignite.internal.IgnitionEx.start(IgnitionEx.java:693) at
org.apache.ignite.Ignition.start(Ignition.java:352) ... 2 more

While debugging the ignite source code I found that ignite internally uses
SpringHelper to get the Spring Application Context and after that it tries
to fetch IgniteConfiguration instance as spring bean using getBeansOfType()
method in one of the classes and from there it does not get the
configuration instance and throws the above exception.

As mentioned in the Ignite documentation I am using the same gridName or
instanceName property in SpringCacheManager in spring-cache.xml.

Can somebody please check the issue? Also I am not sure I am following the
correct method. First I am staring the ignite node with ignite-config.xml
and IgniteSpring.start and once a node is up I am initializing
spring-cache.xml with Ignition.start to configure Ignite as
SpringCacheManager.



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

Re: how to configure apache ignite as cache api and as spring cache provider

Posted by vkulichenko <va...@gmail.com>.
Monil,

You can also use IgniteSpringBean to initialize Ignite separately from
SpringCacheManager, but within Spring context.

So you need to configure following beans:
1. PercentageDataRespository
2. IgniteConfiguration
3. IgniteSpringBean (IgniteConfiguration is injected via 'configuration'
property, PercentageDataRespository is injected into store via
@SpringResource)
4. SpringCacheManager (references IgniteSpringBean by igniteInstanceName)

With this configuration everything is initialized by Spring, so no need to
use Ignition.start().

Note that for the SpringCacheManager you will have to explicitly specify
that it depends on IgniteSpringBean (I believe Spring supports that).
Ideally, you would reference IgniteSpringBean directly instead of relying on
instance name, but unfortunately it's not possible right now. There is a
ticket to improve this: https://issues.apache.org/jira/browse/IGNITE-8880

-Val



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

Re: how to configure apache ignite as cache api and as spring cache provider

Posted by mshah <ms...@gmail.com>.
Hi,

So the issue here is in order to use Ignite as key-value Cache API along
with Spring Repository we have to let the spring application context
initialize fully and then need to start ignite instance passing the
application context.

In order to use Ignite as Spring Cache provider the SpringCacheManager
starts its own ignite instance which would be different than the one started
earlier for key-value caching api.

Is there any way where we can pass the already running ignite instance (one
for caching api) to the Spring Cache Manager and it does not try to create
that instance again?

I see the onApplicationEvent method in SpringCacheManager where its starting
Ignite node with Spring Application Context.(*IgniteSpring.start(cfgPath,
springCtx)*) Please check onApplicationEvent(ContextRefereshedEvent event)
in SpringCacheManager. In this method from where it gets ApplicationContext
instance? Is there any way we can externally pass the application context.?

The last option would be to start a new ignite instance from
SpringCacheManager. This might be silly question but would there be a
different node running (along with one running for cache api) or a different
grid within the same node?

Thank you in advance.

Regards
Monil



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

Re: how to configure apache ignite as cache api and as spring cache provider

Posted by KJQ <kj...@logicdrop.com>.
I dont know if this helps or not but we ran into a similar issue testing the
distributed cache with Shiro authentication.  Shiro was eagerly trying to
create the cache but the Spring Ignite instance (inside the
SpringCacheManager) had not yet been initialized (I believe it initializes
after all the beans are configured).

In our case, we wrapped the "cache" with a Provider<> so that we could
lazily access it when needed after everything started up.



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

Re: how to configure apache ignite as cache api and as spring cache provider

Posted by mshah <ms...@gmail.com>.
Hi Val,

Thank you for the response. I got the options you mentioned. I have still
one more clarification required. 

I have configured Custom Cache Store which internally uses Spring
Repository.  Please check below code.

        public class PercentageModelStore extends CacheStoreAdapter<String,
MstTeucPercentageModel>
{
	@CacheStoreSessionResource
    public CacheStoreSession ses;
	
	@SpringResource(resourceClass=PercentageDataRespository.class)
    public IPercentageDataRespository repository;
	
	@Override public void loadCache(IgniteBiInClosure<String,PercentageModel>
clo, Object... args) 
    {
    		System.out.println("Loading cache entries from database starts !!!!");
	       List<PercentageData> listTeucPercentageData=repository.findAll();
	       for(MstTeucPercentageData data:listTeucPercentageData)
	       {
	    	   clo.apply(data.PercentageId().toString(),
(PercentageModel)this.getModel(data));    	   
	       }
	       System.out.println("Loading cache entries from database ends !!!!");
	       return;
    }
}

I am using SpringResource(PercentageDataRespository.class) to inject the
Spring Repository Component  into my CacheStore. Basically I dont want to go
through JDBC and want to use hibernate for loading data into cache store.

If I go with the first approach you mentioned and start with Ignition.start
I believe this repository would not be injected. This is the main reason I
am using IgniteSpring.start passing the Spring context so that for all my
cache stores I can have injected repositories. This is working fine as of
now. On top of it I want to use Ignite as Spring Cache Provider.

Kindly correct me if I am not getting the concepts.

Regards
Monil



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

Re: how to configure apache ignite as cache api and as spring cache provider

Posted by vkulichenko <va...@gmail.com>.
You need to make sure Ignite is started *before* SpringCacheManager.
Basically, you have two options:

1. Start Ignite manually using Ignition.start, and then start Spring app
with SpringCacheManager that references already started Ignite via
'igniteInstanceName' property.
2. Incorporate IgniteConfiguration into Spring config as a separate bean,
and reference it in SpringCacheManager via 'configuration' property. In this
case SpringCacheManager will take care of starting Ignite, so you don't need
to use Ignition.start().

If Ignite is used ONLY for Spring caching, option 2 might be more
convenient. Otherwise, option 1 makes more sense.

Hope this helps.

-Val



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

Re: how to configure apache ignite as cache api and as spring cache provider

Posted by mshah <ms...@gmail.com>.
Hi,

Thank you for the response. 

I tried the following configuration as you mentioned

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:cache="http://www.springframework.org/schema/cache"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           
http://www.springframework.org/schema/beans/spring-beans.xsd
                            http://www.springframework.org/schema/cache
         					http://www.springframework.org/schema/cache/spring-cache.xsd"
                            >
                            
    
      
     
    <bean class="org.apache.ignite.configuration.IgniteConfiguration">
      <property name="peerClassLoadingEnabled" value="true"/>
      <property name="igniteInstanceName" value="claimgrid"/>
    <property name="cacheConfiguration">
                    <list>
                        <bean
class="org.apache.ignite.configuration.CacheConfiguration">

                            <property name="name"
value="T_MST_TEUC_Q_PERCENTAGE"/>
                            <property name="cacheMode" value="REPLICATED"/>
                            <property name="atomicityMode" value="ATOMIC"/>
                            <property name="partitionLossPolicy"
value="READ_WRITE_SAFE"/>
                            <property name="backups" value="0"/>
                            <property name="groupName" value="masterData"/>
                            <property name="cacheStoreFactory">
                                <bean
class="javax.cache.configuration.FactoryBuilder" factory-method="factoryOf">
                                    <constructor-arg
value="com.mrm.access.benefits.ms.claim.cache.store.SequesterPercentageModelStore"/>
                                </bean>

                            </property>

                            <property name="readThrough" value="true"/>
                            <property name="writeThrough" value="true"/>
                             
                        </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.vm.TcpDiscoveryVmIpFinder">
                     <property name="addresses">
                            <list>  
                                  <value>10.80.211.76</value>
                            </list>
                        </property>
                        </bean>
                </property>
            </bean>
        </property>
    </bean>
    
    
    <bean id="cacheManager"
class="org.apache.ignite.cache.spring.SpringCacheManager">
       	<property name="igniteInstanceName" value="claimgrid" />

    </bean>
    <cache:annotation-driven/>
       
</beans>


Below is the code to start the  spring application and ignite instance


public static void main(String[] args) throws Exception
	{
		ApplicationContext context= SpringApplication.run(Application.class,
args);
		initializeIgnite(context);
		test_cache();
	}

	private static void initializeIgnite(ApplicationContext context) throws
IgniteCheckedException
	{
		logger.debug("Initializing Ignite");
		Ignite ignite=IgniteSpring.start("ignite-config.xml", context);
		logger.debug("Ignite Initialized Successfully");
	}

I am getting the following exception

Exception in thread "main" class
org.apache.ignite.IgniteIllegalStateException: Ignite instance with provided
name doesn't exist. Did you call Ignition.start(..) to start an Ignite
instance? [name=claimgrid]
	at org.apache.ignite.internal.IgnitionEx.grid(IgnitionEx.java:1383)
	at org.apache.ignite.Ignition.ignite(Ignition.java:535)
	at
org.apache.ignite.cache.spring.SpringCacheManager.onApplicationEvent(SpringCacheManager.java:334)
	at
org.apache.ignite.cache.spring.SpringCacheManager.onApplicationEvent(SpringCacheManager.java:146)
	at
org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172)
	at
org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165)
	at
org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139)
	at
org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:400)
	at
org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:354)
	at
org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:888)
	at
org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:553)
	at
org.apache.ignite.internal.util.spring.IgniteSpringHelperImpl.applicationContext(IgniteSpringHelperImpl.java:381)
	at
org.apache.ignite.internal.util.spring.IgniteSpringHelperImpl.loadConfigurations(IgniteSpringHelperImpl.java:104)
	at
org.apache.ignite.internal.util.spring.IgniteSpringHelperImpl.loadConfigurations(IgniteSpringHelperImpl.java:98)
	at
org.apache.ignite.internal.IgnitionEx.loadConfigurations(IgnitionEx.java:744)
	at org.apache.ignite.internal.IgnitionEx.start(IgnitionEx.java:945)
	at org.apache.ignite.internal.IgnitionEx.start(IgnitionEx.java:854)
	at org.apache.ignite.IgniteSpring.start(IgniteSpring.java:90)
	at
com.mrm.access.benefits.ms.claim.Application.initializeIgnite(Application.java:47)
	at com.mrm.access.benefits.ms.claim.Application.main(Application.java:40)





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

Re: how to configure apache ignite as cache api and as spring cache provider

Posted by vkulichenko <va...@gmail.com>.
Ignition.start is supposed to start an Ignite instance, so passing
spring-cache.xml file that doesn't contain any Ignite configuration doesn't
make sense. The SpringCacheManager bean should be part of the Spring
Application Context, it then will be used as an entry point to Ignite
cluster. It looks like you're using annotation based config for Spring, so
that's where you need to configure the bean. I believe you don't need this
second XML at all.

-Val



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