이 콘텐츠는 선택한 언어로 제공되지 않습니다.

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
Red Hat logoGithubRedditYoutubeTwitter

자세한 정보

평가판, 구매 및 판매

커뮤니티

Red Hat 문서 정보

Red Hat을 사용하는 고객은 신뢰할 수 있는 콘텐츠가 포함된 제품과 서비스를 통해 혁신하고 목표를 달성할 수 있습니다.

보다 포괄적 수용을 위한 오픈 소스 용어 교체

Red Hat은 코드, 문서, 웹 속성에서 문제가 있는 언어를 교체하기 위해 최선을 다하고 있습니다. 자세한 내용은 다음을 참조하세요.Red Hat 블로그.

Red Hat 소개

Red Hat은 기업이 핵심 데이터 센터에서 네트워크 에지에 이르기까지 플랫폼과 환경 전반에서 더 쉽게 작업할 수 있도록 강화된 솔루션을 제공합니다.

© 2024 Red Hat, Inc.