My favorites | Sign in
Project Logo
                
Search
for
Updated Nov 26, 2007 by kookster
TenMinuteIntroduction  
Start to finish example of sending and receiving

Ten Minute Introduction

We 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.

Steps

Start 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

end

If 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.


Comment by tieg.zaharia, Jul 13, 2007

Minor intro correction: it looks like the queue() method on the following line has been renamed to destination():

 s.queue :hello_world, '/queue/HelloWorld'
Comment by Tomasz.Wegrzanowski, Mar 18, 2008

'app/processor/hello_world_processor.rb' filename is wrong, it should be 'app/processors/hello_world_processor.rb'

Comment by james.wintermute, Jul 04, 2008
publishes_to :hello_world

This can be negated- indeed, it does not appear to exist. Argument to publish defines this.

Comment by sacketty, Sep 05, 2008
script/generate processor HelloWorld?

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

Comment by l.rieder, Sep 23, 2008

How persistent are those messages?

Comment by erik.scheirer, Sep 27, 2008

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"

Comment by erik.scheirer, Sep 27, 2008

...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 !! ;-)

Comment by heru.on.merb, Nov 18, 2008

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)

from c:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.1.2/lib/active_suppo
rt/dependencies.rb:468:in `const_missing'
from c:/ruby/lib/ruby/1.8/rdoc/ri/ri_options.rb:53 from c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `ge
m_original_require'
from c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `re
quire'
from c:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.1.2/lib/active_suppo
rt/dependencies.rb:510:in `require'
from c:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.1.2/lib/active_suppo
rt/dependencies.rb:355:in `new_constants_in'
from c:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.1.2/lib/active_suppo
rt/dependencies.rb:510:in `require'
from c:/ruby/lib/ruby/1.8/rdoc/usage.rb:72
... 48 levels...
from c:/ruby/lib/ruby/gems/1.8/gems/rails-2.1.2/lib/commands/generate.rb
:1
from c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `ge
m_original_require'
from c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `re
quire'
from C:/temp/demo/script/generate:3
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)
from c:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.1.2/lib/active_suppo
rt/dependencies.rb:468:in `const_missing'
from c:/ruby/lib/ruby/1.8/rdoc/ri/ri_options.rb:53 from c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `ge
m_original_require'
from c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `re
quire'
from c:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.1.2/lib/active_suppo
rt/dependencies.rb:510:in `require'
from c:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.1.2/lib/active_suppo
rt/dependencies.rb:355:in `new_constants_in'
from c:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.1.2/lib/active_suppo
rt/dependencies.rb:510:in `require'
from c:/ruby/lib/ruby/1.8/rdoc/usage.rb:72
... 48 levels...
from c:/ruby/lib/ruby/gems/1.8/gems/rails-2.1.2/lib/commands/generate.rb
:1
from c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `ge
m_original_require'
from c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `re
quire'
from C:/temp/demo/script/generate:3

I'm using rails 2.1.2 on windows.

Any suggestion ?

Comment by javajames, Nov 18, 2008

For those struggling like Erik did, the message seems to go to the Rails log, not the console, as it did for me. FYI.

Comment by bramski, Mar 30, 2009

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

Comment by bramski, Mar 30, 2009

"stomp" is apparently also a required gem.

Comment by paconyc99, Nov 18, 2009

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':

You already defined hello_world to /queue/HelloWorld?):
app/controllers/say_hello_world_controller.rb:2 /usr/local/lib/ruby/1.8/webrick/httpserver.rb:104:in `service' /usr/local/lib/ruby/1.8/webrick/httpserver.rb:65:in `run' /usr/local/lib/ruby/1.8/webrick/server.rb:173:in `start_thread' /usr/local/lib/ruby/1.8/webrick/server.rb:162:in `start' /usr/local/lib/ruby/1.8/webrick/server.rb:162:in `start_thread' /usr/local/lib/ruby/1.8/webrick/server.rb:95:in `start' /usr/local/lib/ruby/1.8/webrick/server.rb:92:in `each' /usr/local/lib/ruby/1.8/webrick/server.rb:92:in `start' /usr/local/lib/ruby/1.8/webrick/server.rb:23:in `start' /usr/local/lib/ruby/1.8/webrick/server.rb:82:in `start'

Rendered rescues/trace (101.0ms) Rendered rescues/request_and_response (1.7ms) Rendering rescues/layout (internal_server_error)

Comment by lianaleahy, Nov 20, 2009

to get this to work on windows I needed to do a couple additional things:

1. install gems

gem install stomp gem install daemons gem install reliable-msg gem install activemessaging

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

Comment by NonSense86, Yesterday (16 hours ago)

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:

parenthesize argument(s) for future version
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
retry(#0) in 10
connect to localhost failed: private method `chomp' called for nil:NilClass? will
retry(#1) in 10

Any Ideas what sould I do?


Sign in to add a comment
Hosted by Google Code