[ 
https://issues.apache.org/jira/browse/GEODE-4039?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16310480#comment-16310480
 ] 

ASF GitHub Bot commented on GEODE-4039:
---------------------------------------

PivotalSarge closed pull request #40: GEODE-4039: Add serialization example.
URL: https://github.com/apache/geode-examples/pull/40
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/README.md b/README.md
index e77194b..42da51d 100644
--- a/README.md
+++ b/README.md
@@ -64,10 +64,9 @@ tutorial.
 
 ### Intermediate
 
-*  PDX & Serialization
+*  [Serialization](serialization/README.md)
 *  Lucene Indexing
 *  OQL Indexing
-*  Functions
 *  [Cache Loader](loader/README.md)
 *  [Cache Writer](writer/README.md)
 *  [Cache Listeners](listener/README.md)
diff --git a/serialization/README.md b/serialization/README.md
new file mode 100644
index 0000000..3544edd
--- /dev/null
+++ b/serialization/README.md
@@ -0,0 +1,61 @@
+<!--
+Licensed to the Apache Software Foundation (ASF) under one or more
+contributor license agreements.  See the NOTICE file distributed with
+this work for additional information regarding copyright ownership.
+The ASF licenses this file to You 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.
+-->
+
+# Geode serialization example
+
+This is a simple example that demonstrates auto-serialization of objects of 
classes that do *not*
+use Java serialization, PDX serialization, or data serialization. 
Reflection-based auto-serialization
+uses Java reflection to put objects into and get objects out of regions 
without relying on Java,
+PDX, or data serialization.
+
+For an object to be stored in a region as either key or value, a copy of the 
object must be sent to
+the server. Typically Java objects are sent across the network by serializing 
them into bytes
+sent across a socket and then deserializing them from the bytes received from 
the socket on the
+other side. This requires those objects' classes to implement a particular 
interface that defines
+how that serialization and deserialization is accomplished, e.g., 
`java.io.Serializable`,
+`org.apache.geode.DataSerializable`, or `org.apache.geode.pdx.PdxSerializable`.
+
+In order to perform queries on objects within the server, the fields of the 
objects must be
+individually accessible. One way to accomplish this is to deserialize the 
objects into instances
+within the server. This requires the relevant classes to be in the server's 
class path. Another way
+to accomplish this is by using the field-addressable format, PDX.
+
+The reflection-based auto-serializer uses Java's reflection and introspection 
to construct a PDX
+instance. This obviates modifying classes to implement a serialization 
interface. This also allows
+the objects to be queried by way of PDX without the classes being preset on 
the server's class
+path.
+
+This example assumes you have installed Java and Geode.
+
+## Steps
+
+1. From the `geode-examples/serialization` directory, build the example and
+   run unit tests
+
+        $ ../gradlew build
+
+2. Next start the locator and two servers
+
+        $ gfsh run --file=scripts/start.gfsh
+
+3. Run the example to create entries in the region
+
+        $ ../gradlew run
+
+4. Shut down the system:
+
+        $ gfsh run --file=scripts/stop.gfsh
diff --git a/serialization/scripts/start.gfsh b/serialization/scripts/start.gfsh
new file mode 100644
index 0000000..1b3af1f
--- /dev/null
+++ b/serialization/scripts/start.gfsh
@@ -0,0 +1,24 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You 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.
+#
+start locator --name=locator --bind-address=127.0.0.1
+
+start server --name=server1 --locators=127.0.0.1[10334] --server-port=0
+start server --name=server2 --locators=127.0.0.1[10334] --server-port=0
+list members
+
+create region --name=example-region --type=REPLICATE --skip-if-exists=true
+describe region --name=example-region
diff --git a/serialization/scripts/stop.gfsh b/serialization/scripts/stop.gfsh
new file mode 100644
index 0000000..5dd4874
--- /dev/null
+++ b/serialization/scripts/stop.gfsh
@@ -0,0 +1,19 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You 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.
+#
+connect --locator=127.0.0.1[10334]
+
+shutdown --include-locators=true
\ No newline at end of file
diff --git 
a/serialization/src/main/java/org/apache/geode_examples/serialization/Country.java
 
