eventbus 0.0.21 → 0.0.22
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/eventbus/connectors/amqp.rb +70 -0
- data/lib/eventbus/connectors/bunny.rb +71 -0
- data/lib/eventbus/connectors/stomp.rb +77 -0
- data/lib/eventbus/service.rb +1 -1
- metadata +13 -10
@@ -0,0 +1,70 @@
|
|
1
|
+
|
2
|
+
require 'amqp'
|
3
|
+
|
4
|
+
module AMQPConnectionDriver
|
5
|
+
|
6
|
+
|
7
|
+
def connection_driver_initialize
|
8
|
+
puts "AMQPConnectionDriver is loaded!"
|
9
|
+
end
|
10
|
+
|
11
|
+
def get_connection
|
12
|
+
new_opts = {
|
13
|
+
:vhost => ENV["BROKER_VHOST"] || "eventbus",
|
14
|
+
:host => ENV["BROKER_HOST"] || "localhost",
|
15
|
+
:port => ENV["BROKER_PORT"] || 5672,
|
16
|
+
:user => ENV["BROKER_USER"] || "eventbus",
|
17
|
+
:pass => ENV["BROKER_PASS"] || "eventbus",
|
18
|
+
# :logging => opts.delete(:logging) || false,
|
19
|
+
#:logfile => opts.delete(:logfile) || STDOUT
|
20
|
+
}
|
21
|
+
|
22
|
+
return AMQP.connect(new_opts)
|
23
|
+
|
24
|
+
end
|
25
|
+
|
26
|
+
def send_raw(content, opts = {})
|
27
|
+
queue_name = opts.delete(:queue_name)
|
28
|
+
EventMachine.run do
|
29
|
+
client = get_connection
|
30
|
+
channel = AMQP::Channel.new(client)
|
31
|
+
|
32
|
+
exchange = channel.direct('eventbus', :durable => true,
|
33
|
+
:persistent => true, :immediate => false,
|
34
|
+
:auto_delete => false)
|
35
|
+
|
36
|
+
puts "Declaring queue: #{queue_name}"
|
37
|
+
q = channel.queue(queue_name, :durable => true, :persistent => true, :immediate => false, :auto_delete => false)
|
38
|
+
q.bind(exchange, :routing_key => queue_name)
|
39
|
+
|
40
|
+
puts "Sending message..."
|
41
|
+
exchange.publish(content, :routing_key => queue_name) do
|
42
|
+
puts "Published to #{queue_name}: #{content}"
|
43
|
+
client.close { EventMachine.stop }
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def watch_queue(listen_queue)
|
49
|
+
EventMachine.run do
|
50
|
+
client = get_connection
|
51
|
+
channel = AMQP::Channel.new(client)
|
52
|
+
|
53
|
+
@logger.info "Listening on #{listen_queue}"
|
54
|
+
|
55
|
+
exchange = channel.direct('eventbus', :durable => true,
|
56
|
+
:persistent => true, :immediate => false,
|
57
|
+
:auto_delete => false)
|
58
|
+
|
59
|
+
q = channel.queue(listen_queue, :durable => true, :persistent => true, :immediate => false, :auto_delete => false)
|
60
|
+
|
61
|
+
|
62
|
+
q.bind(exchange, :routing_key => listen_queue)
|
63
|
+
|
64
|
+
q.subscribe do |headers, payload|
|
65
|
+
yield payload
|
66
|
+
end
|
67
|
+
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
@@ -0,0 +1,71 @@
|
|
1
|
+
# BunnyConnectionDriver is the original implementation of
|
2
|
+
# the AMQP-based broker. I am trying to make this work with
|
3
|
+
# straight Ruby AMQP driver, but this one is the default
|
4
|
+
# until I get that working properly.
|
5
|
+
|
6
|
+
module BunnyConnectionDriver
|
7
|
+
|
8
|
+
@@BunnyConnector_Connection = nil
|
9
|
+
|
10
|
+
def connection_driver_initialize
|
11
|
+
puts "BunnyConnectionDriver is loaded!"
|
12
|
+
end
|
13
|
+
|
14
|
+
def get_connection
|
15
|
+
if @@BunnyConnector_Connection.nil?
|
16
|
+
new_opts = {
|
17
|
+
:vhost => ENV["BROKER_VHOST"] || "eventbus",
|
18
|
+
:host => ENV["BROKER_HOST"] || "localhost",
|
19
|
+
:port => ENV["BROKER_PORT"] || 5672,
|
20
|
+
:user => ENV["BROKER_USER"] || "eventbus",
|
21
|
+
:pass => ENV["BROKER_PASS"] || "eventbus",
|
22
|
+
# :logging => opts.delete(:logging) || false,
|
23
|
+
#:logfile => opts.delete(:logfile) || STDOUT
|
24
|
+
}
|
25
|
+
|
26
|
+
@@BunnyConnector_Connection = Bunny.new(new_opts)
|
27
|
+
@@BunnyConnector_Connection.start
|
28
|
+
end
|
29
|
+
|
30
|
+
return @@BunnyConnector_Connection
|
31
|
+
|
32
|
+
end
|
33
|
+
|
34
|
+
def send_raw(content, opts = {})
|
35
|
+
queue_name = opts.delete(:queue_name)
|
36
|
+
client = get_connection
|
37
|
+
|
38
|
+
exchange = client.exchange('eventbus', :durable => true, :persistent => true, :immediate => false)
|
39
|
+
|
40
|
+
puts "Declaring queue: #{queue_name}"
|
41
|
+
q = client.queue(queue_name, :durable => true, :persistent => true, :immediate => false)
|
42
|
+
q.bind('eventbus', :key => queue_name)
|
43
|
+
|
44
|
+
puts "Publishing content to #{queue_name}: #{content}"
|
45
|
+
exchange.publish(content, :key => queue_name)
|
46
|
+
puts "Done!"
|
47
|
+
|
48
|
+
end
|
49
|
+
|
50
|
+
def watch_queue(listen_queue)
|
51
|
+
|
52
|
+
b = get_connection
|
53
|
+
|
54
|
+
@logger.info "Listening on #{listen_queue}"
|
55
|
+
|
56
|
+
exchange = b.exchange('eventbus', :durable => true, :persistent => true, :immediate => false)
|
57
|
+
|
58
|
+
q = b.queue(listen_queue, :durable => true, :persistent => true, :immediate => false)
|
59
|
+
q.bind(exchange, :key => listen_queue)
|
60
|
+
|
61
|
+
q.pop do |msg|
|
62
|
+
if msg[:delivery_details].nil?
|
63
|
+
# Gotta figure out why this is necessary. Blech.
|
64
|
+
sleep 0.25
|
65
|
+
else
|
66
|
+
yield msg[:payload]
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
end
|
71
|
+
end
|
@@ -0,0 +1,77 @@
|
|
1
|
+
# This module is primarily intended for use with ActiveMQ, though
|
2
|
+
# it should work with any Stomp-speaking broker.
|
3
|
+
require 'stomp'
|
4
|
+
module StompConnectionDriver
|
5
|
+
|
6
|
+
@@StompConnector_Connection = nil
|
7
|
+
|
8
|
+
def connection_driver_initialize
|
9
|
+
puts "StompConnectionDriver is loaded!"
|
10
|
+
end
|
11
|
+
|
12
|
+
def get_connection
|
13
|
+
if @@StompConnector_Connection.nil?
|
14
|
+
opts = {
|
15
|
+
:host => ENV["BROKER_HOST"] || "localhost",
|
16
|
+
:port => ENV["BROKER_PORT"] || 61613,
|
17
|
+
:user => ENV["BROKER_USER"],
|
18
|
+
:pass => ENV["BROKER_PASS"],
|
19
|
+
# :logging => opts.delete(:logging) || false,
|
20
|
+
#:logfile => opts.delete(:logfile) || STDOUT
|
21
|
+
}
|
22
|
+
|
23
|
+
stomp_url = ENV['STOMP_URL'] ||
|
24
|
+
"stomp://#{opts[:user]}:#{opts[:pass]}@#{opts[:host]}:#{opts[:port]}?initialReconnectDelay=5000&randomize=false&useExponentialBackOff=false"
|
25
|
+
@@StompConnector_Connection = Stomp::Client.new(stomp_url)
|
26
|
+
end
|
27
|
+
|
28
|
+
return @@StompConnector_Connection
|
29
|
+
|
30
|
+
end
|
31
|
+
|
32
|
+
# opts basically just gets passed to the Stomp client.send, so it can be things
|
33
|
+
# like :persistent => false, etc. :persistent is true by default.
|
34
|
+
def send_raw(content, opts = {})
|
35
|
+
|
36
|
+
queue_name = opts.delete(:queue_name)
|
37
|
+
queue_name = "/queue/#{queue_name}"
|
38
|
+
|
39
|
+
opts[:persistent] = true if opts[:persistent].nil?
|
40
|
+
|
41
|
+
# "Fix" an ActiveMQ "bug" where it assumes that any message with a content-length
|
42
|
+
# header is a bytes-message instead of a text message. This behavior prevents the
|
43
|
+
# message conent from showing up in the "Message Details" section when you try
|
44
|
+
# to view the message in ActiveMQ's admin interface.
|
45
|
+
#
|
46
|
+
# Note that the *correct* behavior is the opposite of this: All Stomp messages
|
47
|
+
# are recommended to include content-length. However, this is the *less annoying*
|
48
|
+
# and *more useful* behavior.
|
49
|
+
opts[:suppress_content_length] = true if opts[:suppress_content_length].nil?
|
50
|
+
|
51
|
+
puts "Stomp sending message to: #{queue_name}"
|
52
|
+
|
53
|
+
client = get_connection
|
54
|
+
client.publish(queue_name, content, opts)
|
55
|
+
|
56
|
+
end
|
57
|
+
|
58
|
+
def watch_queue(listen_queue)
|
59
|
+
client = get_connection
|
60
|
+
|
61
|
+
queue_name = listen_queue.match('^/') ? listen_queue : "/queue/#{listen_queue}"
|
62
|
+
|
63
|
+
# OK, so some assumption that we're using ActiveMQ here. We'll work that out eventually. The point
|
64
|
+
# is, though, that we want to allow for maximum parallelism -- not just by thread but also across
|
65
|
+
# processes and machines. Therefore, don't lock the queue exclusively (i.e. don't kill other clients!)
|
66
|
+
# and only take blocks of one message rather than claiming large blocks. These assumptions should become
|
67
|
+
# configurable at some point, but for now it's not really a concern.
|
68
|
+
puts "Subscribing to #{queue_name}..."
|
69
|
+
|
70
|
+
client.subscribe(queue_name, {"activemq.prefetchSize" => 1, "activemq.exclusive" => false}) do |msg|
|
71
|
+
yield msg.body
|
72
|
+
end
|
73
|
+
|
74
|
+
|
75
|
+
end
|
76
|
+
|
77
|
+
end
|
data/lib/eventbus/service.rb
CHANGED
@@ -33,7 +33,7 @@ class Service
|
|
33
33
|
|
34
34
|
driver_module = "#{@connection_driver}ConnectionDriver"
|
35
35
|
|
36
|
-
|
36
|
+
require "eventbus/connectors/#{@connection_driver.downcase}"
|
37
37
|
|
38
38
|
# Pretty much just swiped from ActiveSupport "constantize"..
|
39
39
|
conn_module = Object.const_defined?(driver_module) ? Object.const_get(driver_module) : Object.const_missing(driver_module)
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: eventbus
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.22
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-05-
|
12
|
+
date: 2012-05-30 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bunny
|
16
|
-
requirement: &
|
16
|
+
requirement: &13397500 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *13397500
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: amqp
|
27
|
-
requirement: &
|
27
|
+
requirement: &13396960 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :runtime
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *13396960
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: stomp
|
38
|
-
requirement: &
|
38
|
+
requirement: &13396440 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ! '>='
|
@@ -43,10 +43,10 @@ dependencies:
|
|
43
43
|
version: '0'
|
44
44
|
type: :runtime
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *13396440
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: uuid
|
49
|
-
requirement: &
|
49
|
+
requirement: &13396000 !ruby/object:Gem::Requirement
|
50
50
|
none: false
|
51
51
|
requirements:
|
52
52
|
- - ! '>='
|
@@ -54,7 +54,7 @@ dependencies:
|
|
54
54
|
version: '0'
|
55
55
|
type: :runtime
|
56
56
|
prerelease: false
|
57
|
-
version_requirements: *
|
57
|
+
version_requirements: *13396000
|
58
58
|
description: Distributed application framework for developing event-driven automations.
|
59
59
|
email: dev.incognito@spadea.net
|
60
60
|
executables: []
|
@@ -64,6 +64,9 @@ files:
|
|
64
64
|
- lib/eventbus/message.rb
|
65
65
|
- lib/eventbus/service.rb
|
66
66
|
- lib/eventbus/queue.rb
|
67
|
+
- lib/eventbus/connectors/stomp.rb
|
68
|
+
- lib/eventbus/connectors/bunny.rb
|
69
|
+
- lib/eventbus/connectors/amqp.rb
|
67
70
|
homepage: http://spadea.net/?page_id=8
|
68
71
|
licenses: []
|
69
72
|
post_install_message:
|