SPQR 0.3.0, now with event support

spqr
qpid
qmf
ruby
Published

May 11, 2010

I’m pleased to announce yesterday’s release of SPQR 0.3.0, which is available as source, on github, or as a RubyGem. (SPQR is a library to make it painless to publish Ruby objects over the Qpid Management Framework; more details are available elsewhere on this site.)

There are several new features and enhancements in this release, including the ability to access the current Qpid user and context from within managed methods. The most exciting new functionality, though, is support for QMF events. In order to use QMF events, you’ll need a fairly recent version of the Qpid and QMF libraries from their source repository (later than revision 929717 for nearly-complete support; revision 942861 fixes a minor bug that won’t affect most users). Once you have those installed, though, SPQR makes it characteristically easy to make QMF events; see the example below for details:

#!/usr/bin/env ruby
 
# This is a simple logging service that operates over QMF.  It is very similar
# to logservice.rb, except it makes QMF events for log events instead of
# database records.  LogEventService has the same API as LogService; LogEvent
# is a SPQR event class.  See the comments for details on how to use QMF events.
 
require 'spqr/spqr'
require 'spqr/app'
 
class LogEventService
  include SPQR::Manageable
 
  [, , , ].each do |name|
    define_method name do |msg|
      # Construct a new event by passing arguments to .new in the order that they were declared
      ev = LogEvent.new(Time.now.utc.to_i * 1000000000, "#{name.to_s.upcase}", msg.dup)
      
      # You can also set arguments of an event object individually, like this:
      # ev = LogEvent.new
      # ev.l_when = Time.now
      # ev.severity = name.to_s.upcase
      # ev.msg = msg.dup
      
      # Once all of the arguments are set, raise the event:
      ev.bang!
    end
    
    expose name do |args|
      args.declare , , 
    end
  end
 
  def self.find_all
    @singleton ||= LogEventService.new
    [@singleton]
  end
 
  def self.find_by_id(i)
    @singleton ||= LogEventService.new
  end
 
  qmf_package_name 
  qmf_class_name 
end
 
class LogEvent
  # To declare an event class, include SPQR::Raiseable
  include ::SPQR::Raiseable
  
  # Declare arguments with their name, type, and (optional) description
  arg , , "When the event happened"
  arg , , "Event severity:  DEBUG, WARN, INFO, or ERROR"
  arg , , "Log message"
 
  # Declare metadata as appropriate
  qmf_package_name 
  qmf_class_name 
end
 
app = SPQR::App.new( => )
 
# Event classes must be registered just like object classes
app.register LogEventService, LogEvent
 
app.main

As usual, I welcome your feedback.