Chapter 4. Examples This chapter demonstrates the use of AMQ .NET through example programs. For more examples, see the AMQ .NET example suite and the AMQP.Net Lite examples . 4.1. Sending messages This client program connects to a server using <connection-url> , creates a sender for target <address> , sends a message containing <message-body> , closes the connection, and exits. Example: Sending messages namespace SimpleSend { using System; using Amqp; 1 class SimpleSend { static void Main(string[] args) { string url = (args.Length > 0) ? args[0] : 2 "amqp://guest:guest@127.0.0.1:5672"; string target = (args.Length > 1) ? args[1] : "examples"; 3 int count = (args.Length > 2) ? Convert.ToInt32(args[2]) : 10; 4 Address peerAddr = new Address(url); 5 Connection connection = new Connection(peerAddr); 6 Session session = new Session(connection); SenderLink sender = new SenderLink(session, "send-1", target); 7 for (int i = 0; i < count; i++) { Message msg = new Message("simple " + i); 8 sender.Send(msg); 9 Console.WriteLine("Sent: " + msg.Body.ToString()); } sender.Close(); 10 session.Close(); connection.Close(); } } } 1 using Amqp; Imports types defined in the Amqp namespace. Amqp is defined by a project reference to library file Amqp.Net.dll and provides all the classes, interfaces, and value types associated with AMQ .NET. 2 Command line arg[0] url is the network address of the host or virtual host for the AMQP connection. This string describes the connection transport, the user and password credentials, and the port number for the connection on the remote host. url may address a broker, a standalone peer, or an ingress point for a router network. 3 Command line arg[1] target is the name of the message destination endpoint or resource in the remote host. 4 Command line arg[2] count is the number of messages to send. 5 peerAddr is a structure required for creating an AMQP connection. 6 Create the AMQP connection. 7 sender is a client SenderLink over which messages may be sent. The link is arbitrarily named send-1 . Use link names that make sense in your environment and will help to identify traffic in a busy system. Link names are not restricted but must be unique within the same session. 8 In the message send loop a new message is created. 9 The message is sent to the AMQP peer. 10 After all messages are sent then the protocol objects are shut down in an orderly fashion. 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 . <install-dir> \bin\Debug>simple_send "amqp://guest:guest@localhost" service_queue 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 namespace SimpleRecv { using System; using Amqp; 1 class SimpleRecv { static void Main(string[] args) { string url = (args.Length > 0) ? args[0] : 2 "amqp://guest:guest@127.0.0.1:5672"; string source = (args.Length > 1) ? args[1] : "examples"; 3 int count = (args.Length > 2) ? Convert.ToInt32(args[2]) : 10; 4 Address peerAddr = new Address(url); 5 Connection connection = new Connection(peerAddr); 6 Session session = new Session(connection); ReceiverLink receiver = new ReceiverLink(session, "recv-1", source); 7 for (int i = 0; i < count; i++) { Message msg = receiver.Receive(); 8 receiver.Accept(msg); 9 Console.WriteLine("Received: " + msg.Body.ToString()); } receiver.Close(); 10 session.Close(); connection.Close(); } } } 1 using Amqp; Imports types defined in the Amqp namespace. Amqp is defined by a project reference to library file Amqp.Net.dll and provides all the classes, interfaces, and value types associated with AMQ .NET. 2 Command line arg[0] url is the network address of the host or virtual host for the AMQP connection. This string describes the connection transport, the user and password credentials, and the port number for the connection on the remote host. url may address a broker, a standalone peer, or an ingress point for a router network. 3 Command line arg[1] source is the name of the message source endpoint or resource in the remote host. 4 Command line arg[2] count is the number of messages to send. 5 peerAddr is a structure required for creating an AMQP connection. 6 Create the AMQP connection. 7 receiver is a client ReceiverLink over which messages may be received. The link is arbitrarily named recv-1 . Use link names that make sense in your environment and will help to identify traffic in a busy system. Link names are not restricted but must be unique within the same session. 8 A message is received. 9 The messages is accepted. This transfers ownership of the message from the peer to the receiver. 10 After all messages are received then the protocol objects are shut down in an orderly fashion. 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 . <install-dir> \bin\Debug>simple_recv "amqp://guest:guest@localhost" service_queue

Commands:
namespace SimpleSend { using System; using Amqp; 1 class SimpleSend { static void Main(string[] args) { string url = (args.Length > 0) ? args[0] : 2 "amqp://guest:guest@127.0.0.1:5672"; string target = (args.Length > 1) ? args[1] : "examples"; 3 int count = (args.Length > 2) ? Convert.ToInt32(args[2]) : 10; 4 Address peerAddr = new Address(url); 5 Connection connection = new Connection(peerAddr); 6 Session session = new Session(connection); SenderLink sender = new SenderLink(session, "send-1", target); 7 for (int i = 0; i < count; i++) { Message msg = new Message("simple " + i); 8 sender.Send(msg); 9 Console.WriteLine("Sent: " + msg.Body.ToString()); } sender.Close(); 10 session.Close(); connection.Close(); } } }
<install-dir> \bin\Debug>simple_send "amqp://guest:guest@localhost" service_queue
namespace SimpleRecv { using System; using Amqp; 1 class SimpleRecv { static void Main(string[] args) { string url = (args.Length > 0) ? args[0] : 2 "amqp://guest:guest@127.0.0.1:5672"; string source = (args.Length > 1) ? args[1] : "examples"; 3 int count = (args.Length > 2) ? Convert.ToInt32(args[2]) : 10; 4 Address peerAddr = new Address(url); 5 Connection connection = new Connection(peerAddr); 6 Session session = new Session(connection); ReceiverLink receiver = new ReceiverLink(session, "recv-1", source); 7 for (int i = 0; i < count; i++) { Message msg = receiver.Receive(); 8 receiver.Accept(msg); 9 Console.WriteLine("Received: " + msg.Body.ToString()); } receiver.Close(); 10 session.Close(); connection.Close(); } } }
<install-dir> \bin\Debug>simple_recv "amqp://guest:guest@localhost" service_queue