Permalink
Browse files

Added locator utilities and gateway example

  • Loading branch information...
1 parent f5d3f41 commit e67fd731de339116a8072f72b6edd0164d57edb8 @dturanski dturanski committed Sep 13, 2012
Showing with 795 additions and 380 deletions.
  1. +3 −4 .gitignore
  2. +39 −0 advanced/README.md
  3. +28 −0 advanced/build.gradle
  4. +25 −0 advanced/gateway/README.md
  5. +1 −0 advanced/gateway/gateway.properties
  6. +69 −0 advanced/gateway/src/main/java/org/springframework/data/gemfire/examples/GatewayExample.java
  7. +27 −0 advanced/gateway/src/main/java/org/springframework/data/gemfire/examples/London.java
  8. +27 −0 advanced/gateway/src/main/java/org/springframework/data/gemfire/examples/NewYork.java
  9. +8 −0 advanced/gateway/src/main/resources/log4j.properties
  10. +40 −0 advanced/gateway/src/main/resources/london/cache-config.xml
  11. +41 −0 advanced/gateway/src/main/resources/ny/cache-config.xml
  12. +49 −0 advanced/locator-failover/README.md
  13. +54 −0 advanced/locator-failover/src/main/java/org/springframework/data/gemfire/examples/Client.java
  14. +36 −0 advanced/locator-failover/src/main/java/org/springframework/data/gemfire/examples/Server.java
  15. +23 −0 advanced/locator-failover/src/main/resources/client/cache-config.xml
  16. +33 −0 advanced/locator-failover/src/main/resources/server/cache-config.xml
  17. +23 −1 build.gradle
  18. +1 −0 settings.gradle
  19. +154 −31 spring-gemfire-examples-common/src/main/java/org/springframework/data/gemfire/examples/Locator.java
  20. +93 −0 ...examples-common/src/main/java/org/springframework/data/gemfire/examples/LocatorCommandParser.java
  21. +3 −1 ...mfire-examples-common/src/main/java/org/springframework/data/gemfire/examples/domain/Address.java
  22. +5 −1 ...fire-examples-common/src/main/java/org/springframework/data/gemfire/examples/domain/Customer.java
  23. +4 −2 ...-examples-common/src/main/java/org/springframework/data/gemfire/examples/domain/EmailAddress.java
  24. +2 −1 ...fire-examples-common/src/main/java/org/springframework/data/gemfire/examples/domain/LineItem.java
  25. +3 −1 ...gemfire-examples-common/src/main/java/org/springframework/data/gemfire/examples/domain/Order.java
  26. +4 −2 ...mfire-examples-common/src/main/java/org/springframework/data/gemfire/examples/domain/Product.java
  27. +0 −174 ...ire-examples-common/src/main/java/org/springframework/data/gemfire/examples/util/LocatorBean.java
  28. +0 −138 ...n/src/main/java/org/springframework/data/gemfire/examples/util/PersistenceDirectoryGenerator.java
  29. +0 −24 spring-gemfire-examples-common/src/main/resources/cache-locator-context.xml
