本文整理匯總了C#中RabbitMQ.Client.MessagePatterns.Subscription.Ack方法的典型用法代碼示例。如果您正苦於以下問題:C# Subscription.Ack方法的具體用法?C# Subscription.Ack怎麽用?C# Subscription.Ack使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類RabbitMQ.Client.MessagePatterns.Subscription
的用法示例。
在下文中一共展示了Subscription.Ack方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。
示例1: Subscribe
public void Subscribe(string queueName, Action<string> callback)
{
this.Subscriptions = this.Subscriptions ?? Enumerable.Empty<Task>();
this.Subscriptions = this.Subscriptions.Concat(new[] {
Task.Factory.StartNew(delegate {
var factory = new ConnectionFactory()
{
VirtualHost = "/",
HostName = "127.0.0.1",
Port = AmqpTcpEndpoint.UseDefaultPort
};
factory.UserName = "guest";
factory.Password = "guest";
using (var connection = factory.CreateConnection())
{
using (var channel = connection.CreateModel())
{
var subscription = new Subscription(channel, queueName, false);
while(true)
{
var eventArgs = subscription.Next();
var content = eventArgs == null ? string.Empty : Encoding.UTF8.GetString(eventArgs.Body);
subscription.Ack(eventArgs);
callback(content);
}
}
}
}
)});
}
示例2: ProcessMessages
public void ProcessMessages()
{
using (_connection = _factory.CreateConnection())
{
using (var channel = _connection.CreateModel())
{
Console.WriteLine("Listening for Topic <payment.*>");
Console.WriteLine("------------------------------");
Console.WriteLine();
channel.ExchangeDeclare(ExchangeName, "topic");
channel.QueueDeclare(AllQueueName, true, false, false, null);
channel.QueueBind(AllQueueName, ExchangeName, "payment.*");
channel.BasicQos(0, 10, false);
Subscription subscription = new Subscription(channel, AllQueueName, false);
while (true)
{
BasicDeliverEventArgs deliveryArguments = subscription.Next();
var message = deliveryArguments.Body.DeSerializeText();
Console.WriteLine("Message Received '{0}'", message);
subscription.Ack(deliveryArguments);
}
}
}
}
示例3: ProcessMessages
public void ProcessMessages()
{
using (_connection = _factory.CreateConnection())
{
using (var channel = _connection.CreateModel())
{
Console.WriteLine("Listening for Topic <payment.purchaseorder>");
Console.WriteLine("------------------------------------------");
Console.WriteLine();
channel.ExchangeDeclare(ExchangeName, "topic");
channel.QueueDeclare(PurchaseOrderQueueName, true, false, false, null);
channel.QueueBind(PurchaseOrderQueueName, ExchangeName, "payment.purchaseorder");
channel.BasicQos(0, 10, false);
Subscription subscription = new Subscription(channel, PurchaseOrderQueueName, false);
while (true)
{
BasicDeliverEventArgs deliveryArguments = subscription.Next();
var message = (PurchaseOrder)deliveryArguments.Body.DeSerialize(typeof(PurchaseOrder));
var routingKey = deliveryArguments.RoutingKey;
Console.WriteLine("-- Purchase Order - Routing Key <{0}> : {1}, £{2}, {3}, {4}", routingKey, message.CompanyName, message.AmountToPay, message.PaymentDayTerms, message.PoNumber);
subscription.Ack(deliveryArguments);
}
}
}
}
示例4: blockingReceiveMessages
private static void blockingReceiveMessages(Subscription sub, long msgCount)
{
Console.WriteLine("Receiving {0} messages (using a Subscriber)", msgCount);
for (int i = 0; i < msgCount; ++i)
{
Console.WriteLine("Message {0}: {1} (via Subscription.Next())",
i, messageText(sub.Next()));
Console.WriteLine("Message {0} again: {1} (via Subscription.LatestEvent)",
i, messageText(sub.LatestEvent));
sub.Ack();
}
Console.WriteLine("Done.\n");
}
示例5: Consume
private void Consume()
{
bool autoAck = false;
//create a subscription
mSubscription = new Subscription(Model, QueueName, autoAck);
while (isConsuming)
{
BasicDeliverEventArgs e = mSubscription.Next();
byte[] body = e.Body;
onMessageReceived(body);
mSubscription.Ack(e);
}
}
示例6: Main
public static int Main(string[] args) {
if (args.Length < 4) {
Console.Error.WriteLine("Usage: LogTail <uri> <exchange> <exchangetype> <routingkey>");
Console.Error.WriteLine("RabbitMQ .NET client version "+typeof(IModel).Assembly.GetName().Version.ToString());
Console.Error.WriteLine("Parameters:");
Console.Error.WriteLine(" <uri> = \"amqp://user:[email protected]:port/vhost\"");
Console.Error.WriteLine("If the exchange name is the empty string, will instead declare a queue named");
Console.Error.WriteLine("by the routingkey, and consume from that queue.");
return 2;
}
string serverAddress = args[0];
string exchange = args[1];
string exchangeType = args[2];
string routingKey = args[3];
ConnectionFactory cf = new ConnectionFactory();
cf.Uri = serverAddress;
using (IConnection conn = cf.CreateConnection())
{
using (IModel ch = conn.CreateModel()) {
ch.QueueDeclare(routingKey, false, true, true, null);
Subscription sub = new Subscription(ch, routingKey);
if (exchange != "") {
ch.ExchangeDeclare(exchange, exchangeType);
ch.QueueBind(routingKey, exchange, routingKey, null);
}
Console.WriteLine("Consumer tag: " + sub.ConsumerTag);
foreach (BasicDeliverEventArgs e in sub) {
sub.Ack(e);
ProcessSingleDelivery(e);
if (Encoding.UTF8.GetString(e.Body) == "quit") {
Console.WriteLine("Quitting!");
break;
}
}
return 0;
}
}
}
示例7: ReceiveHeadersMessageReceiverOne
public void ReceiveHeadersMessageReceiverOne(IModel model)
{
model.BasicQos(0, 1, false);
Subscription subscription = new Subscription(model, _headersQueueOne, false);
while (true)
{
BasicDeliverEventArgs deliveryArguments = subscription.Next();
StringBuilder messageBuilder = new StringBuilder();
String message = Encoding.UTF8.GetString(deliveryArguments.Body);
messageBuilder.Append("Message from queue: ").Append(message).Append(". ");
foreach (string headerKey in deliveryArguments.BasicProperties.Headers.Keys)
{
byte[] value = deliveryArguments.BasicProperties.Headers[headerKey] as byte[];
messageBuilder.Append("Header key: ").Append(headerKey).Append(", value: ").Append(Encoding.UTF8.GetString(value)).Append("; ");
}
Console.WriteLine(messageBuilder.ToString());
subscription.Ack(deliveryArguments);
}
}
示例8: Listen
public void Listen()
{
Console.WriteLine("Creating channel...");
using (IModel model = conn.CreateModel())
{
var subscription = new Subscription(model, "queue", false);
//while (true)
//{
BasicDeliverEventArgs basicDeliveryEventArgs =
subscription.Next();
var command = StreamExtension.Deserialize<Vaccine.Core.Cqrs.Commands.DomainCommand>(basicDeliveryEventArgs.Body);
//Encoding.UTF8.GetString(basicDeliveryEventArgs.Body);
//Console.WriteLine(((CreateCommand)messageContent).Name);
bus.Send(command);
subscription.Ack(basicDeliveryEventArgs);
//}
}
}
示例9: Main
public static void Main(string[] args)
{
var connectionFactory = new ConnectionFactory {HostName = Settings.RabbitMqHost};
using (var connection = connectionFactory.CreateConnection())
{
using (var model = connection.CreateModel())
{
var subscription = new Subscription(model, Settings.RabbitMqQueue, false);
while (true)
{
var basicDeliveryEventArgs = subscription.Next();
var messageContent = basicDeliveryEventArgs.Body.To<SimpleMessage>();
System.Console.WriteLine(messageContent.Content);
subscription.Ack(basicDeliveryEventArgs);
}
}
}
System.Console.ReadLine();
}
示例10: Start
public void Start()
{
try
{
if (IsListening) return;
var serverAddress = "localhost:" + _port;
var connectionFactory = new ConnectionFactory { Address = serverAddress };
using (var connection = connectionFactory.CreateConnection())
{
using (var channel = connection.CreateModel())
{
Log("Opening listener");
string queueName = channel.QueueDeclare(_queueName, false, false, false, null);
channel.QueueBind(queueName, "amq.direct", queueName, null);
using (_subscription = new Subscription(channel, queueName))
{
IsListening = true;
while (IsListening)
{
foreach (BasicDeliverEventArgs eventArgs in _subscription)
{
//Log(Encoding.UTF8.GetString(eventArgs.Body));
Messenger.Default.Send(new RabbitClientMessage());
_subscription.Ack();
}
}
}
}
}
}
catch (Exception e)
{
Log(e.Message);
}
}
示例11: Main
static void Main(string[] args)
{
var url = ConfigurationManager.AppSettings.Get("RabbitMqUrl");
var connectionFactory = new ConnectionFactory {Uri = url};
using ( var connection = connectionFactory.CreateConnection())
using ( var channel = connection.CreateModel())
{
const string tracingExchangeName = "amq.rabbitmq.trace";
const string tracingQueueName = "some_tracing_queue";
// Create the queue for subscribing to
channel.QueueDeclare(queue: tracingQueueName, durable: false, exclusive: false, autoDelete: true, arguments: null);
channel.QueueBind(queue: tracingQueueName,exchange:tracingExchangeName,routingKey: null);
// Subscribe to messages
using (var subscription = new Subscription(model: channel, queueName: tracingQueueName))
{
var messageEncoding = Encoding.UTF8;
XmlConfigurator.Configure();
var log = LogManager.GetLogger(typeof (Program));
foreach (BasicDeliverEventArgs message in subscription)
{
// Process each message
var body = messageEncoding.GetString(message.Body);
var exchange = message.Exchange;
var routingKey = message.RoutingKey;
log.InfoFormat("{0}|{1}|{2}",exchange,routingKey,body);
subscription.Ack(message);
}
}
}
}
示例12: ProcessMessages
public void ProcessMessages()
{
using (_connection = _factory.CreateConnection())
{
using (var channel = _connection.CreateModel())
{
Console.WriteLine("Listening for Topic <payment.cardpayment>");
Console.WriteLine("-----------------------------------------");
Console.WriteLine();
channel.ExchangeDeclare(ExchangeName, "topic");
channel.QueueDeclare(CardPaymentQueueName,
true, false, false, null);
channel.QueueBind(CardPaymentQueueName, ExchangeName,
"payment.cardpayment");
channel.BasicQos(0, 10, false);
Subscription subscription = new Subscription(channel,
CardPaymentQueueName, false);
while (true)
{
BasicDeliverEventArgs deliveryArguments = subscription.Next();
var message =
(CardPayment)deliveryArguments.Body.DeSerialize(typeof(CardPayment));
var routingKey = deliveryArguments.RoutingKey;
Console.WriteLine("--- Payment - Routing Key <{0}> : {1} : {2}", routingKey, message.CardNumber, message.Amount);
subscription.Ack(deliveryArguments);
}
}
}
}
示例13: Page_Load
protected void Page_Load(object sender, EventArgs e)
{
try
{
var connectionFactory = new ConnectionFactory();
connectionFactory.HostName = ConfigurationManager.AppSettings["hostname"];
connectionFactory.UserName = ConfigurationManager.AppSettings["username"];
connectionFactory.Password = ConfigurationManager.AppSettings["password"];
connectionFactory.Port = Convert.ToInt32(ConfigurationManager.AppSettings["port"]);
connectionFactory.VirtualHost = ConfigurationManager.AppSettings["vhost"];
connectionFactory.Protocol = Protocols.FromEnvironment();
//Response.Write(connectionFactory.HostName + " " + connectionFactory.UserName + " " +
// connectionFactory.Port.ToString() + " " + connectionFactory.Password);
string message = "Hello!!";
//send message to queue
using (IConnection connection =
connectionFactory.CreateConnection())
{
using (IModel model = connection.CreateModel())
{
model.ExchangeDeclare("MyExchange", ExchangeType.Fanout, true);
model.QueueDeclare("MyQueue", true, false, false, new Dictionary<string, object>());
model.QueueBind("MyQueue", "MyExchange", "",
new Dictionary<string, object>());
IBasicProperties basicProperties = model.CreateBasicProperties();
model.BasicPublish("MyExchange", "", false, false,
basicProperties, Encoding.UTF8.GetBytes(message));
}
}
//consume message from queue
using (IConnection connection = connectionFactory.CreateConnection())
{
using (IModel model = connection.CreateModel())
{
var subscription = new Subscription(model, "MyQueue", false);
while (true)
{
BasicDeliverEventArgs basicDeliveryEventArgs =
subscription.Next();
string messageContent =
Encoding.UTF8.GetString(basicDeliveryEventArgs.Body);
//Response.Write(messageContent);
subscription.Ack(basicDeliveryEventArgs);
if (messageContent == message)
{
break;
}
}
}
}
Response.Write("OK");
}
catch (Exception ex)
{
Response.StatusCode = 500;
if (ex.InnerException != null)
Response.Write(ex.InnerException.Message);
Response.Write(ex.Message + Environment.NewLine + ex.StackTrace);
}
}
示例14: Receive
private IMessage Receive (IModel model, int timeout,
bool doAck, IsMatch matcher)
{
string finalName = model.QueueDeclare (QRef.Queue, false);
using (Subscription sub = new Subscription (model, finalName)) {
BasicDeliverEventArgs result;
while (sub.Next (timeout, out result)) {
if (matcher (result)) {
IMessage m = helper.ReadMessage (QRef, result);
if (doAck)
sub.Ack (result);
return m;
}
}
throw new MessageUnavailableException ("Message not available");
}
}
示例15: enumeratingReceiveMessages
private static void enumeratingReceiveMessages(Subscription sub, long msgCount)
{
Console.WriteLine("Receiving {0} messages (using Subscriber's IEnumerator)", msgCount);
int i = 0;
foreach (BasicDeliverEventArgs ev in sub) {
Console.WriteLine("Message {0}: {1}",
i, messageText(ev));
if (++i == msgCount)
break;
sub.Ack();
}
Console.WriteLine("Done.\n");
}