gz_activemessaging 0.13.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (92) hide show
  1. checksums.yaml +15 -0
  2. data/.travis.yml +40 -0
  3. data/Appraisals +19 -0
  4. data/Gemfile +15 -0
  5. data/Gemfile.lock +87 -0
  6. data/README.md +27 -0
  7. data/Rakefile +49 -0
  8. data/VERSION +1 -0
  9. data/activemessaging.gemspec +137 -0
  10. data/gemfiles/activesupport23.gemfile +10 -0
  11. data/gemfiles/activesupport23.gemfile.lock +51 -0
  12. data/gemfiles/activesupport30.gemfile +11 -0
  13. data/gemfiles/activesupport30.gemfile.lock +53 -0
  14. data/gemfiles/activesupport31.gemfile +11 -0
  15. data/gemfiles/activesupport31.gemfile.lock +55 -0
  16. data/gemfiles/activesupport32.gemfile +10 -0
  17. data/gemfiles/activesupport32.gemfile.lock +55 -0
  18. data/generators/a13g_test_harness/a13g_test_harness_generator.rb +19 -0
  19. data/generators/a13g_test_harness/templates/active_messaging_test.rhtml +13 -0
  20. data/generators/a13g_test_harness/templates/active_messaging_test_controller.rb +29 -0
  21. data/generators/a13g_test_harness/templates/index.rhtml +17 -0
  22. data/generators/filter/USAGE +0 -0
  23. data/generators/filter/filter_generator.rb +19 -0
  24. data/generators/filter/templates/filter.rb +12 -0
  25. data/generators/filter/templates/filter_test.rb +28 -0
  26. data/generators/processor/USAGE +8 -0
  27. data/generators/processor/processor_generator.rb +31 -0
  28. data/generators/processor/templates/application_processor.rb +18 -0
  29. data/generators/processor/templates/broker.yml +140 -0
  30. data/generators/processor/templates/jruby_poller +117 -0
  31. data/generators/processor/templates/messaging.rb +12 -0
  32. data/generators/processor/templates/poller +25 -0
  33. data/generators/processor/templates/poller.rb +26 -0
  34. data/generators/processor/templates/processor.rb +8 -0
  35. data/generators/processor/templates/processor_test.rb +20 -0
  36. data/generators/tracer/USAGE +8 -0
  37. data/generators/tracer/templates/controller.rb +14 -0
  38. data/generators/tracer/templates/helper.rb +2 -0
  39. data/generators/tracer/templates/index.rhtml +4 -0
  40. data/generators/tracer/templates/layout.rhtml +16 -0
  41. data/generators/tracer/templates/trace_processor.rb +100 -0
  42. data/generators/tracer/tracer_generator.rb +25 -0
  43. data/init.rb +1 -0
  44. data/lib/activemessaging.rb +133 -0
  45. data/lib/activemessaging/adapter.rb +20 -0
  46. data/lib/activemessaging/adapters/amqp.rb +215 -0
  47. data/lib/activemessaging/adapters/asqs.rb +487 -0
  48. data/lib/activemessaging/adapters/base.rb +71 -0
  49. data/lib/activemessaging/adapters/beanstalk.rb +88 -0
  50. data/lib/activemessaging/adapters/jms.rb +243 -0
  51. data/lib/activemessaging/adapters/reliable_msg.rb +186 -0
  52. data/lib/activemessaging/adapters/stomp.rb +212 -0
  53. data/lib/activemessaging/adapters/synch.rb +95 -0
  54. data/lib/activemessaging/adapters/test.rb +137 -0
  55. data/lib/activemessaging/adapters/wmq.rb +193 -0
  56. data/lib/activemessaging/base_message.rb +28 -0
  57. data/lib/activemessaging/filter.rb +29 -0
  58. data/lib/activemessaging/gateway.rb +429 -0
  59. data/lib/activemessaging/message_sender.rb +30 -0
  60. data/lib/activemessaging/named_base.rb +54 -0
  61. data/lib/activemessaging/processor.rb +44 -0
  62. data/lib/activemessaging/railtie.rb +26 -0
  63. data/lib/activemessaging/test_helper.rb +189 -0
  64. data/lib/activemessaging/threaded_poller.rb +234 -0
  65. data/lib/activemessaging/trace_filter.rb +34 -0
  66. data/lib/generators/active_messaging/install/USAGE +21 -0
  67. data/lib/generators/active_messaging/install/install_generator.rb +39 -0
  68. data/lib/generators/active_messaging/install/templates/application_processor.rb +18 -0
  69. data/lib/generators/active_messaging/install/templates/broker.yml +139 -0
  70. data/lib/generators/active_messaging/install/templates/poller +24 -0
  71. data/lib/generators/active_messaging/install/templates/poller.rb +22 -0
  72. data/lib/generators/active_messaging/install/templates/threaded_poller +46 -0
  73. data/lib/generators/active_messaging/processor/USAGE +2 -0
  74. data/lib/generators/active_messaging/processor/processor_generator.rb +39 -0
  75. data/lib/generators/active_messaging/processor/templates/messaging.rb +12 -0
  76. data/lib/generators/active_messaging/processor/templates/processor.rb +8 -0
  77. data/lib/generators/active_messaging/processor/templates/processor_spec.rb +24 -0
  78. data/lib/generators/active_messaging/processor/templates/processor_test.rb +20 -0
  79. data/lib/tasks/start_consumers.rake +8 -0
  80. data/poller.rb +14 -0
  81. data/test/all_tests.rb +10 -0
  82. data/test/app/config/broker.yml +4 -0
  83. data/test/asqs_test.rb +125 -0
  84. data/test/config_test.rb +42 -0
  85. data/test/filter_test.rb +131 -0
  86. data/test/gateway_test.rb +220 -0
  87. data/test/jms_test.rb +64 -0
  88. data/test/reliable_msg_test.rb +83 -0
  89. data/test/stomp_test.rb +168 -0
  90. data/test/test_helper.rb +36 -0
  91. data/test/tracer_test.rb +57 -0
  92. metadata +202 -0
