このコンテンツは選択した言語では利用できません。

Chapter 4. Examples


This chapter demonstrates the use of AMQ .NET through example programs.

For more examples, see the AMQ .NET example suite.

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

<install-dir>\bin\Debug>simple_send "amqp://guest:guest@localhost" service_queue
Sent: simple 0
Sent: simple 1
Sent: simple 2
Sent: simple 3
Sent: simple 4
Sent: simple 5
Sent: simple 6
Sent: simple 7
Sent: simple 8
Sent: simple 9

<install-dir>\amqpnetlite\bin\Debug>

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

<install-dir>\bin\Debug>simple_recv "amqp://guest:guest@localhost" service_queue
Received: simple 0
Received: simple 1
Received: simple 2
Received: simple 3
Received: simple 4
Received: simple 5
Received: simple 6
Received: simple 7
Received: simple 8
Received: simple 9

<install-dir>\bin\Debug>
Red Hat logoGithubRedditYoutubeTwitter

詳細情報

試用、購入および販売

コミュニティー

Red Hat ドキュメントについて

Red Hat をお使いのお客様が、信頼できるコンテンツが含まれている製品やサービスを活用することで、イノベーションを行い、目標を達成できるようにします。

多様性を受け入れるオープンソースの強化

Red Hat では、コード、ドキュメント、Web プロパティーにおける配慮に欠ける用語の置き換えに取り組んでいます。このような変更は、段階的に実施される予定です。詳細情報: Red Hat ブログ.

会社概要

Red Hat は、企業がコアとなるデータセンターからネットワークエッジに至るまで、各種プラットフォームや環境全体で作業を簡素化できるように、強化されたソリューションを提供しています。

© 2024 Red Hat, Inc.