add initial draft for hello world example

This commit is contained in:
costin
2010-07-15 15:04:51 +03:00
parent 12a38c9332
commit c153c91768
13 changed files with 532 additions and 0 deletions

View File

@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java"/>
<classpathentry kind="src" output="target/classes" path="src/main/resources"/>
<classpathentry kind="src" output="target/test-classes" path="src/test/java"/>
<classpathentry kind="src" output="target/test-classes" path="src/test/resources"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5"/>
<classpathentry kind="con" path="org.maven.ide.eclipse.MAVEN2_CLASSPATH_CONTAINER"/>
<classpathentry kind="output" path="target/classes"/>
</classpath>

View File

@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>hello-word</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.maven.ide.eclipse.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.springframework.ide.eclipse.core.springbuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.springframework.ide.eclipse.core.springnature</nature>
<nature>org.maven.ide.eclipse.maven2Nature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>

View File

@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<beansProjectDescription>
<version>1</version>
<pluginVersion><![CDATA[2.3.2.201003220227-RELEASE]]></pluginVersion>
<configSuffixes>
<configSuffix><![CDATA[xml]]></configSuffix>
</configSuffixes>
<enableImports><![CDATA[false]]></enableImports>
<configs>
</configs>
<configSets>
</configSets>
</beansProjectDescription>

123
samples/hello-world/pom.xml Normal file
View File

@@ -0,0 +1,123 @@
<?xml version="1.0" encoding="UTF-8"?>
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.springframework.data.gemfire.samples</groupId>
<artifactId>hello-world</artifactId>
<version>1.0.0.M1-SNAPSHOT</version>
<name>Spring GemFire - Hello World Sample</name>
<description>A simple Hello World example illustrating the basic steps in configuring GemFire with Spring</description>
<issueManagement>
<system>Issue Tracker</system>
<url>http://jira.springframework.org/browse/SGF</url>
</issueManagement>
<scm>
<url>http://git.springsource.org/spring-gemfire</url>
</scm>
<organization>
<name>SpringSource, a division of VMware</name>
<url>www.SpringSource.org</url>
</organization>
<properties>
<spring.version>3.0.3.RELEASE</spring.version>
<junit.version>4.7</junit.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>jsr250-api</artifactId>
<version>1.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.data.gemfire</groupId>
<artifactId>spring-gemfire</artifactId>
<version>${version}</version>
</dependency>
<!-- gemfire dependency -->
<dependency>
<groupId>com.gemstone.gemfire</groupId>
<artifactId>gemfire</artifactId>
<version>6.0.1.A</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.1</version>
<configuration>
<archive>
<manifestEntries>
<Main-Class>org.springframework.data.gemfire.samples.helloworld.Main</Main-Class>
</manifestEntries>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.1</version>
<configuration>
<mainClass>org.springframework.data.gemfire.samples.helloworld.Main</mainClass>
</configuration>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>gemfire-release-repo</id>
<name>GemFire Release Repository</name>
<url>http://dist.gemstone.com/maven/release/</url>
</repository>
</repositories>
</project>

View File

@@ -0,0 +1,58 @@
/*
* Copyright 2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.gemfire.samples.helloworld;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.gemstone.gemfire.cache.EntryEvent;
import com.gemstone.gemfire.cache.util.CacheListenerAdapter;
/**
* Listener that logs entry operations to the configured logger.
*
* @author Costin Leau
*/
public class CacheLogger extends CacheListenerAdapter<Object, Object> {
private static final Log log = LogFactory.getLog(CacheLogger.class);
@Override
public void afterCreate(EntryEvent<Object, Object> event) {
log.info("Added " + messageLog(event) + " to the cache");
}
@Override
public void afterDestroy(EntryEvent<Object, Object> event) {
log.info("Removed " + messageLog(event) + " from the cache");
}
@Override
public void afterUpdate(EntryEvent<Object, Object> event) {
log.info("Updated " + messageLog(event) + " in the cache");
}
private String messageLog(EntryEvent<Object, Object> event) {
Object key = event.getKey();
Object value = event.getNewValue();
if (event.getOperation().isUpdate()) {
return "[" + key + "=" + event.getOldValue() + " to " + event.getNewValue() + "]";
}
return "[" + key + "=" + value + "]";
}
}

View File

