Chapter 4. Examples
This chapter demonstrates the use of AMQ .NET through example programs.
See the AMQP.Net Lite examples for more sample programs.
4.1. Sending messages Copy linkLink copied to clipboard!
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
- 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
4.2. Receiving messages Copy linkLink copied to clipboard!
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
- 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.