0

I have CacheLoader and CacheWriter written using spring data JPA and data-source as spring bean to connect to database and fetch data...now I need to plug in these loader and writer at server cache...How Can I do that effectively.

I think I need to use below element in the cache.xml and refer this xml file from gfsh command shell :

<initializer>
    <class-name>org.springframework.data.gemfire.support.SpringContextBootstrappingInitializer</class-name>
    <parameter name="contextConfigLocations">
    <string>application-context.xml</string>
    </parameter>
</initializer>

In this context I have the below questions:

1.The above approach of bootstrapping spring application context from gemfire is advised for production ?

2.what are other alternatives to achieve this ?

3.I have done a work around of creating a client cache in my client and have plugged in these cache loader and cache writer and they are getting executed since my client is a spring application but is this approach advised for production?

Arghya Sadhu
  • 41,002
  • 9
  • 78
  • 107

1 Answers1

0

Regarding 1...

This approach is definitely valid and enables users/admins who require GemFire Servers that are primarily configured using Spring Data GemFire's XML namespace to be launched with Gfsh. Currently, Gfsh does not accept a Spring XML context configuration file for bootstrapping a GemFire Server. So, in order to deploy Spring applications using GemFire as peer Caches that are required to be started with Gfsh, you need minimal snippet of cache.xml, hence this approach.

Regarding 2..

None using Gfsh. Of course, if you are not bound to use Gfsh to launch GemFire Server's in production, then a simple Java class with a main method bootstrapping a Spring application context that also configure a GemFire peer node or client Cache would suffice...

public class GemFireServer {
  public static void main(String[] args) {
    ConfigurableApplicationContext applicationContext = new ClassPathApplicationContext("/path/to/spring/data/gemfire/application/context/config.xml");
    applicationContext.registerShutdownHook();
  }
}

However, I will point out that in GemFire 8.0, the ability to launch and bootstrap a GemFire Server using Spring config is planned for the release, tentatively late July. I.e. you will be able to...

gfsh>start server --name=MyServer --log-level=config --spring-xml-location=/path/to/spring/config.xml

Regarding 3...

Typically, CacheLoaders and CacheWriters are used on the Server to load and populate a value on a cache miss and to "write-through" to a backend, external persistent store. Although, you can specify CacheLoaders and CacheWriters on the client, this is atypical and only necessary when the data source is not accessible to the GemFire cluster, perhaps because the data source is specific to the client.

John Blum
  • 7,381
  • 1
  • 20
  • 30
  • Many thanks for your response...I have a scenario like..I need to plug in cache-loader and cache-writer in my server cache...I start my server cache using standard gfsh command shell using a cache.xml file...now since my cache-loaders and cache-writers are using spring data JPA to connect to backend RDBMS...It's not going to work right?So I suppose I need to fall back to old JDBC instead of spring data – Arghya Sadhu Jul 07 '14 at 04:13
  • It will work. You just need to include the required JARs (SDC, SD-JPA, , etc) when starting the GemFire Server in Gfsh using the 'start server' command, --classpath option. Then you should able to inject any necessary JPA components into the CacheLoaders/Writers. I would encourage you to define all GemFire components (CacheLoaders/Writers) in Spring config, however, this will still work if you define GemFire components in GemFire's native cache.xml to, but your Loader/Writers must then extend LazyWiringDeclarableSupport from SDG to be wired properly. – John Blum Jul 11 '14 at 19:25
  • Technically, you only need a very minimal amount of GemFire cache.xml to bootstrap a Spring context in GemFire when launching a Server from Gfsh, for instance.... – John Blum Jul 11 '14 at 19:27
  • org.springframework.data.gemfire.support.SpringContextBootstrappingInitializer classpath:org/spring/data/gemfire/app/initializer-gemfire-context.xml – John Blum Jul 11 '14 at 19:27
  • I would keep the rest of the GemFire components (Regions, Disk Stores, Gateways, CacheLoaders/Writers, listeners, etc) in addition to application components (obviously) in Spring config. I.e. Just use the GemFire cache.xml to bootstrap the Spring context (getting things started) and then the rest of the configuration leverages Spring's expressive and powerful DI framework. See the following ref doc for more details... http://docs.spring.io/spring-data-gemfire/docs/1.4.0.RELEASE/reference/html/gemfire-bootstrap.html – John Blum Jul 11 '14 at 19:29
  • I have tried cache-config.xml: org.springframework.data.gemfire.support.SpringContextBootstrappingInitializer application-context.xml – Arghya Sadhu Jul 14 '14 at 09:21
  • and application-context.xml: warning – Arghya Sadhu Jul 14 '14 at 09:24
  • and start server --name=server1 --cache-xml-file=C\cache-config.xml --classpath="lib\spring-data-gemfire-1.4.0.RELEASE.jar" – Arghya Sadhu Jul 14 '14 at 09:32
  • I get below exception:Unexpected exception parsing XML document from class path resource [application-context.xml]; nested exception is java.lang.NoSuchMethodError: org.springframework.util.StringUtils.isEmpty(Ljava/lang/Object;)Z – Arghya Sadhu Jul 14 '14 at 09:33
  • You need to use version 3.2.1 of the core Spring Framework or later, as required by SDG 1.4.0.RELEASE. The core Spring Framework StringUtils.isEmpty(..) method was added in version 3.2.1. – John Blum Jul 21 '14 at 20:30