此内容没有您所选择的语言版本。

3.6.3. "Hello World" Walk-through


The Qpid Messaging client development libraries contain the functions we need to communicate with the messaging broker and create and manage messages, so our first task is to import them to our program:
Python
from qpid.messaging import *
Copy to Clipboard Toggle word wrap
C++
#include <qpid/messaging/Connection.h>
#include <qpid/messaging/Message.h>
#include <qpid/messaging/Receiver.h>
#include <qpid/messaging/Sender.h>
#include <qpid/messaging/Session.h>

using namespace qpid::messaging;
Copy to Clipboard Toggle word wrap
C#/.NET
using Org.Apache.Qpid.Messaging;  

namespace Org.Apache.Qpid.Messaging {
Copy to Clipboard Toggle word wrap
To communicate with a message broker we need a connection. We get one by creating an instance of a Connection object. The Connection object constructor takes the url of the broker as its parameter:
Python
connection = Connection("localhost:5672")
Copy to Clipboard Toggle word wrap
C++
Connection connection(broker);
Copy to Clipboard Toggle word wrap
C#/.NET
Connection connection = null;
connection = new Connection(broker);
Copy to Clipboard Toggle word wrap
When you connect to a remote server that requires authentication you can provide a connection url in the form username/password@serverurl:port. If you try this with a remote server, remember to open the firewall on the message broker to allow incoming connections for the broker port.
To open a connection using the AMQP 1.0 protocol, specify it like this:
C++
Connection connection(broker, "{protocol:amqp1.0}");
Copy to Clipboard Toggle word wrap
C#/.NET
connection = new Connection(broker, "{protocol:amqp1.0}");
Copy to Clipboard Toggle word wrap
Now that we have a Connection instance configured for our broker, the next step is to open the connection. The Connection object has an open method, which opens a configured connection.
Opening the connection might fail, if, for example, the message broker is off-line, so for languages that support it we will wrap it in a try:except block, and catch any exception.
Remember that Python uses indentation, so be careful with your spacing:
Python
try:
  connection.open()
Copy to Clipboard Toggle word wrap
C++
try {
  connection.open();
Copy to Clipboard Toggle word wrap
C#/.NET
connection.Open();
Copy to Clipboard Toggle word wrap
Now that we have an open connection to the server, we need to create a session. A session is a scoped conversation between our application and the server. The server uses the scope of the session to enforce exclusive access and session-scoped lifetimes of queues.
The Connection object has a createSession method (session in Python) that returns a Session object, so we get a session from the connection that we created previously:
Python
session = connection.session()
Copy to Clipboard Toggle word wrap
C++
Session session = connection.createSession();
Copy to Clipboard Toggle word wrap
C#/.NET
Session session = connection.CreateSession();
Copy to Clipboard Toggle word wrap
The Session object has sender and receiver methods, which take a target or source address as a parameter, and return a Sender and a Receiver object, respectively. These are the objects that we need to send and receive messages, so we will create them by calling the respective methods of our session. We will use the amq.topic exchange for this demo. This is a pre-configured exchange on the broker, so we don't need to create it, and we can rely on its presence:
Python
sender = session.sender("amq.topic")
receiver = session.receiver("amq.topic")
Copy to Clipboard Toggle word wrap
C++
Receiver receiver = session.createReceiver(address); 
Sender sender = session.createSender(address);
Copy to Clipboard Toggle word wrap
C#/.NET
Receiver receiver = session.CreateReceiver(address);  
Sender sender = session.CreateSender(address);
Copy to Clipboard Toggle word wrap
A sender can be thought of as a router. It routes messages from our application to the broker. The parameter we pass to the sender's constructor is the destination on the broker that our messages will be routed to. In this case, our sender will route messages to the amq.topic exchange on the broker. Because our routing target is an exchange, it will be routed further from there by the broker.
A receiver can be thought of as a subscriber. When we create a receiver, the parameter we pass to the constructor is resolved to an object on the server. If the object is a queue, then our receiver is subscribed to that queue. If the object is an exchange, as it is in this case, then a queue is created in the background and subscribed to the exchange for us. We will look in more detail at this later. For now, suffice it to say that our sender will send a message to the amq.topic exchange, and our receiver will receive it in a queue.
Now that we have a sender and a receiver, it's time to create a message to send. The Message object takes as a parameter to its constructor a string that becomes the message.content:
Python
message = Message("Hello World!")
Copy to Clipboard Toggle word wrap
The Message object constructor sets the correct content-type when you set the message.content through the constructor. However, if you set it after creating the Message object by assigning a value to the message.content property, then you also need to set the message.content_type property appropriately.
We can now use the send method of our sender to send the message to the broker:
Python
sender.send(message)
Copy to Clipboard Toggle word wrap
C++
sender.send(Message("Hello world!"));
Copy to Clipboard Toggle word wrap
C#/.NET
sender.Send(new Message("Hello world!"));
Copy to Clipboard Toggle word wrap
The message is sent to the amq.topic exchange on the message broker.
When we created our receiver, in the background the broker created a private temporary queue and subscribed it to the amq.topic exchange for us. The message is now waiting in that queue.
The next step is to retrieve the message from the broker using the fetch method of our receiver:
Python
fetchedmessage = receiver.fetch(timeout=1)
Copy to Clipboard Toggle word wrap
C++
Message message = receiver.fetch(Duration::SECOND * 1);
Copy to Clipboard Toggle word wrap
C#/.NET
Message message = new Message();
message = receiver.Fetch(DurationConstants.SECOND * 1);
Copy to Clipboard Toggle word wrap
The timeout parameter tells fetch how long to wait for a message. If we do not set a timeout the receiver will wait indefinitely for a message to appear on the queue. If we set the timeout to 0, the receiver will check the queue and return immediately if nothing is there. We set it to timeout in 1 second to ensure ample time for our message to be routed and appear in the queue.
We should now have a message, so we will print it out. Fetch returns a Message object, so we will print its content property:
Python
print fetchedmessage.content
Copy to Clipboard Toggle word wrap
C++
std::cout << message.getContent() << std::endl;
Copy to Clipboard Toggle word wrap
C#/.NET
Console.WriteLine("{0}", message.GetContent());
Copy to Clipboard Toggle word wrap
To finish the transaction, acknowledge receipt of the message, which allows the message broker to clear it from the queue (dequeue the message):
Python
session.acknowledge()
Copy to Clipboard Toggle word wrap
C++
session.acknowledge();
Copy to Clipboard Toggle word wrap
C#/.NET
session.Acknowledge();
Copy to Clipboard Toggle word wrap
And finally, catch any exceptions for languages that support exception handling, and print something sensible to the console if they occur, and close our connection to the message broker:
Python
except MessagingError,m:
    print m

connection.close()
Copy to Clipboard Toggle word wrap
C++
} catch(const std::exception& error) {
   std::cerr << error.what() << std::endl;
   connection.close();
   return 1;   
}
Copy to Clipboard Toggle word wrap
C#/.NET
} catch (Exception e) {
   Console.WriteLine("Exception {0}.", e);
   if (connection != null)
     connection.Close();
}
Copy to Clipboard Toggle word wrap
To run the program, save the file as helloworld.py, and then run it using the command python helloworld.py. If the message broker is running on your local machine, you should see the words: "Hello World!" printed on your programlisting.
返回顶部
Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。 了解我们当前的更新.

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

Theme

© 2025 Red Hat