示例#1
0
//		private static async Task StartConcurrentRpc()
//		{
//			Connection conn2 = null;
//			try
//			{
//				conn2 = await new ConnectionFactory().Connect(TargetHost,
//					vhost: VHost, username: _username, password: _password);
//
//				Console.WriteLine("[Connected]");
//
//				Console.WriteLine("Starting Rpc Parallel calls...");
//
//				var newChannel2 = await conn2.CreateChannel();
//				Console.WriteLine("[channel created] " + newChannel2.ChannelNumber);
//				await newChannel2.BasicQos(0, Prefetch, false);
//
//				var rpcHelper = await newChannel2.CreateRpcHelper(ConsumeMode.ParallelWithBufferCopy);
//
//				var watch = new Stopwatch();
//				watch.Start();
//
//				var totalReceived = 0;
//
//
//				var tasks = new Task[ConcurrentCalls];
//
//				for (int i = 0; i < TotalPublish; i += ConcurrentCalls)
//				{
//					for (int j = 0; j < ConcurrentCalls; j++)
//					{
//						var t = MakeCall(rpcHelper, i + j);
//						tasks[j] = t;
//					}
//
//					Task.WaitAll(tasks);
//
//					totalReceived += ConcurrentCalls;
//
////					Console.WriteLine("calls " + totalReceived);
//
//					if (totalReceived >= TotalPublish)
//					{
//						watch.Stop();
//						Console.WriteLine("Rpc stress done. Took " +
//										  watch.Elapsed.TotalMilliseconds +
//										  "ms - rate of " + (TotalPublish / watch.Elapsed.TotalSeconds) + " messages per second");
//						totalReceived = 0;
//					}
//				}
//
//				await Task.Delay(TimeSpan.FromMinutes(5));
//
//				await newChannel2.Close();
//			}
//			catch (AggregateException ex)
//			{
//				Console.WriteLine("[Captured error] " + ex.Message);
//			}
//			catch (Exception ex)
//			{
//				Console.WriteLine("[Captured error 2] " + ex.Message);
//			}
//
//			if (conn2 != null)
//				conn2.Dispose();
//		}

        private static async Task <int> MakeCall(RpcHelper rpcHelper, int y)
        {
            var prop2 = new BasicProperties();
            var req   = new byte[4];

            req[3] = (byte)((y & 0xFF000000) >> 24);
            req[2] = (byte)((y & 0x00FF0000) >> 16);
            req[1] = (byte)((y & 0x0000FF00) >> 8);
            req[0] = (byte)((y & 0x000000FF));

            var rpcCallResult = await rpcHelper.Call("test_ex", "rpc1", prop2, new ArraySegment <byte>(req, 0, 4));

            if (rpcCallResult.stream != null)
            {
                var reply = new byte[4];
                rpcCallResult.stream.Read(reply, 0, rpcCallResult.bodySize);
                var x = BitConverter.ToInt32(reply, 0);
                if (x != y)
                {
                    throw new Exception("Invalid result for call");
                }
            }
//			else if (rpcCallResult.Body != null)
//			{
//				var x = BitConverter.ToInt32(rpcCallResult.Body, 0);
//				if (x != y) throw new Exception("Invalid result for call");
//			}

//			Console.WriteLine("Call " + y + " completed");

            return(y);
        }