SyntaxHighlighter

Monday, September 17, 2012

Spring Integration and GemFire

Spring Integration and GemFire

here's a quick sample of using Spring Integration as a walk-up start to GemFire.  once you get going with this, it's easy to expand and leverage the full potential of it

here's an example of an entity

package org.incompletecode.spring.gemfire.domain;
import java.io.Serializable;
public class SimpleEntity implements Serializable {
private static final long serialVersionUID = 1L;
private Long customerId;
private CustomerType type;
private String name;
public Long getCustomerId() {
return customerId;
}
public void setCustomerId(Long customerId) {
this.customerId = customerId;
}
public CustomerType getType() {
return type;
}
public void setType(CustomerType type) {
this.type = type;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "SimpleEntity [customerId=" + customerId + ", type=" + type
+ ", name=" + name + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ ((customerId == null) ? 0 : customerId.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + ((type == null) ? 0 : type.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
SimpleEntity other = (SimpleEntity) obj;
if (customerId == null) {
if (other.customerId != null)
return false;
} else if (!customerId.equals(other.customerId))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (type != other.type)
return false;
return true;
}
}
view raw gistfile1.java hosted with ❤ by GitHub


here's the supporting enum

package org.incompletecode.spring.gemfire.domain;
public enum CustomerType {
BASIC,GOLD;
}
view raw gistfile1.java hosted with ❤ by GitHub


here's the service to interact with it

package org.incompletecode.spring.gemfire.service;
public interface DataService {
public void save(Object object);
}
view raw gistfile1.java hosted with ❤ by GitHub


now lets setup the gemfire part using the Spring GemFire namespace

<?xml version="1.0" encoding="UTF-8"?>
<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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/gemfire http://www.springframework.org/schema/gemfire/spring-gemfire-1.1.xsd">
<gfe:cache/>
<gfe:cache-server auto-startup="true">
<gfe:subscription-config eviction-type="ENTRY" disk-store="." capacity="1000"/>
</gfe:cache-server>
<gfe:replicated-region id="test.region"/>
<gfe:transaction-manager/>
<bean id="gemfireTemplate"
class="org.springframework.data.gemfire.GemfireTemplate">
<property name="region" ref="test.region"/>
</bean>
</beans>
view raw gistfile1.xml hosted with ❤ by GitHub


and lets setup the Spring Integration to use the GemFire configuration

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:int="http://www.springframework.org/schema/integration"
xmlns:int-gfe="http://www.springframework.org/schema/integration/gemfire"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration-2.2.xsd
http://www.springframework.org/schema/integration/gemfire http://www.springframework.org/schema/integration/gemfire/spring-integration-gemfire-2.2.xsd">
<int:gateway service-interface="org.incompletecode.spring.gemfire.service.DataService">
<int:method name="save" request-channel="channel1"/>
</int:gateway>
<int:channel id="channel1"/>
<int-gfe:outbound-channel-adapter channel="channel1" region="test.region">
<int-gfe:cache-entries>
<entry key="payload.getCustomerId()" value="payload"/>
</int-gfe:cache-entries>
</int-gfe:outbound-channel-adapter>
<int-gfe:inbound-channel-adapter region="test.region" channel="channel2"/>
<int:channel id="channel2"/>
<int:logging-channel-adapter channel="channel2" level="INFO"/>
</beans>
view raw gistfile1.xml hosted with ❤ by GitHub


now, as always, here's a unit test to confirm it's all up and running...

package org.incompletecode.spring.gemfire.service;
import static org.junit.Assert.assertTrue;
import org.incompletecode.spring.gemfire.domain.CustomerType;
import org.incompletecode.spring.gemfire.domain.SimpleEntity;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.gemfire.GemfireTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath:/META-INF/spring/gemfire-context.xml",
"classpath:/META-INF/spring/integration-context.xml"})
public class DataServiceTest {
@Autowired
private DataService dataService;
@Autowired
private GemfireTemplate gemfireTemplate;
@Test
public void testSave() throws Exception {
SimpleEntity simpleEntity = new SimpleEntity();
simpleEntity.setCustomerId(System.currentTimeMillis());
simpleEntity.setName("hello");
simpleEntity.setType(CustomerType.BASIC);
//send
dataService.save(simpleEntity);
//wait
Thread.sleep(1 * 1000);
//finished
assertTrue(gemfireTemplate.getRegion().size() == 1);
assertTrue(gemfireTemplate.containsValue(simpleEntity));
//now send one that will trigger the continuousquery
}
}
view raw gistfile1.java hosted with ❤ by GitHub


here's the maven that got me across the line; getting GemFire package can be a bit tricky...

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.incompletecode.spring.gemfire</groupId>
<artifactId>spring-gemfire</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-gemfire</name>
<repositories>
<repository>
<id>repository.springframework.maven.milestone</id>
<name>Spring Framework Maven Release Repository</name>
<url>http://maven.springframework.org/milestone</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-gemfire</artifactId>
<version>2.2.0.M2</version>
</dependency>
<dependency>
<groupId>com.gemstone.gemfire</groupId>
<artifactId>gemfire</artifactId>
<version>6.6.3</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>3.1.0.RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.14</version>
</dependency>
<dependency>
<groupId>org.springframework.batch</groupId>
<artifactId>spring-batch-core</artifactId>
<version>2.1.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>3.1.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-asm</artifactId>
<version>3.1.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>3.1.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.1.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.1.1.RELEASE</version>
</dependency>
</dependencies>
</project>
view raw gistfile1.xml hosted with ❤ by GitHub



No comments:

Post a Comment