using EasyNetQ;
using EasyNetQ.Topology;
using System;
using System.Collections.Generic;
using System.Text;
using Newtonsoft.Json;
using System.Threading.Tasks;
namespace HKLib.RabbitMQ.Config
{
public class PushManage
{
///
/// 发布消息队列异步
///
///
///
///
///
public async Task SendMQAsync(PushEntity Param, IBus Bus)
{
//one to one
var msg = new Message(Param.BodyData);
IExchange EX = null;
if (Param.SendType == MQEnum.Sub)
{
EX = Bus.Advanced.ExchangeDeclare(Param.ExchangeName, ExchangeType.Fanout);
}
if (Param.SendType == MQEnum.Push)
{
EX = Bus.Advanced.ExchangeDeclare(Param.ExchangeName, ExchangeType.Direct);
}
if (Param.SendType == MQEnum.Top)
{
EX = Bus.Advanced.ExchangeDeclare(Param.ExchangeName, ExchangeType.Topic);
}
await Bus.Advanced.PublishAsync(EX, Param.RouteName, false, msg).ContinueWith(t =>
{
//消息投递失败
if (!t.IsCompleted && t.IsFaulted)
{
// //将消息记录到数据库轮询
// IRepository Repository = new Repository();
// SystemLog Log = new SystemLog
// {
// LogName = "消息队列",
// Source = "发布队列",
// EventData = JsonConvert.SerializeObject(Param.BodyData)
// };
// Repository.Insert(Log);
}
});
}
///
/// 发布消息队列同步
///
///
///
///
public void SendMQ(PushEntity Param, IBus Bus)
{
//one to one
var msg = new Message(Param.BodyData);
IExchange EX = null;
if (Param.SendType == MQEnum.Sub)
{
EX = Bus.Advanced.ExchangeDeclare(Param.ExchangeName, ExchangeType.Fanout);
}
if (Param.SendType == MQEnum.Push)
{
EX = Bus.Advanced.ExchangeDeclare(Param.ExchangeName, ExchangeType.Direct);
}
if (Param.SendType == MQEnum.Top)
{
EX = Bus.Advanced.ExchangeDeclare(Param.ExchangeName, ExchangeType.Topic);
}
Bus.Advanced.Publish(EX, Param.RouteName, false, msg);
}
}
}