public static void Run(string[] args)
        {
            Environment.SetEnvironmentVariable("hazelcast.logging.level", "info");
            Environment.SetEnvironmentVariable("hazelcast.logging.type", "console");

            var config = new Configuration();

            var nearCacheConfig = new NearCacheConfig
            {
                MaxSize = 1000, InvalidateOnChange = true, EvictionPolicy = EvictionPolicy.Lru, InMemoryFormat = InMemoryFormat.Binary
            };

            config.NearCacheConfigs.Add("nearcache-map-*", nearCacheConfig);
            config.NetworkConfig.Addresses.Add("127.0.0.1");

            var client = HazelcastClient.NewHazelcastClient(config);

            var map = client.GetMap <string, string>("nearcache-map-1");

            for (var i = 0; i < 1000; i++)
            {
                map.Put("key" + i, "value" + i);
            }

            var sw = new Stopwatch();

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

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

            map.Destroy();
            client.Shutdown();
        }
예제 #2
0
        public static void Run(string[] args)
        {
            Environment.SetEnvironmentVariable("hazelcast.logging.level", "info");
            Environment.SetEnvironmentVariable("hazelcast.logging.type", "console");

            var config = new ClientConfig();

            //TODO fix here
            // config.SetClusterName("dev").SetClusterPassword("dev-pass");
            config.GetNetworkConfig().AddAddress("127.0.0.1:5701")
            .SetSmartRouting(false);

            var client = HazelcastClient.NewHazelcastClient(config);
            var map    = client.GetMap <int, string>("test");
            var map2   = client.GetMap <int, string>("test3");

            map.Clear();
            map2.Clear();

            long totalDuration = 0;
            int  count         = 100;

            for (int i = 0; i < count; i++)
            {
                TransactionOptions options = new TransactionOptions()
                                             .SetTransactionType(TransactionOptions.TransactionType.TwoPhase);
                ITransactionContext context = client.NewTransactionContext(options);

                var watch = System.Diagnostics.Stopwatch.StartNew();

                context.BeginTransaction();
                var tmap  = context.GetMap <int, string>("test");
                var tmap2 = context.GetMap <int, string>("test2");

                tmap.Set(i, "value");
                tmap2.Set(i, "value");
                context.CommitTransaction();

                watch.Stop();
                totalDuration += watch.ElapsedMilliseconds;
            }
            Console.WriteLine((double)totalDuration / count);
        }
        public static void Run(string[] args)
        {
            // Start the Hazelcast Client and connect to an already running Hazelcast Cluster on 127.0.0.1
            var hz = HazelcastClient.NewHazelcastClient();
            // Get the Distributed List from Cluster.
            var list = hz.GetList <string>("my-distributed-list");

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

            // Remove the first element
            Console.WriteLine("Removed: " + list.Remove(0));
            // There is only one element left
            Console.WriteLine("Current size is " + list.Size());
            // Clear the list
            list.Clear();
            // Shutdown this Hazelcast client
            hz.Shutdown();
        }
        public static void Run(string[] args)
        {
            // Start the Hazelcast Client and connect to an already running Hazelcast Cluster on 127.0.0.1
            var hz = HazelcastClient.NewHazelcastClient();
            // Get a Blocking Queue called "my-distributed-queue"
            var queue = hz.GetQueue <string>("my-distributed-queue");

            // Offer a String into the Distributed Queue
            queue.Offer("item");
            // Poll the Distributed Queue and return the String
            queue.Poll();
            //Timed blocking Operations
            queue.Offer("anotheritem", 500, TimeUnit.Milliseconds);
            queue.Poll(5, TimeUnit.Seconds);
            //Indefinitely blocking Operations
            queue.Put("yetanotheritem");
            Console.WriteLine(queue.Take());
            // Shutdown this Hazelcast Client
            hz.Shutdown();
        }
