diff --git a/samples/hello-world/.classpath b/samples/hello-world/.classpath new file mode 100644 index 00000000..438f0fe3 --- /dev/null +++ b/samples/hello-world/.classpath @@ -0,0 +1,10 @@ + + + + + + + + + + diff --git a/samples/hello-world/.project b/samples/hello-world/.project new file mode 100644 index 00000000..8244458e --- /dev/null +++ b/samples/hello-world/.project @@ -0,0 +1,29 @@ + + + hello-word + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.maven.ide.eclipse.maven2Builder + + + + + org.springframework.ide.eclipse.core.springbuilder + + + + + + org.springframework.ide.eclipse.core.springnature + org.maven.ide.eclipse.maven2Nature + org.eclipse.jdt.core.javanature + + diff --git a/samples/hello-world/.springBeans b/samples/hello-world/.springBeans new file mode 100644 index 00000000..4dff6476 --- /dev/null +++ b/samples/hello-world/.springBeans @@ -0,0 +1,13 @@ + + + 1 + + + + + + + + + + diff --git a/samples/hello-world/pom.xml b/samples/hello-world/pom.xml new file mode 100644 index 00000000..82bcb268 --- /dev/null +++ b/samples/hello-world/pom.xml @@ -0,0 +1,123 @@ + + + + 4.0.0 + org.springframework.data.gemfire.samples + hello-world + 1.0.0.M1-SNAPSHOT + Spring GemFire - Hello World Sample + + A simple Hello World example illustrating the basic steps in configuring GemFire with Spring + + Issue Tracker + http://jira.springframework.org/browse/SGF + + + http://git.springsource.org/spring-gemfire + + + SpringSource, a division of VMware + www.SpringSource.org + + + + 3.0.3.RELEASE + 4.7 + + + + + + junit + junit + ${junit.version} + test + + + org.springframework + spring-context + ${spring.version} + compile + + + org.springframework + spring-tx + ${spring.version} + compile + + + javax.inject + javax.inject + 1 + test + + + javax.annotation + jsr250-api + 1.0 + test + + + org.springframework + spring-test + ${spring.version} + test + + + + org.springframework.data.gemfire + spring-gemfire + ${version} + + + + + com.gemstone.gemfire + gemfire + 6.0.1.A + compile + + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.5 + 1.5 + + + + org.apache.maven.plugins + maven-jar-plugin + 2.1 + + + + org.springframework.data.gemfire.samples.helloworld.Main + + + + + + org.codehaus.mojo + exec-maven-plugin + 1.1 + + org.springframework.data.gemfire.samples.helloworld.Main + + + + + + + + gemfire-release-repo + GemFire Release Repository + http://dist.gemstone.com/maven/release/ + + + \ No newline at end of file diff --git a/samples/hello-world/src/main/java/org/springframework/data/gemfire/samples/helloworld/CacheLogger.java b/samples/hello-world/src/main/java/org/springframework/data/gemfire/samples/helloworld/CacheLogger.java new file mode 100644 index 00000000..8000d296 --- /dev/null +++ b/samples/hello-world/src/main/java/org/springframework/data/gemfire/samples/helloworld/CacheLogger.java @@ -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 { + + private static final Log log = LogFactory.getLog(CacheLogger.class); + + @Override + public void afterCreate(EntryEvent event) { + log.info("Added " + messageLog(event) + " to the cache"); + } + + @Override + public void afterDestroy(EntryEvent event) { + log.info("Removed " + messageLog(event) + " from the cache"); + } + + @Override + public void afterUpdate(EntryEvent event) { + log.info("Updated " + messageLog(event) + " in the cache"); + } + + private String messageLog(EntryEvent event) { + Object key = event.getKey(); + Object value = event.getNewValue(); + + if (event.getOperation().isUpdate()) { + return "[" + key + "=" + event.getOldValue() + " to " + event.getNewValue() + "]"; + } + return "[" + key + "=" + value + "]"; + } +} diff --git a/samples/hello-world/src/main/java/org/springframework/data/gemfire/samples/helloworld/CommandProcessor.java b/samples/hello-world/src/main/java/org/springframework/data/gemfire/samples/helloworld/CommandProcessor.java new file mode 100644 index 00000000..a86bee9a --- /dev/null +++ b/samples/hello-world/src/main/java/org/springframework/data/gemfire/samples/helloworld/CommandProcessor.java @@ -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); + } + } + } +} diff --git a/samples/hello-world/src/main/java/org/springframework/data/gemfire/samples/helloworld/GridToWorldWriter.java b/samples/hello-world/src/main/java/org/springframework/data/gemfire/samples/helloworld/GridToWorldWriter.java new file mode 100644 index 00000000..600489d3 --- /dev/null +++ b/samples/hello-world/src/main/java/org/springframework/data/gemfire/samples/helloworld/GridToWorldWriter.java @@ -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 { + + +} diff --git a/samples/hello-world/src/main/java/org/springframework/data/gemfire/samples/helloworld/HelloWorld.java b/samples/hello-world/src/main/java/org/springframework/data/gemfire/samples/helloworld/HelloWorld.java new file mode 100644 index 00000000..cb915672 --- /dev/null +++ b/samples/hello-world/src/main/java/org/springframework/data/gemfire/samples/helloworld/HelloWorld.java @@ -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 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 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(); + } +} \ No newline at end of file diff --git a/samples/hello-world/src/main/java/org/springframework/data/gemfire/samples/helloworld/Main.java b/samples/hello-world/src/main/java/org/springframework/data/gemfire/samples/helloworld/Main.java new file mode 100644 index 00000000..e229dc0c --- /dev/null +++ b/samples/hello-world/src/main/java/org/springframework/data/gemfire/samples/helloworld/Main.java @@ -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. + *

+ * 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(); + } +} diff --git a/samples/hello-world/src/main/resources/app-context.xml b/samples/hello-world/src/main/resources/app-context.xml new file mode 100644 index 00000000..a89eb575 --- /dev/null +++ b/samples/hello-world/src/main/resources/app-context.xml @@ -0,0 +1,13 @@ + + + + + + + + diff --git a/samples/hello-world/src/main/resources/cache-context.xml b/samples/hello-world/src/main/resources/cache-context.xml new file mode 100644 index 00000000..6df79f6c --- /dev/null +++ b/samples/hello-world/src/main/resources/cache-context.xml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + diff --git a/samples/hello-world/src/test/java/org/springframework/data/gemfire/samples/helloworld/BasicTest.java b/samples/hello-world/src/test/java/org/springframework/data/gemfire/samples/helloworld/BasicTest.java new file mode 100644 index 00000000..f4d8362b --- /dev/null +++ b/samples/hello-world/src/test/java/org/springframework/data/gemfire/samples/helloworld/BasicTest.java @@ -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[] {}); + } +} diff --git a/samples/readme.txt b/samples/readme.txt new file mode 100644 index 00000000..eda5621d --- /dev/null +++ b/samples/readme.txt @@ -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 \ No newline at end of file