static void clientMessageHandlerProxy_MessageReceived(object sender, MessageReceivedEventArgs e) { ClientMessageHandlerProxy clientMessageHandlerProxy = sender as ClientMessageHandlerProxy; Message <TestMessageType> message = clientMessageHandlerProxy.DeserializeMessage <TestMessageType>(e.MessageBody); switch (message.MessageType) { case TestMessageType.Normal: _Log.Info("Received message from routing key '" + e.RoutingKey + "': " + message.SenderId + ", " + message.SentUtc + ", " + message.MessageType.ToString() + ", " + ASCIIEncoding.Default.GetString(message.Body)); break; case TestMessageType.Bulk: if (!_BulkStopwatch.IsRunning) { lock (_BulkLock) { if (!_BulkStopwatch.IsRunning) { Interlocked.Exchange(ref _BulkMessageCounter, 0); _BulkStopwatch.Start(); } } } Interlocked.Increment(ref _BulkMessageCounter); Interlocked.Exchange(ref _BulkMessageLatency, _BulkMessageLatency + (long)(DateTime.UtcNow - message.SentUtc).TotalMilliseconds); if (_BulkStopwatch.ElapsedMilliseconds >= 10000) { lock (_BulkLock) { _BulkStopwatch.Stop(); _Log.InfoFormat("Bulk received {0} messages per second, avg latency is {1} ms" , (((double)_BulkMessageCounter / (double)_BulkStopwatch.ElapsedMilliseconds) * 1000).ToString("N2") , ((double)_BulkMessageLatency / (double)_BulkMessageCounter).ToString("N2")); Interlocked.Exchange(ref _BulkMessageCounter, 0); _BulkStopwatch.Reset(); _BulkStopwatch.Start(); } } break; case TestMessageType.StopBulk: _BulkStopwatch.Stop(); _BulkStopwatch.Reset(); Interlocked.Exchange(ref _BulkMessageCounter, 0); break; } }
protected void Application_Start() { DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(EmailAttribute), typeof(RegularExpressionAttributeAdapter)); #region initialize string baseDirectory = System.AppDomain.CurrentDomain.BaseDirectory; IApplicationSettings applicationSettings = new ApplicationSettings(ConfigurationManager.AppSettings, ConfigurationManager.ConnectionStrings); log4net.Config.XmlConfigurator.ConfigureAndWatch(new FileInfo(applicationSettings.Log4NetConfigPath)); IMessageClientConfigurationSection section = (IMessageClientConfigurationSection)ConfigurationManager.GetSection("clientSettings"); ClientMessageHandlerProxy clientMessageHandlerProxy = new ClientMessageHandlerProxy(section); ApplicationMessageHandler applicationMessageHandler = new ApplicationMessageHandler(clientMessageHandlerProxy); applicationMessageHandler.RefreshApplicationDataRequest += new EventHandler <EventArgs>(applicationMessageHandler_RefreshApplicationDataRequest); applicationMessageHandler.CommunicationStateChanged += new EventHandler <CommunicationStateChangedEventArgs>(applicationMessageHandler_CommunicationStateChanged); applicationMessageHandler.Connect(); IDataStore dataStore = null; try { string dataStoreContextTypeAssemblyName = applicationSettings.DataStoreContextType.Substring(applicationSettings.DataStoreContextType.LastIndexOf(',') + 1).Trim(); string dataStoreContextTypeName = applicationSettings.DataStoreContextType.Substring(0, applicationSettings.DataStoreContextType.IndexOf(',')); Assembly assembly = Assembly.Load(dataStoreContextTypeAssemblyName); // load into default load context Type type = assembly.GetType(dataStoreContextTypeName); dataStore = Activator.CreateInstance(type) as IDataStore; } catch (Exception ex) { throw new ConfigurationErrorsException("Error loading data store object, see inner exception for further details.", ex); } if (dataStore == null) { throw new ConfigurationErrorsException("Datastore is not provided."); } dataStore.Initialize(applicationSettings.DefaultConnectionString); System.Configuration.Configuration config = WebConfigurationManager.OpenWebConfiguration(HostingEnvironment.ApplicationVirtualPath); MembershipSection configSection = (MembershipSection)config.GetSection("system.web/membership"); MembershipSettings membershipSettings = new MembershipSettings( (NameValueCollection)configSection.Providers[configSection.DefaultProvider].Parameters); Workmate.Components.InstanceContainer.Initialize( dataStore , applicationSettings , membershipSettings); // we need to load this after singletons initialize WorkmateMembershipProvider workmateMembershipProvider = new WorkmateMembershipProvider(); WorkmateRoleProvider workmateRoleProvider = new WorkmateRoleProvider(); ArticleManager articleManager = new ArticleManager(dataStore); // the singletons class must be initialized here so it can be used later on InstanceContainer.Initialize( new ApplicationContext(baseDirectory, applicationMessageHandler, applicationSettings, dataStore) , new TicketManager(applicationSettings, workmateMembershipProvider) , workmateMembershipProvider , workmateRoleProvider , articleManager , new RequestHelper() , new ArticleAttachmentManager(dataStore) , new Workmate.Components.ApplicationManager(dataStore) , new ArticleGroupManager(dataStore) , new ArticleGroupThreadManager(dataStore) , new ApplicationDataCache() , new ProfileImageManager(dataStore) , new SystemProfileImageManager(dataStore) , new EmailManager(dataStore) , new EmailPublisher() , new OfficeManager(dataStore) , new DepartmentManager(dataStore) ); #endregion #region startup AreaRegistration.RegisterAllAreas(); RegisterGlobalFilters(GlobalFilters.Filters); RefreshApplicationData(); #endregion #region messaging #endregion }
static void Main(string[] args) { XmlConfigurator.Configure(); IMessageClientConfigurationSection section = (IMessageClientConfigurationSection)ConfigurationManager.GetSection("clientSettings"); using (ClientMessageHandlerProxy clientMessageHandlerProxy = new ClientMessageHandlerProxy(section)) { clientMessageHandlerProxy.MessageReceived += new EventHandler <MessageReceivedEventArgs>(clientMessageHandlerProxy_MessageReceived); clientMessageHandlerProxy.Connect(); Console.WriteLine("Possible Keys:"); Console.WriteLine("\nsrk RoutingKey".PadRight(78) + " - subscribe to routing key".PadRight(40) + "\n -> srk Orders"); Console.WriteLine("\nurk RoutingKey".PadRight(78) + " - unsubscribe to routing key".PadRight(40) + "\n -> urk Orders"); Console.WriteLine("\ns rk=RoutingKey m=\"Message\"".PadRight(78) + " - send message to routing key".PadRight(40) + "\n -> s rk=Orders m=\"Hellow World\" OR s m=\"Hello World\""); Console.WriteLine("\nbs rk=RoutingKey m=\"Message\" s=TotalMessagesPerBulk t=SleepBetweenBulks".PadRight(78) + " - push messages continously to routing key".PadRight(40) + "\n -> bs rk=Orders m=\"Hellow World\" s=100 t=10"); Console.WriteLine("\nsbs".PadRight(78) + " - stop pushing messages"); Console.WriteLine("\nexit".PadRight(78) + " - stop client"); Console.WriteLine("\n\n"); bool running = true; Regex routingKeyRegex = new Regex(@" rk=([a-zA-Z]*)"); Regex messageRegex = new Regex(@" m=""([a-zA-Z ]*)"""); Regex bulkSizeRegex = new Regex(@" s=([0-9]*)"); Regex sleepThrottleRegex = new Regex(@" t=([0-9]*)"); string cmd, routingKey, message, str; Match match; int bulkSize = 10; int sleepThrottle = 10; while (running) { string line = Console.ReadLine(); if (line.Contains(" ")) { cmd = line.Substring(0, line.IndexOf(' ')).Trim(); } else { cmd = line; } routingKey = null; match = routingKeyRegex.Match(line); if (match.Success) { routingKey = match.Groups[1].Value; } message = string.Empty; match = messageRegex.Match(line); if (match.Success) { message = match.Groups[1].Value; } bulkSize = 10; match = bulkSizeRegex.Match(line); if (match.Success) { bulkSize = int.Parse(match.Groups[1].Value); } sleepThrottle = 10; match = sleepThrottleRegex.Match(line); if (match.Success) { sleepThrottle = int.Parse(match.Groups[1].Value); } switch (cmd) { case "srk": str = line.Substring(line.IndexOf(' ')).Trim(); if (!string.IsNullOrEmpty(str)) { clientMessageHandlerProxy.Subscribe(str); } break; case "urk": str = line.Substring(line.IndexOf(' ')).Trim(); if (!string.IsNullOrEmpty(str)) { clientMessageHandlerProxy.Unsubscribe(str); } break; case "s": clientMessageHandlerProxy.Publish <TestMessageType>(TestMessageType.Normal, routingKey, ASCIIEncoding.Default.GetBytes(message)); break; case "bs": if (_KeepBulkSending) { _KeepBulkSending = false; Console.WriteLine("Waiting for bulk push to stop..."); _BulkSendFinished.WaitOne(); Console.WriteLine("Bulk push stopped"); } _KeepBulkSending = true; ThreadPool.QueueUserWorkItem(delegate(object state) { Console.WriteLine("Start pushing messages"); while (_KeepBulkSending) { for (int i = 0; i < bulkSize; i++) { clientMessageHandlerProxy.Publish <TestMessageType>(TestMessageType.Bulk, routingKey, ASCIIEncoding.Default.GetBytes(message)); } Thread.Sleep(sleepThrottle); } _BulkSendFinished.Set(); }); break; case "sbs": if (_KeepBulkSending) { _KeepBulkSending = false; Console.WriteLine("Waiting for bulk push to stop..."); _BulkSendFinished.WaitOne(); clientMessageHandlerProxy.Publish <TestMessageType>(TestMessageType.StopBulk, routingKey, null); Console.WriteLine("Bulk push stopped"); } break; case "exit": running = false; break; } } } }