コード例 #1
0
        public static async Task Main(string[] args)
        {
            var options = new HazelcastOptionsBuilder()
                          .With(args)
                          .WithConsoleLogger()
                          .Build();

            // create an Hazelcast client and connect to a Cloud server
            var cloud = options.Networking.Cloud;

            cloud.DiscoveryToken = "DISCOVERY_TOKEN_HASH"; // copied from Cloud console

            var ssl = options.Networking.Ssl;

            ssl.Enabled = true;
            //ssl.ValidateCertificateChain = false;
            ssl.CertificatePath = "CLIENT_PFX_CERTIFICATE_PATH"; // downloaded from Cloud console

            await using var client = await HazelcastClientFactory.StartNewClientAsync(options);

            // use a map
            await using var map = await client.GetMapAsync <string, string>("ssl-example");

            await map.SetAsync("key", "value");

            var value = await map.GetAsync("key");

            Console.WriteLine($"\"key\": \"{value}\"");
            await client.DestroyAsync(map);
        }
コード例 #2
0
        public static async Task Main(string[] args)
        {
            var options = new HazelcastOptionsBuilder()
                          .With(args)
                          .WithConsoleLogger()
                          .Build();

            // create an Hazelcast client and connect to a server running on localhost
            await using var client = await HazelcastClientFactory.StartNewClientAsync(options);

            // Get the Distributed List from Cluster.
            await using var list = await client.GetListAsync <string>("my-distributed-list");

            // Add elements to the list
            await list.AddAsync("item1");

            await list.AddAsync("item2");

            // Remove the first element
            Console.WriteLine("Removed: " + await list.RemoveAsync(0));
            // There is only one element left
            Console.WriteLine("Current size is " + await list.GetSizeAsync());
            // Clear the list
            await list.ClearAsync();

            await client.DestroyAsync(list);
        }
コード例 #3
0
        public static async Task Main(string[] args)
        {
            var options = new HazelcastOptionsBuilder()
                          .With(args)
                          .WithConsoleLogger()
                          .Build();

            // create an Hazelcast client and connect to a server running on localhost
            await using var client = await HazelcastClientFactory.StartNewClientAsync(options);

            // get the distributed map from the cluster
            await using var map = await client.GetMapAsync <string, string>("simple-example");

            // add values
            await map.SetAsync("key", "value");

            await map.SetAsync("key2", "value2");

            // get values, count, etc...
            Console.WriteLine("Key: " + await map.GetAsync("key"));
            Console.WriteLine("Values: " + string.Join(", ", await map.GetValuesAsync()));
            Console.WriteLine("Keys: " + string.Join(", ", await map.GetKeysAsync()));
            Console.WriteLine("Count: " + await map.GetSizeAsync());

            Console.WriteLine("Entries: " + string.Join(", ", await map.GetEntriesAsync()));
            Console.WriteLine("ContainsKey: " + await map.ContainsKeyAsync("key"));
            Console.WriteLine("ContainsValue: " + await map.ContainsValueAsync("value"));

            // destroy the map
            await client.DestroyAsync(map);
        }
コード例 #4
0
        public static async Task Main(string[] args)
        {
            var options = new HazelcastOptionsBuilder()
                          .With(args)
                          .WithConsoleLogger()
                          .Build();

            // create an Hazelcast client and connect to a server running on localhost
            await using var client = await HazelcastClientFactory.StartNewClientAsync(options);

            await using var rb = await client.GetRingBufferAsync <long>("rb");

            // add two items into ring buffer
            await rb.AddAsync(100);

            await rb.AddAsync(200);

            // we start from the oldest item.
            // if you want to start from the next item, call rb.tailSequence()+1
            var sequence = await rb.GetHeadSequenceAsync();

            Console.WriteLine(await rb.ReadOneAsync(sequence));
            sequence += 1;
            Console.WriteLine(await rb.ReadOneAsync(sequence));

            await client.DestroyAsync(rb);
        }
