Class: Ons::Producer

Inherits:
Object
  • Object
show all
Defined in:
lib/ons/producer.rb

Overview

the ONS Producer

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(access_key, secret_key, producer_id, options = {}) ⇒ Producer

Create a new aliyun ONS Producer instance.

Parameters:

  • access_key (String)

    the access key to aliyun ONS

  • secret_key (String)

    the secret key to aliyun ONS

  • producer_id (String)

    the producer ID

  • options (Hash{String, Symbol => String}) (defaults to: {})

Options Hash (options):

  • :namesrv_addr (String)

    the nameserver used to fetching ons_addr

  • :ons_addr (String)

    the ONS server address

  • :send_timeout (String, Fixnum, Bignum) — default: 3000

    send message timeout



20
21
22
23
24
25
# File 'lib/ons/producer.rb', line 20

def initialize(access_key, secret_key, producer_id, options = {})
  @producer = Internal::Producer.new(access_key, secret_key, producer_id, options)

  # register instance
  self.class.instances << self
end

Class Method Details

.instances<Producer>

Get all the Producer instances.

Returns:

  • (<Producer>)

    all the Producer instances



7
8
9
# File 'lib/ons/producer.rb', line 7

def self.instances
  @instances ||= []
end

Instance Method Details

#send_message(topic, tag, body, key = '') ⇒ String

Send a message.

Examples:

send ‘Hello, World!’ message with tag :tagA which is under topic :TopicTestMQ

producer.send_message('TopicTestMQ', 'tagA', 'Hello, World!')

Parameters:

  • topic (String)

    the message topic

  • tag (String)

    the message tag

  • body (String)

    the message body

  • key (String) (defaults to: '')

    the message key

Returns:

  • (String)

    the message id

See Also:



39
40
41
# File 'lib/ons/producer.rb', line 39

def send_message(topic, tag, body, key = '')
  @producer.send_message(topic, tag, body, key)
end

#send_timer_message(topic, tag, body, timer, key = '') ⇒ String

Send a timer message.

Examples:

send ‘Hello, World!’ message at 30 seconds later

producer.send_timer_message('TopicTestMQ', 'tagA', 'Hello, World!', Time.now + 30)

Parameters:

  • topic (String)

    the message topic

  • tag (String)

    the message tag

  • body (String)

    the message body

  • timer (#to_i)

    when deliver the message

  • key (String) (defaults to: '')

    the message key

Returns:

  • (String)

    the message id

See Also:



56
57
58
# File 'lib/ons/producer.rb', line 56

def send_timer_message(topic, tag, body, timer, key = '')
  @producer.send_timer_message(topic, tag, body, timer.to_i * 1000, key)
end

#shutdownvoid

Note:

this method should be called before program exit, otherwise it would case a memory leak.

This method returns an undefined value.

Shutdown the Producer instance

Please see also Ons.register_cleanup_hooks if you want call it automatically.



76
77
78
# File 'lib/ons/producer.rb', line 76

def shutdown
  @producer.shutdown
end

#startself

Note:

this method should be called before send any message.

Start the Producer instance.

Returns:

  • (self)

    returns itself



65
66
67
68
# File 'lib/ons/producer.rb', line 65

def start
  @producer.start
  self
end