Announcement Announcement Module
Collapse
No announcement yet.
How can CacheServer run via Spring Gemfire API? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How can CacheServer run via Spring Gemfire API?

    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?

    Code:
    private CacheServer server;
    private ConfigurableApplicationContext context;
    
    context = new ClassPathXmlApplicationContext("classpath:/conf/spring/serverContext.xml");
    context.registerShutdownHook();
    if (server == null)
       server = (CacheServer)context.getBean("BookPocCacheServer");
    		
    server.start();

    serverContext.xml
    Code:
    <gfe:cache id="BookPocServerCache" properties-ref="serverCacheProbs" cache-xml-location="classpath:/conf/spring/serverCache.xml" />
    <util:properties id="serverCacheProbs" location="classpath:/conf/spring/serverCache.properties" />
    <gfe:cache-server id="BookPocCacheServer" bind-address="192.168.11.2" auto-startup="true" cache-ref="BookPocServerCache"/>
    serverCache.properties
    Code:
    bind-address=192.168.11.2
    mcast-port=0
    start-locator=192.168.11.2[12345]
    log-level=info

    And the output log is:

    INFO - Starting beans in phase 2147483647
    INFO - Closing org.springframework.context.support.ClassPathXmlAp plicationContext@a97b0b: startup date [Mon Sep 26 22:13:54 CST 2011]; root of context hierarchy
    INFO - Stopping beans in phase 2147483647

    Why the bean will be stopping after it starts successfully?

  • #2
    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.

    Cheers,

    Comment


    • #3
      Thanks Costin. What I want is exactly a long lived process. So I will try to create a thread to do it. Because my project is based on spring framework, it is easier to intergrate gemfire into it. :-)

      Comment


      • #4
        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.

        Comment

        Working...
        X