コード例 #5
0
        public static async Task Main(string[] args)
        {
            var options = new HazelcastOptionsBuilder()
                          .With(args)
                          .WithConsoleLogger()
                          .Build();

            // create an Hazelcast client and connect to a server running on localhost
            await using var client = await HazelcastClientFactory.StartNewClientAsync(options);

            // Get the Distributed MultiMap from Cluster.
            await using var multiMap = await client.GetMultiMapAsync <string, string>("my-distributed-multimap");

            // Put values in the map against the same key
            await multiMap.PutAsync("my-key", "value1");

            await multiMap.PutAsync("my-key", "value2");

            await multiMap.PutAsync("my-key", "value3");

            // Print out all the values for associated with key called "my-key"
            var values = await multiMap.GetAsync("my-key");

            foreach (var item in values)
            {
                Console.WriteLine(item);
            }

            // remove specific key/value pair
            await multiMap.RemoveAsync("my-key", "value2");
        }
コード例 #6
0
        public static async Task Main(string[] args)
        {
            var options = new HazelcastOptionsBuilder()
                          .With(args)
                          .WithConsoleLogger()
                          .Build();

            // server certificate will be validated by OS,
            // signed certificates will just work,
            // self-signed certificates should be registered and allowed
            options.Networking.Ssl.Enabled = true;

            // providing a client pfx certificate will enable mutual authentication
            // if the server is also configured for mutual authentication.
            options.Networking.Ssl.CertificatePath = "CLIENT_PFX_CERTIFICATE_PATH";

            // create an Hazelcast client and connect to a server running on localhost
            await using var client = await HazelcastClientFactory.StartNewClientAsync(options);

            // use a map
            await using var map = await client.GetMapAsync <string, string>("ssl-example");

            await map.SetAsync("key", "value");

            var value = await map.GetAsync("key");

            Console.WriteLine($"\"key\": \"{value}\"");
            await client.DestroyAsync(map);
        }
コード例 #7
0
        public static async Task Main(params string[] args)
        {
            var options = new HazelcastOptionsBuilder()
                          .With(args)
                          .WithConsoleLogger()
                          .Build();

            // server certificate will be validated by OS,
            // signed certificates will just work,
            // self-signed certificates should be registered and allowed
            options.Networking.Ssl.Enabled = true;

            // disable certificate validation
            //options.Networking.Ssl.ValidateCertificateChain = false;

            // validate the server certificate name
            //options.Networking.Ssl.ValidateCertificateName = true;
            //options.Networking.Ssl.CertificateName = "CERTIFICATE CN OR SAN VALUE HERE";

            // create an Hazelcast client and connect to a server running on localhost
            await using var client = await HazelcastClientFactory.StartNewClientAsync(options);

            // use a map
            await using var map = await client.GetMapAsync <string, string>("ssl-example");

            await map.SetAsync("key", "value");

            var value = await map.GetAsync("key");

            Console.WriteLine($"\"key\": \"{value}\"");
            await client.DestroyAsync(map);
        }
コード例 #8
0
        public static async Task Main(string[] args)
        {
            var options = new HazelcastOptionsBuilder()
                          .With(args)
                          .WithConsoleLogger()
                          .Build();

            // create an Hazelcast client and connect to a server running on localhost
            await using var client = await HazelcastClientFactory.StartNewClientAsync(options);

            // get distributed map from cluster
            await using var map = await client.GetMapAsync <string, string>("my-distributed-map");

            // set/get
            await map.SetAsync("key", "value");

            await map.GetAsync("key");

            // concurrent methods, optimistic updating
            await map.PutIfAbsentAsync("somekey", "somevalue");

            await map.ReplaceAsync("key", "value", "newvalue");

            // destroy the map
            await client.DestroyAsync(map);
        }
コード例 #9
0
        public static async Task Main(string[] args)
        {
            var options = new HazelcastOptionsBuilder()
                          .With(args)
                          .WithConsoleLogger()
                          .Build();

            // create an Hazelcast client and connect to a server running on localhost
            await using var client = await HazelcastClientFactory.StartNewClientAsync(options);

            // get the distributed set from the cluster
            await using var set = await client.GetSetAsync <string>("set-example");

            await set.AddAsync("item1");

            await set.AddAsync("item2");

            await set.AddAsync("item3");

            await set.AddAsync("item3");

            Console.WriteLine("All: " + string.Join(", ", await set.GetAllAsync()));

            Console.WriteLine("Contains: " + await set.ContainsAsync("item2"));

            Console.WriteLine("Count: " + await set.GetSizeAsync());

            // destroy the set
            await client.DestroyAsync(set);
        }