@@ -0,0 +1,68 @@
/*
* Copyright 2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.gemfire.samples.helloworld;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* @author Costin Leau
*/
public class CommandProcessor {
private static final Log log = LogFactory.getLog(CommandProcessor.class);
boolean threadActive;
private Thread thread;
void start() {
if (thread == null) {
threadActive = false;
thread = new Thread(new Task(), "cmd-processor");
thread.start();
}
}
void stop() throws Exception {
threadActive = true;
thread.join(3 * 1000);
}
private class Task implements Runnable {
public void run() {
try {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
while (threadActive) {
if (br.ready()) {
String line = br.readLine();
log.info("Read line " + line);
}
}
} catch (IOException ioe) {
// just ignore any exceptions
log.error("Caught exception while processing commands ", ioe);
}
}
}
}

View File

@@ -0,0 +1,27 @@
/*
* Copyright 2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.gemfire.samples.helloworld;
import com.gemstone.gemfire.cache.util.CacheWriterAdapter;
/**
* @author Costin Leau
*/
public class GridToWorldWriter extends CacheWriterAdapter<Object, Object> {
}

View File

@@ -0,0 +1,67 @@
/*
* Copyright 2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.gemfire.samples.helloworld;
import java.util.Map;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Component;
import com.gemstone.gemfire.cache.Region;
/**
* Main bean for interacting with the cache from the console.
*
* @author Costin Leau
*/
@Component
public class HelloWorld {
private static final Log log = LogFactory.getLog(HelloWorld.class);
// inject the region
@Resource
private Region<String, String> region;
// re-inject the region (as a Map)
// the name is required since by default Spring
// injects the beans
@Resource(name = "hw-region")
private Map<String, String> map;
private CommandProcessor processor;
@PostConstruct
void start() {
log.info("Member " + region.getCache().getDistributedSystem().getDistributedMember().getId()
+ " connecting to region [" + region.getName() + "]");
processor = new CommandProcessor();
processor.start();
}
@PreDestroy
void stop() throws Exception {
log.info("Member " + region.getCache().getDistributedSystem().getDistributedMember().getId()
+ " disconnecting from region [" + region.getName() + "]");
processor.stop();
}
}

View File

@@ -0,0 +1,49 @@
/*
* Copyright 2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.gemfire.samples.helloworld;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Hello World startup class.
* Bootstraps the Spring container which in turns starts GemFire and the actual application.
* <p/>
* Accepts as optional parameters location of one (or multiple) application contexts that will
* be used for configuring the Spring container. See the reference documentation for more
* {@link http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/resources.html information}.
*
* Note that in most (if not all) managed environments writing such a class is not needed
* as Spring already provides the required integration.
*
* @see org.springframework.web.context.ContextLoaderListener
* @author Costin Leau
*/
public class Main {
private static final String[] CONFIGS = new String[] { "app-context.xml" };
/**
* @param args
*/
public static void main(String[] args) {
String[] res = (args != null && args.length > 0 ? args : CONFIGS);
AbstractApplicationContext ctx = new ClassPathXmlApplicationContext(res);
// shutdown the context along with the VM
ctx.registerShutdownHook();
}
}

View File

@@ -0,0 +1,13 @@
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="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-3.0.xsd">
<import resource="cache-context.xml"/>
<!-- find beans by scanning the classpath -->
<context:component-scan base-package="org.springframework.data.gemfire.samples.helloworld"/>
</beans>

View File

@@ -0,0 +1,21 @@
<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- GemFire cache bean -->
<bean id="cache" class="org.springframework.data.gemfire.CacheFactoryBean"/>
<!-- hello world region -->
<!-- since no name is given, the region will be named after the bean -->
<bean id="hw-region" class="org.springframework.data.gemfire.RegionFactoryBean" p:cache-ref="cache">
<property name="cacheListeners">
<bean class="org.springframework.data.gemfire.samples.helloworld.CacheLogger"/>
</property>
<property name="cacheWriter">
<bean class="org.springframework.data.gemfire.samples.helloworld.GridToWorldWriter"/>
</property>
</bean>
</beans>

View File

@@ -0,0 +1,28 @@
/*
* Copyright 2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.gemfire.samples.helloworld;
import org.junit.Test;
public class BasicTest {
@Test
public void testBasic() throws Exception {
Main.main(new String[] {});
}
}

26
samples/readme.txt Normal file
View File

@@ -0,0 +1,26 @@
Spring GemFire Samples
----------------------
This folder contains various various demo applications and samples for Spring GemFire.
Please see each folder for detailed instructions (readme.txt).
As a general rule, each demo provides an integration tests that bootstraps
the GemFire platform, installs the demo and its dependencies and interacts with
the application.
SAMPLES OVERVIEW
----------------
* hello-world
A simple demo for configuring and interacting with the GemFire grid.
BUILDING AND DEPLOYMENT
-----------------------
All demos require Maven 2.0.7+ and JDK 1.5+.
Each module should be run from its top folder using maven:
# mvn clean install