You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@qpid.apache.org by tr...@apache.org on 2011/09/12 23:06:45 UTC

svn commit: r1169926 - in /qpid/trunk/qpid/cpp/bindings/qpid/ruby/examples: drain.rb map_receiver.rb map_sender.rb spout.rb

Author: tross
Date: Mon Sep 12 21:06:44 2011
New Revision: 1169926

URL: http://svn.apache.org/viewvc?rev=1169926&view=rev
Log:
QPID-3360 - Ruby implementation of Qpid example applications
Committed patch from Darryl Pierce.

Added:
    qpid/trunk/qpid/cpp/bindings/qpid/ruby/examples/drain.rb
    qpid/trunk/qpid/cpp/bindings/qpid/ruby/examples/map_receiver.rb
    qpid/trunk/qpid/cpp/bindings/qpid/ruby/examples/map_sender.rb
    qpid/trunk/qpid/cpp/bindings/qpid/ruby/examples/spout.rb

Added: qpid/trunk/qpid/cpp/bindings/qpid/ruby/examples/drain.rb
URL: http://svn.apache.org/viewvc/qpid/trunk/qpid/cpp/bindings/qpid/ruby/examples/drain.rb?rev=1169926&view=auto
==============================================================================
--- qpid/trunk/qpid/cpp/bindings/qpid/ruby/examples/drain.rb (added)
+++ qpid/trunk/qpid/cpp/bindings/qpid/ruby/examples/drain.rb Mon Sep 12 21:06:44 2011
@@ -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
+

Added: qpid/trunk/qpid/cpp/bindings/qpid/ruby/examples/map_receiver.rb
URL: http://svn.apache.org/viewvc/qpid/trunk/qpid/cpp/bindings/qpid/ruby/examples/map_receiver.rb?rev=1169926&view=auto
==============================================================================
--- qpid/trunk/qpid/cpp/bindings/qpid/ruby/examples/map_receiver.rb (added)
+++ qpid/trunk/qpid/cpp/bindings/qpid/ruby/examples/map_receiver.rb Mon Sep 12 21:06:44 2011
@@ -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
+

Added: qpid/trunk/qpid/cpp/bindings/qpid/ruby/examples/map_sender.rb
URL: http://svn.apache.org/viewvc/qpid/trunk/qpid/cpp/bindings/qpid/ruby/examples/map_sender.rb?rev=1169926&view=auto
==============================================================================
--- qpid/trunk/qpid/cpp/bindings/qpid/ruby/examples/map_sender.rb (added)
+++ qpid/trunk/qpid/cpp/bindings/qpid/ruby/examples/map_sender.rb Mon Sep 12 21:06:44 2011
@@ -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
+

Added: qpid/trunk/qpid/cpp/bindings/qpid/ruby/examples/spout.rb
URL: http://svn.apache.org/viewvc/qpid/trunk/qpid/cpp/bindings/qpid/ruby/examples/spout.rb?rev=1169926&view=auto
==============================================================================
--- qpid/trunk/qpid/cpp/bindings/qpid/ruby/examples/spout.rb (added)
+++ qpid/trunk/qpid/cpp/bindings/qpid/ruby/examples/spout.rb Mon Sep 12 21:06:44 2011
@@ -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
+



---------------------------------------------------------------------
Apache Qpid - AMQP Messaging Implementation
Project:      http://qpid.apache.org
Use/Interact: mailto:commits-subscribe@qpid.apache.org