You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ignite.apache.org by na...@apache.org on 2021/03/29 22:46:34 UTC

[ignite-extensions] branch master updated: IGNITE-13769 Adds examples of using thin client with spring-data-ext. (#51)

This is an automated email from the ASF dual-hosted git repository.

namelchev pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/ignite-extensions.git


The following commit(s) were added to refs/heads/master by this push:
     new 484a26f  IGNITE-13769 Adds examples of using thin client with spring-data-ext. (#51)
484a26f is described below

commit 484a26f97ca7075ee314ce64fc45d4d812b816b1
Author: Mikhail Petrov <32...@users.noreply.github.com>
AuthorDate: Tue Mar 30 01:45:38 2021 +0300

    IGNITE-13769 Adds examples of using thin client with spring-data-ext. (#51)
---
 ...IgniteClientSpringApplicationConfiguration.java | 53 ++++++++++++++++++++++
 .../springdata20/examples/SpringDataExample.java   | 41 +++++++++++++++--
 2 files changed, 89 insertions(+), 5 deletions(-)

diff --git a/modules/spring-data-2.0-ext/examples/main/java/org/apache/ignite/springdata20/examples/IgniteClientSpringApplicationConfiguration.java b/modules/spring-data-2.0-ext/examples/main/java/org/apache/ignite/springdata20/examples/IgniteClientSpringApplicationConfiguration.java
new file mode 100644
index 0000000..e04a50a
--- /dev/null
+++ b/modules/spring-data-2.0-ext/examples/main/java/org/apache/ignite/springdata20/examples/IgniteClientSpringApplicationConfiguration.java
@@ -0,0 +1,53 @@
+/*
+ * 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.ignite.springdata20.examples;
+
+import org.apache.ignite.Ignition;
+import org.apache.ignite.client.IgniteClient;
+import org.apache.ignite.configuration.ClientConfiguration;
+import org.apache.ignite.springdata20.repository.config.EnableIgniteRepositories;
+import org.apache.ignite.springdata20.repository.config.RepositoryConfig;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+import static org.apache.ignite.configuration.ClientConnectorConfiguration.DFLT_PORT;
+
+/**
+ * Example of Spring application configuration that represents beans required to configure Spring Data repository access
+ * to an Ignite cluster through the thin client.
+ *
+ * Note that both Ignite thin client and Ignite node approaches of Ignite cluster access configuration uses the same API.
+ * Ignite Spring Data integration automatically recognizes the type of provided bean and use the appropriate
+ * cluster connection.
+ *
+ * @see SpringApplicationConfiguration
+ */
+@Configuration
+@EnableIgniteRepositories
+public class IgniteClientSpringApplicationConfiguration {
+    /**
+     * Creates Apache Ignite thin client instance bean which will be used for accessing the Ignite cluster.
+     * Note, that the name of the current bean must match value of {@link RepositoryConfig#igniteInstance}
+     * property that {@link PersonRepository} is marked with. In this particular case, the default value of
+     * {@link RepositoryConfig#igniteInstance} property is used.
+     */
+    @Bean
+    public IgniteClient igniteInstance() {
+        return Ignition.startClient(new ClientConfiguration().setAddresses("127.0.0.1:" + DFLT_PORT));
+    }
+}
diff --git a/modules/spring-data-2.0-ext/examples/main/java/org/apache/ignite/springdata20/examples/SpringDataExample.java b/modules/spring-data-2.0-ext/examples/main/java/org/apache/ignite/springdata20/examples/SpringDataExample.java
index a3d5547..325dd54 100644
--- a/modules/spring-data-2.0-ext/examples/main/java/org/apache/ignite/springdata20/examples/SpringDataExample.java
+++ b/modules/spring-data-2.0-ext/examples/main/java/org/apache/ignite/springdata20/examples/SpringDataExample.java
@@ -22,6 +22,10 @@ import java.util.Iterator;
 import java.util.List;
 import java.util.TreeMap;
 import javax.cache.Cache;
+import org.apache.ignite.Ignite;
+import org.apache.ignite.Ignition;
+import org.apache.ignite.configuration.CacheConfiguration;
+import org.apache.ignite.configuration.IgniteConfiguration;
 import org.apache.ignite.springdata20.examples.model.Person;
 import org.springframework.context.annotation.AnnotationConfigApplicationContext;
 import org.springframework.data.domain.PageRequest;
@@ -40,12 +44,37 @@ public class SpringDataExample {
     private static PersonRepository repo;
 
     /**
-     * Executes the example.
+     * Execute examples involving both approaches to configure Spring Data repository access to an Ignite cluster:
+     *      through Ignite thin client and through Ignite node.
      * @param args Command line arguments, none required.
      */
     public static void main(String[] args) {
-        // Initializing Spring Data context and Ignite repository.
-        igniteSpringDataInit();
+        try (Ignite ignored = startIgniteNode()) {
+            // Ignite node instance is used to configure access to the Ignite cluster.
+            doSpringDataExample(SpringApplicationConfiguration.class);
+
+            // Ignite thin client instance is used to configure access to the Ignite cluster.
+            doSpringDataExample(IgniteClientSpringApplicationConfiguration.class);
+        }
+    }
+
+    /** Starts an Ignite node that simulates an Ignite cluster to which Spring Data repository will perform access. */
+    private static Ignite startIgniteNode() {
+        IgniteConfiguration cfg = new IgniteConfiguration()
+            .setPeerClassLoadingEnabled(true)
+            .setCacheConfiguration(new CacheConfiguration<Long, Person>("PersonCache")
+                .setIndexedTypes(Long.class, Person.class));
+
+        return Ignition.start(cfg);
+    }
+
+    /**
+     * Performs basic Spring Data repository operation.
+     *
+     * @param springAppCfg Class of Spring application configuration that will be used for Spring context initialization.
+     */
+    private static void doSpringDataExample(Class<?> springAppCfg) {
+        igniteSpringDataInit(springAppCfg);
 
         populateRepository();
 
@@ -65,12 +94,14 @@ public class SpringDataExample {
 
     /**
      * Initializes Spring Data and Ignite repositories.
+     *
+     * @param springAppCfg Class of Spring application configuration that will be used for Spring context initialization.
      */
-    private static void igniteSpringDataInit() {
+    private static void igniteSpringDataInit(Class<?> springAppCfg) {
         ctx = new AnnotationConfigApplicationContext();
 
         // Explicitly registering Spring configuration.
-        ctx.register(SpringApplicationConfiguration.class);
+        ctx.register(springAppCfg);
 
         ctx.refresh();