예제 #5
0
        public static void Run(string[] args)
        {
            // Start the Hazelcast Client and connect to an already running Hazelcast Cluster on 127.0.0.1
            var hz = HazelcastClient.NewHazelcastClient();
            // Get a distributed lock called "my-distributed-lock"
            var lck = hz.GetLock("my-distributed-lock");

            // Now create a lock and execute some guarded code.
            lck.Lock();
            try
            {
                //do something here
            }
            finally
            {
                lck.Unlock();
            }

            // Shutdown this Hazelcast Client
            hz.Shutdown();
        }
        public static void Run(string[] args)
        {
            // Start the Hazelcast Client and connect to an already running Hazelcast Cluster on 127.0.0.1
            var hz = HazelcastClient.NewHazelcastClient();

            var rb = hz.GetRingbuffer <long>("rb");

            // add two items into ring buffer
            rb.Add(100);
            rb.Add(200);

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

            Console.WriteLine(rb.ReadOne(sequence));
            sequence += 1;
            Console.WriteLine(rb.ReadOne(sequence));
            // Shutdown this Hazelcast Client
            hz.Shutdown();
        }
예제 #7
0
        public static void Run(string[] args)
        {
            Environment.SetEnvironmentVariable("hazelcast.logging.level", "info");
            Environment.SetEnvironmentVariable("hazelcast.logging.type", "console");

            var config = new ClientConfig();

            config.GetNetworkConfig().AddAddress("127.0.0.1");
            var client = HazelcastClient.NewHazelcastClient(config);

            var map = client.GetMap <string, string>("listener-example");

            var cdown = new CountdownEvent(3);

            map.AddEntryListener(new EntryAdapter <string, string>
            {
                Added = e =>
                {
                    Console.WriteLine("Key '{0}' with value ' {1}' was added.", e.GetKey(), e.GetValue());
                    cdown.Signal();
                },
                Updated = e =>
                {
                    Console.WriteLine("Key '{0}' with value ' {1}' was added.", e.GetKey(), e.GetValue());
                    cdown.Signal();
                },
                Removed = e =>
                {
                    Console.WriteLine("Key '{0}' with value ' {1}' was removed.", e.GetKey(), e.GetOldValue());
                    cdown.Signal();
                }
            }, true);

            map.Put("key", "value");    //first put
            map.Put("key", "valueNew"); //makes update
            map.Remove("key");

            cdown.Wait();
            map.Destroy();
        }
예제 #8
0
        static void Main2(string[] args)
        {
            ClientConfig clientConfig = new ClientConfig();

            clientConfig.GetNetworkConfig().AddAddress("192.168.1.162");

            var hazelcast = HazelcastClient.NewHazelcastClient(clientConfig);

            var hzMap = hazelcast.GetMap <string, string>("mylist");

            //hzMap.Put("key1","Value1");
            //hzMap.Put("key2","Value2");
            //hzMap.Put("key3","Value3");
            //hzMap.Put("key4","Value4");

            Console.WriteLine(hzMap.Get("key1"));
            Console.WriteLine(hzMap.Get("key2"));
            Console.WriteLine(hzMap.Get("key3"));
            Console.WriteLine(hzMap.Get("key4"));

            Console.ReadKey();
        }
예제 #9
0
        public static void Run(string[] args)
        {
            Environment.SetEnvironmentVariable("hazelcast.logging.level", "info");
            Environment.SetEnvironmentVariable("hazelcast.logging.type", "console");

            var config = new ClientConfig();

            config.GetNetworkConfig().AddAddress("127.0.0.1");

            var client = HazelcastClient.NewHazelcastClient(config);

            var map = client.GetMap <int, int>("predicate-example");

            Console.WriteLine("Populating map");

            for (int i = 0; i < 1000; i++)
            {
                map.Put(i, i);
            }

            Console.WriteLine("Map size: " + map.Size());

            var partitionKey = 10;

            //all keys on the same partition of the partitionKey will be returned
            var allPartitionKeys = map.KeySet(new PartitionPredicate(partitionKey, Predicates.True()));

            //keys less than 100 and on the same partition of the partitionKey will be returned
            var result = map.KeySet(new PartitionPredicate(partitionKey, Predicates.IsLessThan("this", 100)));

            Console.Write("\nKey set: ");
            foreach (var key in result)
            {
                Console.Write(key + ", ");
            }

            client.Shutdown();
        }
