778
社区成员
发帖
与我相关
我的任务
分享
static IModel model;
public IModel RetrieveSomeSharedIModelInstance()
{
if (model == null)
{
var factory = new ConnectionFactory()
{
HostName = "10.10.0.210",
UserName = "1",
Password = "1",
//Port = options.Value.RabbitPort,
};
IConnection _connection = factory.CreateConnection();
model = _connection.CreateModel();
model.QueueDeclare(queue: "Test",
durable: true,
exclusive: false,
autoDelete: false,
arguments: null);
}
return model;
}
public void SendMessage(string message)
{
try
{
IModel _channel = RetrieveSomeSharedIModelInstance();
var properties = _channel.CreateBasicProperties();
properties.DeliveryMode = 2;//数据模式:1不持久化,2持久化
string msgJson = DataManage.ObjToJson.ObjectToJsonOfNewton(message);
var body = System.Text.Encoding.UTF8.GetBytes(msgJson);
lock (_channel)
{
_channel.BasicPublish(exchange: "",
routingKey: "Test",
basicProperties: properties,
body: body);
}
}
catch (Exception e)
{
throw (e);
}
}