Skip to content
Permalink
Browse files
IGNITE-13769 Adds examples of using thin client with spring-data-ext. (
  • Loading branch information
ololo3000 committed Mar 29, 2021
1 parent 1844edc commit 484a26f97ca7075ee314ce64fc45d4d812b816b1
Showing 2 changed files with 89 additions and 5 deletions.
@@ -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));
}
}
@@ -22,6 +22,10 @@
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 static void main(String[] args) {

/**
* 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();

0 comments on commit 484a26f

Please sign in to comment.