Class: Ap4r::Util::QueueClient

Inherits:
Object
  • Object
show all
Defined in:
lib/ap4r/util/queue_client.rb

Overview

This class is TOO EXPERIMENTAL

Client class for QueueManager. This class wraps DRb client and provides some helper methods. TODO: many drb calls are executed in a method call such as list_queues. 2006/09/22 by shino

ParseTree is perhaps needed. Now ruby-parser is also available?

Constant Summary collapse

CONFIG_DIR_DEFAULT =
'config'
CONFIG_FILE_DEFAULT =
'queues.cfg'
HOST_DEFAULT =
'localhost'
DEFAULT_QUEUE_PREFIX =
'queue.test.'
DEFAULT_QUEUE_SUFFIX =
'default'
DEFAULT_MULTI_QUEUE =
DEFAULT_QUEUE_PREFIX + '*'
@@config_dir =
CONFIG_DIR_DEFAULT

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_file = CONFIG_FILE_DEFAULT, options = {}, config_dir = @@config_dir) ⇒ QueueClient

Creates new client from a configuration file. Some options are supported.

  • :host

  • :port

  • :name



32
33
34
35
36
37
38
39
40
41
# File 'lib/ap4r/util/queue_client.rb', line 32

def initialize(config_file = CONFIG_FILE_DEFAULT,
               options = {},
               config_dir = @@config_dir)
  @config = ReliableMsg::Config.new(File.join(config_dir, config_file))
  @config.load_no_create
  @host = options[:host] || @config.drb['host'] || 'localhost'
  @port = options[:port] || @config.drb['port'] || 6438
  @name = (options[:name]).to_sym
  @qm = nil
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



25
26
27
# File 'lib/ap4r/util/queue_client.rb', line 25

def config
  @config
end

#nameObject (readonly)

Returns the value of attribute name.



25
26
27
# File 'lib/ap4r/util/queue_client.rb', line 25

def name
  @name
end

Instance Method Details

#drb_uriObject Also known as: uri



98
99
100
# File 'lib/ap4r/util/queue_client.rb', line 98

def drb_uri
  "druby://#{@host}:#{@port}"
end

#list_messages(suffix = DEFAULT_QUEUE_SUFFIX, prefix = DEFAULT_QUEUE_PREFIX) ⇒ Object Also known as: lsm



60
61
62
63
# File 'lib/ap4r/util/queue_client.rb', line 60

def list_messages(suffix = DEFAULT_QUEUE_SUFFIX,
                  prefix = DEFAULT_QUEUE_PREFIX)
  qm.store.queues[prefix.to_s + suffix.to_s]
end

#list_queuesObject Also known as: lsq



56
57
58
# File 'lib/ap4r/util/queue_client.rb', line 56

def list_queues
  qm.store.queues.keys
end

#make_multi_queue(multi_queue = DEFAULT_MULTI_QUEUE) ⇒ Object Also known as: mkmq



88
89
90
# File 'lib/ap4r/util/queue_client.rb', line 88

def make_multi_queue multi_queue = DEFAULT_MULTI_QUEUE
  ReliableMsg::MultiQueue.new(multi_queue.to_s, :drb_uri => drb_uri)
end

#make_queue(suffix = DEFAULT_QUEUE_SUFFIX, prefix = DEFAULT_QUEUE_PREFIX) ⇒ Object Also known as: mkq



65
66
67
68
# File 'lib/ap4r/util/queue_client.rb', line 65

def make_queue(suffix = DEFAULT_QUEUE_SUFFIX,
               prefix = DEFAULT_QUEUE_PREFIX)
  ReliableMsg::Queue.new(prefix.to_s + suffix.to_s, :drb_uri => drb_uri)
end

#multi_queue_get(selector = nil, multi_queue = DEFAULT_MULTI_QUEUE, &block) ⇒ Object Also known as: mqg



92
93
94
95
96
# File 'lib/ap4r/util/queue_client.rb', line 92

def multi_queue_get(selector = nil,
                    multi_queue = DEFAULT_MULTI_QUEUE, &block)
  mq = make_multi_queue multi_queue, :drb_uri => irm_drb_uri
  mq.get selector, &block
end

#queue_get(suffix = DEFAULT_QUEUE_SUFFIX, selector = nil, prefix = DEFAULT_QUEUE_PREFIX, &block) ⇒ Object Also known as: qg



70
71
72
73
74
# File 'lib/ap4r/util/queue_client.rb', line 70

def queue_get(suffix = DEFAULT_QUEUE_SUFFIX, selector = nil,
              prefix = DEFAULT_QUEUE_PREFIX, &block)
  q = make_queue suffix, prefix
  q.get selector, &block
end

#queue_managerObject Also known as: qm



43
44
45
# File 'lib/ap4r/util/queue_client.rb', line 43

def queue_manager
  @qm ||= DRb::DRbObject.new_with_uri(drb_uri)
end

#queue_manager_stopObject Also known as: stop



47
48
49
50
51
52
53
54
# File 'lib/ap4r/util/queue_client.rb', line 47

def queue_manager_stop
  manager = queue_manager
  begin
    manager.stop
  rescue DRb::DRbConnError => error
    error.message
  end
end

#queue_put(suffix = DEFAULT_QUEUE_SUFFIX, message = nil, prefix = DEFAULT_QUEUE_PREFIX, headers = nil) ⇒ Object Also known as: qp



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/ap4r/util/queue_client.rb', line 76

def queue_put(suffix = DEFAULT_QUEUE_SUFFIX,
              message = nil, prefix = DEFAULT_QUEUE_PREFIX,
              headers = nil)
  unless message
    t = Time.now
    message = sprintf("test message %s,%s",
                      t.strftime("%Y/%m/%d %H:%M:%S"), t.usec)
  end
  q = make_queue suffix, prefix
  q.put message, headers
end

#to_sObject



102
103
104
# File 'lib/ap4r/util/queue_client.rb', line 102

def to_s
  @name.to_s
end