天津投入产出系统后端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

75 lines
1.8 KiB

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using QM.Exchange.Interface;
using System.Collections.Concurrent;
using QM.Exchange.Core.ProcessProgram;
namespace QM.Exchange.Core.Services
{
///<summary>
/// 功能描述:MessageQueueService 消息队列服务
/// 作 者:董雪夫
/// 编写日期:2017.8.22
///</summary>
public class MessageQueueService : IService
{
private static ConcurrentQueue<CommonMessage> queue;
private bool _running;
internal MessageQueueService()
{
queue = new ConcurrentQueue<CommonMessage>();
}
/// <summary>
/// 开启消息队列服务
/// </summary>
public void Start()
{
_running = true;
while (_running)
{
var msg = Dequeue();
if (msg != null)
{
CommandExecutor executor = new CommandFactory().CreateExecutor(msg);
executor.Execute();
}
System.Threading.Thread.Sleep(10);
}
}
/// <summary>
/// 关闭
/// </summary>
public void Stop()
{
_running = false;
}
/// <summary>
/// 把消息加入消息队列服务
/// </summary>
/// <param name="msg">消息实体</param>
public static void Enqueue(CommonMessage msg)
{
queue.Enqueue(msg);
}
/// <summary>
/// 从消息队列服务读取服务
/// </summary>
/// <returns>消息实体</returns>
private static CommonMessage Dequeue()
{
CommonMessage msg;
queue.TryDequeue(out msg);
return msg;
}
}
}