Class MCollective::Connector::Stomp
In: plugins/mcollective/connector/stomp.rb
Parent: Base

Handles sending and receiving messages over the Stomp protocol

This plugin supports version 1.1 or 1.1.6 and newer of the Stomp rubygem the versions between those had multi threading issues.

For all versions you can configure it as follows:

   connector = stomp
   plugin.stomp.host = stomp.your.net
   plugin.stomp.port = 6163
   plugin.stomp.user = you
   plugin.stomp.password = secret

All of these can be overriden per user using environment variables:

   STOMP_SERVER, STOMP_PORT, STOMP_USER, STOMP_PASSWORD

Version 1.1.6 onward support supplying multiple connections and it will do failover between these servers, you can configure it as follows:

    connector = stomp
    plugin.stomp.pool.size = 2

    plugin.stomp.pool.host1 = stomp1.your.net
    plugin.stomp.pool.port1 = 6163
    plugin.stomp.pool.user1 = you
    plugin.stomp.pool.password1 = secret
    plugin.stomp.pool.ssl1 = true

    plugin.stomp.pool.host2 = stomp2.your.net
    plugin.stomp.pool.port2 = 6163
    plugin.stomp.pool.user2 = you
    plugin.stomp.pool.password2 = secret
    plugin.stomp.pool.ssl2 = false

Using this method you can supply just STOMP_USER and STOMP_PASSWORD you have to supply the hostname for each pool member in the config. The port will default to 6163 if not specified.

In addition you can set the following options but only when using pooled configuration:

    plugin.stomp.pool.initial_reconnect_delay = 0.01
    plugin.stomp.pool.max_reconnect_delay = 30.0
    plugin.stomp.pool.use_exponential_back_off = true
    plugin.stomp.pool.back_off_multiplier = 2
    plugin.stomp.pool.max_reconnect_attempts = 0
    plugin.stomp.pool.randomize = false
    plugin.stomp.pool.timeout = -1

Methods

connect   disconnect   new   receive   send   subscribe   unsubscribe  

Attributes

connection  [R] 

Public Class methods

[Source]

    # File plugins/mcollective/connector/stomp.rb, line 57
57:             def initialize
58:                 @config = Config.instance
59:                 @subscriptions = []
60:             end

Public Instance methods

Connects to the Stomp middleware

[Source]

     # File plugins/mcollective/connector/stomp.rb, line 63
 63:             def connect
 64:                 if @connection
 65:                     Log.debug("Already connection, not re-initializing connection")
 66:                     return
 67:                 end
 68: 
 69:                 begin
 70:                     host = nil
 71:                     port = nil
 72:                     user = nil
 73:                     password = nil
 74:                     @base64 = get_bool_option("stomp.base64", false)
 75: 
 76:                     # Maintain backward compat for older stomps
 77:                     unless @config.pluginconf.include?("stomp.pool.size")
 78:                         host = get_env_or_option("STOMP_SERVER", "stomp.host")
 79:                         port = get_env_or_option("STOMP_PORT", "stomp.port", 6163).to_i
 80:                         user = get_env_or_option("STOMP_USER", "stomp.user")
 81:                         password = get_env_or_option("STOMP_PASSWORD", "stomp.password")
 82: 
 83:                         Log.debug("Connecting to #{host}:#{port}")
 84:                         @connection = ::Stomp::Connection.new(user, password, host, port, true)
 85:                     else
 86:                         pools = @config.pluginconf["stomp.pool.size"].to_i
 87:                         hosts = []
 88: 
 89:                         1.upto(pools) do |poolnum|
 90:                             host = {}
 91: 
 92:                             host[:host] = get_option("stomp.pool.host#{poolnum}")
 93:                             host[:port] = get_option("stomp.pool.port#{poolnum}", 6163).to_i
 94:                             host[:login] = get_env_or_option("STOMP_USER", "stomp.pool.user#{poolnum}")
 95:                             host[:passcode] = get_env_or_option("STOMP_PASSWORD", "stomp.pool.password#{poolnum}")
 96:                             host[:ssl] = get_bool_option("stomp.pool.ssl#{poolnum}", false)
 97: 
 98:                             Log.debug("Adding #{host[:host]}:#{host[:port]} to the connection pool")
 99:                             hosts << host
100:                         end
101: 
102:                         raise "No hosts found for the STOMP connection pool" if hosts.size == 0
103: 
104:                         connection = {:hosts => hosts}
105: 
106:                         # Various STOMP gem options, defaults here matches defaults for 1.1.6 the meaning of
107:                         # these can be guessed, the documentation isn't clear
108:                         connection[:initial_reconnect_delay] = get_option("stomp.pool.initial_reconnect_delay", 0.01).to_f
109:                         connection[:max_reconnect_delay] = get_option("stomp.pool.max_reconnect_delay", 30.0).to_f
110:                         connection[:use_exponential_back_off] = get_bool_option("stomp.pool.use_exponential_back_off", true)
111:                         connection[:back_off_multiplier] = get_bool_option("stomp.pool.back_off_multiplier", 2).to_i
112:                         connection[:max_reconnect_attempts] = get_option("stomp.pool.max_reconnect_attempts", 0)
113:                         connection[:randomize] = get_bool_option("stomp.pool.randomize", false)
114:                         connection[:backup] = get_bool_option("stomp.pool.backup", false)
115:                         connection[:timeout] = get_option("stomp.pool.timeout", -1).to_i
116: 
117:                         @connection = ::Stomp::Connection.new(connection)
118:                     end
119:                 rescue Exception => e
120:                     raise("Could not connect to Stomp Server: #{e}")
121:                 end
122:             end

Disconnects from the Stomp connection

[Source]

     # File plugins/mcollective/connector/stomp.rb, line 170
170:             def disconnect
171:                 Log.debug("Disconnecting from Stomp")
172:                 @connection.disconnect
173:             end

Receives a message from the Stomp connection

[Source]

     # File plugins/mcollective/connector/stomp.rb, line 125
125:             def receive
126:                 Log.debug("Waiting for a message from Stomp")
127:                 msg = @connection.receive
128: 
129:                 # STOMP puts the payload in the body variable, pass that
130:                 # into the payload of MCollective::Request and discard all the
131:                 # other headers etc that stomp provides
132:                 if @base64
133:                     Request.new(SSL.base64_decode(msg.body))
134:                 else
135:                     Request.new(msg.body)
136:                 end
137:             end

Sends a message to the Stomp connection

[Source]

     # File plugins/mcollective/connector/stomp.rb, line 140
140:             def send(target, msg)
141:                 Log.debug("Sending a message to Stomp target '#{target}'")
142: 
143:                 msg = SSL.base64_encode(msg) if @base64
144: 
145:                 # deal with deprecation warnings in newer stomp gems
146:                 if @connection.respond_to?("publish")
147:                     @connection.publish(target, msg)
148:                 else
149:                     @connection.send(target, msg)
150:                 end
151:             end

Subscribe to a topic or queue

[Source]

     # File plugins/mcollective/connector/stomp.rb, line 154
154:             def subscribe(source)
155:                 unless @subscriptions.include?(source)
156:                     Log.debug("Subscribing to #{source}")
157:                     @connection.subscribe(source)
158:                     @subscriptions << source
159:                 end
160:             end

Subscribe to a topic or queue

[Source]

     # File plugins/mcollective/connector/stomp.rb, line 163
163:             def unsubscribe(source)
164:                 Log.debug("Unsubscribing from #{source}")
165:                 @connection.unsubscribe(source)
166:                 @subscriptions.delete(source)
167:             end

[Validate]