Search

Appendix B. Example programs

download PDF

B.1. Prerequisites

  • Red Hat AMQ Broker with queue named amq.topic and with a queue named service_queue both with read/write permissions. For this illustration the broker was at IP address 10.10.1.1.
  • Red Hat AMQ Interconnect with source and target name amq.topic with suitable permissions. For this illustration the router was at IP address 10.10.2.2.

All the examples run from <install-dir>\bin\Debug.

B.2. HelloWorld simple

HelloWorld-simple is a simple example that creates a Sender and a Receiver for the same address, sends a message to the address, reads a message from the address, and prints the result.

HelloWorld-simple command line options

 Command line:
   HelloWorld-simple [brokerUrl [brokerEndpointAddress]]
 Default:
   HelloWorld-simple amqp://localhost:5672 amq.topic

HelloWorld-simple sample invocation

$ HelloWorld-simple
Hello world!

By default, this program connects to a broker running on localhost:5672. Specify a host and port, and the AMQP endpoint address explicitly on the command line:

$ HelloWorld-simple amqp://someotherhost.com:5672 endpointname

By default, this program addresses its messages to amq.topic. In some Amqp brokers amq.topic is a predefined endpoint address and is immediately available with no broker configuration. If this address does not exist in the broker then use a broker management tool to create it.

B.3. HelloWorld robust

HelloWorld-robust shares all the features of the simple example with additional options and processing:

  • Accessing message properties beyond the simple payload:

    • Header
    • DeliveryAnnotations
    • MessageAnnotations
    • Properties
    • ApplicationProperties
    • BodySection
    • Footer
  • Connection shutdown sequence

HelloWorld-robust command line options

 Command line:
   HelloWorld-robust [brokerUrl [brokerEndpointAddress [payloadText [enableTrace]]]]
 Default:
   HelloWorld-robust amqp://localhost:5672 amq.topic "Hello World"
Note

The simple presence of the enableTrace argument enables tracing. The argument may hold any value.

HelloWorld-robust sample invocation

$ HelloWorld-robust
Broker: amqp://localhost:5672, Address: amq.topic, Payload: Hello World!
body:Hello World!

HelloWorld-robust allows the user to specify a payload string and to enable trace protocol logging.

$ HelloWorld-robust amqp://localhost:5672 amq.topic "My Hello" loggingOn

B.4. Interop.Drain.cs, Interop.Spout.cs (performance exerciser)

AMQ .NET examples Interop.Drain and Interop.Spout illustrate interaction with Red Hat AMQ Interconnect. In this case there is no message broker. Instead the Red Hat AMQ Interconnect registers the addresses requested by the client programs and routes messages between them.

