c# 控制台程序编写RabbitMQ 生产者

using RabbitMQ.Client;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace .RabbitMQWorker
{
class Program
{
static string hostName = string.Empty;
static string userName = string.Empty;
static string password = string.Empty;
static string queueName = string.Empty;

static void Main(string[] args)
{
hostName = ConfigurationManager.AppSettings.Get("HostName");
userName = ConfigurationManager.AppSettings.Get("UserName");
password = ConfigurationManager.AppSettings.Get("Password");
queueName = ConfigurationManager.AppSettings.Get("QueueName");

var factory = new ConnectionFactory() { HostName = hostName, UserName = userName, Password = password };
using (var connection = factory.CreateConnection())
using (var channel = connection.CreateModel())
{
channel.QueueDeclare(queue: queueName,
durable: true,
exclusive: false,
autoDelete: false,
arguments: null);

Console.WriteLine(" Press message to publish.input exit to stop.");

string message = Console.ReadLine();

while (message.ToLower() != "exit")
{
var body = Encoding.UTF8.GetBytes(message);

var properties = channel.CreateBasicProperties();
properties.Persistent = true;

channel.BasicPublish(exchange: "",
routingKey: queueName,
basicProperties: properties,
body: body);
Console.WriteLine(" [x] Sent {0}", message);

message = Console.ReadLine();
}
}

Console.WriteLine(" Press [enter] to exit.");
Console.ReadLine();
}
}
}

c# 控制台程序编写RabbitMQ 生产者

上一篇:【学习DIV+CSS】1. 你必须知道的三个知识


下一篇:oracle PL/SQL(procedure language/SQL)程序设计之游标cursors