Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added locator utilities and gateway example

  • Loading branch information...
commit e67fd731de339116a8072f72b6edd0164d57edb8 1 parent f5d3f41
@dturanski dturanski authored
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 ...g-gemfire-examples-common/src/main/java/org/springframework/data/gemfire/examples/LocatorCommandParser.java
  21. +3 −1 spring-gemfire-examples-common/src/main/java/org/springframework/data/gemfire/examples/domain/Address.java
  22. +5 −1 spring-gemfire-examples-common/src/main/java/org/springframework/data/gemfire/examples/domain/Customer.java
  23. +4 −2 ...ng-gemfire-examples-common/src/main/java/org/springframework/data/gemfire/examples/domain/EmailAddress.java
  24. +2 −1  spring-gemfire-examples-common/src/main/java/org/springframework/data/gemfire/examples/domain/LineItem.java
  25. +3 −1 spring-gemfire-examples-common/src/main/java/org/springframework/data/gemfire/examples/domain/Order.java
  26. +4 −2 spring-gemfire-examples-common/src/main/java/org/springframework/data/gemfire/examples/domain/Product.java
  27. +0 −174 spring-gemfire-examples-common/src/main/java/org/springframework/data/gemfire/examples/util/LocatorBean.java
  28. +0 −138 ...ples-common/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
