|
TenMinuteIntroduction
Start to finish example of sending and receiving
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:
For your particular HelloWorld processor:
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. |
Sign in to add a comment
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.