3.6. Hello World
3.6.1. Red Hat Enterprise Messaging "Hello World"
Here is the "Hello World" example, showing how to send and receive a message with Red Hat Enterprise Messaging using the Qpid Messaging API.
- Python
import sys from qpid.messaging import * connection = Connection("localhost:5672") try: connection.open() session = connection.session() sender = session.sender("amq.topic") receiver = session.receiver("amq.topic") message = Message("Hello World!") sender.send(message) fetchedmessage = receiver.fetch(timeout=1) print fetchedmessage.content session.acknowledge() except MessagingError,m: print m connection.close()
- C#/.NET
using System; using Org.Apache.Qpid.Messaging; namespace Org.Apache.Qpid.Messaging { class Program { static void Main(string[] args) { String broker = args.Length > 0 ? args[0] : "localhost:5672"; String address = args.Length > 1 ? args[1] : "amq.topic"; Connection connection = null; try { connection = new Connection(broker); connection.Open(); Session session = connection.CreateSession(); Receiver receiver = session.CreateReceiver(address); Sender sender = session.CreateSender(address); sender.Send(new Message("Hello world!")); Message message = new Message(); message = receiver.Fetch(DurationConstants.SECOND * 1); Console.WriteLine("{0}", message.GetContentObject()); session.Acknowledge(); connection.Close(); } catch (Exception e) { Console.WriteLine("Exception {0}.", e); if (connection != null) connection.Close(); } } } }
- 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> #include <iostream> using namespace qpid::messaging; int main(int argc, char** argv) { std::string broker = argc > 1 ? argv[1] : "localhost:5672"; std::string address = argc > 2 ? argv[2] : "amq.topic"; Connection connection(broker); try { connection.open(); Session session = connection.createSession(); Receiver receiver = session.createReceiver(address); Sender sender = session.createSender(address); sender.send(Message("Hello world!")); Message message = receiver.fetch(Duration::SECOND * 1); std::cout << message.getContentObject() << std::endl; session.acknowledge(); connection.close(); return 0; } catch(const std::exception& error) { std::cerr << error.what() << std::endl; connection.close(); return 1; } }