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
{
///
/// 功能描述:MessageQueueService 消息队列服务
/// 作 者:董雪夫
/// 编写日期:2017.8.22
///
public class MessageQueueService : IService
{
private static ConcurrentQueue queue;
private bool _running;
internal MessageQueueService()
{
queue = new ConcurrentQueue();
}
///
/// 开启消息队列服务
///
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);
}
}
///
/// 关闭
///
public void Stop()
{
_running = false;
}
///
/// 把消息加入消息队列服务
///
/// 消息实体
public static void Enqueue(CommonMessage msg)
{
queue.Enqueue(msg);
}
///
/// 从消息队列服务读取服务
///
/// 消息实体
private static CommonMessage Dequeue()
{
CommonMessage msg;
queue.TryDequeue(out msg);
return msg;
}
}
}