예제 #10
0
        private static void Run(string[] args)
        {
            //This will enable client statistics
            Environment.SetEnvironmentVariable("hazelcast.client.statistics.enabled", "true");

            //set the statistics send period, default value is 3 seconds
            Environment.SetEnvironmentVariable("hazelcast.client.statistics.period.seconds", "3");

            var config = new ClientConfig();

            config.GetNetworkConfig().AddAddress("127.0.0.1");

            var nearCacheConfig = new NearCacheConfig();

            nearCacheConfig.SetMaxSize(1000)
            .SetInvalidateOnChange(true)
            .SetEvictionPolicy("Lru")
            .SetInMemoryFormat(InMemoryFormat.Binary);

            config.AddNearCacheConfig("myMap", nearCacheConfig);

            var client = HazelcastClient.NewHazelcastClient(config);

            //Let's generate some statistics
            var map  = client.GetMap <string, string>("myMap");
            var task = Task.Factory.StartNew(() =>
            {
                for (var i = 0; i < 100000; i++)
                {
                    map.Put("key-" + i, "value-" + i);
                    Thread.Sleep(500);
                }
            });

            //After client connected you can use Management Center to visualize client statistics
            task.Wait();
            client.Shutdown();
        }
예제 #11
0
        public static void Run(string[] args)
        {
            Environment.SetEnvironmentVariable("hazelcast.logging.level", "info");
            Environment.SetEnvironmentVariable("hazelcast.logging.type", "console");

            var config = new ClientConfig();

            config.GetNetworkConfig().AddAddress("127.0.0.1");
            var client = HazelcastClient.NewHazelcastClient(config);

            var theLock = client.GetLock("lock-example");

            var i = 0;

            Action increment = () =>
            {
                for (var j = 0; j < 100; j++)
                {
                    theLock.Lock();
                    try
                    {
                        i++;
                    }
                    finally
                    {
                        theLock.Unlock();
                    }
                }
            };
            var task1 = Task.Factory.StartNew(increment);
            var task2 = Task.Factory.StartNew(increment);

            Task.WaitAll(task1, task2);

            Console.WriteLine("final value: " + i);

            theLock.Destroy();
        }
예제 #12
0
        public static void Run(string[] args)
        {
            // Start the Hazelcast Client and connect to an already running Hazelcast Cluster on 127.0.0.1
            var hz = HazelcastClient.NewHazelcastClient();
            // Get the Distributed Set from Cluster.
            var set = hz.GetSet <string>("my-distributed-set");

            // Add items to the set with duplicates
            set.Add("item1");
            set.Add("item1");
            set.Add("item2");
            set.Add("item2");
            set.Add("item2");
            set.Add("item3");
            // Get the items. Note that there are no duplicates.
            foreach (var item in set)
            {
                Console.WriteLine(item);
            }

            // Shutdown this Hazelcast client
            hz.Shutdown();
        }
