IGNITE-6371 .NET: Thin client example This closes #2830
Project: http://git-wip-us.apache.org/repos/asf/ignite/repo Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/5ec744cf Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/5ec744cf Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/5ec744cf Branch: refs/heads/ignite-5932 Commit: 5ec744cf7fb0db0658f91176bf98dfe5ccb05be2 Parents: 0f3f7d2 Author: Pavel Tupitsyn <[email protected]> Authored: Fri Oct 13 12:25:34 2017 +0300 Committer: Pavel Tupitsyn <[email protected]> Committed: Fri Oct 13 12:25:34 2017 +0300 ---------------------------------------------------------------------- .../client/cache/ClientCacheRequest.java | 8 +- .../Examples/Example.cs | 6 +- .../Examples/ExamplesTest.cs | 42 ++++-- .../Apache.Ignite.Examples.csproj | 2 + .../examples/Apache.Ignite.Examples/App.config | 4 + .../ThinClient/ThinClientPutGetExample.cs | 93 ++++++++++++ .../ThinClient/ThinClientQueryExample.cs | 147 +++++++++++++++++++ 7 files changed, 283 insertions(+), 19 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ignite/blob/5ec744cf/modules/core/src/main/java/org/apache/ignite/internal/processors/platform/client/cache/ClientCacheRequest.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/platform/client/cache/ClientCacheRequest.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/platform/client/cache/ClientCacheRequest.java index 1aaa22c..b290a5b 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/processors/platform/client/cache/ClientCacheRequest.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/platform/client/cache/ClientCacheRequest.java @@ -19,7 +19,7 @@ package org.apache.ignite.internal.processors.platform.client.cache; import org.apache.ignite.IgniteCache; import org.apache.ignite.binary.BinaryRawReader; -import org.apache.ignite.internal.processors.cache.GridCacheContext; +import org.apache.ignite.internal.processors.cache.DynamicCacheDescriptor; import org.apache.ignite.internal.processors.platform.client.ClientConnectionContext; import org.apache.ignite.internal.processors.platform.client.ClientRequest; import org.apache.ignite.internal.processors.platform.client.ClientStatus; @@ -77,13 +77,13 @@ class ClientCacheRequest extends ClientRequest { * @return Cache. */ protected IgniteCache rawCache(ClientConnectionContext ctx) { - GridCacheContext<Object, Object> cacheCtx = ctx.kernalContext().cache().context().cacheContext(cacheId); + DynamicCacheDescriptor cacheDesc = ctx.kernalContext().cache().cacheDescriptor(cacheId); - if (cacheCtx == null) + if (cacheDesc == null) throw new IgniteClientException(ClientStatus.CACHE_DOES_NOT_EXIST, "Cache does not exist [cacheId= " + cacheId + "]", null); - String cacheName = cacheCtx.cache().name(); + String cacheName = cacheDesc.cacheName(); return ctx.kernalContext().grid().cache(cacheName); } http://git-wip-us.apache.org/repos/asf/ignite/blob/5ec744cf/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Examples/Example.cs ---------------------------------------------------------------------- diff --git a/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Examples/Example.cs b/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Examples/Example.cs index 9c3625f..4d1eeb6 100644 --- a/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Examples/Example.cs +++ b/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Examples/Example.cs @@ -40,7 +40,7 @@ namespace Apache.Ignite.Core.Tests.Examples public bool NeedsTestDll { get; private set; } /** Name */ - public string Name { get; private set; } + public Type ExampleType { get; private set; } /// <summary> /// Runs this example. @@ -92,7 +92,7 @@ namespace Apache.Ignite.Core.Tests.Examples ConfigPath = GetConfigPath(sourceCode), NeedsTestDll = sourceCode.Contains("-assembly="), _runAction = GetRunAction(type), - Name = type.Name + ExampleType = type }; } } @@ -119,7 +119,7 @@ namespace Apache.Ignite.Core.Tests.Examples public override string ToString() { // This will be displayed in TeamCity and R# test runner - return Name; + return ExampleType.Name; } } } \ No newline at end of file http://git-wip-us.apache.org/repos/asf/ignite/blob/5ec744cf/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Examples/ExamplesTest.cs ---------------------------------------------------------------------- diff --git a/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Examples/ExamplesTest.cs b/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Examples/ExamplesTest.cs index edc95fa..48e471f 100644 --- a/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Examples/ExamplesTest.cs +++ b/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Examples/ExamplesTest.cs @@ -20,9 +20,15 @@ namespace Apache.Ignite.Core.Tests.Examples extern alias ExamplesDll; using System; using System.Collections.Generic; + using System.Configuration; using System.IO; using System.Linq; using Apache.Ignite.Core.Tests.Process; + using Apache.Ignite.Examples.Compute; + using Apache.Ignite.Examples.Datagrid; + using Apache.Ignite.Examples.Messaging; + using Apache.Ignite.Examples.Misc; + using Apache.Ignite.Examples.ThinClient; using NUnit.Framework; /// <summary> @@ -35,21 +41,23 @@ namespace Apache.Ignite.Core.Tests.Examples private static readonly Example[] AllExamples = Example.GetExamples().ToArray(); /** */ - private static readonly string[] LocalOnlyExamples = + private static readonly Type[] LocalOnlyExamples = { - "LifecycleExample", "ClientReconnectExample", "MultiTieredCacheExample" + typeof(LifecycleExample), typeof(ClientReconnectExample), typeof(MultiTieredCacheExample) }; /** */ - private static readonly string[] RemoteOnlyExamples = + private static readonly Type[] RemoteOnlyExamples = { - "PeerAssemblyLoadingExample", "MessagingExample", "NearCacheExample" + typeof(PeerAssemblyLoadingExample), typeof(MessagingExample), typeof(NearCacheExample), + typeof(ThinClientPutGetExample), typeof(ThinClientQueryExample) }; /** */ - private static readonly string[] NoDllExamples = + private static readonly Type[] NoDllExamples = { - "BinaryModeExample", "NearCacheExample", "PeerAssemblyLoadingExample" + typeof(BinaryModeExample), typeof(NearCacheExample), typeof(PeerAssemblyLoadingExample), + typeof(ThinClientPutGetExample) }; /** Config file path. */ @@ -70,7 +78,7 @@ namespace Apache.Ignite.Core.Tests.Examples { StopRemoteNodes(); - if (LocalOnlyExamples.Contains(example.Name)) + if (LocalOnlyExamples.Contains(example.ExampleType)) { Assert.IsFalse(example.NeedsTestDll, "Local-only example should not mention test dll."); Assert.IsNull(example.ConfigPath, "Local-only example should not mention app.config path."); @@ -109,7 +117,7 @@ namespace Apache.Ignite.Core.Tests.Examples Assert.IsTrue(PathUtil.ExamplesAppConfigPath.EndsWith(example.ConfigPath, StringComparison.OrdinalIgnoreCase), "All examples should use the same app.config."); - Assert.IsTrue(example.NeedsTestDll || NoDllExamples.Contains(example.Name), + Assert.IsTrue(example.NeedsTestDll || NoDllExamples.Contains(example.ExampleType), "Examples that allow standalone nodes should mention test dll."); StartRemoteNodes(); @@ -133,8 +141,18 @@ namespace Apache.Ignite.Core.Tests.Examples // Stop it after topology check so we don't interfere with example. Ignition.ClientMode = false; - using (var ignite = Ignition.StartFromApplicationConfiguration( - "igniteConfiguration", _configPath)) + var fileMap = new ExeConfigurationFileMap { ExeConfigFilename = _configPath }; + var config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None); + var section = (IgniteConfigurationSection) config.GetSection("igniteConfiguration"); + + // Disable client connector so that temporary node does not occupy the port. + var cfg = new IgniteConfiguration(section.IgniteConfiguration) + { + ClientConnectorConfigurationEnabled = false, + CacheConfiguration = null + }; + + using (var ignite = Ignition.Start(cfg)) { var args = new List<string> { @@ -219,7 +237,7 @@ namespace Apache.Ignite.Core.Tests.Examples // ReSharper disable once MemberCanBeMadeStatic.Global public IEnumerable<Example> TestCasesLocal { - get { return AllExamples.Where(x => !RemoteOnlyExamples.Contains(x.Name)); } + get { return AllExamples.Where(x => !RemoteOnlyExamples.Contains(x.ExampleType)); } } /// <summary> @@ -231,7 +249,7 @@ namespace Apache.Ignite.Core.Tests.Examples { get { - return AllExamples.Where(x => !LocalOnlyExamples.Contains(x.Name)); + return AllExamples.Where(x => !LocalOnlyExamples.Contains(x.ExampleType)); } } } http://git-wip-us.apache.org/repos/asf/ignite/blob/5ec744cf/modules/platforms/dotnet/examples/Apache.Ignite.Examples/Apache.Ignite.Examples.csproj ---------------------------------------------------------------------- diff --git a/modules/platforms/dotnet/examples/Apache.Ignite.Examples/Apache.Ignite.Examples.csproj b/modules/platforms/dotnet/examples/Apache.Ignite.Examples/Apache.Ignite.Examples.csproj index 3206457..0aaa249 100644 --- a/modules/platforms/dotnet/examples/Apache.Ignite.Examples/Apache.Ignite.Examples.csproj +++ b/modules/platforms/dotnet/examples/Apache.Ignite.Examples/Apache.Ignite.Examples.csproj @@ -52,6 +52,8 @@ <Reference Include="System.Transactions" /> </ItemGroup> <ItemGroup> + <Compile Include="ThinClient\ThinClientPutGetExample.cs" /> + <Compile Include="ThinClient\ThinClientQueryExample.cs" /> <Compile Include="Compute\ClosureExample.cs" /> <Compile Include="Compute\TaskExample.cs" /> <Compile Include="Compute\PeerAssemblyLoadingExample.cs" /> http://git-wip-us.apache.org/repos/asf/ignite/blob/5ec744cf/modules/platforms/dotnet/examples/Apache.Ignite.Examples/App.config ---------------------------------------------------------------------- diff --git a/modules/platforms/dotnet/examples/Apache.Ignite.Examples/App.config b/modules/platforms/dotnet/examples/Apache.Ignite.Examples/App.config index 8f78382..1249ed8 100644 --- a/modules/platforms/dotnet/examples/Apache.Ignite.Examples/App.config +++ b/modules/platforms/dotnet/examples/Apache.Ignite.Examples/App.config @@ -30,6 +30,10 @@ localhost="127.0.0.1" peerAssemblyLoadingMode="CurrentAppDomain"> <atomicConfiguration atomicSequenceReserveSize="10" /> + <cacheConfiguration> + <cacheConfiguration name="default-cache" /> + </cacheConfiguration> + <discoverySpi type="TcpDiscoverySpi"> <ipFinder type="TcpDiscoveryMulticastIpFinder"> <endpoints> http://git-wip-us.apache.org/repos/asf/ignite/blob/5ec744cf/modules/platforms/dotnet/examples/Apache.Ignite.Examples/ThinClient/ThinClientPutGetExample.cs ---------------------------------------------------------------------- diff --git a/modules/platforms/dotnet/examples/Apache.Ignite.Examples/ThinClient/ThinClientPutGetExample.cs b/modules/platforms/dotnet/examples/Apache.Ignite.Examples/ThinClient/ThinClientPutGetExample.cs new file mode 100644 index 0000000..9158b09 --- /dev/null +++ b/modules/platforms/dotnet/examples/Apache.Ignite.Examples/ThinClient/ThinClientPutGetExample.cs @@ -0,0 +1,93 @@ +/* + * 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. + */ + +namespace Apache.Ignite.Examples.ThinClient +{ + using System; + using Apache.Ignite.Core; + using Apache.Ignite.Core.Client; + using Apache.Ignite.Core.Client.Cache; + using Apache.Ignite.ExamplesDll.Binary; + + /// <summary> + /// Demonstrates Ignite.NET "thin" client cache operations. + /// <para /> + /// 1) Set this class as startup object (Apache.Ignite.Examples project -> right-click -> Properties -> + /// Application -> Startup object); + /// 2) Start example (F5 or Ctrl+F5). + /// <para /> + /// This example must be run with standalone Apache Ignite node: + /// 1) Run %IGNITE_HOME%/platforms/dotnet/bin/Apache.Ignite.exe: + /// Apache.Ignite.exe -configFileName=platforms\dotnet\examples\apache.ignite.examples\app.config + /// 2) Start example. + /// <para /> + /// This example can also work via pure Java node started with ignite.bat/ignite.sh. + /// The only requirement is that the nodes have to create the cache named "default-cache" in advance. + /// </summary> + public static class ThinClientPutGetExample + { + /// <summary> Cache name. </summary> + private const string CacheName = "default-cache"; + + [STAThread] + public static void Main() + { + var cfg = new IgniteClientConfiguration + { + Host = "127.0.0.1" + }; + + using (IIgniteClient igniteClient = Ignition.StartClient(cfg)) + { + Console.WriteLine(); + Console.WriteLine(">>> Cache put-get client example started."); + + ICacheClient<int, Organization> cache = igniteClient.GetCache<int, Organization>(CacheName); + + PutGet(cache); + } + + Console.WriteLine(); + Console.WriteLine(">>> Example finished, press any key to exit ..."); + Console.ReadKey(); + } + + /// <summary> + /// Execute individual Put and Get. + /// </summary> + /// <param name="cache">Cache instance.</param> + private static void PutGet(ICacheClient<int, Organization> cache) + { + // Create new Organization to store in cache. + Organization org = new Organization( + "Microsoft", + new Address("1096 Eddy Street, San Francisco, CA", 94109), + OrganizationType.Private, + DateTime.Now + ); + + // Put created data entry to cache. + cache.Put(1, org); + + // Get recently created employee as a strongly-typed fully de-serialized instance. + Organization orgFromCache = cache.Get(1); + + Console.WriteLine(); + Console.WriteLine(">>> Retrieved organization instance from cache: " + orgFromCache); + } + } +} http://git-wip-us.apache.org/repos/asf/ignite/blob/5ec744cf/modules/platforms/dotnet/examples/Apache.Ignite.Examples/ThinClient/ThinClientQueryExample.cs ---------------------------------------------------------------------- diff --git a/modules/platforms/dotnet/examples/Apache.Ignite.Examples/ThinClient/ThinClientQueryExample.cs b/modules/platforms/dotnet/examples/Apache.Ignite.Examples/ThinClient/ThinClientQueryExample.cs new file mode 100644 index 0000000..49c87b7 --- /dev/null +++ b/modules/platforms/dotnet/examples/Apache.Ignite.Examples/ThinClient/ThinClientQueryExample.cs @@ -0,0 +1,147 @@ +/* + * 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. + */ + +namespace Apache.Ignite.Examples.ThinClient +{ + using System; + using Apache.Ignite.Core; + using Apache.Ignite.Core.Cache.Query; + using Apache.Ignite.Core.Client; + using Apache.Ignite.Core.Client.Cache; + using Apache.Ignite.ExamplesDll.Binary; + using Apache.Ignite.ExamplesDll.Datagrid; + + /// <summary> + /// Demonstrates Ignite.NET "thin" client cache queries. + /// <para /> + /// 1) Set this class as startup object (Apache.Ignite.Examples project -> right-click -> Properties -> + /// Application -> Startup object); + /// 2) Start example (F5 or Ctrl+F5). + /// <para /> + /// This example must be run with standalone Apache Ignite node: + /// 1) Run %IGNITE_HOME%/platforms/dotnet/bin/Apache.Ignite.exe: + /// Apache.Ignite.exe -configFileName=platforms\dotnet\examples\apache.ignite.examples\app.config -assembly=[path_to_Apache.Ignite.ExamplesDll.dll] + /// 2) Start example. + /// </summary> + public class ThinClientQueryExample + { + /// <summary> Cache name. </summary> + private const string CacheName = "default-cache"; + + [STAThread] + public static void Main() + { + var cfg = new IgniteClientConfiguration + { + Host = "127.0.0.1" + }; + + using (IIgniteClient igniteClient = Ignition.StartClient(cfg)) + { + Console.WriteLine(); + Console.WriteLine(">>> Cache query client example started."); + + ICacheClient<int, Employee> cache = igniteClient.GetCache<int, Employee>(CacheName); + + // Populate cache with sample data entries. + PopulateCache(cache); + + // Run scan query example. + ScanQueryExample(cache); + } + + Console.WriteLine(); + Console.WriteLine(">>> Example finished, press any key to exit ..."); + Console.ReadKey(); + } + + /// <summary> + /// Queries organizations of specified type. + /// </summary> + /// <param name="cache">Cache.</param> + private static void ScanQueryExample(ICacheClient<int, Employee> cache) + { + const int zip = 94109; + + var qry = cache.Query(new ScanQuery<int, Employee>(new ScanQueryFilter(zip))); + + Console.WriteLine(); + Console.WriteLine(">>> Private organizations (scan):"); + + foreach (var entry in qry) + { + Console.WriteLine(">>> " + entry.Value); + } + } + + /// <summary> + /// Populate cache with data for this example. + /// </summary> + /// <param name="cache">Cache.</param> + private static void PopulateCache(ICacheClient<int, Employee> cache) + { + cache.Put(1, new Employee( + "James Wilson", + 12500, + new Address("1096 Eddy Street, San Francisco, CA", 94109), + new[] { "Human Resources", "Customer Service" }, + 1)); + + cache.Put(2, new Employee( + "Daniel Adams", + 11000, + new Address("184 Fidler Drive, San Antonio, TX", 78130), + new[] { "Development", "QA" }, + 1)); + + cache.Put(3, new Employee( + "Cristian Moss", + 12500, + new Address("667 Jerry Dove Drive, Florence, SC", 29501), + new[] { "Logistics" }, + 1)); + + cache.Put(4, new Employee( + "Allison Mathis", + 25300, + new Address("2702 Freedom Lane, San Francisco, CA", 94109), + new[] { "Development" }, + 2)); + + cache.Put(5, new Employee( + "Breana Robbin", + 6500, + new Address("3960 Sundown Lane, Austin, TX", 78130), + new[] { "Sales" }, + 2)); + + cache.Put(6, new Employee( + "Philip Horsley", + 19800, + new Address("2803 Elsie Drive, Sioux Falls, SD", 57104), + new[] { "Sales" }, + 2)); + + cache.Put(7, new Employee( + "Brian Peters", + 10600, + new Address("1407 Pearlman Avenue, Boston, MA", 12110), + new[] { "Development", "QA" }, + 2)); + } + } +}