コード例 #10
0
        public static async Task Main(string[] args)
        {
            var options = new HazelcastOptionsBuilder()
                          .With(args)
                          .WithConsoleLogger()
                          .Build();

            // create an Hazelcast client and connect to a server running on localhost
            await using var client = await HazelcastClientFactory.StartNewClientAsync(options);

            // Get a Blocking Queue called "my-distributed-queue"
            var queue = await client.GetQueueAsync <string>("my-distributed-queue");

            // Offer a String into the Distributed Queue
            await queue.PutAsync("item");

            // Poll the Distributed Queue and return the String
            await queue.PollAsync();

            //Timed blocking Operations
            await queue.OfferAsync("anotheritem", TimeSpan.FromMilliseconds(500));

            await queue.PollAsync(TimeSpan.FromSeconds(5));

            //Indefinitely blocking Operations
            await queue.PutAsync("yetanotheritem");

            Console.WriteLine(await queue.TakeAsync());
        }
コード例 #11
0
        public static void Main(string[] args)
        {
            var options = new HazelcastOptionsBuilder()
                          .With(args)
                          .WithConsoleLogger()
                          .With("Logging:LogLevel:Hazelcast.Examples.LoggingExample.B", "Information")
                          .Build();

            var loggerFactory = options.LoggerFactory.Service;

            var loggerA = loggerFactory.CreateLogger <A>();

            // default level is Debug - everything shows
            loggerA.LogDebug("debug.a");
            loggerA.LogInformation("info.a");
            loggerA.LogWarning("warning.a");

            var loggerB = loggerFactory.CreateLogger <B>();

            // level is info - first line is skipped
            loggerB.LogDebug("debug.b");
            loggerB.LogInformation("info.b");
            loggerB.LogWarning("warning.b");

            // flush logs!
            loggerFactory.Dispose();
        }
コード例 #12
0
        public static async Task Main(string[] args)
        {
            var options = new HazelcastOptionsBuilder()
                          .With(args)
                          .WithConsoleLogger()
                          .Build();

            // create an Hazelcast client and connect to a server running on localhost
            await using var client = await HazelcastClientFactory.StartNewClientAsync(options);

            // Get the Distributed Set from Cluster.
            await using var set = await client.GetSetAsync <string>("my-distributed-set");

            // Add items to the set with duplicates
            await set.AddAsync("item1");

            await set.AddAsync("item1");

            await set.AddAsync("item2");

            await set.AddAsync("item2");

            await set.AddAsync("item2");

            await set.AddAsync("item3");

            // Get the items. Note that there are no duplicates.
            await foreach (var item in set)
            {
                Console.WriteLine(item);
            }

            await client.DestroyAsync(set);
        }
コード例 #13
0
        public static async Task Main(params string[] args)
        {
            var options = new HazelcastOptionsBuilder()
                          .With(args)
                          .WithConsoleLogger()
                          .Build();

            // create an Hazelcast client and connect to a server running on localhost
            await using var client = await HazelcastClientFactory.StartNewClientAsync(options);

            // get the distributed map from the cluster
            await using var map = await client.GetMapAsync <string, string>("simple-example");

            // create tasks that add values to the map
            var tasks = new List <Task>();

            for (var i = 0; i < 100; i++)
            {
                var key  = "key " + i;
                var task = map.SetAsync(key, " value " + i).ContinueWith(t => { Console.WriteLine("Added " + key); });
                tasks.Add(task);
            }

            // await all tasks
            await Task.WhenAll(tasks.ToArray());

            // destroy the map
            await client.DestroyAsync(map);
        }
コード例 #14
0
        public static async Task Main(string[] args)
        {
            var options = new HazelcastOptionsBuilder()
                          .With(args)
                          .WithConsoleLogger()
                          .Build();

            // create an Hazelcast client and connect to a server running on localhost
            await using var client = await HazelcastClientFactory.StartNewClientAsync(options);

            // get distributed topic from cluster
            await using var topic = await client.GetTopicAsync <string>("my-distributed-topic");

            // subscribe to event
            await topic.SubscribeAsync(on => on.Message(OnMessage));

            // publish a message to the Topic
            await topic.PublishAsync("Hello to distributed world");

            // allow event to trigger
            await Task.Delay(1_000);

            // destroy the topic
            await client.DestroyAsync(topic);
        }