예제 #13
0
        private static void Run(string[] args)
        {
            Environment.SetEnvironmentVariable("hazelcast.logging.level", "info");
            Environment.SetEnvironmentVariable("hazelcast.logging.type", "console");

            var clientConfig        = new ClientConfig();
            var clientNetworkConfig = clientConfig.GetNetworkConfig();

            clientNetworkConfig.GetCloudConfig()
            .SetEnabled(true)
            .SetDiscoveryToken("DISCOVERY_TOKEN_HASH");    //Discovery token copied from Cloud console

            var client = HazelcastClient.NewHazelcastClient(clientConfig);

            var map = client.GetMap <string, string>("ssl-example");

            map.Put("key", "value");

            Console.WriteLine("Key: " + map.Get("key"));

            map.Destroy();
            client.Shutdown();
        }
        public static void Run(string[] args)
        {
            Environment.SetEnvironmentVariable("hazelcast.logging.level", "info");
            Environment.SetEnvironmentVariable("hazelcast.logging.type", "console");

            var config = new ClientConfig();

            config.GetNetworkConfig().AddAddress("127.0.0.1");
            var client = HazelcastClient.NewHazelcastClient(config);

            var semaphore = client.GetSemaphore("example-semaphore");

            semaphore.Init(1);
            var    i         = 0;
            Action increment = () =>
            {
                for (var j = 0; j < 100; j++)
                {
                    semaphore.Acquire();
                    try
                    {
                        i++;
                    }
                    finally
                    {
                        semaphore.Release();
                    }
                }
            };

            var task1 = Task.Factory.StartNew(increment);
            var task2 = Task.Factory.StartNew(increment);

            Task.WaitAll(task1, task2);
            Console.WriteLine("Final value: " + i);
            semaphore.Destroy();
        }
        private static void Run(string[] args)
        {
            Environment.SetEnvironmentVariable("hazelcast.logging.level", "info");
            Environment.SetEnvironmentVariable("hazelcast.logging.type", "console");

            var clientConfig        = new Configuration();
            var clientNetworkConfig = clientConfig.NetworkConfig;

            clientNetworkConfig.ConfigureHazelcastCloud(hzCloudConfig =>
            {
                hzCloudConfig.Enabled        = true;
                hzCloudConfig.DiscoveryToken = "DISCOVERY_TOKEN_HASH";//Discovery token copied from Cloud console
            });

            //Server certificate will be validated by OS,
            //Download and register the server trust store to your OS
            clientNetworkConfig.ConfigureSSL(sslConfig =>
            {
                sslConfig.Enabled = true;
                //in order to disable certificate validating uncomment below line
                // sslConfig.ValidateCertificateChain = false;

                //Client certificate file path downloaded from cloud console
                sslConfig.CertificateFilePath = "CLIENT_PFX_CERTIFICATE_PATH";
            });

            var client = HazelcastClient.NewHazelcastClient(clientConfig);

            var map = client.GetMap <string, string>("ssl-example");

            map.Put("key", "value");

            Console.WriteLine("Key: " + map.Get("key"));

            map.Destroy();
            client.Shutdown();
        }
예제 #16
0
        private static void Run(string[] args)
        {
            Environment.SetEnvironmentVariable("hazelcast.logging.level", "info");
            Environment.SetEnvironmentVariable("hazelcast.logging.type", "console");

            var clientConfig        = new Configuration();
            var clientNetworkConfig = clientConfig.NetworkConfig;

            //replace with your actual server host/ip and port
            clientNetworkConfig.AddAddress("127.0.0.1:5701");


            //Server certificate will be validated by OS,
            //signed certificates will just work,
            //self-signed certificates should be registered by OS depended way and allowed.
            clientNetworkConfig.ConfigureSSL(sslConfig =>
            {
                sslConfig.Enabled = true;
                //in order to disable certificate validating uncomment below line
                // sslConfig.ValidateCertificateChain = false;

                //in order to validate the server certificate name use below
                // sslConfig.ValidateCertificateName = true;
                // sslConfig.CertificateName = "CERTIFICATE CN OR SAN VALUE HERE";
            });

            var client = HazelcastClient.NewHazelcastClient(clientConfig);

            var map = client.GetMap <string, string>("ssl-example");

            map.Put("key", "value");

            Console.WriteLine("Key: " + map.Get("key"));

            map.Destroy();
            client.Shutdown();
        }