@@ -0,0 +1,117 @@
1
+ #!/bin/sh
2
+
3
+ JRUBY_CMD=`which jruby`
4
+ RAILS_ROOT="$(dirname $0)/.."
5
+ POLLER_RB="$RAILS_ROOT/vendor/plugins/activemessaging/poller.rb"
6
+ OUT="$RAILS_ROOT/tmp/poller.output"
7
+ PID_FILE="$RAILS_ROOT/tmp/poller0.pid"
8
+
9
+ if [ -z "$JRUBY_CMD" ] ; then
10
+ echo "Could not find jruby on your path."
11
+ exit 1
12
+ fi
13
+
14
+ if [ ! -f $POLLER_RB ] ; then
15
+ echo "Could not find the poller file at: $POLLER_RB"
16
+ exit 1
17
+ fi
18
+
19
+ function start() {
20
+ if [[ -s $PID_FILE && -n "$(ps -A | grep "^[ \t]*$(< $PID_FILE)")" ]] ; then
21
+ PID=$(< $PID_FILE)
22
+ echo "Poller already running with pid $PID."
23
+ exit 1
24
+ fi
25
+ $JRUBY_CMD $POLLER_RB "$@" >> $OUT 2>&1 &
26
+ PID=$!
27
+ echo $PID > $PID_FILE
28
+ echo "Poller started with pid=$PID"
29
+ }
30
+
31
+ function stop() {
32
+ if [[ -z "$(ps -A | grep "^[ \t]*$(< $PID_FILE)")" ]] ; then
33
+ echo "Poller is not currently running."
34
+ exit 1
35
+ fi
36
+ if [ -z "$FORCE" ] ; then
37
+ echo "Sending TERM signal to poller."
38
+ kill -TERM $(< $PID_FILE)
39
+ else
40
+ echo "Sending KILL signal to poller."
41
+ kill -KILL $(< $PID_FILE)
42
+ fi
43
+ rm $PID_FILE
44
+ }
45
+
46
+ function restart() {
47
+ stop
48
+ start
49
+ }
50
+
51
+ function run() {
52
+ exec $JRUBY_CMD $POLLER_RB "$@"
53
+ }
54
+
55
+ function zap() {
56
+ echo "Resetting to stopped state."
57
+ [ -f $PID_FILE ] && rm $PID_FILE
58
+ }
59
+
60
+ function usage() {
61
+ cat <<EOF
62
+ Usage: poller <command> <options> -- <application options>
63
+
64
+ * where <command> is one of:
65
+ start start an instance of the application
66
+ stop stop all instances of the application
67
+ restart stop all instances and restart them afterwards
68
+ run start the application and stay on top
69
+ zap set the application to a stopped state
70
+
71
+ * and where <options> may contain several of the following:
72
+
73
+ -t, --ontop Stay on top (does not daemonize)
74
+ -f, --force Force operation
75
+ EOF
76
+
77
+ }
78
+
79
+ CMD=$1
80
+ shift
81
+
82
+ for i in "1" "2" ; do
83
+ case "$1" in
84
+ "-f"|"--force")
85
+ FORCE="true"
86
+ shift
87
+ ;;
88
+ "-t"|"--ontop")
89
+ ONTOP="true"
90
+ shift
91
+ ;;
92
+ esac
93
+ done
94
+
95
+ [ "$1" == "--" ] && shift
96
+
97
+ case "$CMD" in
98
+ "start")
99
+ start
100
+ ;;
101
+ "stop")
102
+ stop
103
+ ;;
104
+ "run")
105
+ run
106
+ ;;
107
+ "restart")
108
+ restart
109
+ ;;
110
+ "zap")
111
+ zap
112
+ ;;
113
+ "usage"|*)
114
+ usage
115
+ exit 1
116
+ ;;
117
+ esac
@@ -0,0 +1,12 @@
1
+ #
2
+ # Add your destination definitions here
3
+ # can also be used to configure filters, and processor groups
4
+ #
5
+ ActiveMessaging::Gateway.define do |s|
6
+ #s.destination :orders, '/queue/Orders'
7
+ #s.filter :some_filter, :only=>:orders
8
+ #s.processor_group :group1, :order_processor
9
+
10
+ s.destination :<%= singular_name %>, '/queue/<%= class_name %>'
11
+
12
+ end
@@ -0,0 +1,25 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ ENV['APP_ROOT'] ||= File.expand_path(File.join(File.dirname(__FILE__), '..'))
4
+ APP_ROOT = ENV['APP_ROOT']
5
+ ENV['APP_NAME'] ||= "poller"
6
+ APP_NAME = ENV['APP_NAME']
7
+ script_file = File.join(APP_ROOT, 'lib', 'poller.rb')
8
+ tmp_dir = File.join(APP_ROOT, 'tmp')
9
+
10
+ require 'rubygems'
11
+ require 'daemons'
12
+
13
+ options = {
14
+ :app_name => APP_NAME,
15
+ :dir_mode => :normal,
16
+ :dir => tmp_dir,
17
+ :multiple => true,
18
+ :ontop => false,
19
+ :mode => :load,
20
+ :backtrace => true,
21
+ :monitor => true,
22
+ :log_output => true
23
+ }
24
+
25
+ Daemons.run(script_file,options)
@@ -0,0 +1,26 @@
1
+ #!/usr/bin/env ruby
2
+ # Make sure stdout and stderr write out without delay for using with daemon like scripts
3
+ STDOUT.sync = true; STDOUT.flush
4
+ STDERR.sync = true; STDERR.flush
5
+
6
+ # The daemons gem sets our cwd to /, which Rails doesn't work well with.
7
+ Dir.chdir APP_ROOT
8
+
9
+ #Try to Load Merb
10
+ merb_init_file = File.expand_path(File.dirname(__FILE__)+'/../config/merb_init')
11
+ if File.exists? merb_init_file
12
+ require File.expand_path(File.dirname(__FILE__)+'/../config/boot')
13
+ #need this because of the CWD
14
+ Merb.root = MERB_ROOT
15
+ require merb_init_file
16
+ else
17
+ # Load Rails
18
+ RAILS_ROOT=File.expand_path(File.join(File.dirname(__FILE__), '..'))
19
+ require File.join(RAILS_ROOT, 'config', 'boot')
20
+ require File.join(RAILS_ROOT, 'config', 'environment')
21
+ end
22
+
23
+ # Start it up!
24
+ require 'activemessaging'
25
+ ActiveMessaging.load_activemessaging
26
+ ActiveMessaging.start
@@ -0,0 +1,8 @@
1
+ class <%= class_name %>Processor < ApplicationProcessor
2
+
3
+ subscribes_to :<%= singular_name %>
4
+
5
+ def on_message(message)
6
+ logger.debug "<%= class_name %>Processor received: " + message
7
+ end
8
+ end
@@ -0,0 +1,20 @@
1
+ require File.dirname(__FILE__) + '/../test_helper'
2
+ require 'activemessaging/test_helper'
3
+ require File.dirname(__FILE__) + '/../../app/processors/application'
4
+
5
+ class <%= class_name %>ProcessorTest < Test::Unit::TestCase
6
+ include ActiveMessaging::TestHelper
7
+
8
+ def setup
9
+ load File.dirname(__FILE__) + "/../../app/processors/<%= file_name %>_processor.rb"
10
+ @processor = <%= class_name %>Processor.new
11
+ end
12
+
13
+ def teardown
14
+ @processor = nil
15
+ end
16
+
17
+ def test_<%= file_name %>_processor
18
+ @processor.on_message('Your test message here!')
19
+ end
20
+ end
@@ -0,0 +1,8 @@
1
+ Description:
2
+ Generates a stub ActiveMessaging Tracing Controller.
3
+
4
+ Examples:
5
+ ./script/generate tracer Tracer
6
+ will create:
7
+ /app/processors/tracer_processor.rb
8
+ /app/views/tracer/index.rhtml
@@ -0,0 +1,14 @@
1
+ class <%= class_name %>Controller < ApplicationController
2
+ include ActiveMessaging::MessageSender
3
+
4
+ publishes_to :trace
5
+
6
+ def index
7
+ end
8
+
9
+ def clear
10
+ publish :trace, "<trace-control>clear</trace-control>"
11
+ redirect_to :action=>'index'
12
+ end
13
+
14
+ end
@@ -0,0 +1,2 @@
1
+ module <%= class_name %>Helper
2
+ end
@@ -0,0 +1,4 @@
1
+ <%=button_to 'Clear', :action=>'clear'%>
2
+
3
+ <%=image_tag '../trace.png', :id => 'graph' %>
4
+
@@ -0,0 +1,16 @@
1
+ <html>
2
+ <head>
3
+ <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
4
+ <title>Tracer</title>
5
+ <%=javascript_include_tag :defaults %>
6
+ </head>
7
+ <body>
8
+ <h2>Tracer</h2>
9
+
10
+ <% if @flash[:note] -%>
11
+ <div id="flash"><%= @flash[:note] %></div>
12
+ <% end -%>
13
+
14
+ <%= @content_for_layout %>
15
+ </body>
16
+ </html>
@@ -0,0 +1,100 @@
1
+ require 'mpath'
2
+ require 'active_support'
3
+
4
+ class Dot
5
+
6
+ attr_accessor :name, :nodes, :edges, :clean_names
7
+
8
+ def initialize name
9
+ @name = name
10
+ @nodes = {}
11
+ @clean_names = {}
12
+ @edges = []
13
+ yield self
14
+ end
15
+
16
+ def node name, params = {}
17
+ @nodes[clean_name(name)] = params.stringify_keys.reverse_merge "label"=>name
18
+ end
19
+
20
+ def clean_name name
21
+ @clean_names[name] = "node#{@clean_names.length+1}" if @clean_names[name].nil?
22
+ @clean_names[name]
23
+ end
24
+
25
+ def edge from, to
26
+ edge = [clean_name(from), clean_name(to)]
27
+ @edges << edge unless @edges.member? edge
28
+ end
29
+
30
+ def to_s
31
+ dot = "digraph #{@name} {\n"
32
+ @nodes.each do |node_name, options|
33
+ dot += "\t#{node_name.to_s}"
34
+ optionstrings = []
35
+ options.keys.sort.each do |key|
36
+ optionstrings << "#{key}=\"#{options[key]}\""
37
+ end
38
+ dot += " [#{optionstrings.join(', ')}]" if optionstrings.length>0
39
+ dot += ";\n"
40
+ end
41
+ @edges.each {|e| dot += "\t#{e[0].to_s}->#{e[1].to_s};\n"}
42
+ dot += "}\n"
43
+ end
44
+
45
+ def == other
46
+ (other.name == name) && (other.nodes == nodes) && (other.edges == edges) && (other.clean_names == clean_names)
47
+ end
48
+ end
49
+
50
+ class TraceProcessor < ActiveMessaging::Processor
51
+ subscribes_to :trace
52
+
53
+ @@dot = Dot.new("Trace") {}
54
+
55
+ class << self
56
+
57
+ end
58
+
59
+ def dot
60
+ @@dot
61
+ end
62
+
63
+ def on_message(message)
64
+ xml = Mpath.parse(message)
65
+ if (xml.sent?) then
66
+ from = xml.sent.from.to_s
67
+ queue = xml.sent.queue.to_s
68
+
69
+ @@dot.node from
70
+ @@dot.node queue, "shape" => 'box'
71
+ @@dot.edge from, queue #hah - could do from => to
72
+ elsif (xml.received?) then
73
+ by = xml.received.by.to_s
74
+ queue = xml.received.queue.to_s
75
+
76
+ @@dot.node queue, "shape" => 'box'
77
+ @@dot.node by
78
+ @@dot.edge queue, by
79
+ elsif (xml.trace_control) then
80
+ command = xml.trace_control.to_s
81
+ begin
82
+ send command
83
+ rescue
84
+ puts "TraceProcessor: I don't understand the command #{command}"
85
+ end
86
+ end
87
+ create_image
88
+ end
89
+
90
+ def create_image
91
+ File.open(DOT_FILE, "w") {|f| f.puts @@dot.to_s }
92
+ output_file = APP_ROOT + "/public/trace.png"
93
+ `dot -Tpng -o #{output_file} #{DOT_FILE}`
94
+ end
95
+
96
+ def clear
97
+ @@dot = Dot.new("Trace") {}
98
+ end
99
+
100
+ end
@@ -0,0 +1,25 @@
1
+ class TracerGenerator < RubiGen::Base
2
+ def manifest
3
+ record do |m|
4
+ path = 'app/controllers'
5
+ m.directory path
6
+ m.template 'controller.rb', File.join(path, "#{file_name}_controller.rb")
7
+
8
+ path = 'app/processors'
9
+ m.directory path
10
+ m.template 'trace_processor.rb', File.join(path, "#{file_name}_processor.rb")
11
+
12
+ path = 'app/helpers'
13
+ m.directory path
14
+ m.template 'helper.rb', File.join(path, "#{file_name}_helper.rb")
15
+
16
+ path = 'app/views/layouts'
17
+ m.directory path
18
+ m.file 'layout.rhtml', File.join(path, "#{file_name}.rhtml")
19
+
20
+ path = "app/views/#{file_name}"
21
+ m.directory path
22
+ m.file 'index.rhtml', File.join(path, "index.rhtml")
23
+ end
24
+ end
25
+ end
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'activemessaging'
@@ -0,0 +1,133 @@
1
+ require 'logger'
2
+ require 'active_support/all'
3
+ require 'ostruct'
4
+
5
+ if defined?(Rails::Railtie)
6
+ require 'activemessaging/railtie.rb'
7
+ end
8
+
9
+ module ActiveMessaging
10
+
11
+ ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
12
+
13
+ # Used to indicate that the processing for a thread shoud complete
14
+ class StopProcessingException < Interrupt #:nodoc:
15
+ end
16
+
17
+ # Used to indicate that the processing on a message should cease,
18
+ # and the message should be returned back to the broker as best it can be
19
+ class AbortMessageException < Exception #:nodoc:
20
+ end
21
+
22
+ # Used to indicate that the processing on a message should cease,
23
+ # but no further action is required
24
+ class StopFilterException < Exception #:nodoc:
25
+ end
26
+
27
+ def self.logger
28
+ @@logger = nil unless defined? @@logger
29
+ @@logger ||= Rails.logger if defined? Rails
30
+ @@logger ||= Logger.new(STDOUT)
31
+ @@logger
32
+ end
33
+
34
+ def self.logger=(logger)
35
+ @@logger = logger
36
+ end
37
+
38
+ def self.app_root
39
+ @@app_root ||= (ENV['APP_ROOT'] || (defined?(::Rails) && ::Rails.root) || ENV['RAILS_ROOT'] || File.expand_path(Dir.pwd))
40
+ end
41
+
42
+ def self.app_env
43
+ @@app_env ||= (ENV['APP_ENV'] || (defined?(::Rails) && ::Rails.env) || ENV['RAILS_ENV'] || 'development')
44
+ end
45
+
46
+ def self.load_extensions
47
+ require 'logger'
48
+ require 'activemessaging/gateway'
49
+ require 'activemessaging/adapter'
50
+ require 'activemessaging/message_sender'
51
+ require 'activemessaging/processor'
52
+ require 'activemessaging/filter'
53
+ require 'activemessaging/trace_filter'
54
+
55
+ # load all under the adapters dir
56
+ Dir[File.join(ROOT, 'lib', 'activemessaging', 'adapters', '*.rb')].each do |a|
57
+ begin
58
+ adapter_name = File.basename(a, ".rb")
59
+ require 'activemessaging/adapters/' + adapter_name
60
+ rescue RuntimeError, LoadError => e
61
+ logger.warn "ActiveMessaging: adapter #{adapter_name} not loaded: #{ e.message }"
62
+ end
63
+ end
64
+ end
65
+
66
+ def self.load_config
67
+ # Skip loading config if there are no custom processors as the generator
68
+ # is likely running since application_processor.rb and another processor
69
+ # should exist when running rails.
70
+ if Dir["#{app_root}/app/processors/*.rb"].size() <= 1
71
+ return
72
+ end
73
+ path = File.expand_path("#{app_root}/config/messaging.rb")
74
+ begin
75
+ load path
76
+ rescue
77
+ raise $!, " ActiveMessaging: problems trying to load '#{path}': \n\t#{$!.message}"
78
+ end
79
+ end
80
+
81
+ def self.load_processors(first=true)
82
+ logger.info "ActiveMessaging: Loading #{app_root}/app/processors/application_processor.rb" if first
83
+ load "#{app_root}/app/processors/application_processor.rb" if File.exist?("#{app_root}/app/processors/application_processor.rb")
84
+ Dir["#{app_root}/app/processors/*.rb"].each do |f|
85
+ unless f.match(/\/application_processor.rb/)
86
+ logger.info "ActiveMessaging: Loading #{f}" if first
87
+ load f
88
+ end
89
+ end
90
+ end
91
+
92
+ def self.reload_activemessaging
93
+ # this is resetting the messaging.rb
94
+ ActiveMessaging::Gateway.filters = []
95
+ ActiveMessaging::Gateway.named_destinations = {}
96
+ ActiveMessaging::Gateway.processor_groups = {}
97
+
98
+ # now load the config
99
+ load_config
100
+ load_processors(false)
101
+ end
102
+
103
+ def self.load_activemessaging
104
+ load_extensions
105
+ load_config
106
+ load_processors
107
+ end
108
+
109
+ def self.start
110
+ if ActiveMessaging::Gateway.subscriptions.empty?
111
+ err_msg = <<-EOM
112
+
113
+ ActiveMessaging Error: No subscriptions.
114
+
115
+ If you have no processor classes in app/processors, add them using the command:
116
+ script/generate processor DoSomething"
117
+
118
+ If you have processor classes, make sure they include in the class a call to 'subscribes_to':
119
+ class DoSomethingProcessor < ActiveMessaging::Processor
120
+ subscribes_to :do_something
121
+ # ...
122
+ end
123
+
124
+ EOM
125
+ puts err_msg
126
+ logger.error err_msg
127
+ exit
128
+ end
129
+
130
+ Gateway.start
131
+ end
132
+
133
+ end