コード例 #15
0
        public static async Task Main(string[] args)
        {
            var options = new HazelcastOptionsBuilder()
                          .With(args)
                          .WithConsoleLogger()
                          .Build();

            // customize options for this example
            options.Serialization.AddDataSerializableFactory(
                ExampleDataSerializableFactory.FactoryId,
                new ExampleDataSerializableFactory());

            // create an Hazelcast client and connect to a server running on localhost
            await using var client = await HazelcastClientFactory.StartNewClientAsync(options);

            // get the distributed map from the cluster
            await using var map = await client.GetMapAsync <int, Employee>("identified-data-serializable-example");

            // create and add an employee
            Console.WriteLine("Adding employee 'the employee'.");
            var employee = new Employee {
                Id = 1, Name = "the employee"
            };
            await map.SetAsync(employee.Id, employee);

            // retrieve employee
            var result = await map.GetAsync(employee.Id);

            Console.WriteLine(result != null
                ? $"Gotten employee '{result.Name}'."
                : "Employee not found?");

            // destroy the map
            await client.DestroyAsync(map);
        }
コード例 #16
0
        public static async Task Main(string[] args)
        {
            var options = new HazelcastOptionsBuilder()
                          .With(args)
                          .WithConsoleLogger()
                          .Build();

            // create an Hazelcast client and connect to a server running on localhost
            var hz1 = await HazelcastClientFactory.StartNewClientAsync(options);

            var connected = new SemaphoreSlim(0);

            // create another Hazelcast client and connect to a server running on localhost
            options.AddSubscriber(on => on
                                  .StateChanged((c, eventArgs) =>
            {
                Console.WriteLine($"State: {eventArgs.State}");
                if (eventArgs.State == ClientState.Connected)
                {
                    connected.Release();
                }
            }));
            var hz2 = await HazelcastClientFactory.StartNewClientAsync(options);

            // wait for the event
            await connected.WaitAsync();

            // terminate the clients
            await hz1.DisposeAsync();

            await hz2.DisposeAsync();
        }
コード例 #17
0
        public static async Task Main(string[] args)
        {
            var options = new HazelcastOptionsBuilder()
                          .With(args)
                          .WithConsoleLogger()
                          .Build();

            // create an Hazelcast client and connect to a server running on localhost

            options.Serialization.AddDataSerializableFactory(
                EntryProcessorDataSerializableFactory.FactoryId,
                new EntryProcessorDataSerializableFactory());

            await using var client = await HazelcastClientFactory.StartNewClientAsync(options);

            // Get the Distributed Map from Cluster.
            await using var map = await client.GetMapAsync <string, string>("my-distributed-map");

            //Set the key-value pair
            await map.SetAsync("key", "value");

            // Run the IdentifiedEntryProcessor class on the Hazelcast Cluster Member holding the key called "key"
            await map.ExecuteAsync <string>(new IdentifiedEntryProcessor("duh"), "key");

            // Show that the IdentifiedEntryProcessor updated the value.
            Console.WriteLine("new value:" + await map.GetAsync("key"));
        }
 /// <summary>
 /// Sets user secrets.
 /// </summary>
 /// <param name="builder">The options builder.</param>
 /// <param name="assembly">The assembly.</param>
 /// <param name="key">The key.</param>
 /// <param name="optional">Whether secrets are optional.</param>
 /// <returns>This options builder.</returns>
 public static HazelcastOptionsBuilder WithUserSecrets(this HazelcastOptionsBuilder builder, Assembly assembly, string key = HazelcastOptions.Hazelcast, bool optional = true)
 {
     return(builder
            .WithAltKey(key)
            .With(configurationBuilder =>
     {
         configurationBuilder.AddUserSecrets(assembly, optional);
     }));
 }
