Cache Loading with Spring Best Practice

classic Classic list List threaded Threaded
3 messages Options
pgarg pgarg
Reply | Threaded
Open this post in threaded view
|

Cache Loading with Spring Best Practice

asked by nicolas bihan

Hi,

I'm looking at the best practice on how to load caches when application starts. I'm using Spring, and was thinking to preload caches in org.springframework.web.WebApplicationInitializer but the WebApplicationInitializer is invoked before the Node is started. Not sure how to handle this properly Thanks.

-----
This post is migrated from now discontinued Apache Ignite forum at
http://apacheignite.readme.io/v1.0/discuss
pgarg pgarg
Reply | Threaded
Open this post in threaded view
|

Re: Cache Loading with Spring Best Practice

commented by valentin kulichenko

You can use LifecycleBean with injected Ignite instance for this. It will be invoked right after the node startup.

Here is the implementation example:

public class MyLifecycleBean implements LifecycleBean {
    @IgniteInstanceResource
    private Ignite ignite;

    @Override public void onLifecycleEvent(LifecycleEventType evt) throws IgniteException {
        if (evt == LifecycleEventType.AFTER_NODE_START) {
            IgniteCache<Object, Object> cache = ignite.cache("myCache");

            // Load cache logic goes here...
        }
    }
}


Lifecycle bean should be configured like this:

<bean class="org.apache.ignite.IgniteConfiguration">
    ...
    <property name="lifecycleBeans">
        <list>
            <bean class="com.mycompany.MyLifecycleBean"/>
        </list>
    </property>
    ...
</bean>


-----
This post is migrated from now discontinued Apache Ignite forum at
http://apacheignite.readme.io/v1.0/discuss
pgarg pgarg
Reply | Threaded
Open this post in threaded view
|

Re: Cache Loading with Spring Best Practice

commented by nicolas bihan

Fantastic! Didn't think about using LifecycleBean... Thanks Valentin!

-----
This post is migrated from now discontinued Apache Ignite forum at
http://apacheignite.readme.io/v1.0/discuss