You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ignite.apache.org by al...@apache.org on 2021/02/15 08:39:39 UTC
[ignite] branch master updated: IGNITE-13902 Add Ignite thin client
Spring bean - Fixes #8752.
This is an automated email from the ASF dual-hosted git repository.
alexpl pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/ignite.git
The following commit(s) were added to refs/heads/master by this push:
new 5d7b59c IGNITE-13902 Add Ignite thin client Spring bean - Fixes #8752.
5d7b59c is described below
commit 5d7b59cb3e3c4d741117053527b7d0f7b1c5fa41
Author: Mikhail Petrov <pm...@gmail.com>
AuthorDate: Mon Feb 15 11:32:04 2021 +0300
IGNITE-13902 Add Ignite thin client Spring bean - Fixes #8752.
Signed-off-by: Aleksey Plekhanov <pl...@gmail.com>
---
.../org/apache/ignite/IgniteClientSpringBean.java | 258 +++++++++++++++++++++
.../java/org/apache/ignite/IgniteSpringBean.java | 2 +
.../internal/IgniteClientSpringBeanTest.java | 61 +++++
.../ignite/internal/ignite-client-spring-bean.xml | 40 ++++
.../ignite/testsuites/IgniteSpringTestSuite.java | 2 +
5 files changed, 363 insertions(+)
diff --git a/modules/spring/src/main/java/org/apache/ignite/IgniteClientSpringBean.java b/modules/spring/src/main/java/org/apache/ignite/IgniteClientSpringBean.java
new file mode 100644
index 0000000..20b46b9
--- /dev/null
+++ b/modules/spring/src/main/java/org/apache/ignite/IgniteClientSpringBean.java
@@ -0,0 +1,258 @@
+/*
+ * 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;
+
+import java.util.Collection;
+import java.util.List;
+import org.apache.ignite.cache.query.FieldsQueryCursor;
+import org.apache.ignite.cache.query.SqlFieldsQuery;
+import org.apache.ignite.client.ClientCache;
+import org.apache.ignite.client.ClientCacheConfiguration;
+import org.apache.ignite.client.ClientCluster;
+import org.apache.ignite.client.ClientClusterGroup;
+import org.apache.ignite.client.ClientCompute;
+import org.apache.ignite.client.ClientException;
+import org.apache.ignite.client.ClientServices;
+import org.apache.ignite.client.ClientTransactions;
+import org.apache.ignite.client.IgniteClient;
+import org.apache.ignite.client.IgniteClientFuture;
+import org.apache.ignite.configuration.ClientConfiguration;
+import org.springframework.beans.factory.DisposableBean;
+import org.springframework.context.SmartLifecycle;
+import org.springframework.context.event.ContextRefreshedEvent;
+
+/**
+ * Represents Ignite client Spring bean that provides the ability to automatically start Ignite client during
+ * Spring Context initialization. It requires {@link ClientConfiguration} to be set before bean use
+ * (see {@link #setClientConfiguration(ClientConfiguration)}}).
+ *
+ * A note should be taken that Ignite client instance is started after all other
+ * Spring beans have been initialized and right before Spring context is refreshed.
+ * That implies that it's not valid to reference {@link IgniteClientSpringBean} from
+ * any kind of Spring bean init methods like {@link javax.annotation.PostConstruct}.
+ * If it's required to reference {@link IgniteClientSpringBean} for other bean
+ * initialization purposes, it should be done from a {@link ContextRefreshedEvent}
+ * listener method declared in that bean.
+ *
+ * <h3 class="header">Spring XML Configuration Example</h3>
+ * <pre name="code" class="xml">
+ * <bean id="igniteClient" class="org.apache.ignite.IgniteClientSpringBean">
+ * <property name="clientConfiguration">
+ * <bean class="org.apache.ignite.configuration.ClientConfiguration">
+ * <property name="addresses">
+ * <list>
+ * <value>127.0.0.1:10800</value>
+ * </list>
+ * </property>
+ * </bean>
+ * </property>
+ * </bean>
+ * </pre>
+ */
+public class IgniteClientSpringBean implements IgniteClient, SmartLifecycle {
+ /** Default Ignite client {@link SmartLifecycle} phase. */
+ public static final int DFLT_IGNITE_CLI_LIFECYCLE_PHASE = 0;
+
+ /** Whether this component is initialized and running. */
+ private volatile boolean isRunning;
+
+ /** Ignite client instance to which operations are delegated. */
+ private IgniteClient cli;
+
+ /** Ignite client configuration. */
+ private ClientConfiguration cfg;
+
+ /** Ignite client {@link SmartLifecycle} phase. */
+ private int phase = DFLT_IGNITE_CLI_LIFECYCLE_PHASE;
+
+ /** {@inheritDoc} */
+ @Override public boolean isAutoStartup() {
+ return true;
+ }
+
+ /** {@inheritDoc} */
+ @Override public void stop(Runnable callback) {
+ stop();
+
+ callback.run();
+ }
+
+ /** {@inheritDoc} */
+ @Override public void stop() {
+ isRunning = false;
+ }
+
+ /** {@inheritDoc} */
+ @Override public boolean isRunning() {
+ return isRunning;
+ }
+
+ /** {@inheritDoc} */
+ @Override public void start() {
+ if (cfg == null)
+ throw new IllegalArgumentException("Ignite client configuration must be set.");
+
+ cli = Ignition.startClient(cfg);
+
+ isRunning = true;
+ }
+
+ /** {@inheritDoc} */
+ @Override public int getPhase() {
+ return phase;
+ }
+
+ /** {@inheritDoc} */
+ @Override public <K, V> ClientCache<K, V> getOrCreateCache(String name) throws ClientException {
+ return cli.getOrCreateCache(name);
+ }
+
+ /** {@inheritDoc} */
+ @Override public <K, V> IgniteClientFuture<ClientCache<K, V>> getOrCreateCacheAsync(String name)
+ throws ClientException
+ {
+ return cli.getOrCreateCacheAsync(name);
+ }
+
+ /** {@inheritDoc} */
+ @Override public <K, V> ClientCache<K, V> getOrCreateCache(ClientCacheConfiguration cfg) throws ClientException {
+ return cli.getOrCreateCache(cfg);
+ }
+
+ /** {@inheritDoc} */
+ @Override public <K, V> IgniteClientFuture<ClientCache<K, V>> getOrCreateCacheAsync(ClientCacheConfiguration cfg)
+ throws ClientException
+ {
+ return cli.getOrCreateCacheAsync(cfg);
+ }
+
+ /** {@inheritDoc} */
+ @Override public <K, V> ClientCache<K, V> cache(String name) {
+ return cli.cache(name);
+ }
+
+ /** {@inheritDoc} */
+ @Override public Collection<String> cacheNames() throws ClientException {
+ return cli.cacheNames();
+ }
+
+ /** {@inheritDoc} */
+ @Override public IgniteClientFuture<Collection<String>> cacheNamesAsync() throws ClientException {
+ return cli.cacheNamesAsync();
+ }
+
+ /** {@inheritDoc} */
+ @Override public void destroyCache(String name) throws ClientException {
+ cli.destroyCache(name);
+ }
+
+ /** {@inheritDoc} */
+ @Override public IgniteClientFuture<Void> destroyCacheAsync(String name) throws ClientException {
+ return cli.destroyCacheAsync(name);
+ }
+
+ /** {@inheritDoc} */
+ @Override public <K, V> ClientCache<K, V> createCache(String name) throws ClientException {
+ return cli.createCache(name);
+ }
+
+ /** {@inheritDoc} */
+ @Override public <K, V> IgniteClientFuture<ClientCache<K, V>> createCacheAsync(String name) throws ClientException {
+ return cli.createCacheAsync(name);
+ }
+
+ /** {@inheritDoc} */
+ @Override public <K, V> ClientCache<K, V> createCache(ClientCacheConfiguration cfg) throws ClientException {
+ return cli.createCache(cfg);
+ }
+
+ /** {@inheritDoc} */
+ @Override public <K, V> IgniteClientFuture<ClientCache<K, V>> createCacheAsync(ClientCacheConfiguration cfg)
+ throws ClientException
+ {
+ return cli.createCacheAsync(cfg);
+ }
+
+ /** {@inheritDoc} */
+ @Override public IgniteBinary binary() {
+ return cli.binary();
+ }
+
+ /** {@inheritDoc} */
+ @Override public FieldsQueryCursor<List<?>> query(SqlFieldsQuery qry) {
+ return cli.query(qry);
+ }
+
+ /** {@inheritDoc} */
+ @Override public ClientTransactions transactions() {
+ return cli.transactions();
+ }
+
+ /** {@inheritDoc} */
+ @Override public ClientCompute compute() {
+ return cli.compute();
+ }
+
+ /** {@inheritDoc} */
+ @Override public ClientCompute compute(ClientClusterGroup grp) {
+ return cli.compute(grp);
+ }
+
+ /** {@inheritDoc} */
+ @Override public ClientCluster cluster() {
+ return cli.cluster();
+ }
+
+ /** {@inheritDoc} */
+ @Override public ClientServices services() {
+ return cli.services();
+ }
+
+ /** {@inheritDoc} */
+ @Override public ClientServices services(ClientClusterGroup grp) {
+ return cli.services(grp);
+ }
+
+ /** {@inheritDoc} */
+ @Override public void close() throws Exception {
+ cli.close();
+ }
+
+ /** Sets Ignite client configuration. */
+ public IgniteClientSpringBean setClientConfiguration(ClientConfiguration cfg) {
+ this.cfg = cfg;
+
+ return this;
+ }
+
+ /** Gets Ignite client configuration. */
+ public ClientConfiguration getClientConfiguration() {
+ return cfg;
+ }
+
+ /**
+ * Sets {@link SmartLifecycle} phase during which the current bean will be initialized. Note, underlying Ignite
+ * client will be closed during handling of {@link DisposableBean} since {@link IgniteClient}
+ * implements {@link AutoCloseable} interface.
+ */
+ public IgniteClientSpringBean setPhase(int phase) {
+ this.phase = phase;
+
+ return this;
+ }
+}
diff --git a/modules/spring/src/main/java/org/apache/ignite/IgniteSpringBean.java b/modules/spring/src/main/java/org/apache/ignite/IgniteSpringBean.java
index e9a3c5e..eedbef4 100644
--- a/modules/spring/src/main/java/org/apache/ignite/IgniteSpringBean.java
+++ b/modules/spring/src/main/java/org/apache/ignite/IgniteSpringBean.java
@@ -507,6 +507,7 @@ public class IgniteSpringBean implements Ignite, DisposableBean, SmartInitializi
return g.atomicLong(name, initVal, create);
}
+ /** {@inheritDoc} */
@Override public IgniteAtomicLong atomicLong(String name, AtomicConfiguration cfg, long initVal,
boolean create) throws IgniteException {
checkIgnite();
@@ -543,6 +544,7 @@ public class IgniteSpringBean implements Ignite, DisposableBean, SmartInitializi
return g.atomicStamped(name, initVal, initStamp, create);
}
+ /** {@inheritDoc} */
@Override public <T, S> IgniteAtomicStamped<T, S> atomicStamped(String name, AtomicConfiguration cfg,
@Nullable T initVal, @Nullable S initStamp, boolean create) throws IgniteException {
checkIgnite();
diff --git a/modules/spring/src/test/java/org/apache/ignite/internal/IgniteClientSpringBeanTest.java b/modules/spring/src/test/java/org/apache/ignite/internal/IgniteClientSpringBeanTest.java
new file mode 100644
index 0000000..ffb4c66
--- /dev/null
+++ b/modules/spring/src/test/java/org/apache/ignite/internal/IgniteClientSpringBeanTest.java
@@ -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.
+ */
+
+package org.apache.ignite.internal;
+
+import org.apache.ignite.IgniteClientSpringBean;
+import org.apache.ignite.client.IgniteClient;
+import org.apache.ignite.configuration.ClientConfiguration;
+import org.apache.ignite.testframework.GridTestUtils;
+import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest;
+import org.junit.Test;
+import org.springframework.context.support.AbstractApplicationContext;
+import org.springframework.context.support.ClassPathXmlApplicationContext;
+
+/** Tests {@link IgniteClientSpringBean} declaration and configuration. */
+public class IgniteClientSpringBeanTest extends GridCommonAbstractTest {
+ /** Tests {@link IgniteClientSpringBean} declaration through Spring XML configuration. */
+ @Test
+ public void testXmlConfiguration() {
+ try (
+ AbstractApplicationContext ctx = new ClassPathXmlApplicationContext(
+ "org/apache/ignite/internal/ignite-client-spring-bean.xml")
+ ) {
+ IgniteClient cli = ctx.getBean(IgniteClient.class);
+
+ assertNotNull(cli);
+ assertEquals(1, cli.cluster().nodes().size());
+ }
+ }
+
+ /** Tests {@link IgniteClientSpringBean} behaviour in case {@link ClientConfiguration} is not specified. */
+ @Test
+ public void testOmittedClientConfigurationFailure() {
+ GridTestUtils.assertThrowsAnyCause(
+ log,
+ () -> {
+ IgniteClientSpringBean igniteCliSpringBean = new IgniteClientSpringBean();
+
+ igniteCliSpringBean.start();
+
+ return null;
+ },
+ IllegalArgumentException.class,
+ "Ignite client configuration must be set."
+ );
+ }
+}
diff --git a/modules/spring/src/test/java/org/apache/ignite/internal/ignite-client-spring-bean.xml b/modules/spring/src/test/java/org/apache/ignite/internal/ignite-client-spring-bean.xml
new file mode 100644
index 0000000..cabbabc
--- /dev/null
+++ b/modules/spring/src/test/java/org/apache/ignite/internal/ignite-client-spring-bean.xml
@@ -0,0 +1,40 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<!--
+ 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.
+-->
+
+<beans xmlns="http://www.springframework.org/schema/beans"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
+ <bean id="igniteClient" class="org.apache.ignite.IgniteClientSpringBean">
+ <property name="clientConfiguration">
+ <bean class="org.apache.ignite.configuration.ClientConfiguration">
+ <property name="addresses">
+ <list>
+ <value>127.0.0.1:10800</value>
+ </list>
+ </property>
+ </bean>
+ </property>
+ </bean>
+
+ <!--
+ The following bean is declared after "igniteClient" bean to test that Ignite node and Ignite client beans are
+ initialized in correct order regardless of the declaration order.
+ -->
+ <bean id="ignite" class="org.apache.ignite.IgniteSpringBean"/>
+</beans>
diff --git a/modules/spring/src/test/java/org/apache/ignite/testsuites/IgniteSpringTestSuite.java b/modules/spring/src/test/java/org/apache/ignite/testsuites/IgniteSpringTestSuite.java
index e628669..7582afc 100644
--- a/modules/spring/src/test/java/org/apache/ignite/testsuites/IgniteSpringTestSuite.java
+++ b/modules/spring/src/test/java/org/apache/ignite/testsuites/IgniteSpringTestSuite.java
@@ -32,6 +32,7 @@ import org.apache.ignite.encryption.SpringEncryptedCacheRestartClientTest;
import org.apache.ignite.encryption.SpringEncryptedCacheRestartTest;
import org.apache.ignite.internal.GridFactorySelfTest;
import org.apache.ignite.internal.GridSpringBeanSerializationSelfTest;
+import org.apache.ignite.internal.IgniteClientSpringBeanTest;
import org.apache.ignite.internal.IgniteDynamicCacheConfigTest;
import org.apache.ignite.internal.IgniteSpringBeanTest;
import org.apache.ignite.internal.processors.cache.distributed.dht.GridCacheDhtMultiBackupTest;
@@ -52,6 +53,7 @@ import org.junit.runners.Suite;
@Suite.SuiteClasses({
GridSpringBeanSerializationSelfTest.class,
IgniteSpringBeanTest.class,
+ IgniteClientSpringBeanTest.class,
GridFactorySelfTest.class,
IgniteResourceSelfTestSuite.class,