コード例 #19
0
        public static async Task Main(params string[] args)
        {
            var options = new HazelcastOptionsBuilder()
                          .With(args)
                          .WithConsoleLogger()
                          .Build();

            // create an Hazelcast client and connect to a server running on localhost
            await using var client = await HazelcastClientFactory.StartNewClientAsync(options);

            // get the distributed map from the cluster
            await using var map = await client.GetMapAsync <string, string>("listener-example");

            var count   = 3;
            var counted = new SemaphoreSlim(0);

            // subscribe to events
            var id = await map.SubscribeAsync(handle => handle
                                              .EntryAdded((m, a) =>
            {
                Console.WriteLine("Added '{0}': '{1}'", a.Key, a.Value);
                if (Interlocked.Decrement(ref count) == 0)
                {
                    counted.Release();
                }
            })
                                              .EntryUpdated((m, a) =>
            {
                Console.WriteLine("Updated '{0}': '{1}' (was: '{2}')", a.Key, a.Value, a.OldValue);
                if (Interlocked.Decrement(ref count) == 0)
                {
                    counted.Release();
                }
            })
                                              .EntryRemoved((m, a) =>
            {
                Console.WriteLine("Removed '{0}': '{1}'", a.Key, a.OldValue);
                if (Interlocked.Decrement(ref count) == 0)
                {
                    counted.Release();
                }
            }));

            // trigger events
            await map.SetAsync("key", "value");    // add

            await map.SetAsync("key", "valueNew"); //update

            await map.RemoveAsync("key");

            // wait for events
            await counted.WaitAsync();

            // unsubscribe
            await map.UnsubscribeAsync(id);
        }
        public static async Task Main(string[] args)
        {
            var options = new HazelcastOptionsBuilder()
                          .With(args)
                          .WithConsoleLogger()
                          .Build();

            // create an Hazelcast client and connect to a server running on localhost
            options.Serialization.AddPortableFactory(SamplePortableFactory.FactoryId, new SamplePortableFactory());
            await using var client = await HazelcastClientFactory.StartNewClientAsync(options);
        }
コード例 #21
0
        public static async Task Main(string[] args)
        {
            var options = new HazelcastOptionsBuilder()
                          .With(args)
                          .WithConsoleLogger()
                          .Build();

            // create an Hazelcast client and connect to a server running on localhost
            await using var client = await HazelcastClientFactory.StartNewClientAsync(options);

            // get the distributed map from the cluster
            await using var map = await client.GetMapAsync <string, HazelcastJsonValue>("json-example");

            // add values
            Console.WriteLine("Populate map");
            await map.SetAsync("item1", new HazelcastJsonValue("{ \"age\": 4 }"));

            await map.SetAsync("item2", new HazelcastJsonValue("{ \"age\": 20 }"));

            // count
            Console.WriteLine("Count");
            Console.WriteLine($"{await map.GetSizeAsync()} entries");

            // get all
            Console.WriteLine("List");
            var entries = await map.GetEntriesAsync();

#if NETCOREAPP
            foreach (var(key, value) in entries)
            {
                Console.WriteLine($"[{key}]: {value}");
            }
#else
            foreach (var kvp in entries)
            {
                Console.WriteLine($"[{kvp.Key}]: {kvp.Value}");
            }
#endif

            // read
            Console.WriteLine("Query");
            var values = await map.GetValuesAsync(Query.Predicates.LessThan("age", 6));

            Console.WriteLine($"Retrieved {values.Count} entries with 'age < 6'.");
            foreach (var value in values)
            {
                Console.WriteLine($"Entry value: {value}");
            }

            // destroy the map
            await client.DestroyAsync(map);
        }
コード例 #22
0
        public static async Task Main(string[] args)
        {
            var options = new HazelcastOptionsBuilder()
                          .With(args)
                          .WithConsoleLogger()
                          .Build();

            // configure NearCache
            options.NearCaches["nearcache-map-*"] = new NearCacheOptions
            {
                MaxSize            = 1000,
                InvalidateOnChange = true,
                EvictionPolicy     = EvictionPolicy.Lru,
                InMemoryFormat     = InMemoryFormat.Binary
            };

            // create an Hazelcast client and connect to a server running on localhost
            await using var client = await HazelcastClientFactory.StartNewClientAsync(options);

            // get the distributed map from the cluster
            await using var map = await client.GetMapAsync <string, string>("nearcache-map-1");

            // add values
            for (var i = 0; i < 1000; i++)
            {
                await map.SetAsync("key" + i, "value" + i);
            }

            // get values, first pass
            var sw = new Stopwatch();

            sw.Start();
            for (var i = 0; i < 1000; i++)
            {
                await map.GetAsync("key" + i);
            }
            Console.WriteLine("Got values in " + sw.ElapsedMilliseconds + " millis");

            // get values, second pass
            sw.Restart();
            for (var i = 0; i < 1000; i++)
            {
                await map.GetAsync("key" + i);
            }
            Console.WriteLine("Got cached values in " + sw.ElapsedMilliseconds + " millis");

            // destroy the map
            await client.DestroyAsync(map);
        }
        public static async Task Main(string[] args)
        {
            var options = new HazelcastOptionsBuilder()
                          .With(args)
                          .WithConsoleLogger()
                          .Build();

            // register the global json serializer
            options.Serialization.GlobalSerializer.Creator = () => new GlobalJsonSerializer();

            // create an Hazelcast client and connect to a server running on localhost
            await using var client = await HazelcastClientFactory.StartNewClientAsync(options);

            // get the distributed maps from the cluster
            await using var accountMap = await client.GetMapAsync <int, Account>("account-map");

            await using var customerMap = await client.GetMapAsync <int, Customer>("customer-map");

            //GlobalSerializer will serialize/deserialize all non-builtin types
            var account = new Account
            {
                Email       = "*****@*****.**",
                Active      = true,
                CreatedDate = new DateTime(2013, 1, 20, 0, 0, 0, DateTimeKind.Utc),
                Roles       = new List <string> {
                    "User", "Admin"
                }
            };

            var customer = new Customer
            {
                Id        = 1001,
                Name      = "James",
                Surname   = "Doe",
                Birthday  = new DateTime(1990, 5, 2, 0, 0, 0, DateTimeKind.Utc),
                AccountId = 1
            };

            await accountMap.PutAsync(1, account);

            await customerMap.PutAsync(1001, customer);

            Console.WriteLine(await accountMap.GetAsync(1));
            Console.WriteLine(await customerMap.GetAsync(1001));

            await accountMap.DestroyAsync();

            await customerMap.DestroyAsync();
        }
