public void Publish(IWork work, string routingKey = null, IBasicProperties properties = null) { if (work == null) throw new ArgumentNullException(nameof(work)); if (string.IsNullOrWhiteSpace(routingKey)) throw new ArgumentNullException(nameof(routingKey)); if (properties == null) { properties = this.Channel.CreateBasicProperties(); //properties.AppId = string.Empty; //properties.ClusterId = string.Empty; //properties.ContentEncoding = string.Empty; //properties.ContentType = "application/json"; // Set by the Message Serializer //properties.CorrelationId = string.Empty; properties.DeliveryMode = Constants.Persistent; //properties.Expiration = string.Empty; //properties.MessageId = string.Empty; //properties.Priority = 0; //properties.ReplyTo = string.Empty; //properties.Type = msg.GetType().Name; // Set by the Message Serializer, use this field to store the type of object encoded in the Body. Allows for a factory deserializer to easily determine what to return //properties.UserId = string.Empty; } if (!this.Channel.IsOpen) throw new InvalidOperationException("Channel is not open"); var bytes = WorkSerializer.Serialize(work); properties.ContentEncoding = WorkSerializer.ContentEncoding; properties.ContentType = WorkSerializer.ContentType; properties.Type = work.GetType().FullName; var WorkExchange = Constants.WorkExchangeSettings; this.Channel.ExchangeDeclare(WorkExchange.Name, WorkExchange.ExchangeType, WorkExchange.Durable, WorkExchange.AutoDelete, WorkExchange.Arguments); this.Channel.BasicPublish(WorkExchange.Name, routingKey, properties, bytes); }
private void WorkItemHandler(IBasicProperties props, IWork iWork) { var work = iWork as SampleWorkItem; if (work == null) throw new Exception($"Don't know how to work on '{iWork.GetType().FullName}' items"); this.InternalState = InternalStateEnum.Busy; var currentStatus = new ServiceComponentStatus() { Process = this.Process, ServiceComponent = this.GetType().Name, Status = "Working", SubStatus = "Starting" }; this.PublishEvent(currentStatus); var workItemType = work.ItemType; var workItemId = work.ItemId; var workDelay = work.WorkDelay; currentStatus.SubStatus = "Executing"; this.PublishEvent(currentStatus); Thread.Sleep(workDelay); currentStatus.SubStatus = "Completing"; this.PublishEvent(currentStatus); this.InternalState = InternalStateEnum.Idle; this.PublishEvent("Idle"); }