private static void StartReceiver() { Task.Run(async() => { var tfactory = new AmqpConnectionFactory(); var connection = await tfactory.OpenConnectionAsync("amqp://localhost:5672", TimeSpan.FromSeconds(10)); var session = connection.CreateSession(new AmqpSessionSettings()); var receicerSettings = new AmqpLinkSettings { LinkName = $"receiver-{DateTime.UtcNow.Ticks}", Role = true, TotalLinkCredit = 300, Source = new Source { Address = QueueName, }, Target = new Target() }; var receiver = new ReceivingAmqpLink(session, receicerSettings); while (true) { AmqpMessage message = await receiver.ReceiveMessageAsync(TimeSpan.FromSeconds(20)); if (message != null) { receiver.DisposeDelivery(message, true, AmqpConstants.AcceptedOutcome); DisplayMessage(new StreamReader(message.BodyStream).ReadToEnd()); } } }); }
public async Task SendReceiveSample() { string queue = "SendReceiveSample"; broker.AddQueue(queue); var factory = new AmqpConnectionFactory(); var connection = await factory.OpenConnectionAsync(addressUri); var session = await connection.OpenSessionAsync(); var sender = await session.OpenLinkAsync <SendingAmqpLink>("sender", queue); var outcome = await sender.SendMessageAsync(AmqpMessage.Create("Hello World!")); await sender.CloseAsync(); var receiver = await session.OpenLinkAsync <ReceivingAmqpLink>("receiver", queue); var message = await receiver.ReceiveMessageAsync(); string body = (string)message.ValueBody.Value; receiver.AcceptMessage(message); await receiver.CloseAsync(); await connection.CloseAsync(); }
public async Task InitAsync() { AmqpConnectionFactory factory = new AmqpConnectionFactory(); factory.TlsSettings.CertificateValidationCallback = (a, b, c, d) => true; factory.TlsSettings.CheckCertificateRevocation = false; factory.TlsSettings.Protocols = System.Security.Authentication.SslProtocols.Tls12; this.connection = await factory.OpenConnectionAsync(new Uri(this.options.Address), this.options.Sasl, TimeSpan.FromSeconds(30)); this.session = this.connection.CreateSession(new AmqpSessionSettings()); this.link = this.CreateLink(); await Task.WhenAll( this.session.OpenAsync(this.session.DefaultOpenTimeout), this.link.OpenAsync(this.link.DefaultOpenTimeout)); }
public async Task <bool> SendAsync(string body) { try { var tfactory = new AmqpConnectionFactory(); var connection = await tfactory.OpenConnectionAsync(new Uri("amqp://*****:*****@localhost:5672"), TimeSpan.FromSeconds(30)); var session = connection.CreateSession(CreateSessionSettings()); var sender = CreateSender(session); await Task.WhenAll( session.OpenAsync(session.DefaultOpenTimeout), sender.OpenAsync(sender.DefaultOpenTimeout)); var message = AmqpMessage.Create(new AmqpValue { Value = body }); //message.Header.Value = "amqpHeader"; //message.Footer.Value = "amqpFooter"; message.Properties.AbsoluteExpiryTime = new DateTime(2025, 10, 27); message.Properties.Subject = "propertiesSubject"; message.Properties.To = "propertiesTo"; message.Properties.ReplyTo = "propertiesReplyTo"; message.Properties.Value = "thePropertiesValue"; message.Properties.ContentType = new Microsoft.Azure.Amqp.Encoding.AmqpSymbol("appliciton/json"); //message.ApplicationProperties.Value = CreateProperties(); var tag = new ArraySegment <byte>(BitConverter.GetBytes(_tagCount++)); var outcome = await sender.SendMessageAsync(message, tag, AmqpConstants.NullBinary, TimeSpan.FromSeconds(5)); if (outcome.DescriptorCode == Accepted.Code) { return(true); } else { return(false); } } catch (Exception ex) { return(false); } }
private static void TestLocal() { StartReceiver(); var keyPressed = ConsoleKey.A; do { Console.WriteLine("Press Q to quit, any other key to send message"); keyPressed = Console.ReadKey().Key; if (keyPressed != ConsoleKey.Q) { Task.Run(async() => { var tfactory = new AmqpConnectionFactory(); var connection = await tfactory.OpenConnectionAsync(new Uri("amqp://localhost:5672"), TimeSpan.FromSeconds(30)); var session = connection.CreateSession(new AmqpSessionSettings()); var sender = CreateSender(session); await Task.WhenAll( session.OpenAsync(session.DefaultOpenTimeout), sender.OpenAsync(sender.DefaultOpenTimeout)); var message = AmqpMessage.Create(new AmqpValue() { Value = $"Message number {++count}" }); var tag = new ArraySegment <byte>(BitConverter.GetBytes(_tagCount++)); var outcome = await sender.SendMessageAsync(message, tag, AmqpConstants.NullBinary, TimeSpan.FromSeconds(5)); var sent = outcome.DescriptorCode == Accepted.Code; if (sent) { Console.WriteLine("Message Sent!"); } else { Console.WriteLine("Message NOT Sent!"); } }); } } while (keyPressed != ConsoleKey.Q); }
private void StartReceiver() { Task.Run(async() => { var tfactory = new AmqpConnectionFactory(); var connection = await tfactory.OpenConnectionAsync("amqp://localhost:5672", TimeSpan.FromSeconds(10)); var session = connection.CreateSession(CreateSessionSettings()); var receicerSettings = new AmqpLinkSettings { LinkName = $"receiver-{DateTime.UtcNow.Ticks}", Role = true, Source = new Source { Address = QueueName, //DynamicNodeProperties = CreateProperties(), //Value = "sourceValue", Durable = 1 }, Properties = CreateProperties() }; var receiver = new ReceivingAmqpLink(session, receicerSettings); await Task.WhenAll( session.OpenAsync(session.DefaultOpenTimeout), receiver.OpenAsync(receiver.DefaultOpenTimeout)); while (true) { AmqpMessage message = await receiver.ReceiveMessageAsync(TimeSpan.FromSeconds(20)); if (message != null) { receiver.DisposeDelivery(message, true, AmqpConstants.AcceptedOutcome); ReceiveMessage(message); } } }); }
async Task RunClientAsync(string address) { AmqpConnectionFactory factory = new AmqpConnectionFactory(); factory.Settings.TransportProviders.Add( new TlsTransportProvider( new TlsTransportSettings() { CertificateValidationCallback = (a, b, c, d) => true }, AmqpVersion.V100)); AmqpConnection connection = await factory.OpenConnectionAsync(address); AmqpSession session = connection.CreateSession(new AmqpSessionSettings()); await session.OpenAsync(TimeSpan.FromSeconds(20)); SendingAmqpLink sLink = new SendingAmqpLink(session, AmqpUtils.GetLinkSettings(true, queue, SettleMode.SettleOnSend)); await sLink.OpenAsync(TimeSpan.FromSeconds(20)); AmqpMessage message = AmqpMessage.Create(new AmqpValue() { Value = "AmqpConnectionFactoryTest" }); Outcome outcome = await sLink.SendMessageAsync(message, EmptyBinary, NullBinary, TimeSpan.FromSeconds(10)); Assert.Equal(Accepted.Code, outcome.DescriptorCode); ReceivingAmqpLink rLink = new ReceivingAmqpLink(session, AmqpUtils.GetLinkSettings(false, queue, SettleMode.SettleOnDispose, 10)); await rLink.OpenAsync(TimeSpan.FromSeconds(20)); var receivedMessage = await rLink.ReceiveMessageAsync(TimeSpan.FromSeconds(20)); Assert.NotNull(receivedMessage); outcome = await rLink.DisposeMessageAsync(receivedMessage.DeliveryTag, new Accepted(), false, TimeSpan.FromSeconds(20)); Assert.Equal(Accepted.Code, outcome.DescriptorCode); await connection.CloseAsync(TimeSpan.FromSeconds(20)); }
private static void TestLocalReceiver() { var tfactory = new AmqpConnectionFactory(); var connection = tfactory.OpenConnectionAsync("amqp://localhost:5672", TimeSpan.FromSeconds(10)).Result; var session = connection.CreateSession(new AmqpSessionSettings()); var receicerSettings = new AmqpLinkSettings { LinkName = $"receiver-{DateTime.UtcNow.Ticks}", Role = true, TotalLinkCredit = 300, Source = new Source { Address = QueueName }, Target = new Target() }; var receiver = new ReceivingAmqpLink(session, receicerSettings); Task.WhenAll( session.OpenAsync(TimeSpan.FromSeconds(30)), receiver.OpenAsync(TimeSpan.FromSeconds(30))).Wait(); Console.WriteLine("Listening"); while (true) { AmqpMessage message = receiver.ReceiveMessageAsync(receiver.DefaultOpenTimeout).Result; if (message != null) { receiver.DisposeDelivery(message, true, AmqpConstants.AcceptedOutcome); DisplayMessage(new StreamReader(message.BodyStream).ReadToEnd()); } } }
static void Main(string[] args) { try { if (args.Length == 0) { PrintUsage(args); return; } #region Prepare Arguments // Parse arguments and assign values foreach (var arg in args) { if (!arg.Contains(":")) { PrintUsage(args); return; } var parsedArg = arg.SplitClean(':'); var name = parsedArg[0]; var value = parsedArg[1]; switch (name) { case "server": server = value; break; case "port": port = int.Parse(value); break; case "api": api = int.Parse(value); break; case "vhost": virtualHost = value; break; case "u": case "user": case "username": username = value; break; case "p": case "pass": case "password": password = value; break; case "reconnect": reconnectInterval = short.Parse(value); break; case "heartbeat": requestedHeartbeat = ushort.Parse(value); break; case "cmd": case "command": command = value; break; case "exchange": exchangeName = value; break; case "type": exchangeType = (AmqpExchangeTypes)Enum.Parse(typeof(AmqpExchangeTypes), value, true); break; case "queue": queueName = value; break; case "key": case "routing": case "routingkey": routingKey = value; break; case "count": count = byte.Parse(value); break; } } #endregion Prepare Arguments #region Validate Arguments if (!commands.Contains(command)) { PrintUsage(args); return; } #endregion Validate Arguments // Create a new client using the supplied arguments client = AmqpConnectionFactory.Create(server, port, api, virtualHost, username, password, reconnectInterval, requestedHeartbeat); // Hook up event handlers client.Blocked += Client_Blocked; client.Connected += Client_Connected; client.Disconnected += Client_Disconnected; client.ConnectionAborted += Client_ConnectionAborted; // If this is an AMQP operation, connect if (command == "rx" || command == "tx") { Console.WriteLine("Connecting to: {0}", AmqpHelper.GetConnectionInfo(client)); Console.WriteLine("Press enter to exit"); client.Connect(); Console.ReadLine(); client.Disconnect(); } // Otherwise this is a REST API operation so execute it else { if (command == "exchanges") { Console.WriteLine("Listing exchanges for {0}:{1} and virtual host '{2}'", server, api, virtualHost); foreach (var exchange in client.GetExchanges(virtualHost)) { Console.WriteLine("name:{0}, type:{1}, auto delete:{2}, durable:{3}", exchange.Name, exchange.Type.ToString().ToLower(), exchange.AutoDelete, exchange.Durable); } } else if (command == "queues") { Console.WriteLine("Listing queues for {0}:{1} and virtual host '{2}'", server, api, virtualHost); foreach (var queue in client.GetQueues(virtualHost)) { Console.WriteLine("name:{0}, auto delete:{1}, durable:{2}, state:{3}, policy:{4}, exclusive:{5}", queue.Name, queue.AutoDelete, queue.Durable, queue.State, queue.Policy, queue.ExclusiveConsumerTag); } } } } catch (Exception ex) { Console.WriteLine("{0}", ex); //Console.ReadLine(); Environment.Exit(-1); } }