b/serialization/src/main/java/org/apache/geode_examples/serialization/Country.java
new file mode 100644
index 0000000..39b8e8a
--- /dev/null
+++ 
b/serialization/src/main/java/org/apache/geode_examples/serialization/Country.java
@@ -0,0 +1,118 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more 
contributor license
+ * agreements. See the NOTICE file distributed with this work for additional 
information regarding
+ * copyright ownership. The ASF licenses this file to You 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.apache.geode_examples.serialization;
+
+/**
+ * <strong>Explicitly</strong> not serializable by java.io.Serializable,
+ * org.apache.geode.DataSerializable, or org.apache.geode.pdx.PdxSerializable.
+ */
+public class Country {
+  protected String name;
+  protected String capitol;
+  protected String language;
+  protected String currency;
+  protected int population;
+
+  public Country() {
+    this("", "", "", "", 0);
+  }
+
+  protected Country(String name, String capitol, String language, String 
currency, int population) {
+    this.name = name;
+    this.capitol = capitol;
+    this.language = language;
+    this.currency = currency;
+    this.population = population;
+  }
+
+  public String getName() {
+    return name;
+  }
+
+  public String getCapitol() {
+    return capitol;
+  }
+
+  public void setCapitol(String capitol) {
+    this.capitol = capitol;
+  }
+
+  public String getLanguage() {
+    return language;
+  }
+
+  public void setLanguage(String language) {
+    this.language = language;
+  }
+
+  public String getCurrency() {
+    return currency;
+  }
+
+  public void setCurrency(String currency) {
+    this.currency = currency;
+  }
+
+  public int getPopulation() {
+    return population;
+  }
+
+  public void setPopulation(int population) {
+    this.population = population;
+  }
+
+  public String toString() {
+    StringBuilder builder = new StringBuilder();
+    if (name != null && !name.isEmpty()) {
+      builder.append(name);
+      builder.append(" (");
+
+      if (capitol != null && !capitol.isEmpty()) {
+        if (0 < builder.length() && '(' != builder.charAt(builder.length() - 
1)) {
+          builder.append(", ");
+        }
+        builder.append("Capitol: ");
+        builder.append(capitol);
+      }
+
+      if (language != null && !language.isEmpty()) {
+        if (0 < builder.length() && '(' != builder.charAt(builder.length() - 
1)) {
+          builder.append(", ");
+        }
+        builder.append("Language: ");
+        builder.append(language);
+      }
+
+      if (currency != null && !currency.isEmpty()) {
+        if (0 < builder.length() && '(' != builder.charAt(builder.length() - 
1)) {
+          builder.append(", ");
+        }
+        builder.append("Currency: ");
+        builder.append(currency);
+      }
+
+      if (0 < population) {
+        if (0 < builder.length() && '(' != builder.charAt(builder.length() - 
1)) {
+          builder.append(", ");
+        }
+        builder.append("Population: ");
+        builder.append(population);
+      }
+
+      builder.append(")");
+    }
+    return builder.toString();
+  }
+}
diff --git 
a/serialization/src/main/java/org/apache/geode_examples/serialization/Example.java
 
