From 2273c62236d666ab677d964591f564f81908d6ad Mon Sep 17 00:00:00 2001 From: Jonathan Robie Date: Wed, 16 Jun 2010 18:53:07 +0000 Subject: Added Chuck Rolke's C++ .NET material from https://issues.apache.org/jira/browse/QPID-2671. git-svn-id: https://svn.apache.org/repos/asf/qpid/trunk/qpid@955346 13f79535-47bb-0310-9956-ffa450edef68 --- doc/book/src/Programming-In-Apache-Qpid.xml | 118 +++++++++++++++++++++++++--- 1 file changed, 109 insertions(+), 9 deletions(-) diff --git a/doc/book/src/Programming-In-Apache-Qpid.xml b/doc/book/src/Programming-In-Apache-Qpid.xml index 8c1ee6d252..f44ef4b178 100644 --- a/doc/book/src/Programming-In-Apache-Qpid.xml +++ b/doc/book/src/Programming-In-Apache-Qpid.xml @@ -52,7 +52,7 @@ - For Python and C++, Qpid defines its own messaging API, the + For Python, C++, and .NET, Qpid defines its own messaging API, the Qpid Messaging API, which is conceptually similar in each supported language. @@ -264,6 +264,93 @@ finally: + + + + +
+ A Simple Messaging Program in .NET C# + + The following .NET C# program shows how to create a connection, + create a session, send messages using a sender, and receive + messages using a receiver. + + + "Hello world!" in .NET C# + +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.GetContent()); + session.Acknowledge(); + + connection.Close(); + } catch (Exception e) { + Console.WriteLine("Exception {0}.", e); + if (null != connection) + connection.Close(); + } + } + } +} + + + + + + Selects the Qpid Messaging namespace. A project reference to the Org.Apache.Qpid.Messaging dll defines the Qpid Messaging namespace objects and methods. + + + Establishes the connection with the messaging broker. + + + Creates a session object, which maintains the state of all interactions with the messaging broker, and manages senders and receivers. + + + Creates a receiver that reads from the given address. + + + Creates a sender that sends to the given address. + + + Reads the next message. The duration is optional, if omitted, will wait indefinitely for the next message. + + + Acknowledges messages that have been read. To guarantee delivery, a message remains on the messaging broker until it is acknowledged by a client. session.acknowledge() acknowledges all unacknowledged messages for the given session—this allows acknowledgements to be batched, which is more efficient than acknowledging messages individually. + + + Closes the connection, all sessions managed by the connection, and all senders and receivers managed by each session. + + + + + +
+ + + + + +
Addresses @@ -295,10 +382,11 @@ finally: A queue stores each message until it has been received and acknowledged, and only one receiver can receive a given message - There are exceptions to this rule; for instance, - a receiver can use browse mode, which leaves - messages on the queue for other receivers to - read. + + There are exceptions to this rule; for instance, + a receiver can use browse mode, which leaves + messages on the queue for other receivers to + read.. A topic immediately delivers a message to all eligible receivers; if there are no eligible receivers, it discards the @@ -325,14 +413,14 @@ finally: using two programs that take an address as a command line parameter. spout sends messages to the target address, drain receives messages from - the source address. The source code is available in both C++ - and Python, and can be found in the examples directory for each + the source address. The source code is available in C++, Python, and + .NET C# and can be found in the examples directory for each language. These programs can use any address string as a source or a destination, and have many command line options to configure behavior—use the -h option for documentation on these options. - Currently, the Python and C++ + Currently, the C++, Python, and .NET C# implementations of drain and spout have slightly different options. This tutorial uses the C++ implementation. The @@ -1378,7 +1466,7 @@ enable("qpid.messaging.io", DEBUG) Messaging API, a program can ask a session for the next receiver; that is, the receiver that is responsible for the next available message. The following example shows how this - is done in C++ and Python. + is done in C++, Python, and .NET C#. Receiving Messages from Multiple Sources @@ -1401,6 +1489,18 @@ receiver2 = session.receiver(address) message = session.next_receiver().fetch() print message.content ]]> + + .NET C#: + +
-- cgit v1.2.1