修改BUG:使用线程安全的堆栈队列,排队消息,防止漏掉消息不推送

This commit is contained in:
Argo-Lenovo 2017-04-06 18:25:51 +08:00
parent 42c1c0db29
commit 40ad692272
4 changed files with 18 additions and 23 deletions

View File

@ -23,7 +23,7 @@ namespace Bootstrap.Admin
if (publisherElement.Mode == PublisherMode.Off) return;
ExceptionHelper.Log(ex, additionalInfo);
CacheManager.Clear(k => k == ExceptionHelper.RetrieveExceptionsDataKey);
NotificationHelper.PushMessage(new MessageBody() { Category = "Notification", Message = ex.Message });
NotificationHelper.MessagePool.Enqueue(new MessageBody() { Category = "Notification", Message = ex.Message });
}
}
}

View File

@ -43,15 +43,18 @@ namespace Bootstrap.Admin.Controllers
WebSocket socket = arg.WebSocket;
while (socket.State == WebSocketState.Open)
{
if (!NotificationHelper.Push)
if (NotificationHelper.MessagePool.IsEmpty)
{
await System.Threading.Tasks.Task.Delay(300);
continue;
}
ArraySegment<byte> buffer = new ArraySegment<byte>(new byte[1024]);
buffer = new ArraySegment<byte>(Encoding.UTF8.GetBytes(NotificationHelper.Message.ToString()));
await socket.SendAsync(buffer, WebSocketMessageType.Text, true, CancellationToken.None);
NotificationHelper.Push = false;
var msg = new MessageBody();
if (NotificationHelper.MessagePool.TryDequeue(out msg))
{
ArraySegment<byte> buffer = new ArraySegment<byte>(new byte[1024]);
buffer = new ArraySegment<byte>(Encoding.UTF8.GetBytes(msg.ToString()));
await socket.SendAsync(buffer, WebSocketMessageType.Text, true, CancellationToken.None);
}
}
}
}

View File

@ -2,6 +2,7 @@
using Longbow.Caching;
using Longbow.ExceptionManagement;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
@ -14,8 +15,15 @@ namespace Bootstrap.DataAccess
/// </summary>
public static class NotificationHelper
{
/// <summary>
///
/// </summary>
internal const string RetrieveNotificationsDataKey = "NotificationHelper-RetrieveNotifications";
/// <summary>
///
/// </summary>
public static readonly ConcurrentQueue<MessageBody> MessagePool = new ConcurrentQueue<MessageBody>();
/// <summary>
/// 新用户注册的通知的面板显示
/// </summary>
/// <returns></returns>
@ -112,22 +120,6 @@ namespace Bootstrap.DataAccess
}
return ret;
}
/// <summary>
/// 推送的消息
/// </summary>
public static MessageBody Message { get; private set; }
/// <summary>
/// 获得/设置 是否推送消息
/// </summary>
public static bool Push { get; set; }
/// <summary>
///
/// </summary>
public static void PushMessage(MessageBody message)
{
Push = true;
Message = message;
}
}
/// <summary>
///

View File

@ -145,7 +145,7 @@ namespace Bootstrap.DataAccess
}
CacheCleanUtility.ClearCache(userIds: p.Id == 0 ? string.Empty : p.Id.ToString());
ret = true;
if (p.UserStatus == 1) NotificationHelper.PushMessage(new MessageBody() { Category = "Users", Message = string.Format("{0}-{1}", p.UserName, p.Description) });
if (p.UserStatus == 1) NotificationHelper.MessagePool.Enqueue(new MessageBody() { Category = "Users", Message = string.Format("{0}-{1}", p.UserName, p.Description) });
}
catch (DbException ex)
{