Ellin.com
Not Very Interesting
Connecting Spring Integration to GemFire

This page was converted from my old blog and hasn’t been reviewed. If you see an error please let me know in the comments.

Spring Integration is an appication integration framework that builds upon the core Spring framework. It allows for a high level of abstraction between the actual messaging infrastructure and the implementation logic. This allows the devloper to focus on adding value via buesiness logic without having to implement boiler plate code.

Spring Integration provides a number of adapters that can be used by a developer out of the box. One such out of the box adapter is for connections to GemFire.

This post will focus on Gemfire data sources. A later post will talk about GemFire data sinks.

  • Inbound Channel Adapter, produces Events based on region operations, such as creats, updates, deletes, etc.

  • Continuous Query Channel adapter, This adapter will read events from a region using GemFire’s continuous query functionality. The continuos query will act as an event source.

In addition I will be using Spring Boot to build the framework for the demo project. I will also be using Spring XD to load data into GemFire for demonstration purposes.

Prequisistes

Both of the examples I will be showing use a remote GemFire instance. Before trying out these samples make sure you have a running GemFire instance and can add data to a GemFire region. I will be using the Spring XD Demo to post data into a Customer Region.

Continuous Query Channel adapter

Using Spring Integration we will define an input channel, a transformer and a an outbound channel.

Integration

The cqInputChannel will be a source of data. This source will use a GemFire continuous query to receive data from the Customer region as it is updated. The data store in this region will be a PDX Serialized JSON object.

A transform in the middle will convert the PDX Instance into a String representation.

files will be an outbound data sink and will write the data as it is received to a flat file. In this case a file called HelloWorld in /Users/jellin

[code lang=”xml”]
<gfe:client-cache id="client-cache" pool-name="client-pool"/>

<gfe:pool id="client-pool" subscription-enabled="true">
<gfe:server host="localhost" port="40404" />
</gfe:pool>

<gfe:cq-listener-container id="queryListenerContainer" cache="client-cache"
pool-name="client-pool"/>

<int-gfe:cq-inbound-channel-adapter id="cqInputChannel"
cq-listener-container="queryListenerContainer"
query="select * from /Customers" />

<gfe:pool id="client-pool" subscription-enabled="true">
<gfe:server host="localhost" port="40404" />
</gfe:pool>

<file:outbound-channel-adapter id="files"
mode="APPEND"
charset="UTF-8"
directory="/Users/jellin/"
filename-generator-expression="‘HelloWorld’"
/>

<int:transformer input-channel="cqInputChannel" output-channel="files" method="toString">

</int:transformer>
[/code]

  1. First we define a client cache.
  2. The cqListener container which references the cache and the pool
  3. the cqInputChannel defines the query and a reference to the listener container
  4. the files defines the outbound channel which is the file data will be appended to.
  5. the transformer uses a simple Java POJO to map PDXInstances to a String.

Since we are using a continuous query we don’t need to define the Customers region on the client.

Since we will be using Spring Boot there is very little additional work we need to do other than defining the above Integration.xml file.

The Spring Configuration class uses AutoConfiguration to bootstrap Spring.

[code lang=”java”]
@Configuration
@ComponentScan
@EnableAutoConfiguration
@ImportResource("integration.xml")
public class Application {

public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
[/code]

Running the Demo

Start GemFire

[code lang=”bash”]
gemfire-server cq-demo.xml
[/code]

Start Spring XD

[code lang=”bash”]
xd-singlenode
[/code]

Start xd-shell

[code lang=”bash”]
xd-shell
[/code]

Start your Spring Boot application by running Application.java in your IDE

Start a stream in the XD Shell to pipe data posted via http to GemFire

[code lang=”text”]
http | gemfire-json-server –regionName=Customers –keyExpression=payload.getField(‘lastname’) –host localhost –port 40404 –deploy
[/code]

Post data to Gemfire using the XD Shell

[code lang=”text”]
http post –target http://localhost:9090 –data {"firstname":"BOB","lastname":"JONES"}
[/code]

if everything was setup correctly you should see a new line of data in your HelloWorld file each time data is added to the Customers region.

Source Code is available here.


Last modified on 2014-09-21

comments powered by Disqus