This forum is now a read-only archive. All commenting, posting, registration services have been turned off. Those needing community support and/or wanting to ask questions should refer to the Tag/Forum map, and to http://spring.io/questions for a curated list of stackoverflow tags that Pivotal engineers, and the community, monitor.

How can CacheServer run via Spring Gemfire API?

Sep 26th, 2011, 09:24 AM

I can run cacheserver via batch command. It works. Now I want to integrate spring into gemfire and launch a cacheserver using spring gemfire API. It can be successfully launched and after that it stops soon. So can anyone can demonstrate any example about launching the cacheserver via spring gemfire API?

Because all beans defined in an application context (including those by Spring GemFire) need to obey its lifecycle. Basically you have defined a cache server, started the context retrieved the CacheServer (which is already started once the context is fully initialized), started it again and then end the application. The JVM exists, calling your shutdown hook, which shuts down the context which shuts down the factory bean backing the cache server.
If you want a long lived process, simply create the appropriate thread, bind the context to it and you're set.
The whole point of the Spring integration and gemfire is to use the Spring facilities - hence in this case the CacheServer being managed by the context. If that's not what you want, you can use the vanilla GemFire API or configure the FactoryBean yourself.

If you're using Spring inside a container (aka Tomcat or WebSphere) then you can simply deploy it as an WAR and Spring's ContextLoaderListener will automatically bind itself to the lifecycle of the app. If not then you need to define and manage the lifecycle of the app yourself. Note that you would need to do that with or without Spring - a.i. if you do CacheServer.start() at some point you need to also call the stop() method.