public DeferredProcessExitCode Run(MessageBroker messageBroker, bool runImmediately) { if (runImmediately || messageBroker == null) { try { DeferredProcessExitCode exitcode = this.Execute(messageBroker); //CloudLogger.Instance.Info(this.GetType().Name, "Run", "Execute", exitcode.ToString(), this.ToJSON()); return(exitcode); } catch { return(DeferredProcessExitCode.Error); } } else { try { messageBroker.SendDeferredProcessAsMessage(this); return(DeferredProcessExitCode.Success); } catch { return(DeferredProcessExitCode.Error); } } }
public static DeferredProcessExitCode CreateAndExecute(MessageBroker messageBroker, BrokeredMessage message, int instanceNumber, int threadNumber) { DeferredProcessExitCode retval = DeferredProcessExitCode.NothingToDo; DeferredProcess newprocess = null; try { Type deferredType = Assembly.GetAssembly(typeof(DeferredProcess)).GetType(message.Properties["Class"].ToString()); if (deferredType == null) { return(DeferredProcessExitCode.NothingToDo); } newprocess = (DeferredProcess)Activator.CreateInstance(deferredType); newprocess.ID = message.Properties["ID"] != null ? message.Properties["ID"].ToString() : ""; foreach (PropertyInfo pi in deferredType.GetProperties()) { Type declaringType = pi.PropertyType; try { switch (declaringType.Name) { case "Double": pi.SetValue(newprocess, double.Parse(message.Properties[pi.Name].ToString()), null); break; case "Float": pi.SetValue(newprocess, float.Parse(message.Properties[pi.Name].ToString()), null); break; case "Int64": pi.SetValue(newprocess, Int64.Parse(message.Properties[pi.Name].ToString()), null); break; case "Int32": pi.SetValue(newprocess, Int32.Parse(message.Properties[pi.Name].ToString()), null); break; case "DateTime": pi.SetValue(newprocess, DateTime.Parse(message.Properties[pi.Name].ToString()), null); break; case "Boolean": pi.SetValue(newprocess, Boolean.Parse(message.Properties[pi.Name].ToString()), null); break; default: pi.SetValue(newprocess, message.Properties[pi.Name], null); break; } } catch { //Ignore the property setting error } } //Time the execution long ts = DateTime.UtcNow.Ticks; retval = newprocess.Execute(messageBroker); double milliseconds = (new TimeSpan(DateTime.UtcNow.Ticks - ts)).TotalMilliseconds; } catch { retval = DeferredProcessExitCode.Error; } finally { newprocess = null; } return(retval); }
private void Run() { hubmessagingtopic = "skillcow"; // RoleEnvironment.GetConfigurationSettingValue("servicebustopic" + messageTopicNumber); // TokenProvider and URI of our service namespace tP = TokenProvider.CreateSharedSecretTokenProvider(issuer, key); uri = ServiceBusEnvironment.CreateServiceUri("sb", "chaindateservicebus", string.Empty); // Create NamespaceManager for the "HowToSample" service namespace NamespaceManager namespaceManager = new NamespaceManager(uri, tP); while (true) { try { //Wait for topic to become available if (namespaceManager.TopicExists(hubmessagingtopic)) { // Create a new "AllMessages" subscription on our "TestTopic" if (!namespaceManager.SubscriptionExists(hubmessagingtopic, "allmessages")) { namespaceManager.CreateSubscription(hubmessagingtopic, "allmessages"); } break; } Thread.Sleep(1000); } catch { } } // URI address and token for our "HowToSample" namespace tP = TokenProvider.CreateSharedSecretTokenProvider(issuer, key); uri = ServiceBusEnvironment.CreateServiceUri("sb", "chaindateservicebus", string.Empty); // Retrieve MessageReceiver for the "AllMessages" subscription factory = MessagingFactory.Create(uri, tP); MessageReceiver messageReceiver = factory.CreateMessageReceiver(hubmessagingtopic + "/subscriptions/AllMessages"); //For resending messages //topic = factory.CreateMessageSender(hubmessagingtopic); messageBroker = new MessageBroker(messageTopicNumber); Random random = RandomHelper.Instance; // Continuously process messages received from the "HighMessages" subscription while (true) { BrokeredMessage message = null; while (true) //Keep reconnecting { try { message = messageReceiver.Receive(); break; } catch { tP = TokenProvider.CreateSharedSecretTokenProvider(issuer, key); uri = ServiceBusEnvironment.CreateServiceUri("sb", "chaindateservicebus", string.Empty); factory = MessagingFactory.Create(uri, tP); messageReceiver = factory.CreateMessageReceiver(hubmessagingtopic + "/subscriptions/AllMessages"); } } if (message != null) { try { string msgtype = message.GetBody <string>(); Interlocked.Increment(ref MessagesProcessed); switch (msgtype) { case "deferred": DeferredProcessExitCode deferredProcessExitCode = DeferredProcess.CreateAndExecute(messageBroker, message, instanceNumber, threadNumber); break; default: break; } try { message.Complete(); } catch { } } catch (Exception ex) { // Indicate a problem, unlock message in subscription message.Abandon(); } finally { } } } }