diff options
author | Stephen D. Huston <shuston@apache.org> | 2011-10-21 14:42:12 +0000 |
---|---|---|
committer | Stephen D. Huston <shuston@apache.org> | 2011-10-21 14:42:12 +0000 |
commit | f83677056891e436bf5ba99e79240df2a44528cd (patch) | |
tree | 625bfd644b948e89105630759cf6decb0435354d /cpp/bindings/qpid/ruby/examples | |
parent | ebfd9ff053b04ab379acfc0fefedee5a31b6d8a5 (diff) | |
download | qpid-python-QPID-2519.tar.gz |
Merged out from trunkQPID-2519
git-svn-id: https://svn.apache.org/repos/asf/qpid/branches/QPID-2519@1187375 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'cpp/bindings/qpid/ruby/examples')
-rw-r--r-- | cpp/bindings/qpid/ruby/examples/client.rb | 50 | ||||
-rw-r--r-- | cpp/bindings/qpid/ruby/examples/drain.rb | 111 | ||||
-rw-r--r-- | cpp/bindings/qpid/ruby/examples/hello_world.rb | 49 | ||||
-rw-r--r-- | cpp/bindings/qpid/ruby/examples/map_receiver.rb | 63 | ||||
-rw-r--r-- | cpp/bindings/qpid/ruby/examples/map_sender.rb | 52 | ||||
-rw-r--r-- | cpp/bindings/qpid/ruby/examples/server.rb | 51 | ||||
-rw-r--r-- | cpp/bindings/qpid/ruby/examples/spout.rb | 126 |
7 files changed, 502 insertions, 0 deletions
diff --git a/cpp/bindings/qpid/ruby/examples/client.rb b/cpp/bindings/qpid/ruby/examples/client.rb new file mode 100644 index 0000000000..f42f25cfc9 --- /dev/null +++ b/cpp/bindings/qpid/ruby/examples/client.rb @@ -0,0 +1,50 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# + +$:.unshift File.join(File.dirname(__FILE__), "..", "lib") + +require 'qpid' + +if __FILE__ == $0 + broker = ARGV[1] || "amqp:tcp:localhost:5672" + options = ARGV[2] || "" + + connection = Qpid::Messaging::Connection.new broker, options + connection.open + session = connection.create_session + sender = session.create_sender "service_queue" + response_queue = Qpid::Messaging::Address.new("#response-queue", "", + :create => :always, + :delete => :always) + receiver = session.create_receiver response_queue + + ["Twas brillig, and the slithy toves", + "Did gire and gymble in the wabe.", + "All mimsy were the borogroves,", + "And the mome raths outgrabe."].each do |line| + request = Qpid::Messaging::Message.new :content => line + request.reply_to = response_queue + sender.send request + response = receiver.fetch + puts "#{request.content} -> #{response.content}" + end + + connection.close +end + diff --git a/cpp/bindings/qpid/ruby/examples/drain.rb b/cpp/bindings/qpid/ruby/examples/drain.rb new file mode 100644 index 0000000000..a6cf35e189 --- /dev/null +++ b/cpp/bindings/qpid/ruby/examples/drain.rb @@ -0,0 +1,111 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# + +$:.unshift File.join(File.dirname(__FILE__), "..", "lib") + +require 'qpid' +require 'optparse' + +options = { + :broker => "localhost", + :timeout => Qpid::Messaging::Duration::IMMEDIATE, + :count => 1, + :forever => false, + :connection_options => "" +} + +opts = OptionParser.new do |opts| + opts.banner = "Usage: drain.rb [OPTIONS] ADDRESS" + + opts.separator "" + opts.separator "Drains messages from the specified address" + opts.separator "" + + opts.on("-h", "--help", + "show this message") do + puts opts + exit + end + + opts.on("-b", "--broker VALUE", + "url of broker to connect to") do |broker| + options[:broker] = broker + end + + opts.on("-t", "--timeout VALUE", Integer, + "timeout in seconds to wait before exiting") do |timeout| + options[:timeout] = Qpid::Messaging::Duration.new timeout * 1000 + end + + opts.on("-f", "--forever", + "ignore timeout and wait forever") do + options[:forever] = true + end + + opts.on("--connection-options VALUE", + "connection options string in the form {name1:value,name2:value2}") do |conopts| + options[:connection_options] = conopts + end + + opts.on("-c", "--count VALUE", Integer, + "number of messages to read before exiting") do |count| + options[:count] = count + end +end + +opts.parse!(ARGV) + +options[:address] = ARGV[0] || "" + +connection = Qpid::Messaging::Connection.new options[:broker], options[:connection_options] +connection.open + +def render_map map + print "{" + map.keys.sort.each_with_index {|key,index| print "#{index > 0 ? ', ' : ''}#{key}:#{map[key]}"} + print "}" +end + +begin + session = connection.create_session + receiver = session.create_receiver options[:address] + done = false + count = 0 + options[:timeout] = Qpid::Messaging::Duration::FOREVER if options[:forever] + + while !done && (count < options[:count]) + message = receiver.fetch(options[:timeout]) + print "Message(properties=" + render_map message.properties + print ", content=" + if message.content_type == "amqp/map" + print "'#{render_map message.content}')" + else + print "'#{message.content}'" + end + print ")\n" + session.acknowledge message + count += 1 + end +rescue Exception => error + puts "Exception: #{error.to_s}" +end + +connection.close + diff --git a/cpp/bindings/qpid/ruby/examples/hello_world.rb b/cpp/bindings/qpid/ruby/examples/hello_world.rb new file mode 100644 index 0000000000..703febeba1 --- /dev/null +++ b/cpp/bindings/qpid/ruby/examples/hello_world.rb @@ -0,0 +1,49 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# + +$:.unshift File.join(File.dirname(__FILE__), "..", "lib") + +require 'qpid' + +# This is your classic Hello World application, written in +# Ruby, that uses Qpid. It demonstrates how to send and +# also receive messages. +# +if __FILE__ == $0 + broker = ARGV[0] || "localhost:5672" + address = ARGV[1] || "amq.topic" + options = ARGV[2] || "" + + connection = Qpid::Messaging::Connection.new broker + connection.open + session = connection.create_session + receiver = session.create_receiver address + sender = session.create_sender address + + # Send a simple message + sender.send Qpid::Messaging::Message.new :content => "Hello world!" + + # Now receive the message + message = receiver.fetch Qpid::Messaging::Duration::SECOND + puts "#{message.content}" + session.acknowledge + + connection.close +end + diff --git a/cpp/bindings/qpid/ruby/examples/map_receiver.rb b/cpp/bindings/qpid/ruby/examples/map_receiver.rb new file mode 100644 index 0000000000..805943a0a4 --- /dev/null +++ b/cpp/bindings/qpid/ruby/examples/map_receiver.rb @@ -0,0 +1,63 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# + +$:.unshift File.join(File.dirname(__FILE__), "..", "lib") + +require 'qpid' + +broker = ARGV[0] || "amqp:tcp:127.0.0.1:5672" +address = ARGV[1] || "message_queue; {create: always}" +options = ARGV[2] || "" + +connection = Qpid::Messaging::Connection.new broker, options +connection.open + +def display_value value + case value + when Array + result = "" + value.each_with_index {|element, index| result += "#{', ' if index > 0}#{element}"} + return "[#{result}]" + end + + value.to_s +end + +begin + session = connection.create_session + receiver = session.create_receiver address + + message = receiver.fetch + content = message.content + + print "content-type:#{message.content_type}" + print "{" + content.keys.sort.each_with_index do |key, index| + print "#{', ' if index > 0}#{key}:#{display_value content[key]}" + end + print "}\n" + + session.acknowledge + +rescue Exception => error + puts "Exception: #{error.message}" +end + +connection.close + diff --git a/cpp/bindings/qpid/ruby/examples/map_sender.rb b/cpp/bindings/qpid/ruby/examples/map_sender.rb new file mode 100644 index 0000000000..fa0c6e4562 --- /dev/null +++ b/cpp/bindings/qpid/ruby/examples/map_sender.rb @@ -0,0 +1,52 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# + +$:.unshift File.join(File.dirname(__FILE__), "..", "lib") + +require 'qpid' + +broker = ARGV[0] || "amqp:tcp:127.0.0.1:5672" +address = ARGV[1] || "message_queue; {create: always}" +options = ARGV[2] || [] + +connection = Qpid::Messaging::Connection.new broker, options +connection.open + +begin + session = connection.create_session + sender = session.create_sender address + message = Qpid::Messaging::Message.new + + content = { + :id => 987654321, + :name => "Widget", + :percent => 0.99, + :colors => ["red", "green", "blue"] + } + + message.content = content + + sender.send message + +rescue Exception => error + puts "Exception: #{error.message}" +end + +connection.close + diff --git a/cpp/bindings/qpid/ruby/examples/server.rb b/cpp/bindings/qpid/ruby/examples/server.rb new file mode 100644 index 0000000000..ead9d58472 --- /dev/null +++ b/cpp/bindings/qpid/ruby/examples/server.rb @@ -0,0 +1,51 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# + +$:.unshift File.join(File.dirname(__FILE__), "..", "lib") + +require 'qpid' + +if __FILE__ == $0 + broker = ARGV[0] || "amqp:tcp:localhost:5672" + options = ARGV[1] || "" + + connection = Qpid::Messaging::Connection.new broker, options + connection.open + session = connection.create_session + receiver = session.create_receiver "service_queue; {create:always}" + + loop do + request = receiver.fetch + address = request.reply_to + + if !address.nil? + sender = session.create_sender address + response = Qpid::Messaging::Message.new :content => request.content.upcase + sender.send response + puts "Processed request: #{request.content} -> #{response.content}" + session.acknowledge + else + puts "Error: no reply address specified for request: #{request.content}" + session.reject request + end + end + + connection.close +end + diff --git a/cpp/bindings/qpid/ruby/examples/spout.rb b/cpp/bindings/qpid/ruby/examples/spout.rb new file mode 100644 index 0000000000..c012e31f9d --- /dev/null +++ b/cpp/bindings/qpid/ruby/examples/spout.rb @@ -0,0 +1,126 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# + +$:.unshift File.join(File.dirname(__FILE__), "..", "lib") + +require 'qpid' +require 'optparse' + +options = { + :broker => "127.0.0.1", + :address => "", + :timeout => 0, + :count => 1, + :properties => {}, + :content => nil, + :mapped => {} +} + +opts = OptionParser.new do |opts| + opts.banner = "Usage: spout.rb [OPTIONS] ADDRESS" + + opts.on("-h", "--help", + "show this message") do |help| + puts opts + exit + end + + opts.on("-b","--broker VALUE", + "url of broker to connect to ") do |broker| + options[:broker] = broker + end + + opts.on("-t", "--timeout VALUE", Integer, + "exit after the specified time") do |timeout| + options[:timeout] = Qpid::Messaging::Duration.new timeout * 1000 + end + + opts.on("-c", "--count VALUE", Integer, + "stop after count messages have been sent, zero disables") do |count| + options[:count] = count + end + + opts.on("-i", "--id VALUE", + "use the supplied id instead of generating one") do |id| + options[:id] = id + end + + opts.on("--reply-to VALUE", + "specify reply-to address") do |replyto| + options[:replyto] = replyto + end + + opts.on("-P", "--property VALUE", + "specify message property") do |property| + name = property.split(/=/)[0] + value = property.split(/=/)[1] + options[:properties][name] = value + end + + opts.on("-M", "--map VALUE", + "specify entry for map content") do |mapped| + name = mapped.split(/=/)[0] + value = mapped.split(/=/)[1] + options[:mapped][name] = value + end + + opts.on("--content VALUE", + "specify textual content") do |content| + options[:content] = content + end + + opts.on(nil, "--connection-options VALUE", + "connection options string in the form {name1:value1, name2:value2}") do |conopts| + options[:connection_options] = conopts + end +end + +begin + opts.parse!(ARGV) +rescue => error + opts.parse(["-h"]) +end + +# now get the non-arg options +options[:address] = ARGV[0] unless ARGV[0].nil? + +connection = Qpid::Messaging::Connection.new options[:broker], options[:connection_options] +connection.open +session = connection.create_session +sender = session.create_sender options[:address] +message = Qpid::Messaging::Message.new + +options[:properties].each_key {|key| message.properties[key] = options[:properties][key]} + +(1..options[:count]).each do |count| + if !options[:mapped].keys.empty? + message.content = options[:mapped] + elsif options[:content] + message.content = options[:content] + end + message.content = options[:content] unless options[:content].nil? + message.properties["spout-id"] = "#{count}" + message.reply_to = options[:replyto] unless options[:replyto].nil? || options[:replyto].empty? + sender.send message +end + +# session.sync + +connection.close + |