public override MessageItem ProcessMessageItem(MessageItem messageItem) { if (messageItem == null || messageItem.CustomData == null || !messageItem.CustomData.ContainsKey("FeedBackId")) { throw new Exception("Invalid message item received in FeedBackProcessor"); } string feedBackId = messageItem.CustomData["FeedBackId"]; var feedBack = _feedBackManager.GetFeedBack(feedBackId); var account = _accountManager.GetUserById(feedBack.FromUserId); _blackListDataManager.SaveBlockedUser(new BlockedUser() { ByEmail = feedBack.EmailId, ToEmail = account.UserName }); var blockedCount = _blackListDataManager.GetBlockedCountForUser(account.UserName); if (blockedCount >= TaskConfig.BlockLimit) { _accountManager.BlockUser(account.Id); } messageItem.IsExit = true; return messageItem; }
public override MessageItem ProcessMessageItem(MessageItem messageItem) { try { IEmailMessageHandler emailHandler = GetEmailMessageHandler(messageItem); if (emailHandler != null) { var isSuccessful = emailHandler.SendEmail(messageItem); if (!isSuccessful) { throw new Exception("Failed to send email!"); } } else { throw new Exception("Invalid email handler!"); } } catch (Exception ex) { Logger.LogException(ex, _soruce, "ProcessMessageItem",Severity.Critical); Logger.LogFailedMessage(messageItem); } messageItem.IsExit = true; // Drop the message item return messageItem; }
private IEmailMessageHandler GetEmailMessageHandler(MessageItem messageItem) { //if (messageItem is FeedBackMessageItem) //{ // return new FeedBackEmailHandler(); //} //if (messageItem is AccountMessageItem) //{ // return new AccountEmailHandler(); //} //if (messageItem is AddFriendMessageItem) //{ // return new AddFriendEmailHandler(); //} //if (messageItem is SendReplyMessageItem) //{ // return new SendReplyEmailHandler(); //} return null; }
public void Start() { MessageItem baseMessageItem = null; try { while (IsActive) { baseMessageItem = ConsumeMessageItem(); if (baseMessageItem != null && !string.IsNullOrEmpty(baseMessageItem.RoutingKey) && !string.IsNullOrEmpty(baseMessageItem.MessageId) && !baseMessageItem.IsExit) { _sleepTime = 5000; MessageItem processedItem = null; var isExceptionOccurred = false; try { processedItem = ProcessMessageItem(baseMessageItem); } catch (StopProcessingException) { throw; } catch (Exception ex) { Logger.LogException(ex, _source, "Start", Severity.Critical); if (processedItem == null) { processedItem = new MessageItem() { MessageId = baseMessageItem.MessageId, IsExit = true }; } isExceptionOccurred = true; } finally { if (processedItem == null) processedItem = new MessageItem() { MessageId = baseMessageItem.MessageId, RoutingKey = baseMessageItem.RoutingKey }; processedItem.StackTrace = processedItem.StackTrace ?? new List<string>(); processedItem.StackTrace.Add(GetType().Name); if (isExceptionOccurred) Logger.LogFailedMessage(baseMessageItem); processedItem.RoutingKey = PublishRoutingKey; } if (processedItem.MessageId != baseMessageItem.MessageId) { throw new Exception("Process method returned an Invalid QueueItem. MessageId Do not match."); } if (processedItem.IsExit || PublishProcessedItem(processedItem)) { ConsumeHandler.DeleteMessageItem(RabbitMqChannel, baseMessageItem.MessageId); } else { ConsumeHandler.RequeueMessageItem(RabbitMqChannel, processedItem.MessageId); } _rabbitMqChannel = null; } else if (baseMessageItem != null) { throw new Exception("Invalid QueueItem. Does not contain complete information."); } else // If no message in the queue then wait for some time { if (IsActive) Sleep(); } } } catch (StopProcessingException processingException) { processingException.Data["Message"] = "Message to stop execution received from task."; Logger.LogException(processingException, _source, "Start",Severity.Critical); } catch (Exception ex) { Logger.LogException(ex, _source, "Start", Severity.Critical); if (baseMessageItem != null) { baseMessageItem.IsExit = true; } } finally { _taskWaitHandle.Close(); _stopWaitHandle.Set(); } }
public abstract MessageItem ProcessMessageItem(MessageItem messageItem);
protected bool PublishProcessedItem(MessageItem messageItem) { bool success = false; try { if (messageItem == null || string.IsNullOrEmpty(messageItem.RoutingKey)) throw new ArgumentException("Routing key is missing from message item"); success = PublishHandler.Publish(messageItem); } catch (Exception ex) { Logger.LogException(ex, _source, "PublishProcessedItem", Severity.Critical); } return success; }
public bool UpdateFeedBackStatus(string feedBackId, string status) { try { if (string.Equals(status, "blocked", StringComparison.OrdinalIgnoreCase)) { var messageItem = new MessageItem() { CustomData = new Dictionary<string, string>(){{"FeedBackId", feedBackId}}, RoutingKey = RoutingKeys.BLOCK_USER }; _queue.Publish(messageItem); } return _feedBackDataManager.UpdateFeedBackStatus(feedBackId, status); } catch (Exception ex) { Logger.LogException(ex, _source, "UpdateFeedBackStatus", Severity.Normal); return false; } }
public bool Publish(MessageItem item) { var settings = new JsonSerializerSettings() { TypeNameHandling = TypeNameHandling.Objects, }; String jsonified = JsonConvert.SerializeObject(item, settings); byte[] message = Encoding.UTF8.GetBytes(jsonified); Connection = RQConnection.GetConnection(); using (RQChannel rabbitMqChannel = new RQChannel(Connection)) { if (string.IsNullOrEmpty(ExchangeName)) rabbitMqChannel.Channel.BasicPublish(string.Empty, QueueName, true, false, rabbitMqChannel.BasicProperties, message); else rabbitMqChannel.Channel.BasicPublish(ExchangeName, item.RoutingKey, true, false, rabbitMqChannel.BasicProperties, message); //string exchange, string routingKey, bool mandatory, bool immediate, IBasicProperties basicProperties, byte[] body } return true; }