Interop.Drain command line options

 $ Interop.Drain.exe --help
 Usage: interop.drain [OPTIONS] --address STRING
 Create a connection, attach a receiver to an address, and receive messages.

 Options:
  --broker [amqp://guest:guest@127.0.0.1:5672] - AMQP 1.0 peer connection address
  --address STRING     []      - AMQP 1.0 terminus name
  --timeout SECONDS    [1]     - time to wait for each message to be received
  --forever            [false] - use infinite receive timeout
  --count INT          [1]     - receive this many messages and exit; 0 disables count based exit
  --initial-credit INT [10]    - receiver initial credit
  --reset-credit INT   [5]     - reset credit to initial-credit every reset-credit messages
  --quiet              [false] - do not print each message's content
  --help                       - print this message and exit

 Exit codes:
  0 - successfully received all messages
  1 - timeout waiting for a message
  2 - other error

Interop.Spout command line options

 $ interop.spout --help
 Usage: Interop.Spout [OPTIONS] --address STRING
 Create a connection, attach a sender to an address, and send messages.

 Options:
  --broker [amqp://guest:guest@127.0.0.1:5672] - AMQP 1.0 peer connection address
  --address STRING  []      - AMQP 1.0 terminus name
  --timeout SECONDS [0]     - send for N seconds; 0 disables timeout
  --durable         [false] - send messages marked as durable
  --count INT       [1]     - send this many messages and exit; 0 disables count based exit
  --id STRING       [guid]  - message id
  --replyto STRING  []      - message ReplyTo address
  --content STRING  []      - message content
  --print           [false] - print each message's content
  --help                    - print this message and exit

 Exit codes:
  0 - successfully received all messages
  2 - other error

Interop.Spout and Interop.Drain sample invocation

In one window run Interop.drain. Drain waits forever for one message to arrive.

$ Interop.Drain.exe --broker amqp://10.10.2.2:5672 --forever --count 1 --address amq.topic

In another window run Interop.spout. Spout sends a message to the broker address and exits.

$ interop.spout --broker amqp://10.10.2.2:5672 --address amq.topic
$

Now in the first window drain will have received the message from spout and then exited.

$ Interop.Drain.exe --broker amqp://10.10.2.2:5672 --forever --count 1 --address amq.topic
Message(Properties=properties(message-id:9803e781-14d3-4fa7-8e39-c65e18f3e8ea:0), ApplicationProperties=, Body=
$

B.5. Interop.Client, Interop.Server (request-response)

This example shows a simple broker-based server that will accept strings from a client, convert them to upper case, and send them back to the client. It has two components:

  • client - sends lines of poetry to the server and prints responses.
  • server - a simple service that will convert incoming strings to upper case and return them to the requester.

In this example the server and client share a service endpoint in the broker named service_queue. The server listens for messages at the service endpoint. Clients create temporary dynamic ReplyTo queues, embed the temporary name in the requests, and send the requests to the server. After receiving and processing each request the server sends the reply to the client’s temporary ReplyTo address.

Interop.Client command line options

 Command line:
   Interop.Client [peerURI [loopcount]]
 Default:
   Interop.Client amqp://guest:guest@localhost:5672 1

Interop.Server command line options

 Command line:
   Interop.Server [peerURI]
 Default:
   Interop.Server amqp://guest:guest@localhost:5672

Interop.Client, Interop.Server sample invocation

The programs may be launched with these command lines:

$ Interop.Server.exe amqp://guest:guest@localhost:5672
$ Interop.Client.exe amqp://guest:guest@localhost:5672

PeerToPeer.Server creates a listener on the address given in the command line. This address initializes a ContainerHost class object that listens for incoming connections. Received messages are forwarded asynchronously to a RequestProcessor class object.

PeerToPeer.Client opens a connection to the server and starts sending messages to the server.

PeerToPeer.Client command line options

 Command line:
   PeerToPeer.Client [peerURI]
 Default:
   PeerToPeer.Client amqp://guest:guest@localhost:5672

PeerToPeer.Server command line options

 Command line:
   PeerToPeer.Server [peerURI]
 Default:
   PeerToPeer.Server amqp://guest:guest@localhost:5672

PeerToPeer.Client, PeerToPeer.Server sample invocation

In one window run the PeerToPeer.Server

$ PeerToPeer.Server.exe
Container host is listening on 127.0.0.1:5672
Request processor is registered on request_processor
Press enter key to exist...
Received a request hello 0
...

In another window run PeerToPeer.Client. PeerToPeer.Client sends messages the the server and prints responses as they are received.

$ PeerToPeer.Client.exe
Running request client...
Sent request properties(message-id:command-request,reply-to:client-57db8f65-6e3d-474c-a05e-8ca63b69d7c0) body hello 0
Received response:  body reply0
Received response:  body reply1
^C
Red Hat logoGithubRedditYoutubeTwitter

Learn

Try, buy, & sell

Communities

About Red Hat Documentation

We help Red Hat users innovate and achieve their goals with our products and services with content they can trust.

Making open source more inclusive

Red Hat is committed to replacing problematic language in our code, documentation, and web properties. For more details, see the Red Hat Blog.

About Red Hat

We deliver hardened solutions that make it easier for enterprises to work across platforms and environments, from the core datacenter to the network edge.

© 2024 Red Hat, Inc.