static IModel model;
Public IModel RetrieveSomeSharedIModelInstance ()
{
If (model==null)
{
Var factory=new ConnectionFactory ()
{
The HostName="10.10.0.210",
The UserName="1",
Password="1",
//the Port=options. Value. RabbitPort,
};
IConnection _connection=factory. The CreateConnection ();
The model=_connection. CreateModel ();
Model. QueueDeclare (queue: "Test",
Durable: true,
Exclusive: false,
AutoDelete: false,
The arguments: null);
}
Return the model;
}
Public void SendMessage (string message)
{
Try
{
IModel _channel=RetrieveSomeSharedIModelInstance ();
Var properties.=_channel CreateBasicProperties ();
The properties. DeliveryMode=2;//data model: 1 don't persistent, 2 persistence
String msgJson=DataManage. ObjToJson. ObjectToJsonOfNewton (message);
Var body=System. Text.. Encoding UTF8. GetBytes (msgJson);
The lock (_channel)
{
_channel. BasicPublish (exchange: ", "
RoutingKey: "Test",
BasicProperties: properties,
Body: body);
}
}
The catch (Exception e)
{
Throw (e);
}
}
According to the official documentation, should avoid using IModel instance multiple threads at the same time, when use lock, so this lock can only be added to the BasicPublish, the above code if lock QueueDeclare also on the inside, namely multithreaded statement queue at the same time, will be submitted to the exception: "Pipelining of requests forbidden",
Statement queue only operate single-threaded? Is locked?
If not in before sending the message, how to ensure that the queue is there?