From eb92423c6062500d0d9495c37b5ced6dc71f9b68 Mon Sep 17 00:00:00 2001 From: Jonathan Robie Date: Wed, 12 May 2010 21:50:44 +0000 Subject: Added callouts for C++ and Python examples. Attempted formatting for WCF examples (not yet there ;-> ) git-svn-id: https://svn.apache.org/repos/asf/qpid/trunk/qpid@943709 13f79535-47bb-0310-9956-ffa450edef68 --- doc/book/src/Programming-In-Apache-Qpid.xml | 548 +++++++++++++--------------- 1 file changed, 248 insertions(+), 300 deletions(-) (limited to 'doc') diff --git a/doc/book/src/Programming-In-Apache-Qpid.xml b/doc/book/src/Programming-In-Apache-Qpid.xml index 93d9c4db54..5af14139f6 100644 --- a/doc/book/src/Programming-In-Apache-Qpid.xml +++ b/doc/book/src/Programming-In-Apache-Qpid.xml @@ -142,35 +142,59 @@ #include #include -#include +#include ]]> 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); + Connection connection(broker); try { - connection.open(); - Session session = connection.createSession(); + connection.open(); + Session session = connection.createSession(); - Receiver receiver = session.createReceiver(address); - Sender sender = session.createSender(address); + 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.getContent() << std::endl; - session.acknowledge(); + Message message = receiver.fetch(Duration::SECOND * 1); + + session.acknowledge(); - connection.close(); + connection.close(); return 0; - } catch(const std::exception& error) { - std::cerr << error.what() << std::endl; + } catch(const std::exception& error) { + connection.close(); return 1; } -}]]> +} + + + + 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. + + @@ -190,28 +214,53 @@ import sys from qpid.messaging import * broker = "localhost:5672" if len(sys.argv)<2 else sys.argv[1] -address = "amq.topic" if len(sys.argv)<3 else sys.argv[2] +address = "amq.topic" if len(sys.argv)<3 else sys.argv[2]]]> connection = Connection(broker) try: - connection.open() - session = connection.session() + connection.open() + session = connection.session() - sender = session.sender(address) - receiver = session.receiver(address) + sender = session.sender(address) + receiver = session.receiver(address) sender.send(Message("Hello world!")); - message = receiver.fetch(timeout=1) + message = receiver.fetch(timeout=1) print message.content - session.acknowledge() # acknowledge message receipt + session.acknowledge() except MessagingError,m: print m finally: - connection.close() -]]> + connection.close() + + + + + 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. + + + @@ -2713,108 +2762,6 @@ producer.send(m); - -
- Java JMS Selector Syntax - The AMQP Java JMS Messaging Client supports the following syntax for JMS selectors. - - - Comments: - - - - - - Reserved Words (case insensitive) - - - - - - Literals (case insensitive) - - )? // eg: 5.5 or 5. or 5.5E10 or 5.E10 - | "." (["0"-"9"])+ ()? // eg: .5 or .5E10 - | (["0"-"9"])+ // eg: 5E10 -) -EXPONENT: "E" (["+","-"])? (["0"-"9"])+ -STRING_LITERAL: "'" ( ("''") | ~["'"] )* "'" -]]> - - - - - Identifiers (case insensitive) - - - - - - - Grammar - - andExpression )* ) -andExpression := ( equalityExpression ( equalityExpression )* ) -equalityExpression := ( comparisonExpression ( "=" comparisonExpression - | "<>" comparisonExpression - | - | )* ) -comparisonExpression := - ( addExpression - ( ">" addExpression - | ">=" addExpression - | "<" addExpression - | "<=" addExpression - | stringLitteral ( stringLitteral )? - | ( )? - | addExpression addExpression - | addExpression addExpression - | "(" ( "," )* ")" - | "(" ( "," )* ")" - )* - ) - -addExpression := multExpr ( ( "+" multExpr | "-" multExpr ) )* -multExpr := unaryExpr ( "*" unaryExpr | "/" unaryExpr | "%" unaryExpr )* -unaryExpr := ( "+" unaryExpr | "-" unaryExpr | unaryExpr | primaryExpr ) -primaryExpr := ( literal | variable | "(" orExpression ")" ) -literal := ( - | - | - | - | - | - | - | - ) - -variable := ( | )]]> - - - -
- @@ -2839,70 +2786,71 @@ variable := ( | )]]> - public static int GreetingCount - { - get { return greetingCount; } - } + channelFactory = + new ChannelFactory(amqpBinding, clientEndpoint); + IHelloService clientProxy = channelFactory.CreateChannel(); - public void SayHello(string greeting) - { - Console.WriteLine("Service received: " + greeting); - greetingCount++; - } + clientProxy.SayHello("Greetings from WCF client"); - static void Main(string[] args) + // wait for service to process the greeting + while (HelloService.GreetingCount == 0) { - try - { - AmqpBinding amqpBinding = new AmqpBinding(); - amqpBinding.BrokerHost = "localhost"; - amqpBinding.BrokerPort = 5672; - - ServiceHost serviceHost = new ServiceHost(typeof(HelloService)); - serviceHost.AddServiceEndpoint(typeof(IHelloService), - amqpBinding, "amqp:hello_service_node"); - serviceHost.Open(); - - // Send the service a test greeting - Uri amqpClientUri = new Uri("amqp:amq.direct?routingkey=hello_service_node"); - EndpointAddress clientEndpoint = new EndpointAddress(amqpClientUri); - ChannelFactory channelFactory = - new ChannelFactory(amqpBinding, clientEndpoint); - IHelloService clientProxy = channelFactory.CreateChannel(); - - clientProxy.SayHello("Greetings from WCF client"); - - // wait for service to process the greeting - while (HelloService.GreetingCount == 0) - { - Thread.Sleep(100); - } - channelFactory.Close(); - serviceHost.Close(); - } - catch (Exception e) - { - Console.WriteLine("Exception: {0}", e); - } + Thread.Sleep(100); } + channelFactory.Close(); + serviceHost.Close(); + } + catch (Exception e) + { + Console.WriteLine("Exception: {0}", e); + } } + } } ]]> @@ -2931,89 +2879,89 @@ namespace Apache.Qpid.Documentation.HelloService 0) - { - broker = args[0]; - } - - if (args.Length > 1) - { - port = int.Parse(args[1]); - } - - if (args.Length > 2) - { - target = args[2]; - } - - if (args.Length > 3) - { - source = args[3]; - } - - AmqpBinaryBinding binding = new AmqpBinaryBinding(); - binding.BrokerHost = broker; - binding.BrokerPort = port; - - IChannelFactory receiverFactory = binding.BuildChannelFactory(); - receiverFactory.Open(); - IInputChannel receiver = receiverFactory.CreateChannel(new EndpointAddress("amqp:" + source)); - receiver.Open(); - - IChannelFactory senderFactory = binding.BuildChannelFactory(); - senderFactory.Open(); - IOutputChannel sender = senderFactory.CreateChannel(new EndpointAddress("amqp:" + target)); - sender.Open(); - - sender.Send(Message.CreateMessage(MessageVersion.None, "", new HelloWorldBinaryBodyWriter())); - - Message message = receiver.Receive(); - XmlDictionaryReader reader = message.GetReaderAtBodyContents(); - while (!reader.HasValue) - { - reader.Read(); - } - - byte[] binaryContent = reader.ReadContentAsBase64(); - string text = Encoding.UTF8.GetString(binaryContent); - - Console.WriteLine(text); - - senderFactory.Close(); - receiverFactory.Close(); - } + using System; + using System.ServiceModel; + using System.ServiceModel.Channels; + using System.ServiceModel.Description; + using System.Text; + using System.Xml; + using Apache.Qpid.Channel; + + public class HelloWorld + { + static void Main(string[] args) + { + String broker = "localhost"; + int port = 5672; + String target = "amq.topic"; + String source = "my_topic_node"; + + if (args.Length > 0) + { + broker = args[0]; + } + + if (args.Length > 1) + { + port = int.Parse(args[1]); + } + + if (args.Length > 2) + { + target = args[2]; + } + + if (args.Length > 3) + { + source = args[3]; + } + + AmqpBinaryBinding binding = new AmqpBinaryBinding(); + binding.BrokerHost = broker; + binding.BrokerPort = port; + + IChannelFactory receiverFactory = binding.BuildChannelFactory(); + receiverFactory.Open(); + IInputChannel receiver = receiverFactory.CreateChannel(new EndpointAddress("amqp:" + source)); + receiver.Open(); + + IChannelFactory senderFactory = binding.BuildChannelFactory(); + senderFactory.Open(); + IOutputChannel sender = senderFactory.CreateChannel(new EndpointAddress("amqp:" + target)); + sender.Open(); + + sender.Send(Message.CreateMessage(MessageVersion.None, "", new HelloWorldBinaryBodyWriter())); + + Message message = receiver.Receive(); + XmlDictionaryReader reader = message.GetReaderAtBodyContents(); + while (!reader.HasValue) + { + reader.Read(); + } + + byte[] binaryContent = reader.ReadContentAsBase64(); + string text = Encoding.UTF8.GetString(binaryContent); + + Console.WriteLine(text); + + senderFactory.Close(); + receiverFactory.Close(); } + } - public class HelloWorldBinaryBodyWriter : BodyWriter - { - public HelloWorldBinaryBodyWriter() : base (true) {} + public class HelloWorldBinaryBodyWriter : BodyWriter + { + public HelloWorldBinaryBodyWriter() : base (true) {} - protected override void OnWriteBodyContents(XmlDictionaryWriter writer) - { - byte[] binaryContent = Encoding.UTF8.GetBytes("Hello world!"); + protected override void OnWriteBodyContents(XmlDictionaryWriter writer) + { + byte[] binaryContent = Encoding.UTF8.GetBytes("Hello world!"); - // wrap the content: - writer.WriteStartElement("Binary"); - writer.WriteBase64(binaryContent, 0, binaryContent.Length); - } + // wrap the content: + writer.WriteStartElement("Binary"); + writer.WriteBase64(binaryContent, 0, binaryContent.Length); } + } } ]]> @@ -3151,25 +3099,25 @@ namespace Apache.Qpid.Samples.Channel.HelloWorld For example, on output: On input the headers can be accessed from the Message or extracted from the operation context @@ -3180,9 +3128,9 @@ namespace Apache.Qpid.Samples.Channel.HelloWorld To engage TLS/SSL: Currently the WCF client only provides SASL PLAIN (i.e. username and @@ -3197,12 +3145,12 @@ namespace Apache.Qpid.Samples.Channel.HelloWorld binding itself. Here is a sample using ClientCredentials: (bindingParameters); +ClientCredentials credentials = new ClientCredentials(); +credentials.UserName.UserName = "guest"; +credentials.UserName.Password = "guest"; +bindingParameters = new BindingParameterCollection(); +bindingParameters.Add(credentials); +readerFactory = binding.BuildChannelFactory(bindingParameters); ]]> @@ -3223,14 +3171,15 @@ namespace Apache.Qpid.Samples.Channel.HelloWorld Server code: Because this operation involves two transaction resources, the @@ -3247,21 +3196,20 @@ namespace Apache.Qpid.Samples.Channel.HelloWorld TransactionScope: channelFactory = - new ChannelFactory(amqpBinding, clientEndpoint); - IHelloService clientProxy = channelFactory.CreateChannel(); - - using (TransactionScope ts = new TransactionScope()) - { - AmqpProperties amqpProperties = new AmqpProperties(); - clientProxy.SayHello("Greetings from WCF client"); - // increment ExactlyOnceSent counter on DB - - ts.Complete(); - } +AmqpProperties myDefaults = new AmqpProperties(); +myDefaults.Durable = true; +amqpBinding.DefaultMessageProperties = myDefaults; +ChannelFactory channelFactory = +new ChannelFactory(amqpBinding, clientEndpoint); +IHelloService clientProxy = channelFactory.CreateChannel(); + +using (TransactionScope ts = new TransactionScope()) +{ + AmqpProperties amqpProperties = new AmqpProperties(); + clientProxy.SayHello("Greetings from WCF client"); + // increment ExactlyOnceSent counter on DB + ts.Complete(); +} ]]> -- cgit v1.2.1