コード例 #24
0
 /// <summary>
 /// Sets the logger factory
 /// </summary>
 /// <param name="builder">The options builder.</param>
 /// <returns>The options builder.</returns>
 public static HazelcastOptionsBuilder WithConsoleLogger(this HazelcastOptionsBuilder builder)
 {
     return(builder
            .With("Logging:LogLevel:Default", "Debug")
            .With("Logging:LogLevel:System", "Information")
            .With("Logging:LogLevel:System", "Information")
            .With((configuration, o) =>
     {
         // configure logging factory and add the console provider
         o.LoggerFactory.Creator = () => LoggerFactory.Create(b =>
                                                              b
                                                              .AddConfiguration(configuration.GetSection("logging"))
                                                              .AddConsole());
     }));
 }
コード例 #25
0
 public static HazelcastOptionsBuilder WithHConsoleLogger(this HazelcastOptionsBuilder builder)
 {
     return(builder
            .With("Logging:LogLevel:Default", "None")
            .With("Logging:LogLevel:System", "None")
            .With("Logging:LogLevel:Microsoft", "None")
            .With((configuration, options) =>
     {
         // configure logging factory and add the console provider
         options.LoggerFactory.Creator = () => LoggerFactory.Create(loggingBuilder =>
                                                                    loggingBuilder
                                                                    .AddConfiguration(configuration.GetSection("logging"))
                                                                    .AddHConsole());
     }));
 }
コード例 #26
0
        public static async Task Main(string[] args)
        {
            var options = new HazelcastOptionsBuilder()
                          .With(args)
                          .WithConsoleLogger()
                          .Build();

            // create an Hazelcast client and connect to a server running on localhost
            await using var client = await HazelcastClientFactory.StartNewClientAsync(options);

            // get distributed topic from cluster
            await using var topic = await client.GetTopicAsync <string>("topic-example");

            var count   = 100;
            var counted = new SemaphoreSlim(0);

            // subscribe to event
            await topic.SubscribeAsync(on => on
                                       .Message((sender, args) =>
            {
                Console.WriteLine($"Got message {args.Payload}");
                if (Interlocked.Decrement(ref count) == 0)
                {
                    counted.Release();
                }
            }));

            // publish messages
            for (var i = 0; i < 100; i++)
            {
                await topic.PublishAsync($"Message {i}");
            }

            // start publisher task
            //var publisher = Task.Run(async () =>
            //{
            //    for (var i = 0; i < 100; i++)
            //    {
            //        await topic.PublishAsync($"Message {i}");
            //    }
            //});

            // wait for all events
            await counted.WaitAsync();

            // destroy the topic
            await client.DestroyAsync(topic);
        }