b/serialization/src/main/java/org/apache/geode_examples/serialization/Example.java
new file mode 100644
index 0000000..7c4e27e
--- /dev/null
+++ 
b/serialization/src/main/java/org/apache/geode_examples/serialization/Example.java
@@ -0,0 +1,107 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more 
contributor license
+ * agreements. See the NOTICE file distributed with this work for additional 
information regarding
+ * copyright ownership. The ASF licenses this file to You 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.apache.geode_examples.serialization;
+
+import org.apache.geode.cache.Region;
+import org.apache.geode.cache.client.ClientCache;
+import org.apache.geode.cache.client.ClientCacheFactory;
+import org.apache.geode.cache.client.ClientRegionShortcut;
+import org.apache.geode.pdx.ReflectionBasedAutoSerializer;
+
+import java.util.HashSet;
+import java.util.Set;
+
+public class Example {
+  public static final String ARENDELLE = "Arendelle";
+  public static final String BORDURIA = "Borduria";
+  public static final String CASCADIA = "Cascadia";
+  public static final String ELBONIA = "Elbonia";
+  public static final String FLORIN = "Florin";
+  public static final String GRAUSTARK = "Graustark";
+  public static final String LATVERIA = "Latveria";
+  public static final String MARKOVIA = "Markovia";
+  public static final String PARADOR = "Parador";
+  public static final String SIERRA_GORDO = "Sierra Gordo";
+  final Region<String, Country> region;
+
+  public Example(Region<String, Country> region) {
+    this.region = region;
+  }
+
+  public static void main(String[] args) {
+    // connect to the locator using default port 10334
+    ClientCache cache = new ClientCacheFactory().addPoolLocator("127.0.0.1", 
10334)
+        .set("log-level", "WARN")
+        .setPdxSerializer(
+            new 
ReflectionBasedAutoSerializer("org.apache.geode_examples.serialization.Country"))
+        .create();
+
+    // create a local region that matches the server region
+    Region<String, Country> region =
+        cache.<String, 
Country>createClientRegionFactory(ClientRegionShortcut.PROXY)
+            .create("example-region");
+
+    Example example = new Example(region);
+    example.insertValues();
+    example.printValues(example.getKeys());
+
+    cache.close();
+  }
+
+  Country create(String name) {
+    return create(name, name + " City");
+  }
+
+  Country create(String name, String capitol) {
+    return create(name, capitol, "");
+  }
+
+  Country create(String name, String capitol, String language) {
+    return create(name, capitol, language, "", 0);
+  }
+
+  Country create(String name, String capitol, String language, String 
currency, int population) {
+    return new Country(name, capitol, language, currency, population);
+  }
+
+  Set<String> getKeys() {
+    return new HashSet<>(region.keySetOnServer());
+  }
+
+  void insertValues() {
+    insertValue(create(ARENDELLE, "Arendelle City", "Arendellii", "Arendelle 
Krona", 76573));
+    insertValue(create(BORDURIA, "SzohĂ´d", "Bordurian", "Bordurian Dinar", 
1000000));
+    insertValue(create(CASCADIA, "Portland", "Pacific Northwest English", 
"United States Dollar",
+        16029520));
+    insertValue(create(ELBONIA));
+    insertValue(create(FLORIN));
+    insertValue(create(GRAUSTARK, "Edelweiss"));
+    insertValue(create(LATVERIA, "Doomstadt", "Latverian", "Latverian Franc", 
500000));
+    insertValue(create(MARKOVIA, "Markovburg", "German"));
+    insertValue(create(PARADOR));
+    insertValue(create(SIERRA_GORDO, "Rio Lindo", "Spanish"));
+  }
+
+  void insertValue(Country country) {
+    region.put(country.getName(), country);
+  }
+
+  void printValues(Set<String> keys) {
+    for (String key : keys) {
+      Country country = region.get(key);
+      System.out.println(key + ": " + country);
+    }
+  }
+}
diff --git a/settings.gradle b/settings.gradle
index e43f7f8..6bb89ec 100644
--- a/settings.gradle
+++ b/settings.gradle
@@ -29,3 +29,4 @@ include 'writer'
 include 'listener'
 include 'async'
 include 'luceneSpatial'
+include 'serialization'


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


> Create new geode-example about serialization
> --------------------------------------------
>
>                 Key: GEODE-4039
>                 URL: https://issues.apache.org/jira/browse/GEODE-4039
>             Project: Geode
>          Issue Type: New Feature
>          Components: examples
>            Reporter: Michael Dodge
>            Assignee: Michael Dodge
>
> Create a new example that shows how to use Java serialization, PDX 
> serialization, and data serialization.



--
This message was sent by Atlassian JIRA
(v6.4.14#64029)

Reply via email to