Spring Bootloader: Part 1

On a recent project, I was asked to write a wrapper for GemFire to make it Spring enabled. We did this so we could deploy custom applications using Spring Data GemFire as an extension to GemFire proper. We could also use other Spring conventions when talking to a database as a typical L2 cache using POJOs to map the data into GemFire. So I went down the path of doing a typical Java application that loads Spring in the main method.

GemFireRunner.java

package com.cl9p.gemfire.runnable;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class GemFireRunner {
    public static void main(String args[]) {
        String springContextFile = "gemfire-context.xml";
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext(springContextFile);
    }
}

gemfire-context.xml

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:gfe="http://www.springframework.org/schema/gemfire"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="
        http://www.springframework.org/schema/gemfire   http://www.springframework.org/schema/gemfire/spring-gemfire.xsd
		http://www.springframework.org/schema/beans     http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context   http://www.springframework.org/schema/context/spring-context.xsd
		http://www.springframework.org/schema/util      http://www.springframework.org/schema/util/spring-util.xsd
		">
    <context:component-scan base-package="com.cl9p"/>
    <bean id="kvTemplate" class="org.springframework.data.gemfire.GemfireTemplate"
          p:region-ref="generic-key-value"/>
    <util:properties id="cache-properties" location="classpath:cache.properties" />
    <gfe:cache id="cache-server" properties-ref="cache-properties"/>
    <gfe:partitioned-region id="generic-key-value" cache-ref="cache-server"/>
</beans>

cache.xml

<!DOCTYPE cache PUBLIC
        "-//GemStone Systems, Inc.//GemFire Declarative Caching 6.5//EN"
        "http://www.gemstone.com/dtd/cache6_5.dtd">
<cache>
    <cache-server port="40404"/>
    <resource-manager critical-heap-percentage="80" eviction-heap-percentage="75"/>
</cache>

cache.properties

locators=localhost[10334]
start-locator=localhost[10334]
server-bind-address=localhost
cache-xml-file=cache.xml
mcast-port=0
statistic-sampling-enabled=true
log-level=info

Although this code will run, it is not very clean. To run on a typical Linux machine there is no log location, no pid file, no init script. Oh, and don’t even me started if I wanted to run this as a service on Windows. Enter

apache-commons-daemon

Our modified file is located here GemFireRunner.java. Running this file will now run in a properly contained Thread using Runnable and we have a number of facilities at our disposal. To verify everything is running properly you should see the following when you run the class.

[info 2013/02/21 20:37:22.905 CST   tid=0x1] Partitioned Region /generic-key-value is born with prId=2 ident:#generic-key-value
2013-Feb-21 20:37:22,908 INFO  [RegionFactoryBean]: Created new cache region [generic-key-value]
2013-Feb-21 20:37:22,949 INFO  [GemFireRunner]: instance 700235173 init 0
2013-Feb-21 20:37:22,950 INFO  [GemFireRunner]: loading on port 1200
2013-Feb-21 20:37:22,950 INFO  [GemFireRunner]: starting...
2013-Feb-21 20:37:22,950 INFO  [GemFireRunner]: started acceptor loop

Now we need a way to package the application with all of the dependencies.

Tagged , , , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: