实体类:
DocumentType.cs
public enum DocumentType
{
//日志
Journal = 1,
//论文
Thesis = 2,
//会议文件
Meeting = 3
}
MessageModel.cs
public class MessageModel
{
public string Title { get; set; }
public string Author { get; set; }
public DocumentType DocType { get; set; }
public override string ToString()
{
return Title;
}
/// <summary>
/// 验证消息,Title与Author不能为空
/// </summary>
/// <returns></returns>
public bool IsVlid()
{
return !string.IsNullOrWhiteSpace(Title) && !string.IsNullOrWhiteSpace(Author);
}
}
异常代码类MessageException.cs:
public class MessageException : Exception
{
public MessageException(string message) : base(message)
{
}
public MessageException(string message, Exception innerException) : base(message, innerException)
{
}
}
public class NoRPCConsumeException : Exception
{
public NoRPCConsumeException(string message) : base(message)
{
}
public NoRPCConsumeException(string message, Exception innerException) : base(message, innerException)
{
}
}
①第一个控制台应用程序
namespace PCApp
{
class Program
{
private static IConnection _senderConnection;
private static IModel _channel;
private static int _interval = 1; //消息发送间隔
private static bool isExit;
private static void Main(string[] args)
{
Setup();
Console.WriteLine("准备发送消息到extractQueue队列:");
Send();
WaitCommand();
}
/// <summary>
/// 初始化
/// </summary>
private static void Setup()
{
var factory = new ConnectionFactory
{
HostName = "localhost",
UserName = "guest",
Password = "guest",
// virtual host只是起到一个命名空间的作用,所以可以多个user共同使用一个virtual host,
//vritual_host= '/',这个是系统默认的,就是说当我们创建一个到rabbitmq的connection时候,它的命名空间是'/',需要注意的是不同的命名空间之间的资源是不能访问的,比如 exchang,queue ,bingding等
//VirtualHost = "test",
AutomaticRecoveryEnabled = true,
TopologyRecoveryEnabled = true
};
try
{
_senderConnection = factory.CreateConnection();
_senderConnection.ConnectionShutdown += _senderConnection_ConnectionShutdown;
_channel = _senderConnection.CreateModel();
_channel.QueueDeclare("extractQueue", false, false, false, null);
}
catch (BrokerUnreachableException ex)
{
Console.WriteLine("ERROR: RabbitMQ服务器未启动!");
Thread.Sleep(2000);
isExit = true;
}
}
private static void _senderConnection_ConnectionShutdown(object sender, ShutdownEventArgs e)
{
Console.WriteLine("连接已关闭. " + e.ReplyText);
}
/// <summary>
/// 等待接收指令
/// </summary>
private static void WaitCommand()
{
while (!isExit)
{
string line = Console.ReadLine().ToLower().Trim();
string[] arr = line.Split(new[] {' '});
string cmd = arr[0];
switch (cmd)
{
case "exit":
Close();
isExit = true;
break;
case "go":
int count = 10;
if (arr.Length > 1)
{
int.TryParse(arr[1], out count);
}
Send(count);
break;
case "interval":
int.TryParse(arr[1], out _interval);
break;
case "clear":
Console.Clear();
break;
default:
break;
}
}
Console.WriteLine("Goodbye!");
}
public static void Send(int msgCount = 10)
{
Console.WriteLine("---------- 开始发送------------");
for (int i = 1; i <= msgCount; i++)
{
string title = "测试文档" + i;
string author = "lexworld" + i;
int docType = i%2 + 1;
string jsonFormat = "{{\"Title\":\"{0}\",\"Author\":\"{1}\",\"DocType\":{2}}}";
string message = string.Format(jsonFormat, title, author, docType);
byte[] body = Encoding.UTF8.GetBytes(message);
try
{
_channel.BasicPublish("", "extractQueue", null, body);
}
catch (AlreadyClosedException ex)
{
Console.WriteLine("ERROR: " + ex.Message);
break;
}
Console.WriteLine("Time:" + DateTime.Now + " MSG:" + title);
if (_interval > 0)
{
Thread.Sleep(_interval*1000);
}
}
Console.WriteLine("---------- 结束 ------------");
}
private static string GetMessage()
{
string argLine = string.Join(" ", Environment.GetCommandLineArgs());
string args = argLine.Substring(argLine.IndexOf(" ") + 1);
Console.WriteLine("args:" + args);
string[] arr = args.Split(new[] {','});
string jsonFormat = "{{\"Title\":\"{0}\",\"Author\":\"{1}\",\"DocType\":{2}}}";
return string.Format(jsonFormat, arr[0], arr[1], arr[2]);
}
private static void Close()
{
if (_channel != null && _channel.IsOpen)
{
_channel.Close();
}
if (_senderConnection != null && _senderConnection.IsOpen)
{
_senderConnection.Close();
}
}
}
}