コード例 #27
0
 /// <summary>
 /// Configure an <see cref="ILoggerFactory"/> with a logger to <see cref="HConsole"/>.
 /// </summary>
 /// <param name="builder">The options builder.</param>
 /// <param name="hazelcastLevel">The log level for Hazelcast code.</param>
 /// <returns>This options builder.</returns>
 public static HazelcastOptionsBuilder WithHConsoleLogger(this HazelcastOptionsBuilder builder, LogLevel hazelcastLevel = LogLevel.Debug)
 {
     return(builder
            .With("Logging:LogLevel:Default", "Debug")
            .With("Logging:LogLevel:System", "Information")
            .With("Logging:LogLevel:Microsoft", "Information")
            .With("Logging:LogLevel:Hazelcast", hazelcastLevel.ToString())
            .With((configuration, hazelcastOptions) =>
     {
         // configure logging factory and add the logging provider
         hazelcastOptions.LoggerFactory.Creator = () => LoggerFactory.Create(loggingBuilder =>
                                                                             loggingBuilder
                                                                             .AddConfiguration(configuration.GetSection("logging"))
                                                                             .AddHConsole());
     }));
 }
コード例 #28
0
        public static async Task Main(string[] args)
        {
            var options = new HazelcastOptionsBuilder()
                          .With(args)
                          .WithConsoleLogger()
                          .Build();

            // create an Hazelcast client and connect to a server running on localhost
            options.Serialization.GlobalSerializer = new GlobalSerializerOptions
            {
                Creator = () => new GlobalSerializer()
            };
            await using var client = await HazelcastClientFactory.StartNewClientAsync(options);

            //GlobalSerializer will serialize/deserialize all non-builtin types
        }
コード例 #29
0
        public static async Task Main(string[] args)
        {
            var options = new HazelcastOptionsBuilder()
                          .With(args)
                          .WithConsoleLogger()
                          .Build();

            // create an Hazelcast client and connect to a server running on localhost
            await using var client = await HazelcastClientFactory.StartNewClientAsync(options);

            var map1 = await client.GetMapAsync <int, string>("test1");

            var map2 = await client.GetMapAsync <int, string>("test2");

            await map1.ClearAsync();

            await map2.ClearAsync();

            const int count         = 100;
            var       stopwatch     = Stopwatch.StartNew();
            long      totalDuration = 0;

            for (var i = 0; i < count; i++)
            {
                stopwatch.Restart();

                await using (var transactionContext = await client.BeginTransactionAsync(
                                 new TransactionOptions {
                    Type = TransactionOptions.TransactionType.TwoPhase
                }))
                {
                    var txMap1 = await transactionContext.GetMapAsync <int, string>("test1");

                    var txMap2 = await transactionContext.GetMapAsync <int, string>("test2");

                    await txMap1.SetAsync(i, "value");

                    await txMap2.SetAsync(i, "value");

                    await transactionContext.CommitAsync();
                }

                totalDuration += stopwatch.ElapsedMilliseconds;
            }

            Console.WriteLine((double)totalDuration / count);
        }
コード例 #30
0
        //
        // this is a complete example of a simple console application where
        // every component is configured and created explicitly.
        //
        // configuration (https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration)
        //
        //   environment
        //     optional Hazelcast.Build(...) 'environmentName' argument: Development, Staging, Production (default), ...
        //     falls back to DOTNET_ENVIRONMENT + ASPNETCORE_ENVIRONMENT environment variables
        //     determines <env>, default is Production
        //
        //   configuration file
        //     appsettings.json
        //     appsettings.<env>.json
        //     hazelcast.json
        //     hazelcast.<env>.json
        //
        //     {
        //       "hazelcast": {
        //         "networking": {
        //           "addresses": [ "server:port" ]
        //         }
        //       }
        //     }
        //
        //   environment variables
        //     hazelcast__networking__addresses__0=server:port (standard .NET)
        //     hazelcast.networking.addresses.0=server:port (hazelcast-specific)
        //
        //   command line
        //     hazelcast:networking:addresses:0=server:port (standard .NET)
        //     hazelcast.networking.addresses.0=server:port (hazelcast-specific)
        //

        public static async Task Main(string[] args)
        {
            // build options
            var options = new HazelcastOptionsBuilder()
                          .With(args)
                          .WithConsoleLogger()
                          .Build();

            // create a logger, a client factory and a client
            var logger = options.LoggerFactory.Service.CreateLogger <Worker>();

            await using var client = await HazelcastClientFactory.StartNewClientAsync(options); // disposed when method exits

            // create the worker, and run
            var worker = new Worker(client, logger);
            await worker.RunAsync();
        }