本文整理汇总了C#中IModel.BasicPublish方法的典型用法代码示例。如果您正苦于以下问题:C# IModel.BasicPublish方法的具体用法?C# IModel.BasicPublish怎么用?C# IModel.BasicPublish使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IModel
的用法示例。
在下文中一共展示了IModel.BasicPublish方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CallService
private void CallService(IModel channel, string queueName)
{
var properties = channel.CreateBasicProperties();
properties.ReplyTo = queueName;
properties.CorrelationId = (++correlationId).ToString();
channel.BasicPublish("", Constants.RpcQueueName, properties, Encoding.UTF8.GetBytes(n.ToString()));
}
示例2: GenerateQueueItems
//public static void Main(string[] args)
//{
// var factory = new ConnectionFactory() { HostName = "localhost" };
// using (var connection = factory.CreateConnection())
// using (var channel = connection.CreateModel())
// {
// channel.QueueDeclare(queue: "task_queue",
// durable: true,
// exclusive: false,
// autoDelete: false,
// arguments: null);
// var properties = channel.CreateBasicProperties();
// properties.SetPersistent(true);
// for (var i = 0; i < 10; i++)
// {
// var text = String.Format("Message Numero {0}", i);
// var message = GetMessage(args, text);
// var body = Encoding.UTF8.GetBytes(message);
// GenerateQueueItems(args, channel, properties, body, message);
// }
// }
// Console.WriteLine(" Press [enter] to exit.");
// Console.ReadLine();
//}
private static void GenerateQueueItems(string[] args, IModel channel, IBasicProperties properties, byte[] body, string message)
{
channel.BasicPublish(exchange: "",
routingKey: "task_queue",
basicProperties: properties,
body: body);
Console.WriteLine(" [x] Sent {0}", message);
}
示例3: SendDurableMessageToDurableQueue
private static void SendDurableMessageToDurableQueue(IModel model)
{
IBasicProperties basicProperties = model.CreateBasicProperties();
basicProperties.SetPersistent(true);
byte[] payload = Encoding.UTF8.GetBytes("This is a persistent message from Visual Studio");
PublicationAddress address = new PublicationAddress(ExchangeType.Topic, "DurableExchange", "durable");
model.BasicPublish(address, basicProperties, payload);
}
示例4: sendMessages
private static void sendMessages(IModel ch, string queueName, long msgCount) {
Console.WriteLine("Sending {0} messages to queue {1} via the amq.direct exchange.", msgCount, queueName);
while (msgCount --> 0) {
ch.BasicPublish("amq.direct", queueName, null, Encoding.UTF8.GetBytes("Welcome to Caerbannog!"));
}
Console.WriteLine("Done.\n");
}
示例5: Publish
public void Publish(IModel model, string queue, string message)
{
var properties = model.CreateBasicProperties();
properties.SetPersistent(true);
var nextPublishSeqNo = model.NextPublishSeqNo;
Console.Out.WriteLine("Published seq no = {0}", nextPublishSeqNo);
model.BasicPublish("", queue, properties, Encoding.UTF8.GetBytes(message));
}
示例6: DemoExchange
// Broadcasts messages to anyone who would listen (Radio/TV)
static void DemoExchange(IModel model)
{
model.ExchangeDeclare("myexchange", "fanout");
while (true)
{
var message = Console.ReadLine();
var body = Encoding.UTF8.GetBytes(message);
model.BasicPublish("myexchange", "", null, body);
}
}
示例7: Exec
// ReSharper disable ConditionIsAlwaysTrueOrFalse
public static void Exec(bool durable, bool ack, IModel model, IConnection connection)
{
var queueName = "CSharpPublish" + (durable ? "Durable" : "NotDurable");
if (!string.IsNullOrWhiteSpace(_overrideQueueName))
queueName = _overrideQueueName;
Console.WriteLine("Durability {0} enabled", durable ? "IS" : "is NOT");
Console.WriteLine("ACK {0} enabled", ack ? "IS" : "is NOT");
Console.WriteLine("Creating queue {0}...", queueName);
model.QueueDeclare(queueName, durable, false, false, null);
var msg = Encoding.UTF8.GetBytes("Hello from my PC !");
var properties = new BasicProperties { DeliveryMode = (byte)(durable ? 2 : 1) };
if (_publish)
{
for (int i = 0; i < _numMsgs; i++)
model.BasicPublish(string.Empty, queueName, properties, msg);
Console.WriteLine("Published!");
}
uint msgs = 0;
if (_receive)
{
Console.WriteLine("Reading...");
var fromMac = false;
while (connection.IsOpen)
{
var result = model.BasicGet(queueName, ack == false);
if (result == null)
break;
if (msgs % 3144 == 0)
Console.WriteLine(msgs);
if (ack)
model.BasicAck(result.DeliveryTag, false);
var msgText = Encoding.UTF8.GetString(result.Body);
if (msgText.Contains("Mac"))
fromMac = true;
msgs++;
}
if (fromMac)
{
Console.ForegroundColor = ConsoleColor.Blue;
Console.WriteLine("Received messages from the mac");
Console.ResetColor();
}
}
}
示例8: RunBadMessageDemo
private static void RunBadMessageDemo(IModel model)
{
Console.WriteLine("Enter your message. Quit with 'q'.");
while (true)
{
string message = Console.ReadLine();
if (message.ToLower() == "q") break;
IBasicProperties basicProperties = model.CreateBasicProperties();
basicProperties.SetPersistent(true);
byte[] messageBuffer = Encoding.UTF8.GetBytes(message);
model.BasicPublish("", RabbitMqService.BadMessageBufferedQueue, basicProperties, messageBuffer);
}
}
示例9: ScheduleTask
private static void ScheduleTask(string message, IModel channel)
{
message = (!String.IsNullOrEmpty(message))
? message
: "Hello World!";
byte[] body = System.Text.Encoding.UTF8.GetBytes(message);
IBasicProperties properties = channel.CreateBasicProperties();
properties.DeliveryMode = 2;
channel.BasicPublish("", "task_queue", properties, body);
Console.WriteLine(" [x] Sent {0}", message);
}
示例10: Publish
private static void Publish(IModel channel, QueueName queueName, string message, bool isDurable)
{
var buffer = StringToBytes(message);
var routingKey = QueueNameHelper.Instance.Name(queueName);
//消息持久化,防止丢失
if (isDurable)
{
//设置消息为持久化
var properties = channel.CreateBasicProperties();
properties.Persistent = true;
properties.DeliveryMode = 2;
channel.BasicPublish(exchange: "",
routingKey: routingKey,
basicProperties: properties,
body: buffer);
return;
}
//消息非持久化
channel.BasicPublish(exchange: "",
routingKey: routingKey,
basicProperties: null,
body: buffer);
}
示例11: DemoQueue
// Sends a message to a queue to be handled by available worker
static void DemoQueue(IModel model)
{
model.QueueDeclare("myqueue", true, false, false, null);
var properties = model.CreateBasicProperties();
properties.SetPersistent(true);
while (true)
{
var message = Console.ReadLine();
var body = Encoding.UTF8.GetBytes(message);
model.BasicPublish("", "myqueue", properties, body);
}
}
示例12: RunBufferedMessageExample
private static void RunBufferedMessageExample(IModel model)
{
string filePath = @"c:\large_file.txt";
ConsoleKeyInfo keyInfo = Console.ReadKey();
while (true)
{
if (keyInfo.Key == ConsoleKey.Enter)
{
IBasicProperties basicProperties = model.CreateBasicProperties();
basicProperties.SetPersistent(true);
byte[] fileContents = File.ReadAllBytes(filePath);
model.BasicPublish("", RabbitMqService.LargeMessageBufferedQueue, basicProperties, fileContents);
}
keyInfo = Console.ReadKey();
}
}
示例13: PublishMessage
private void PublishMessage(IModel channel, ITask task)
{
if (channel == null)
throw new ArgumentNullException("channel");
if (task == null)
throw new ArgumentNullException("task");
string message = task.GetTaskType();
var body = Encoding.UTF8.GetBytes(message);
var properties = channel.CreateBasicProperties();
properties.Persistent = true;
channel.BasicPublish(exchange: "",
routingKey: _defaultQueue,
basicProperties: properties,
body: body);
Console.WriteLine(" [x] Sending: {0}", message);
}
示例14: processGeometry
private static void processGeometry(String request, IModel channel, IBasicProperties replyProps, IBasicProperties props) {
//TODO replace with id from rabbitMQ task?
Newtonsoft.Json.Linq.JObject jobject = new Newtonsoft.Json.Linq.JObject();//new JProperty("RequestId", "test"));
byte[] responseBytes = null;
try {
OperatorCursor geomOpParts = JsonConvert.DeserializeObject<OperatorCursor>(request);
double distance = Double.NegativeInfinity;
bool spatialRelationship = false;
List<Proximity2DResult> proximityResults = null;
GeometryCursor geomCursor = geomOpParts.GenerateCursor(ref distance, ref spatialRelationship, ref proximityResults);
if (geomCursor != null) {
Geometry geom = null;
JArray geomArray = new JArray();
while ((geom = geomCursor.Next()) != null) {
geomArray.Add(GeometryEngine.GeometryToWkt(geom, 0));
}
jobject = new JObject(
new JProperty("geometry_results", geomArray)
);
responseBytes = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(jobject, Formatting.Indented));
channel.BasicPublish(exchange: "",
routingKey: props.ReplyTo,
basicProperties: replyProps,
body: responseBytes);
return;
} else if (proximityResults.Count > 0) {
foreach (Proximity2DResult prox in proximityResults) {
jobject = new JObject( new JProperty("proximity_results", JsonConvert.SerializeObject(prox)));
responseBytes = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(jobject, Formatting.Indented));
channel.BasicPublish(exchange: "",
routingKey: props.ReplyTo,
basicProperties: replyProps,
body: responseBytes);
}
return;
} else if (!Double.IsNegativeInfinity(distance)) {
jobject.Add("distance_results", distance);
} else {
jobject.Add("spatial_results", spatialRelationship);
}
} catch (Exception e) {
jobject.Add("Error", e.Message);
}
responseBytes = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(jobject, Formatting.Indented));
channel.BasicPublish(exchange: "",
routingKey: props.ReplyTo,
basicProperties: replyProps,
body: responseBytes);
}
示例15: ProcessTx
private static void ProcessTx(int userID, GetTransactionResponse tx, UInt64 nxtAccountID, CurrencyType currency)
{
var cacheKey = currency.ToString() + tx.SenderRS + tx.Transaction + "create";
if (tx.Type != TransactionType.Ordinary || tx.Recipient != nxtAccountID)
{
//如果交易类型不是普通交易或者收款账户非当前账户(非收款),忽略,并加入忽略名单,防止多次请求交易明细,浪费资源
Cache.Add(cacheKey, tx.Confirmations);
return;
}
object obj = null;
if (Cache.TryGet(cacheKey, out obj)) return;
Log.Info("发现新的交易....");
var actualAmount = tx.AmountNQT / 100000000;
#region 发送交易到处理消息队列
var mqname = "NXTReceiveTransactionListener";
_channel = _mqpool.GetMQChannel(mqname);
var exchangeName = Utilities.GenerateVirtualCoinReceivePaymentExchangeAndQueueName(currency).Item1;
var build = new BytesMessageBuilder(_channel);
var cmd = new CreateReceivePaymentTransaction(tx.Transaction, tx.RecipientRS, actualAmount, currency);
build.WriteBytes(Encoding.UTF8.GetBytes(IoC.Resolve<IJsonSerializer>().Serialize(cmd)));
((IBasicProperties)build.GetContentHeader()).DeliveryMode = 2;
try
{
Log.Info("交易将被发送到Exchange->{0}", exchangeName);
_channel.BasicPublish(exchangeName, string.Empty, ((IBasicProperties)build.GetContentHeader()), build.GetContentBody());
Log.Info("交易成功发送到Exchange->{0}", exchangeName);
Cache.Add(cacheKey, tx.Confirmations);
}
catch (RabbitMQ.Client.Exceptions.AlreadyClosedException ex)
{
Log.Error("发送{0}新交易创建指令时发现消息队列服务器已关闭".FormatWith(Config.CoinCode), ex);
try
{
_channel = _mqpool.GetMQChannel(mqname);
_channel.BasicPublish(exchangeName, string.Empty, ((IBasicProperties)build.GetContentHeader()), build.GetContentBody());
Log.Info("交易成功发送到Exchange->{0}", exchangeName);
}
catch (Exception eex)
{
Log.Fatal("重新链接消息队列发送NXT新交易仍然出错了".FormatWith(Config.CoinCode), eex);
}
}
catch (Exception ex)
{
Log.Error("发送{0}新交易创建指令时出现错误".FormatWith(Config.CoinCode), ex);
}
#endregion
#region 提取币到总账户中
try
{
var nxtclient = new NXTClient4Net(Config.NXTServer, Config.SecretPhrase + userID);
var result = nxtclient.SendMoneyAsnc(Config.NxtSumAccount, actualAmount - 1).Result;
Log.Info("用户充值的币发送到总帐户成功");
}
catch (Exception ex)
{
Log.Fatal("用户ID={0}充值的{1}NXT发送到总帐户失败了,请及时处理".FormatWith(userID, actualAmount), ex);
}
#endregion
}