public void TestMethod_ReceiveWaiterZero() { string testName = "ReceiveWaiterZero"; Connection connection = new Connection(testTarget.Address); Session session = new Session(connection); ReceiverLink receiver = new ReceiverLink(session, "receiver-" + testName, testTarget.Path); Message msg = receiver.Receive(TimeSpan.Zero); Assert.IsTrue(msg == null); SenderLink sender = new SenderLink(session, "sender-" + testName, testTarget.Path); msg = new Message() { Properties = new Properties() { MessageId = "123456" } }; sender.Send(msg, null, null); for (int i = 0; i < 1000; i++) { msg = receiver.Receive(TimeSpan.Zero); if (msg != null) { receiver.Accept(msg); break; } #if NETFX_CORE System.Threading.Tasks.Task.Delay(10).Wait(); #else Thread.Sleep(10); #endif } Assert.IsTrue(msg != null, "Message not received"); sender.Close(); receiver.Close(); session.Close(); connection.Close(); }
public void Retry() { string name = "Retry"; List <Message> messages = new List <Message>(); host.RegisterMessageProcessor(name, new TestMessageProcessor(50, messages)); linkProcessor = new TestLinkProcessor(); host.RegisterLinkProcessor(linkProcessor); int count = 80; var connection = new Connection(Address); var session = new Session(connection); var sender = new SenderLink(session, "send-link", name); for (int i = 0; i < count; i++) { var m = new Message("msg" + i) { ApplicationProperties = new ApplicationProperties(), Header = new Header() { DeliveryCount = 0 }, Properties = new Properties() { MessageId = name + i, GroupId = name } }; sender.Send(m, Timeout); } sender.Close(); host.RegisterMessageSource(name, new TestMessageSource(new Queue <Message>(messages))); var receiver = new ReceiverLink(session, "recv-link", name); var message = receiver.Receive(); var deliveryCount = message.Header.DeliveryCount; var delay = 10 * deliveryCount; var scheduleTime = DateTime.UtcNow.AddSeconds(delay); using (var ts = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) { var sndr = new SenderLink(session, "retry", "test.queue"); // create a new message to be queued with scheduled delivery time var retry = new Message(message.GetBody <string>()) { Header = message.Header, Footer = message.Footer, Properties = message.Properties, ApplicationProperties = message.ApplicationProperties }; retry.ApplicationProperties[Constants.SCHEDULED_ENQUEUE_TIME_UTC] = scheduleTime; sndr.Send(retry); receiver.Accept(message); } receiver.Close(); session.Close(); connection.Close(); }
public void ContainerHostProcessorOrderTest() { string name = "ContainerHostProcessorOrderTest"; List <Message> messages = new List <Message>(); var processor = new TestMessageProcessor(50, messages); host.RegisterMessageProcessor(name, processor); linkProcessor = new TestLinkProcessor(); host.RegisterLinkProcessor(linkProcessor); int count = 80; var connection = new Connection(Address); var session = new Session(connection); var sender = new SenderLink(session, "send-link", name); for (int i = 0; i < count; i++) { var message = new Message("msg" + i); message.Properties = new Properties() { GroupId = name }; sender.Send(message, Timeout); } sender.Close(); Assert.Equal(count, messages.Count); host.RegisterMessageSource(name, new TestMessageSource(new Queue <Message>(messages))); var receiver = new ReceiverLink(session, "recv-link", name); for (int i = 0; i < count; i++) { var message = receiver.Receive(); receiver.Accept(message); } receiver.Close(); sender = new SenderLink(session, "send-link", "any"); for (int i = 0; i < count; i++) { var message = new Message("msg" + i); message.Properties = new Properties() { GroupId = name }; sender.Send(message, Timeout); } sender.Close(); session.Close(); connection.Close(); }
static async Task <int> SslReConnectionTestAsync() { try{ ConnectionFactory factory = new ConnectionFactory(); String certFile = "C:\\location\\certification.cer"; factory.SSL.RemoteCertificateValidationCallback = ValidateServerCertificate; factory.SSL.LocalCertificateSelectionCallback = (a, b, c, d, e) => X509Certificate.CreateFromCertFile(certFile); factory.SSL.ClientCertificates.Add(X509Certificate.CreateFromCertFile(certFile)); factory.SASL.Profile = SaslProfile.External; Connection.DisableServerCertValidation = false; Address address = new Address("amqps://*****:*****@host:5671"); Connection connection = await factory.CreateAsync(address); Session session = new Session(connection); ReceiverLink receiver = new ReceiverLink(session, "receiver-link", "amqp"); Message message = receiver.Receive();; while (message != null) { Console.WriteLine("Received " + message.Body); receiver.Accept(message); message = receiver.Receive(); } await receiver.CloseAsync(); await session.CloseAsync(); await connection.CloseAsync(); return(0); } catch (Exception e) { Console.WriteLine("Exception {0}.", e); return(1); } }
static void Main(string[] args) { // Раскоментируйте для изучения протокола ;-) //Trace.TraceLevel = TraceLevel.Frame; //Trace.TraceListener = (l, f, a) => Console.WriteLine(DateTime.Now.ToString("[hh:mm:ss.fff]") + " " + string.Format(f, a)); Address address = new Address("amqp://*****:*****@localhost:5672"); Connection connection = new Connection(address); Session session = new Session(connection); Attach recvAttach = new Attach() { Source = new Source() { Address = "orders-amqp-10-consumer", Durable = 1, }, Target = new Target() { Durable = 1, } }; ReceiverLink receiver = new ReceiverLink(session, "netcore_amqp_10_consumer", recvAttach, null); Console.WriteLine("Receiver connected to broker."); while (true) { Message message = receiver.Receive(); if (message == null) { Console.WriteLine("Client exiting."); break; } Console.WriteLine("Received " + System.Text.Encoding.UTF8.GetString((byte[])message.Body) ); receiver.Accept(message); //специально НЕ подтвержадаем - а только принимаем //будет расти NACK сообщения, хотя мы их будем получать System.Threading.Thread.Sleep(3000); } receiver.Close(); session.Close(); connection.Close(); }
/// <summary> /// Method for browse or selector receive /// </summary> /// <param name="receiver">receiver link</param> /// <param name="options">receiver options</param> private void ReceiveAll(ReceiverLink receiver, ReceiverOptions options) { Message message = null; while ((message = receiver.Receive(options.Timeout)) != null) { Formatter.LogMessage(message, options); Utils.TsSnapStore(this.ptsdata, 'F', options.LogStats); if (!String.IsNullOrEmpty(options.MsgSelector)) { receiver.Accept(message); } } }
static void ReceiveMessages(string node, int count, string sessionId) { Trace.WriteLine(TraceLevel.Information, "Establishing a connection..."); Address address = new Address(sbNamespace, 5671, keyName, keyValue); Connection connection = new Connection(address); Trace.WriteLine(TraceLevel.Information, "Creating a session..."); Session session = new Session(connection); Trace.WriteLine(TraceLevel.Information, "Accepting a message session '{0}'...", sessionId ?? "<any>"); Map filters = new Map(); filters.Add(new Symbol("com.microsoft:session-filter"), sessionId); ReceiverLink receiver = new ReceiverLink( session, "sessionful-receiver-link", new Source() { Address = node, FilterSet = filters }, null); for (int i = 0; i < count; i++) { Message message = receiver.Receive(30000); if (message == null) { break; } if (i == 0) { Trace.WriteLine(TraceLevel.Information, "Received message from session '{0}'", message.Properties.GroupId); } receiver.Accept(message); } Trace.WriteLine(TraceLevel.Information, "Finished receiving. Shutting down..."); Trace.WriteLine(TraceLevel.Information, ""); receiver.Close(); session.Close(); connection.Close(); }