예제 #17
0
        private static void Run(string[] args)
        {
            Environment.SetEnvironmentVariable("hazelcast.logging.level", "info");
            Environment.SetEnvironmentVariable("hazelcast.logging.type", "console");

            var clientConfig = new Configuration();

            clientConfig.ConfigureNetwork(networkConfig =>
            {
                //replace with your actual server host/ip and port
                networkConfig.Addresses.Add("127.0.0.1:5701");
            });

            //Server certificate will be validated by OS,
            //signed certificates will just work,
            //self-signed certificates should be registered by OS depended way and allowed.
            clientConfig.NetworkConfig.ConfigureSSL(sslConfig =>
            {
                sslConfig.Enabled = true;
                //in order to disable certificate validating uncomment below line
                // sslConfig.ValidateCertificateChain = false;

                //Providing a client pfx certificate will enable mutual authenticating if server also configured mutual auth.
                // sslConfig.CertificateFilePath = "CLIENT_PFX_CERTIFICATE_PATH";
            });

            var client = HazelcastClient.NewHazelcastClient(clientConfig);

            var map = client.GetMap <string, string>("ssl-example");

            map.Put("key", "value");

            Console.WriteLine("Key: " + map.Get("key"));

            map.Destroy();
            client.Shutdown();
        }
        public static void Run(string[] args)
        {
            // Start the Hazelcast Client and connect to an already running Hazelcast Cluster on 127.0.0.1
            var hz = HazelcastClient.NewHazelcastClient();
            // Get the Distributed MultiMap from Cluster.
            var multiMap = hz.GetMultiMap <string, string>("my-distributed-multimap");

            // Put values in the map against the same key
            multiMap.Put("my-key", "value1");
            multiMap.Put("my-key", "value2");
            multiMap.Put("my-key", "value3");
            // Print out all the values for associated with key called "my-key"
            var values = multiMap.Get("my-key");

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

            // remove specific key/value pair
            multiMap.Remove("my-key", "value2");
            // Shutdown this Hazelcast Client
            hz.Shutdown();
        }