7 .gitignore
@@ -1,7 +1,4 @@
-tutorial/
-example/
-more-examples/
-quickstart/quickstart-other
+locator1*/
.settings
.project
.classpath
@@ -9,6 +6,8 @@ quickstart/quickstart-other
.gradle
#Compile source#
*.class
+*.lk
+*.if
build
vf.*
bin
View
39 advanced/README.md
@@ -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
28 advanced/build.gradle
@@ -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
25 advanced/gateway/README.md
@@ -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.
View
1  advanced/gateway/gateway.properties
@@ -0,0 +1 @@
+gemfire.license.key=my license key
View
69 advanced/gateway/src/main/java/org/springframework/data/gemfire/examples/GatewayExample.java
@@ -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);
+ }
+}
View
27 advanced/gateway/src/main/java/org/springframework/data/gemfire/examples/London.java
@@ -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);
+ }
+}
View
27 advanced/gateway/src/main/java/org/springframework/data/gemfire/examples/NewYork.java
@@ -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);
+ }
+
+}
View
8 advanced/gateway/src/main/resources/log4j.properties
@@ -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
View
40 advanced/gateway/src/main/resources/london/cache-config.xml
@@ -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>
View
41 advanced/gateway/src/main/resources/ny/cache-config.xml
@@ -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>
View
49 advanced/locator-failover/README.md
@@ -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.
+
+
+
View
54 advanced/locator-failover/src/main/java/org/springframework/data/gemfire/examples/Client.java
@@ -0,0 +1,54 @@
+/*
+ * 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.IOException;
+
+import org.springframework.context.ApplicationContext;
+import org.springframework.context.support.ClassPathXmlApplicationContext;
+import org.springframework.data.gemfire.examples.domain.Customer;
+import org.springframework.data.gemfire.examples.domain.EmailAddress;
+
+import com.gemstone.gemfire.cache.Region;
+
+public class Client {
+
+ @SuppressWarnings("unchecked")
+ public static void main(String args[]) throws IOException {
+
+ System.out.println("Start locators on ports 10334 and 10335 and the Server. Press <Enter> to continue");
+ System.in.read();
+
+ ApplicationContext context = new ClassPathXmlApplicationContext("client/cache-config.xml");
+ Region<Long,Customer> region = context.getBean(Region.class);
+
+
+ Customer dave = new Customer(1L,new EmailAddress("dave@matthews.com"),"Dave","Matthews");
+ Customer alicia = new Customer(2L,new EmailAddress("alicia@keys.com"),"Alicia","Keys");
+ region.put(dave.getId(),dave);
+ region.put(alicia.getId(),alicia);
+
+ System.out.println("Stop the primary locator on port 10334. Press <Enter> to continue");
+ System.in.read();
+
+ Customer john = new Customer(3L,new EmailAddress("john@mayer.com"),"John","Mayer");
+ Customer gwen = new Customer(4L,new EmailAddress("gwen@stefani.com"),"Gwen","Stefani");
+ region.put(john.getId(),john);
+ region.put(gwen.getId(),gwen);
+
+ System.exit(0);
+ }
+}
View
36 advanced/locator-failover/src/main/java/org/springframework/data/gemfire/examples/Server.java
@@ -0,0 +1,36 @@
+/*
+ * 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.IOException;
+
+import org.springframework.context.support.ClassPathXmlApplicationContext;
+
+public class Server {
+ public static void main(String args[]) throws IOException {
+
+ System.out
+ .println("Start locators on 10334 and 10335 and Press <Enter> to continue");
+ System.in.read();
+
+ new ClassPathXmlApplicationContext("server/cache-config.xml");
+
+ System.out.println("Press <Enter> to terminate the server");
+ System.in.read();
+ System.exit(0);
+ }
+
+}
View
23 advanced/locator-failover/src/main/resources/client/cache-config.xml
@@ -0,0 +1,23 @@
+<?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:util="http://www.springframework.org/schema/util"
+ xmlns:gfe="http://www.springframework.org/schema/gemfire"
+ 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">
+
+ <util:properties id="gemfire-props">
+ <prop key="mcast-port">0</prop>
+ </util:properties>
+
+ <gfe:client-cache properties-ref="gemfire-props"/>
+
+ <gfe:client-region id="Customer" data-policy="empty"/>
+
+ <gfe:pool>
+ <gfe:locator host="localhost" port="10334"/>
+ <gfe:locator host="localhost" port="10335"/>
+ </gfe:pool>
+
+</beans>
View
33 advanced/locator-failover/src/main/resources/server/cache-config.xml
@@ -0,0 +1,33 @@
+<?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:util="http://www.springframework.org/schema/util"
+ xmlns:gfe="http://www.springframework.org/schema/gemfire"
+ 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">
+
+
+ <util:properties id="gemfireProperties">
+ <prop key="mcast-port">0</prop>
+ <prop key="locators">localhost[10334],localhost[10335]</prop>
+ </util:properties>
+
+ <gfe:cache properties-ref="gemfireProperties"/>
+
+ <gfe:cache properties-ref="gemfire-props" />
+
+ <!-- Note the SpEL expression here used to dynamically configure an available
+ port in the given range -->
+ <gfe:cache-server port="#{portGenerator.generatePort(40404,40410)}" />
+
+ <bean id="portGenerator"
+ class="org.springframework.data.gemfire.examples.util.ServerPortGenerator" />
+
+ <gfe:replicated-region id="Customer">
+ <gfe:cache-listener>
+ <bean class="org.springframework.data.gemfire.examples.util.LoggingCacheListener"/>
+ </gfe:cache-listener>
+ </gfe:replicated-region>
+
+</beans>
View
24 build.gradle
@@ -61,7 +61,7 @@ subprojects { subproject ->
}
dependencies {
- compile("org.springframework.data:spring-data-gemfire:$version")
+ compile("org.springframework.data:spring-data-gemfire:$version")
compile "javax.inject:javax.inject:1"
compile "javax.annotation:jsr250-api:1.0"
runtime "cglib:cglib-nodep:2.2"
@@ -100,6 +100,7 @@ subprojects { subproject ->
main = "org.springframework.data.gemfire.examples.$mainClass"
classpath = sourceSets.main.runtimeClasspath
standardInput = System.in
+ systemProperties = System.getProperties()
if (rootProject.hasProperty('javaArgs')) {
args = ["$javaArgs"]
}
@@ -110,9 +111,30 @@ project('spring-gemfire-examples-common') {
dependencies {
compile "org.springframework.data:spring-data-gemfire:$version"
compile "org.hibernate.javax.persistence:hibernate-jpa-2.0-api:1.0.1.Final"
+ compile "commons-cli:commons-cli:1.2"
}
}
+tasks.addRule("Pattern: <COMMAND>-locator-<PORT>") { String taskName ->
+ def command
+ if ( taskName.startsWith('start-locator') || taskName.startsWith('stop-locator') ) {
+ command = '--' + taskName.split('-')[0]
+
+
+ def project = rootProject.allprojects.find{it.name == 'spring-gemfire-examples-common'}
+
+ task(taskName,dependsOn:project.classes) << {
+ javaexec {
+ main = 'org.springframework.data.gemfire.examples.Locator'
+
+ classpath = project.sourceSets.main.runtimeClasspath
+ standardInput = System.in
+ args = [command, '--port',"${taskName.split('-').last()}"]
+ }
+ }
+ }
+}
+
task wrapper(type: Wrapper) {
description = 'Generates gradlew[.bat] scripts'
View
1  settings.gradle
@@ -3,3 +3,4 @@ rootProject.name = 'spring-gemfire-examples'
include 'spring-gemfire-examples-common'
include ':quickstart:spring-cache', ':quickstart:repository', ':quickstart:gemfire-template',':quickstart:cq',':quickstart:transaction'
include ':basic:replicated',':basic:replicated-cs',':basic:partitioned',':basic:write-through',':basic:function',':basic:persistence'
+include ':advanced:gateway',':advanced:locator-failover',':advanced:server-failover'
View
185 spring-gemfire-examples-common/src/main/java/org/springframework/data/gemfire/examples/Locator.java
@@ -1,40 +1,163 @@
-/*
- * 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 Wayne Lund
- * @author David Turanski
- *
- */
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Properties;
-import org.springframework.context.support.ClassPathXmlApplicationContext;
+import com.gemstone.gemfire.admin.AdminDistributedSystem;
+import com.gemstone.gemfire.admin.AdminDistributedSystemFactory;
+import com.gemstone.gemfire.admin.AdminException;
+import com.gemstone.gemfire.admin.DistributedSystemConfig;
+import com.gemstone.gemfire.admin.DistributionLocator;
+import com.gemstone.gemfire.admin.DistributionLocatorConfig;
public class Locator {
+ static final int DEFAULT_PORT = 10334;
+ static final int MAX_WAIT_TIME = 15000;
+
+ public static void main(String args[]) {
- /**
- * @param args
- */
- public static void main(String[] args) throws Exception {
- String resource = ("cache-locator-context.xml");
- ClassPathXmlApplicationContext mainContext = new ClassPathXmlApplicationContext(new String[] {resource}, false);
- mainContext.setValidating(true);
- mainContext.refresh();
+ if (!(System.getenv().containsKey("GEMFIRE_HOME")) ||
+ System.getenv().get("GEMFIRE_HOME") == null ||
+ System.getenv().get("GEMFIRE_HOME").isEmpty()){
+ System.out.println("The environment variable GEMFIRE_HOME in not defined");
+ System.exit(1);
+ }
+
+ Map<String, Object> options = LocatorCommandParser.parseOptions(args);
+ if (options == null) {
+ System.exit(1);
+ }
- Thread.sleep(Long.MAX_VALUE);
+ int port = DEFAULT_PORT;
+ if (options.containsKey("port")) {
+ port = (Integer) options.get("port");
+ }
+
+ DistributedSystemConfig distributedSystemConfig = AdminDistributedSystemFactory.defineDistributedSystem();
+
+ AdminDistributedSystem adminDistributedSystem = AdminDistributedSystemFactory
+ .getDistributedSystem(distributedSystemConfig);
+
+
+ DistributionLocatorConfig locatorConfig = adminDistributedSystem.addDistributionLocator().getConfig();
+ locatorConfig.setHost("localhost");
+ locatorConfig.setPort(port);
+
+ String workingDir = (String)options.get("dir");
+
+ /*
+ * If using the default, create the directory
+ */
+ if (workingDir == null) {
+ workingDir = new File(".").getAbsolutePath() + File.separator + "locator" + port;
+ File locatorDir = new File(workingDir);
+ if (!locatorDir.exists()) {
+ locatorDir.mkdir();
+ }
+ }
+ /*
+ * If directory passed as a command argument, it must exist
+ */
+ else {
+ File locatorDir = new File (workingDir);
+ if (!locatorDir.exists()) {
+ System.err.println(" Directory " + workingDir + " does not exist.");
+ System.exit(1);
+ }
+ }
+
+ System.out.println("\nSetting working directory to " + workingDir);
+
+
+
+ locatorConfig.setWorkingDirectory(workingDir);
+
+ String propertiesFile = (String)options.get("properties");
+ if (propertiesFile != null && options.get("command").equals("start")){
+ FileInputStream is;
+ Properties properties = new Properties();
+ properties.put("mcast-port", "0");
+
+ try {
+ is = new FileInputStream(propertiesFile);
+ properties.load(is);
+ }
+ catch (FileNotFoundException e) {
+ System.out.println("cannot find properties file :" + propertiesFile);
+ System.exit(1);
+ }
+ catch (IOException e) {
+ System.out.println("cannot read properties file :" + propertiesFile);
+ System.exit(1);
+ }
+
+ locatorConfig.setDistributedSystemProperties(properties);
+ }
+
+ locatorConfig.setProductDirectory(System.getenv().get("GEMFIRE_HOME"));
+
+ for (DistributionLocator locator : adminDistributedSystem.getDistributionLocators()) {
+
+ try {
+ if (options.get("command").equals("stop")) {
+ if (stopLocator(locator, MAX_WAIT_TIME)) {
+ System.out.println(String.format("locator stopped on %s[%s]", locator.getConfig().getHost(),
+ locator.getConfig().getPort()));
+ } else {
+ System.out.println(String.format("failed to stop locator on %s[%s]", locator.getConfig()
+ .getHost(), locator.getConfig().getPort()));
+ }
+ }
+
+ if (options.get("command").equals("start")) {
+ if (startLocator(locator, MAX_WAIT_TIME)) {
+ System.out.println(String.format("locator running on %s[%s]", locator.getConfig().getHost(),
+ locator.getConfig().getPort()));
+ for (Entry<Object,Object> prop: locator. getConfig().getDistributedSystemProperties().entrySet()) {
+ System.out.println( prop.getKey() + "=" + prop.getValue());
+ }
+ } else {
+ System.out.println(String.format("failed to start locator on %s[%s]", locator.getConfig()
+ .getHost(), locator.getConfig().getPort()));
+ }
+ }
+
+ } catch (Exception e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ }
+
+ }
+
+ static boolean stopLocator(DistributionLocator locator, int maxWaitTime) throws AdminException,
+ InterruptedException {
+ if (locator.isRunning()) {
+ locator.stop();
+ int waitTime = 0;
+ while (waitTime < maxWaitTime && locator.isRunning()) {
+ locator.waitToStop(1000);
+ waitTime += 1000;
+ }
+ }
+ return !locator.isRunning();
}
-}
+ static boolean startLocator(DistributionLocator locator, int maxWaitTime) throws AdminException,
+ InterruptedException {
+ if (!locator.isRunning()) {
+ locator.start();
+ int waitTime = 0;
+ while (waitTime < maxWaitTime && !locator.isRunning()) {
+ locator.waitToStart(1000);
+ waitTime += 1000;
+ }
+ }
+ return locator.isRunning();
+ }
+}
View
93 ...emfire-examples-common/src/main/java/org/springframework/data/gemfire/examples/LocatorCommandParser.java
@@ -0,0 +1,93 @@
+package org.springframework.data.gemfire.examples;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.commons.cli.BasicParser;
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.HelpFormatter;
+import org.apache.commons.cli.Options;
+import org.apache.commons.cli.ParseException;
+
+public class LocatorCommandParser {
+ private static Options options = new Options();
+ private static void initOptions() {
+ options.addOption("start", false, "Start the locator");
+ options.addOption("stop", false, "Stop the locator");
+ options.addOption("status", false, "Check locator status");
+ options.addOption("port", true, "the locator port");
+ options.addOption("dir", true, "the locator working directory");
+ options.addOption("properties", true, "a gemfire properties file");
+ options.addOption("help", false, "display this message");
+ }
+
+ public static Map<String,Object> parseOptions(String [] args) {
+ initOptions();
+ HashMap<String,Object> optionsMap = new HashMap<String,Object>();
+ BasicParser parser = new BasicParser();
+
+ try {
+ CommandLine cl = parser.parse(options, args);
+ if (cl.hasOption("help")){
+ usage("help");
+ return null;
+ }
+
+ String command = getCommand(cl);
+
+ if (command==null){
+ return null;
+ }
+
+ optionsMap.put("command",command);
+
+ if (cl.hasOption("port")){
+ optionsMap.put("port",Integer.parseInt(cl.getOptionValue("port")));
+ }
+
+ if (cl.hasOption("dir")){
+ optionsMap.put("dir",cl.getOptionValue("dir"));
+ }
+
+ if (cl.hasOption("properties")){
+ optionsMap.put("properties",cl.getOptionValue("properties"));
+ }
+ }
+
+ catch (ParseException e) {
+ usage(e.getMessage());
+ return null;
+ }
+
+ return optionsMap;
+
+ }
+
+ private static String getCommand(CommandLine cl) {
+ String command = null;
+ if(cl.hasOption("start")){
+ command = "start";
+ }
+
+ if(cl.hasOption("stop")){
+ if (command == null){
+ command = "stop";
+ } else {
+ usage("ERROR: Invalid command option: 'stop'. Command already provided '" + command + "'." );
+ return null;
+ }
+ }
+
+ if (command==null){
+ usage("ERROR: No command option provided. One of 'start', 'stop' or 'status' is required.");
+ }
+
+ return command;
+ }
+
+ private static void usage(String msg){
+ HelpFormatter f = new HelpFormatter();
+ f.printHelp(msg, options);
+ }
+
+}
View
4 spring-gemfire-examples-common/src/main/java/org/springframework/data/gemfire/examples/domain/Address.java
@@ -24,9 +24,11 @@
*
* @author Oliver Gierke
*/
-@SuppressWarnings("serial")
+
public class Address implements Serializable {
+ private static final long serialVersionUID = -2639944658538569230L;
+
private final String street, city, country;
/**
View
6 spring-gemfire-examples-common/src/main/java/org/springframework/data/gemfire/examples/domain/Customer.java
@@ -29,9 +29,13 @@
* @author Oliver Gierke
* @author David Turanski
*/
-@SuppressWarnings("serial")
+
@Region
public class Customer extends AbstractPersistentEntity {
+
+
+ private static final long serialVersionUID = -3860687524824507124L;
+
private EmailAddress emailAddress;
private String firstname, lastname;
private Set<Address> addresses = new HashSet<Address>();
View
6 ...gemfire-examples-common/src/main/java/org/springframework/data/gemfire/examples/domain/EmailAddress.java
@@ -28,9 +28,11 @@
*
* @author Oliver Gierke
*/
-@SuppressWarnings("serial")
-public final class EmailAddress implements Serializable {
+public final class EmailAddress implements Serializable {
+
+ private static final long serialVersionUID = -2990839949384592331L;
+
private static final String EMAIL_REGEX = "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
private static final Pattern PATTERN = Pattern.compile(EMAIL_REGEX);
View
3  spring-gemfire-examples-common/src/main/java/org/springframework/data/gemfire/examples/domain/LineItem.java
@@ -23,9 +23,10 @@
/**
* @author Oliver Gierke
*/
-@SuppressWarnings("serial")
+
public class LineItem implements Serializable {
+ private static final long serialVersionUID = 6571108615511329726L;
private BigDecimal price;
private int amount;
private Long productId;
View
4 spring-gemfire-examples-common/src/main/java/org/springframework/data/gemfire/examples/domain/Order.java
@@ -28,10 +28,12 @@
* @author Oliver Gierke
* @author David Turanski
*/
-@SuppressWarnings("serial")
+
@Region
public class Order extends AbstractPersistentEntity {
+ private static final long serialVersionUID = -3779061453639083037L;
+
private Long customerId;
private Address billingAddress;
private Address shippingAddress;
View
6 spring-gemfire-examples-common/src/main/java/org/springframework/data/gemfire/examples/domain/Product.java
@@ -31,11 +31,13 @@
* @author Oliver Gierke
* @author David Turanski
*/
-@SuppressWarnings("serial")
+
@Region
@javax.persistence.Entity
public class Product extends AbstractPersistentEntity {
-
+
+ private static final long serialVersionUID = 831295555713696643L;
+
private String name, description;
private BigDecimal price;
@javax.persistence.Transient
View
174 ...ng-gemfire-examples-common/src/main/java/org/springframework/data/gemfire/examples/util/LocatorBean.java
@@ -1,174 +0,0 @@
-/*
- * 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.util;
-
-/**
- * @author Wayne Lund
- * @author David Turanski
- *
- */
-
-
-import java.io.File;
-import java.net.InetAddress;
-import java.util.Properties;
-
-import com.gemstone.gemfire.distributed.Locator;
-import com.gemstone.gemfire.distributed.internal.InternalLocator;
-import com.gemstone.gemfire.i18n.LogWriterI18n;
-
-public class LocatorBean {
-
- private boolean throwOnBindFailure = false;
- private InetAddress bind;
- private int port1 = 10334;
- private int port2 = 10335;
- public int getPort1() {
- return port1;
- }
-
- public void setPort1(int port1) {
- this.port1 = port1;
- }
-
- public int getPort2() {
- return port2;
- }
-
- public void setPort2(int port2) {
- this.port2 = port2;
- }
-
- private Locator locator;
- private File log;
- private File state;
- private boolean peerLocator = true;
- private boolean serverLocator = true;
- private String hostnameForClients;
- private String locators;
-
- public String getLocators() {
- return locators;
- }
-
- public void setLocators(String locators) {
- this.locators = locators;
- }
-
- public void startLocator() {
- try {
- startLocator(port1, true);
- } catch (Exception x) {
- startLocator(port2, throwOnBindFailure);
- }
- }
-
- public void startLocator(int port, boolean throwOnFail) {
- try {
- Properties props = new Properties();
- props.setProperty("locators", locators);
- props.setProperty("mcast-port", "0");
- locator = InternalLocator.startLocator(port, log, state, (LogWriterI18n)null, (LogWriterI18n)null, bind, true, props, peerLocator, serverLocator, hostnameForClients);
- //locator = Locator.startLocatorAndDS(port, log, props);
- System.out.println("Started locator bind="+bind+" port="+port+" locators="+locators);
- }catch (Exception x) {
- if (throwOnFail) {
- throw new RuntimeException(x);
- } else {
- System.err.println("Locator start failure for port["+port+"]: ("+x.getMessage()+")");
- }
- }
- }
-
- public void stopLocator() {
- if (locator!=null) {
- locator.stop();
- locator=null;
- }
- }
-
- public boolean isThrowOnBindFailure() {
- return throwOnBindFailure;
- }
-
- public void setThrowOnBindFailure(boolean throwOnBindFailure) {
- this.throwOnBindFailure = throwOnBindFailure;
- }
-
- public InetAddress getBind() {
- return bind;
- }
-
- public void setBind(InetAddress bind) {
- this.bind = bind;
- }
-
- public int getPort() {
- return port1;
- }
-
- public void setPort(int port) {
- this.port1 = port;
- }
-
- public Locator getLocator() {
- return locator;
- }
-
- public void setLocator(Locator locator) {
- this.locator = locator;
- }
-
- public File getLog() {
- return log;
- }
-
- public void setLog(File log) {
- this.log = log;
- }
-
- public File getState() {
- return state;
- }
-
- public void setState(File state) {
- this.state = state;
- }
-
- public boolean isPeerLocator() {
- return peerLocator;
- }
-
- public void setPeerLocator(boolean peerLocator) {
- this.peerLocator = peerLocator;
- }
-
- public boolean isServerLocator() {
- return serverLocator;
- }
-
- public void setServerLocator(boolean serverLocator) {
- this.serverLocator = serverLocator;
- }
-
- public String getHostnameForClients() {
- return hostnameForClients;
- }
-
- public void setHostnameForClients(String hostnameForClients) {
- this.hostnameForClients = hostnameForClients;
- }
-}
View
138 ...s-common/src/main/java/org/springframework/data/gemfire/examples/util/PersistenceDirectoryGenerator.java
@@ -1,138 +0,0 @@
-/*
- * 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.util;
-
-/**
- * @author Wayne Lund
- * @author David Turanski
- *
- */
-
-import java.io.File;
-import java.io.IOException;
-import java.io.RandomAccessFile;
-import java.nio.channels.FileChannel;
-import java.nio.channels.FileLock;
-import java.nio.channels.OverlappingFileLockException;
-
-public class PersistenceDirectoryGenerator {
-
- private File parent;
- private volatile FileLock lock;
- private File selected;
-
- public void setParent(File f) {
- parent = f;
- }
-
- public File getParent() {
- return parent;
- }
-
- public void unlock() {
- FileLock lock = this.lock;
- this.lock = null;
- if (lock != null) {
- try {
- lock.release();
- } catch (IOException x) {
- throw new RuntimeException(x);
- }
- lock = null;
- }
- if (selected!=null) {
- selected.delete();
- }
- this.selected = null;
- }
-
- public File createDirectory(String folder) {
- if (this.selected==null) {
- findDirectory();
- }
- File f = new File(selected, folder);
- if (f.exists() && f.isDirectory()) {
- return f;
- }
- if (!f.mkdirs()) {
- throw new RuntimeException("Unable to create directory:"+f);
- }
- return f;
-
- }
-
- public synchronized File findDirectory() {
- if (lock!=null) {
- return selected;
- }
- for (int i = 0; i < 100; i++) {
- File dir = new File(parent, "node" + i);
- System.err.println("Dir Name: " + dir.getAbsolutePath());
- if (!dir.exists()) {
- if (!dir.mkdirs()) {
- throw new RuntimeException("Unable to create directory for persistence:" + dir);
- }
- }
- if (checkDirectory(dir)) {
- System.err.println("Directory for persistence is:"+dir);
- selected = dir.getAbsoluteFile();
- selected.deleteOnExit();
- return selected;
- }
- }
- throw new RuntimeException("Unable to locate persistence directory.");
- }
-
- public boolean checkDirectory(File f) {
- return lock(f);
- }
-
- private boolean lock(File directory) {
- String name = "node.lock";
- File lockfile = new File(directory, name);
- lockfile.deleteOnExit();
- try {
- RandomAccessFile rf = new RandomAccessFile(lockfile, "rw");
- FileChannel fc = rf.getChannel();
- System.err.println("Attempting to lock:"+lockfile);
- lock = fc.tryLock();
- System.err.println("Locked:"+lock);
- if (lock!=null) {
- //gemfire doesn't clean up
- File f = new File(directory,"BACKUPdefault.if");
- boolean result = true;
- if (f.exists()) result = f.delete();
- f = new File(directory,"DRLK_IFdefault.lk");
- if (result && f.exists()) result = f.delete();
- return result;
- }
- } catch (IOException x) {
-
- } catch (OverlappingFileLockException e) {
- // File is already locked in this thread or virtual machine
- }
- return false;
- }
-
- public File getSelected() {
- return selected;
- }
-
- public void setSelected(File selected) {
- this.selected = selected;
- }
-
-}
View
24 spring-gemfire-examples-common/src/main/resources/cache-locator-context.xml
@@ -1,24 +0,0 @@
-<?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:context="http://www.springframework.org/schema/context" xmlns:util="http://www.springframework.org/schema/util"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/gemfire classpath:org/springframework/data/gemfire/config/spring-gemfire-1.1.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-3.0.xsd">
-
- <context:property-placeholder system-properties-mode="OVERRIDE" location="classpath:/common.properties" />
-
- <!-- create a unique directory base directory for this node -->
- <bean id="findDiskDir" class="com.gemstone.gemfire.util.PersistenceDirectoryGenerator" destroy-method="unlock" init-method="findDirectory">
- <property name="parent" value="${data.location}/locator" />
- </bean>
-
- <bean id="locator" class="com.gemstone.gemfire.util.LocatorBean" init-method="startLocator" destroy-method="stopLocator">
- <property name="port1" value="${locator.port1}" />
- <property name="port2" value="${locator.port2}" />
- <property name="throwOnBindFailure" value="false" />
- <property name="state" value="#{findDiskDir.getSelected()}/locator.state" />
- <property name="log" value="#{findDiskDir.getSelected()}/locator.log" />
- <property name="locators" value="${locator.host1}[${locator.port1}],${locator.host2}[${locator.port2}]"/>
- </bean>
-</beans>
Please sign in to comment.
Something went wrong with that request. Please try again.