Ten Minute IntroductionWe create a simple message consumer to receive messages, and a rails web controller that allows you to send a message when you click a button. This example uses the stomp adapter and an activemq message broker, but other brokers and adapters are now available. StepsStart ActiveMQ bin/activemq Create the rails application rails demo Add the activemessaging plugin script/plugin install http://activemessaging.googlecode.com/svn/trunk/plugins/activemessaging Create a processor script/generate processor HelloWorld This does the following if they don't exist already: - add the 'config/broker.yml' file to specify broker connections by environment
- add the 'config/messaging.rb' file to map Stomp destinations to symbolic names
- add the 'app/processor' folder where all prcessors that handle messages go
- add the 'app/processor/application.rb' ApplicationProcessor class, parent to all processors
For your particular HelloWorld processor: - add the 'app/processor/hello_world_processor.rb'
By default, the processor will be subscribed to a queue ':hello_world' class HelloWorldProcessor < ApplicationProcessor
subscribes_to :hello_world
def on_message(message)
logger.debug "received: " + message
end
endIf the generator created a new 'config/messaging.rb', it will have by default the queue symbol name to Stomp destination mapping: s.destination :hello_world, '/queue/HelloWorld' Create a controller with an index action. We will use this to send our messages script/generate controller SayHelloWorld index Add the following to the new controller to allow us to access activemessaging require 'activemessaging/processor'
include ActiveMessaging::MessageSender
publishes_to :hello_world Modify the controller to send a message def say_hello
publish :hello_world, "<say>Hello World!</say>"
end Add a button to send the message <%=button_to "Say Hello", :action=>"say_hello" %> Send a message. Browse to http://localhost:3000/say_hello_world and click the "Say Hello" button. This adds a say action to the queue, which automatically gets created on first request. Now, to begin consuming the queue, start the poller.rb script by running the poller daemon script: script/poller run The Processor logs the message to the console.
|
Minor intro correction: it looks like the queue() method on the following line has been renamed to destination():
'app/processor/hello_world_processor.rb' filename is wrong, it should be 'app/processors/hello_world_processor.rb'
This can be negated- indeed, it does not appear to exist. Argument to publish defines this.
didn't work for me. I had a "class not found javax.jms.MessageListerner?" error I had to copy activemq-all-5.1.0.jar into the jruby/lib directory to fix it
How persistent are those messages?
Rails 2.1.0.
Followed the steps above exactly. Set the broker.yml to point at remote activeMQ. Ports, etc. all work, have run remote tests to it all day long, no issues.
I hit the 'say_hello_world' action URL in my browser, page comes up fine.
Started script/poller run just fine, no exceptions.
when click the 'say hello' button on the page, I get an exception:
"Template is missing
Missing template say_hello_world/say_hello.html.erb in view path /Users/me/code/ybur/deepdebate.with.activemessaging.test/deepdebate/app/views"
...so I added the template in app/views/say_hello_world/say_hello.html.erb as a copy of the index... that was there. Restarted mongrel cluster, hit first instance with the right url, got the page, clicked the 'say hello' button and nada on the console.
I checked the activeMQ Q itself for a queue/ named "HelloWorld?' and its there -- every time I press the 'say hello' button in the page I see the msgs send and msgs received # increasing by one, so its definitely going to the Q and back.
BUt no console output from 'script/poller run'
This is ruby 1.8.6 and still rails 2.1.0.
Thanks anyone who can verify or correct me, I am keen to start using this nifty too NOW !! ;-)
I try to generate processor but trap in error like this:
PS C:\temp\demo> ruby C:\temp\demo\script\generate processor HelloWorld? c:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.1.2/lib/active_support/dependenci es.rb:276:in `load_missing_constant': uninitialized constant RI::Paths (NameErro? r)
rt/dependencies.rb:468:in `const_missing' m_original_require' quire' rt/dependencies.rb:510:in `require' rt/dependencies.rb:355:in `new_constants_in' rt/dependencies.rb:510:in `require' :1 m_original_require' quire' PS C:\temp\demo> ruby C:\temp\demo\script\generate processor HelloWorld? c:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.1.2/lib/active_support/dependenci es.rb:276:in `load_missing_constant': uninitialized constant RI::Paths (NameErro? r) rt/dependencies.rb:468:in `const_missing' m_original_require' quire' rt/dependencies.rb:510:in `require' rt/dependencies.rb:355:in `new_constants_in' rt/dependencies.rb:510:in `require' :1 m_original_require' quire'I'm using rails 2.1.2 on windows.
Any suggestion ?
For those struggling like Erik did, the message seems to go to the Rails log, not the console, as it did for me. FYI.
Can you please add the requirement for "gem install reliable-msg" to this demo? Seems like that is a required gem of this particular plugin. The application throws a very nasty exception without this gem being installed. A quick google search brings up this forum posting:
http://markmail.org/message/qmt5wvmjs4oluwsw
And that should REALLy be documented here. Thanks and great work, this will save me a handful in debugging and testing our JMS services.
-Bram
"stomp" is apparently also a required gem.
Hi, can anyone help. I keep getting this message when execute "http://localhost:3000/say_hello_world" from the browser:
Processing ApplicationController?#index (for 127.0.0.1 at 2009-11-18 22:42:37) GET?
RuntimeError? ( ActiveMessaging: problems trying to load '/Users/aa/Projects/bashprojects/ruby/activemq/demo/config/messaging.rb':
Rendered rescues/trace (101.0ms) Rendered rescues/request_and_response (1.7ms) Rendering rescues/layout (internal_server_error)
to get this to work on windows I needed to do a couple additional things:
1. install gems
2. make sure to install ActiveMQ directly to my C:\ to avoid space in path issues
3. add a template in app/views/say_hello_world/say_hello.html.erb as a copy of the index
When I run the script/poller run I get this: ActiveMessaging: adapter reliable_msg not loaded: no such file to load -- reliab le-msg c:/ruby/lib/ruby/gems/1.8/gems/stomp-1.1.3/lib/stomp/connection.rb:156: warning:
ActiveMessaging: adapter wmq not loaded: no such file to load -- wmq/wmq "C:/Users/NonSense?/My Documents/Aptana RadRails? Workspace/mq" "C:/Users/NonSense?/My Documents/Aptana RadRails? Workspace/mq/config/messaging.rb " ActiveMessaging: Loading C:/Users/NonSense?/My Documents/Aptana RadRails? Workspac e/mq/app/processors/application.rb ActiveMessaging: Loading C:/Users/NonSense?/My Documents/Aptana RadRails? Workspac e/mq/app/processors/hello_world_processor.rb => Subscribing to /queue/HelloWorld? (processed by HelloWorldProcessor?) connect to localhost failed: private method `chomp' called for nil:NilClass? will connect to localhost failed: private method `chomp' called for nil:NilClass? willAny Ideas what sould I do?