예제 #19
0
        public Cache(string[] hosts)
        {
            try
            {
                var clientConfig = new ClientConfig();
                clientConfig.GetNetworkConfig().AddAddress(hosts);
                clientConfig.GetSerializationConfig().
                AddPortableFactory(PersonPortableFactory.FACTORY_ID, new PersonPortableFactory());

                _hazelcast = HazelcastClient.NewHazelcastClient(clientConfig);
                _map       = _hazelcast.GetMap <int, Person>("persons");

                Console.WriteLine("Hazelcast cluster addresses:");
                foreach (var h in hosts)
                {
                    Console.WriteLine(h);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Hazelcast: connection failed.");
                Console.WriteLine(e.Message);
            }
        }
        public static void Run(string[] args)
        {
            Environment.SetEnvironmentVariable("hazelcast.logging.level", "info");
            Environment.SetEnvironmentVariable("hazelcast.logging.type", "console");

            var config = new ClientConfig();

            config.GetNetworkConfig().AddAddress("127.0.0.1");

            var client = HazelcastClient.NewHazelcastClient(config);
            var map    = client.GetMap <string, HazelcastJsonValue>("json-example");

            map.Put("item1", new HazelcastJsonValue("{ \"age\": 4 }"));
            map.Put("item2", new HazelcastJsonValue("{ \"age\": 20 }"));

            var result = map.Values(Predicates.IsLessThan("age", 6));

            Console.WriteLine("Retrieved " + result.Count + " values whose age is less than 6.");
            Console.WriteLine("Entry is: " + result.First().ToString());

            Console.WriteLine("Finished");

            client.Shutdown();
        }
예제 #21
0
        public async Task <ActionResult <IEnumerable <MetricItem> > > GetMetricItems(string userName)
        {
            // We should clear context each time
            ClearContext();

            string hazelcastIP = Environment.GetEnvironmentVariable("ENV_HIP");

            if (string.IsNullOrEmpty(hazelcastIP))
            {
                hazelcastIP = "127.0.0.1";
            }
            var cfg = new ClientConfig();

            cfg.GetNetworkConfig().AddAddress(hazelcastIP);
            var client = HazelcastClient.NewHazelcastClient(cfg);

            IMap <string, double> mapMetricsAggregatedData = client.GetMap <string, double>("metrics-aggregated-data");
            ICollection <string>  keys = mapMetricsAggregatedData.KeySet();
            MetricItem            metricItem;

            foreach (var key in keys)
            {
                string[] keyParts = key.Split('*');
                if (String.Equals(userName, keyParts[0]))
                {
                    metricItem       = new MetricItem();
                    metricItem.Id    = key;
                    metricItem.Value = mapMetricsAggregatedData.Get(key);;

                    _context.MetricItems.Add(metricItem);
                    await _context.SaveChangesAsync();
                }
            }

            return(await _context.MetricItems.ToListAsync());
        }
예제 #22
0
        /// <summary>
        /// Starts the Hazelcast Client on 127.0.0.1, connects to an already running Hazelcast Cluster and gets the Distributed Map from Cluster
        /// </summary>
        public static void SetupStoreage()
        {
            var client = HazelcastClient.NewHazelcastClient();

            mapMetricsAggregatedData = client.GetMap <string, double>("metrics-aggregated-data");
        }
예제 #23
0
        private static void Main(string[] args)
        {
            Environment.SetEnvironmentVariable("hazelcast.logging.type", "console");
            Environment.SetEnvironmentVariable("hazelcast.client.request.timeout", "250000");

            var clientConfig = new ClientConfig();

            clientConfig.GetNetworkConfig().AddAddress("192.168.2.50:5701");
            clientConfig.GetNetworkConfig().SetConnectionAttemptLimit(1000);
            hazelcast = HazelcastClient.NewHazelcastClient(clientConfig);

            Console.WriteLine("Client Ready to go");

            stats = new Stats();
            //Thread.Sleep(100000);
            if (args != null && args.Length > 0)
            {
                foreach (var _arg in  args)
                {
                    var arg = _arg.Trim();
                    //if (arg.startsWith("t")) {
                    //    THREAD_COUNT = Integer.parseInt(arg.substring(1));
                    //} else if (arg.startsWith("c")) {
                    //    ENTRY_COUNT = Integer.parseInt(arg.substring(1));
                    //} else if (arg.startsWith("v")) {
                    //    VALUE_SIZE = Integer.parseInt(arg.substring(1));
                    //} else if (arg.startsWith("g")) {
                    //    GET_PERCENTAGE = Integer.parseInt(arg.substring(1));
                    //} else if (arg.startsWith("p")) {
                    //    PUT_PERCENTAGE = Integer.parseInt(arg.substring(1));
                    //}
                }
            }
            else
            {
                Console.WriteLine("Help: sh test.sh t200 v130 p10 g85 ");
                Console.WriteLine("    // means 200 threads, value-size 130 bytes, 10% put, 85% get");
                Console.WriteLine("");
            }
            Console.WriteLine("Starting Test with ");
            Console.WriteLine("      Thread Count: " + THREAD_COUNT);
            Console.WriteLine("       Entry Count: " + ENTRY_COUNT);
            Console.WriteLine("        Value Size: " + VALUE_SIZE);
            Console.WriteLine("    Get Percentage: " + GET_PERCENTAGE);
            Console.WriteLine("    Put Percentage: " + PUT_PERCENTAGE);
            Console.WriteLine(" Remove Percentage: " + (100 - (PUT_PERCENTAGE + GET_PERCENTAGE)));


            var tasks = new List <Task>();

            for (var i = 0; i < THREAD_COUNT; i++)
            {
                var t = new Task(() => HzTask(hazelcast), TaskCreationOptions.LongRunning);
                tasks.Add(t);
                t.Start();
            }

            var tm = new Thread(StatDisplayTask);

            tm.Start();

            Task.WaitAll(tasks.ToArray());
            tm.Abort();
            Console.WriteLine("--THE END--");
            //Task.Factory.StartNew(StatDisplayTask);

            //startNew.Wait();

            //StatDisplayTask();

            //Task.Factory.Scheduler.MaximumConcurrencyLevel = THREAD_COUNT;

            Console.ReadKey();
        }