//public void InitializeBus(string queName = null, string exchangeName = null, string routingKey = null)
 //{
 //    if (!string.IsNullOrEmpty(queName)) this.RabbitSettings.QueueName = queName;
 //    if (!string.IsNullOrEmpty(exchangeName)) this.RabbitSettings.ExchangeName = exchangeName;
 //    if (!string.IsNullOrEmpty(routingKey)) this.RabbitSettings.RoutingKey = routingKey;
 //}
 public bool Submit(PlatformActivity activity)
 {
     var message = Newtonsoft.Json.JsonConvert.SerializeObject(activity);
     var messageBodyBytes = Encoding.UTF8.GetBytes(message);
     this.Channel.BasicPublish(this.RabbitSettings.ExchangeName, this.RabbitSettings.RoutingKey, null, messageBodyBytes);
     this.ecapLogger.CreateInfoLog(this.GetType(), System.Reflection.MethodBase.GetCurrentMethod(), new Dictionary<string, dynamic>()
     {
         { this.RabbitSettings.GetVariableName(() => this.RabbitSettings), this.RabbitSettings },
         { activity.GetVariableName(() => activity), activity }
     });
     this.Connection.Close();
     this.Channel.Close();
     return true;
 }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            args = new[] {"1", "r", "1", "5", "0"};
            var watch = System.Diagnostics.Stopwatch.StartNew();
            // the code that you want to measure comes here

            //try
            //{
            //    PlatformActivityTypes[] patArr = new[] { PlatformActivityTypes.Insert, PlatformActivityTypes.Update, PlatformActivityTypes.Delete };

            //    Random rnd = new Random();
            //    for (int i = 0; i < 1; i++)
            //    {
            //        string routingKey = QueueingHelper.GetRoutinKey("tenant1", typeof(Person), patArr[i % 3]);
            //        IServiceBus bus = new RabbitMQBusProvider().GetBus(queName: "Default-Queue", exchangeName: null, routingKey: routingKey);
            //        Person oldPerson = new Person()
            //        {
            //            DisplayName = string.Format("old name = {0}", i + 1)
            //        };
            //        Person newPerson = new Person()
            //        {
            //            DisplayName = string.Format("new name = {0}", i + 1)
            //        };

            //        PlatformActivity activity = new PlatformActivity()
            //        {
            //            Id = string.Format("userId = {0}", i + 1),
            //            Type = PlatformActivityTypes.Update,
            //            PreviousPlayload = Newtonsoft.Json.JsonConvert.SerializeObject(oldPerson, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }),
            //            LatestPlayload = Newtonsoft.Json.JsonConvert.SerializeObject(newPerson, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }),
            //            PublishTime = DateTime.UtcNow,
            //            ServerInfo = Newtonsoft.Json.JsonConvert.SerializeObject(ServerHealth.Info, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }),
            //            SiteToken = new SiteToken() { UserId = "fakeUser" },
            //            ServiceName = PlatformServices.PlatformDataService
            //        };
            //        bus.Submit(activity);
            //        Console.WriteLine("A new activity has been published with routing key => {0}.............", routingKey);
            //        Thread.Sleep(rnd.Next(800, 1000));
            //    }
            //}
            //catch (Exception exception)
            //{
            //    int u = 0;
            //    //throw;
            //}

            int idStart = 1, idEnd = 4;
            string itr = args[0];
            Random r = new Random();
            int iteration = 0;
            Int32.TryParse(itr, out iteration);
            bool isRandom = false;
            Console.Write("press R to choose data random : ");
            string com = args[1];
            if (!string.IsNullOrWhiteSpace(com) && com.ToLower()[0] == 'r')
            {
                isRandom = true;
                Console.Write("Enter an id range : ");
                //string data = args[2];//Console.ReadLine();
                //var x = data.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                idStart = int.Parse(args[2]);
                idEnd = int.Parse(args[3]);
            }

            Console.Write("Enter sleep time : ");
            int sleepTime = Int32.Parse(args[args.Count() - 1]);

            for (int i = 0; i < iteration; i++)
            {
                IServiceBus bus = new RabbitMQBusProvider().GetBus(queName: "Default-Queue", exchangeName: null, routingKey: null);
                Person oldPerson = new Person()
                {
                    DisplayName = string.Format("old name = {0}", i + 1)
                };
                Person newPerson = new Person()
                {
                    DisplayName = string.Format("new name = {0}", i + 1)
                };

                PlatformActivity activity = new PlatformActivity()
                {
                    Id = isRandom ? r.Next(idStart, idEnd).ToString() : string.Format("userId = {0}", i + 1),
                    Type = PlatformActivityTypes.Update,
                    PreviousPlayload = Newtonsoft.Json.JsonConvert.SerializeObject(oldPerson, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }),
                    LatestPlayload = Newtonsoft.Json.JsonConvert.SerializeObject(newPerson, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }),
                    PublishTime = DateTime.UtcNow,
                    ServerInfo = Newtonsoft.Json.JsonConvert.SerializeObject(ServerHealth.Info, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }),
                    SiteToken = new SiteToken() { UserId = "fakeUser" },
                    ServiceName = PlatformServices.PlatformDataService
                };
                bus.Submit(activity);
                Thread.Sleep(sleepTime);
            }

            watch.Stop();
            var elapsedMs = watch.ElapsedMilliseconds;
            Console.WriteLine("Time elapsed {0} seconds", (double)elapsedMs / 1000);
            Console.ReadLine();
        }