Exemple #1
0
        /// <summary>Send a message to another service</summary>
        /// <param name="receiverId">Identifier of the service receiving the message</param>
        /// <param name="commonMessage">Message sent</param>
        /// <returns>Response of the common message sent</returns>
        public async Task <CommonMessage> SendCommonMessage(string receiverId, CommonMessage commonMessage)
        {
            var receiverIdKey = Constants.ReceiverIdPrefix + receiverId;
            var request       = Client.GetQueue <CommonMessage>(receiverIdKey);
            var isSent        = Task.Run(() => request.Offer(commonMessage)).Result;

            if (!isSent)
            {
                throw new Exception("Unable to send the common message with id " + commonMessage.Id + "!");
            }

            BusConnection.DisplayCommonMessage(commonMessage);

            var response = Task.Run(() => Client.GetQueue <CommonMessage>(_idKey).Take()).Result;

            BusConnection.DisplayCommonMessage(response);

            if (!response.Id.Equals(commonMessage.Id))
            {
                throw new Exception("Common message response's id " + response.Id + " is not the same as the common message request's id " + commonMessage.Id + "!");
            }

            return(response);
        }
        private static void Main11(string[] args)
        {
            Environment.SetEnvironmentVariable("hazelcast.logging.class", "console");

            Console.WriteLine("INIT APP");
            ClientConfig clientConfig = new ClientConfig();

            clientConfig.SetNetworkConfig(new ClientNetworkConfig().AddAddress(("127.0.0.1:5701")));
            IHazelcastInstance client = HazelcastClient.NewHazelcastClient(clientConfig);
            var queue = client.GetQueue <byte[]>("a queue");

            var watch = Stopwatch.StartNew();
            var task  = Task.Factory.StartNew(
                () =>
            {
                byte[] loop = queue.Poll(15, TimeUnit.SECONDS);
                while (loop != null && loop.Length != 0)
                {
                    loop = queue.Poll(15, TimeUnit.SECONDS);
                    if (loop != null)
                    {
                        var a = new [] { loop[0], loop[1] };
                        Console.WriteLine(BitConverter.ToInt16(a, 0));
                    }
                }
            });

            const int byteLen = 1024 * 500;

            byte[] buf = new byte[byteLen];

            for (int i = 0; i < 1000; ++i)
            {
                byte[] bytes = BitConverter.GetBytes((short)i);
                Array.Copy(bytes, buf, bytes.Length);
                queue.Put(buf);
            }

            queue.Put(new byte[0]);

            task.Wait();
            var elapsed = watch.Elapsed;

            Console.WriteLine("Time: {0}", elapsed.TotalMilliseconds);

            client.Shutdown();
        }