Chapter 4. Examples This chapter demonstrates the use of Red Hat build of Apache Qpid Proton DotNet through example programs. The examples are available from the source package or upstream, for more details and examples, see the Qpid Proton DotNet examples . 4.1. Sending messages This client program connects to a server using <serverHost> and <serverPort> , creates a sender for target <address> , sends 100 messages containing String and exits. Example: Sending messages using System; using Apache.Qpid.Proton.Client; 1 namespace Apache.Qpid.Proton.Examples.HelloWorld { class Program { private static readonly int MessageCount = 100; 2 static void Main(string[] args) { string serverHost = Environment.GetEnvironmentVariable("HOST") ?? "localhost"; 3 int serverPort = Convert.ToInt32(Environment.GetEnvironmentVariable("PORT") ?? "5672"); 4 string address = Environment.GetEnvironmentVariable("ADDRESS") ?? "send-receive-example"; IClient client = IClient.Create(); 5 ConnectionOptions options = new ConnectionOptions(); 6 options.User = Environment.GetEnvironmentVariable("USER"); options.Password = Environment.GetEnvironmentVariable("PASSWORD"); using IConnection connection = client.Connect(serverHost, serverPort, options); 7 using ISender sender = connection.OpenSender(address); 8 for (int i = 0; i < MessageCount; ++i) { IMessage<string> message = IMessage<string>.Create(string.Format("Hello World! [{0}]", i)); 9 ITracker tracker = sender.Send(message); 10 tracker.AwaitSettlement(); 11 Console.WriteLine(string.Format("Sent message to {0}: {1}", sender.Address, message.Body)); } } } } 1 using Apache.Qpid.Proton.Client; Imports types defined in the Proton namespace. Proton is defined by a project reference to library file Proton.Net.dll and provides all the classes, interfaces, and value types associated with Red Hat build of Apache Qpid Proton DotNet. 2 The number of messages to send. 3 serverHost is the network address of the host or virtual host for the AMQP connection and can be configured by setting the Environment variable 'HOST'. 4 serverPort is the port on the host that the broker is accepting connections and can be configured by setting the environment variable PORT . 5 Client is the container that can create multiple Connections to a broker. 6 options is used for various setting, including 'User' and 'Password'. See Section 5.1, "Connection Options" for more information. 7 connection is the AMQP Connection to a broker. 8 Create a sender for transferring messages to the broker. 9 In the message send loop a new message is created. 10 The message is sent to the broker. 11 Wait for the broker to settle the message. Running the example To run the example program, compile it and execute it from the command line. For more information, see Chapter 3, Getting started . <source-dir> \bin\Debug>Example.Send 4.2. Receiving messages This client program connects to a server using <connection-url> , creates a receiver for source <address> , and receives messages until it is terminated or it reaches <count> messages. Example: Receiving messages using System; using Apache.Qpid.Proton.Client; 1 namespace Apache.Qpid.Proton.Examples.HelloWorld { class Program { private static readonly int MessageCount = 100; 2 static void Main(string[] args) { string serverHost = Environment.GetEnvironmentVariable("HOST") ?? "localhost"; 3 int serverPort = Convert.ToInt32(Environment.GetEnvironmentVariable("PORT") ?? "5672"); 4 string address = Environment.GetEnvironmentVariable("ADDRESS") ?? "send-receive-example"; IClient client = IClient.Create(); 5 ConnectionOptions options = new ConnectionOptions(); 6 options.User = Environment.GetEnvironmentVariable("USER"); options.Password = Environment.GetEnvironmentVariable("PASSWORD"); using IConnection connection = client.Connect(serverHost, serverPort, options); 7 using IReceiver receiver = connection.OpenReceiver(address); 8 for (int i = 0; i < MessageCount; ++i) { IDelivery delivery = receiver.Receive(); 9 IMessage<object> received = delivery.Message(); 10 Console.WriteLine("Received message with body: " + received.Body); } } } } 1 using Apache.Qpid.Proton.Client; Imports types defined in the Proton namespace. Proton is defined by a project reference to library file Proton.Net.dll and provides all the classes, interfaces, and value types associated with Red Hat build of Apache Qpid Proton DotNet. 2 The number of messages to receive. 3 serverHost is the network address of the host or virtual host for the AMQP connection and can be configured by setting the Environment variable HOST . 4 serverPort is the port on the host that the broker is accepting connections and can be configured by setting the environment variable PORT . 5 Client is the container that can create multiple Connections to a broker. 6 options is used for various setting, including 'User' and 'Password'. See Section 5.1, "Connection Options" for more information. 7 connection is the AMQP Connection to a broker. 8 Create a receiver for receiving messages from the broker. 9 In the message receive loop a new delivery is received. 10 The message is obtained from the delivery . Running the example To run the example program, compile it and execute it from the command line. For more information, see Chapter 3, Getting started . <source-dir> \bin\Debug>Example.Receive

Commands:
using System; using Apache.Qpid.Proton.Client; 1 namespace Apache.Qpid.Proton.Examples.HelloWorld { class Program { private static readonly int MessageCount = 100; 2 static void Main(string[] args) { string serverHost = Environment.GetEnvironmentVariable("HOST") ?? "localhost"; 3 int serverPort = Convert.ToInt32(Environment.GetEnvironmentVariable("PORT") ?? "5672"); 4 string address = Environment.GetEnvironmentVariable("ADDRESS") ?? "send-receive-example"; IClient client = IClient.Create(); 5 ConnectionOptions options = new ConnectionOptions(); 6 options.User = Environment.GetEnvironmentVariable("USER"); options.Password = Environment.GetEnvironmentVariable("PASSWORD"); using IConnection connection = client.Connect(serverHost, serverPort, options); 7 using ISender sender = connection.OpenSender(address); 8 for (int i = 0; i < MessageCount; ++i) { IMessage<string> message = IMessage<string>.Create(string.Format("Hello World! [{0}]", i)); 9 ITracker tracker = sender.Send(message); 10 tracker.AwaitSettlement(); 11 Console.WriteLine(string.Format("Sent message to {0}: {1}", sender.Address, message.Body)); } } } }
<source-dir> \bin\Debug>Example.Send
using System; using Apache.Qpid.Proton.Client; 1 namespace Apache.Qpid.Proton.Examples.HelloWorld { class Program { private static readonly int MessageCount = 100; 2 static void Main(string[] args) { string serverHost = Environment.GetEnvironmentVariable("HOST") ?? "localhost"; 3 int serverPort = Convert.ToInt32(Environment.GetEnvironmentVariable("PORT") ?? "5672"); 4 string address = Environment.GetEnvironmentVariable("ADDRESS") ?? "send-receive-example"; IClient client = IClient.Create(); 5 ConnectionOptions options = new ConnectionOptions(); 6 options.User = Environment.GetEnvironmentVariable("USER"); options.Password = Environment.GetEnvironmentVariable("PASSWORD"); using IConnection connection = client.Connect(serverHost, serverPort, options); 7 using IReceiver receiver = connection.OpenReceiver(address); 8 for (int i = 0; i < MessageCount; ++i) { IDelivery delivery = receiver.Receive(); 9 IMessage<object> received = delivery.Message(); 10 Console.WriteLine("Received message with body: " + received.Body); } } } }
<source-dir> \bin\Debug>Example.Receive