View
@@ -1,14 +1,13 @@
-tutorial/
-example/
-more-examples/
-quickstart/quickstart-other
+locator1*/
.settings
.project
.classpath
.springBeans
.gradle
#Compile source#
*.class
+*.lk
+*.if
build
vf.*
bin
View
@@ -0,0 +1,39 @@
+Spring Data GemFire - Advanced Examples
+========================================
+
+These examples demonstrate additional GemFire features and require a full installation of GemFire. A trial version may be obtained [here](http://www.vmware.com/products/application-platform/vfabric-gemfire/overview.html).
+
+
+#Starting and Stopping Locators
+
+These examples require GemFire locators to be running. The locator is used to discover distributed system members and used to obtain member network addresses. For example, in a client-server topology locator addresses are known to the client and servers. The client pool uses the locator to obtain server connections on start up. Typically there are two locators for each distributed system, a primary and backup locator.
+
+The spring-gemfire-examples-common project includes a Locator utility to start and stop locators on localhost. It is required that you defing an environment variable GEMFIRE_HOME, set to the location of the GemFire product installation. Alternatively you can pass this location as a parameter to the locator utility.
+
+## Starting a Locator
+
+Open a command window or terminal session, navigate to the examples directory and type
+
+ gradle -q start-locator-<port-number>
+
+For example, to start two locators with GEMFIRE_HOME set:
+
+ gradle -q start-locator-10334
+ gradle -q start-locator-10335
+
+## Stopping a Locator
+
+ gradle -q stop-locator-10334
+ gradle -q stop-locator-10335
+
+By default, each locator instance keeps runtime files, e.g., logs in a directory location ./locator<port-number>. Review the contents of these logs, especially if you are unable to start or stop a locator. You may also pass a command line parameter to specify an alternate working directory for a locator but you must create the directory first:
+
+ gradle -q start-locator-10334 -Pdir=/foo/bar
+
+## Running the Locator utility from the IDE
+
+Import the spring-gemfire-examples-common project into your IDE. From there you should be able to run the Locator utility as a Java program, providing appropriate command line options, e.g.,
+
+ --start --port 10334 --gemfire_home /foo/bar
+
+If you run it with no options you will get a help message.
View
@@ -0,0 +1,28 @@
+description = 'Spring Data GemFire Advanced Examples'
+
+project('gateway') {
+
+ description = 'Spring Data GemFire Gateway Example'
+
+ def env = System.getenv()
+
+ dependencies {
+ compile project(':spring-gemfire-examples-common')
+ }
+}
+
+project('locator-failover') {
+
+ description = 'Spring Data GemFire Locator Failover Example'
+ dependencies {
+ compile project(':spring-gemfire-examples-common')
+ }
+}
+
+
+project('server-failover') {
+ description = 'Spring Data GemFire Server Failover Example'
+ dependencies {
+ compile project(':spring-gemfire-examples-common')
+ }
+}
View
@@ -0,0 +1,25 @@
+Spring Data GemFire - Gateway Example
+=====================================
+
+See the [README](..README.md) in the advanced root directory for information about locators.
+
+Note: This example requires a valid [GemFire license key](http://pubs.vmware.com/vfabric51/topic/com.vmware.vfabric.gemfire.6.6/deploying/licensing/licensing.html?resultof=%22%6c%69%63%65%6e%73%69%6e%67%22%20%22%6c%69%63%65%6e%73%22%20) for a Data Management Node. GemFire checks for a valid license before starting a gateway.
+
+If you have downloaded the trial version, you will need to obtain a key from GemFire sales. Edit gateway.properties to set the license key:
+
+ gemfire.license.key=my license key
+
+To run this example, Open a command window and start two locators:
+
+ gradle -q start-locator-10334
+ gradle -q start-locator-10335
+
+Start one of the processes:
+
+ gradle -q run-gateway -PmainClass=NewYork
+
+In another command window, run:
+
+ gradle -q run-gateway -PmainClass=London
+
+Each of these initializes an Order region which is gateway enabled and a gateway hub. Since each process uses a different locator, and multicast is disabled (mcast-port=0), each cache runs in a separate Distributed System, simulating two remote installations. Both processes will prompt you to press ENTER to update the region. Wait for both processes to start first. You should see a log message from the remote process's LoggingCacheListener indicating the remote region was updated. Press ENTER a second time to terminate the process.
@@ -0,0 +1 @@
+gemfire.license.key=my license key
@@ -0,0 +1,69 @@
+/*
+ * Copyright 2012 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.examples;
+
+import java.io.File;
+import java.io.IOException;
+
+import org.springframework.context.ApplicationContext;
+import org.springframework.context.support.ClassPathXmlApplicationContext;
+import org.springframework.data.gemfire.examples.domain.Address;
+import org.springframework.data.gemfire.examples.domain.Order;
+
+import com.gemstone.gemfire.cache.Cache;
+import com.gemstone.gemfire.cache.Region;
+import com.gemstone.gemfire.cache.util.GatewayHub;
+
+/**
+ * @author David Turanski
+ *
+ */
+public class GatewayExample {
+ @SuppressWarnings({ "deprecation", "unchecked" })
+ public static void run(String location, long id) {
+
+ File diskStoreDirectory = new File(location);
+ if (!diskStoreDirectory.exists()){
+ diskStoreDirectory.mkdir();
+ }
+
+ ApplicationContext context = new ClassPathXmlApplicationContext(
+ location + "/cache-config.xml");
+ Cache cache = context.getBean(Cache.class);
+ GatewayHub gwh = cache.getGatewayHub(location);
+
+ if (gwh.isRunning()){
+ System.out.println("gateway is running on " + gwh.getPort());
+ } else {
+ System.out.println("gateway is not started");
+ }
+
+ Region<Long, Order> region = context.getBean(Region.class);
+
+ try {
+ System.out.println("Press <Enter> to update region " + region.getName());
+ System.in.read();
+ region.put(id, new Order(1L, 1L, new Address("street", "city",
+ "country")));
+ System.out.println("Press <Enter> to quit");
+ System.in.read();
+ } catch (IOException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ System.exit(0);
+ }
+}
@@ -0,0 +1,27 @@
+/*
+ * Copyright 2012 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.examples;
+
+
+/**
+ * @author David Turanski
+ *
+ */
+public class London {
+ public static void main(String args[]) {
+ GatewayExample.run("london",2);
+ }
+}
@@ -0,0 +1,27 @@
+/*
+ * Copyright 2012 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.examples;
+
+/**
+ * @author David Turanski
+ *
+ */
+public class NewYork {
+ public static void main(String args[]) {
+ GatewayExample.run("ny", 1);
+ }
+
+}
@@ -0,0 +1,8 @@
+log4j.rootCategory=WARN, stdout
+
+log4j.appender.stdout=org.apache.log4j.ConsoleAppender
+log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
+log4j.appender.stdout.layout.ConversionPattern=%p [%t] [%C{1}] - %m%n
+
+#log4j.category.org.springframework.data.gemfire.examples=DEBUG
+log4j.category.org.springframework=DEBUG
@@ -0,0 +1,40 @@
+<?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"
+ xmlns:util="http://www.springframework.org/schema/util"
+ xmlns:context="http://www.springframework.org/schema/context"
+ 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/util http://www.springframework.org/schema/util/spring-util.xsd
+ http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
+
+ <context:property-placeholder location="file:gateway.properties"/>
+
+ <util:properties id="gemfireProperties">
+ <prop key="mcast-port">0</prop>
+ <prop key="locators">localhost[10334]</prop>
+ <prop key="log-level">info</prop>
+ <prop key="license-data-management">${gemfire.license.key}</prop>
+<!-- <prop key="log-file">london.log</prop> -->
+ </util:properties>
+
+ <gfe:cache properties-ref="gemfireProperties"/>
+
+ <gfe:replicated-region id="Order" enable-gateway="true" hub-id="london">
+ <gfe:cache-listener>
+ <bean class="org.springframework.data.gemfire.examples.util.LoggingCacheListener"/>
+ </gfe:cache-listener>
+ </gfe:replicated-region>
+
+ <gfe:gateway-hub id="london" port="12345" >
+ <gfe:gateway gateway-id="ny">
+ <gfe:gateway-endpoint port="12346" host="localhost" endpoint-id="ny"/>
+ <gfe:gateway-queue persistent="true" disk-store-ref="diskstore"/>
+ </gfe:gateway>
+ </gfe:gateway-hub>
+
+ <gfe:disk-store id="diskstore">
+ <gfe:disk-dir location="london"/>
+ </gfe:disk-store>
+</beans>
@@ -0,0 +1,41 @@
+<?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"
+ xmlns:util="http://www.springframework.org/schema/util"
+ xmlns:context="http://www.springframework.org/schema/context"
+ 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/util http://www.springframework.org/schema/util/spring-util.xsd
+ http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
+
+ <context:property-placeholder location="file:gateway.properties" system-properties-mode="OVERRIDE"/>
+
+ <util:properties id="gemfireProperties">
+ <prop key="mcast-port">0</prop>
+ <prop key="locators">localhost[10335]</prop>
+ <prop key="log-level">info</prop>
+ <prop key="license-data-management">${gemfire.license.key}</prop>
+<!-- <prop key="log-file">newyork.log</prop> -->
+ </util:properties>
+
+ <gfe:cache properties-ref="gemfireProperties"/>
+
+ <gfe:replicated-region id="Order" enable-gateway="true" hub-id="ny">
+ <gfe:cache-listener>
+ <bean class="org.springframework.data.gemfire.examples.util.LoggingCacheListener"/>
+ </gfe:cache-listener>
+ </gfe:replicated-region>
+
+ <gfe:gateway-hub id="ny" port="12346">
+ <gfe:gateway gateway-id="london">
+ <gfe:gateway-endpoint port="12345" host="localhost" endpoint-id="london"/>
+ <gfe:gateway-queue persistent="true" disk-store-ref="diskstore"/>
+ </gfe:gateway>
+ </gfe:gateway-hub>
+
+ <gfe:disk-store id="diskstore">
+ <gfe:disk-dir location="ny"/>
+ </gfe:disk-store>
+
+</beans>
@@ -0,0 +1,49 @@
+Spring Data GemFire - Locator Failover Example
+==============================================
+
+See the [README](..README.md) in the advanced root directory for information about locators.
+
+This example creates simple a client server system to demonstrate what happens when the primary locator is down either after initialization or before initialization.
+
+# Scenario 1 - Locator Down After Initialization
+
+Start two locators:
+
+ gradle -q start-locator-10334
+ gradle -q start-locator-10335
+
+Start the server:
+
+ gradle -q run-locator-failover -PmainClass=Server
+
+Start the client:
+
+ gradle -q run-locator-failover -PmainClass=Client
+
+
+The client will create two Customer entries which will be indicated by Server log messages.
+
+When prompted in the client, stop the primary locator:
+
+ gradle -q stop-locator-10334
+
+The client will create two more Customer entries.
+
+You should see some warning messages in the Server process like:
+
+ Could not connect to distribution locator localhost<v0>:10334: java.net.ConnectException: Connection refused
+
+But everything should still work. In this case, the client already has a connection to the server so the locator is not used.
+
+
+#Scenario 2 - Locator Down Before Initialization
+
+Following scenario 1, the primary locator (port 10334) is stopped and the secondary locator (port 10335) is running. You may also leave the Server running or restart it. When the Client starts, you will see an info log message something like:
+
+ [info ... EDT <poolTimer-gemfirePool-2> tid=0x17] locator localhost/127.0.0.1:10334 is not running.
+ java.net.ConnectException: Connection refused ... (stack trace follows)
+
+The secondary locator will take over and the application will run as before.
+
+
+
Oops, something went wrong.

0 comments on commit e67fd73

Please sign in to comment.