Tuesday, July 22, 2008

JMS JRuby Producer and Consumer

So , after scouring the internet trying to find 1 consolidated location that demonstrated an easy way to use JMS inside of JRuby , I finally used some internet examples and some research on the SUN JMS API website to get this working.  The biggest thing I had to figure out was how to properly structure the message from the message producer.  This is a basic setup using activemq 5.1 , jruby 1.1.2 and some basic ruby skills.  Here is what I did.

 1 - fire up out of the box activemq installation.  Literally that easy, just run bin/activemq & 
 2 - copy activemq-all-5.1.0.jar from activemq installation directory to the same directory as your JRuby scripts
 3 - create the 2 scripts below , consumer.rb and producer.rb and you are off and running.  For the Rails enthusiasts , I can imagine a very nice replacement for ActiveMessaging where you create a simple JMS library that will do an async post of messages to the queue, very nice indeed.
4 - Run each script in a separate window (jruby consumer.rb  AND jruby producer.rb).  Producer.rb will simply give you a ">" prompt to type some text to demonstrate the concept


Here is the code I ended up with:


consumer.rb
------------------------
require "java"
require "activemq-all-5.1.0.jar"

include_class "org.apache.activemq.ActiveMQConnectionFactory"
include_class "org.apache.activemq.util.ByteSequence"
include_class "org.apache.activemq.command.ActiveMQBytesMessage"
include_class "javax.jms.MessageListener"
include_class "javax.jms.Session"

class MessageHandler
include javax.jms.Session
include javax.jms.MessageListener

def onMessage(serialized_message)
message_body = serialized_message.get_content.get_data.inject("") { |body, byte| body << byte }
puts message_body
end

def run
factory = ActiveMQConnectionFactory.new("tcp://localhost:61616")
connection = factory.create_connection();
session = connection.create_session(false, Session::AUTO_ACKNOWLEDGE);
queue = session.create_queue("test1-queue");

consumer = session.create_consumer(queue);
consumer.set_message_listener(self);

connection.start();
puts "Listening..."
end
end

handler = MessageHandler.new
handler.run

producer.rb
--------------
require "java"
require "activemq-all-5.1.0.jar"
require 'readline'

include_class "org.apache.activemq.ActiveMQConnectionFactory"
include_class "org.apache.activemq.util.ByteSequence"
include_class "org.apache.activemq.command.ActiveMQBytesMessage"
include_class "javax.jms.MessageListener"
include_class "javax.jms.Session"

class MessageHandler
include javax.jms.Session
include javax.jms.MessageListener

def initialize
factory = ActiveMQConnectionFactory.new("tcp://localhost:61616")
connection = factory.create_connection();
@session = connection.create_session(false, Session::AUTO_ACKNOWLEDGE);
queue = @session.create_queue("test1-queue");

@producer = @session.create_producer(queue);
end

def send_message(line)
puts "received input of #{line}"
m = @session.createTextMessage() ;
m.set_text(line)
@producer.send(m)
end

end

handler = MessageHandler.new
loop do
line = Readline::readline('> ', true)
handler.send_message(line)
end

25 comments:

Francesco said...

Great post, very useful, as usually people limit to textMessage, as opposite to bytesMessage, as you do.

Just to know: are really necessaries the include_class you put in the header? Is it not sufficient the require 'activemq-all.jar'

IT said...

JMS Training Institutes in Chennai JMS Training Institutes in Chennai | JSP Training Institutes in Chennai | Spring Training Institutes in Chennai Spring Training Institutes in ChennaiMicroServices Training Institutes In Chennai Java MicroServices Training Institutes In Chennai
Java EE Training Institutes in Chennai Java EE Training Institutes in Chennai

IT said...

Java Training Institutes Java Training Institutes
Java Spring Hibernate Training Institutes in Chennai J2EE Training Institutes in Chennai J2EE Training Institutes in Chennai Java Spring Hibernate Training Institutes in Chennai | Hibernate Training Institutes in Chennai Core Java Training Institutes in Chennai Core Java Training Institutes in Chennai

Hibernate Online Training Hibernate Online Training Hibernate Training in Chennai Hibernate Training in Chennai Java Online Training Java Online Training Hibernate Training Institutes in ChennaiHibernate Training Institutes in Chennai

IT said...

I have read your blog its very attractive and impressive. I like it your blog.


JavaEE Training in Chennai JavaEE Training in Chennai

Java Training in Chennai Core Java Training in Chennai Core Java Training in Chennai

Java Online Training Java Online Training Core Java 8 Training in Chennai Java 8 Training in Chennai

IT said...

Interesting Article

JMS training | Java Online Training

Java Training in Chennai | JMS training online

Unknown said...

Thank you for some other informative website. I have a venture that I am simply now running on, and I’ve been at the glance out for such info.
Data entry services in India

Government Jobs said...

organic cold pressed oils
natural cold pressed oils
organic oil
organic oil in jaipur
organic cold pressed oil in jaipur
natural oil
natural oil shop in jaipur
pure herbal oil
ayurvedic oil store in jaipur
ayurvedic oil

Government Jobs said...


law college
law college in Jaipur
Best law college in Jaipur
Law Course In Jaipur
Top College Of law In Jaipur
Vidyasthali Law College
Best Law College
Jaipur Law College

Government Jobs said...

Thanks for sharing this unique information with us. Your post is really awesome. Your blog is really helpful for me..
Top college in Jaipur|law college|law college in Jaipur|Best law college in Jaipur

Government Jobs said...

Hey Your site is awesome and full of information. I have read you posts they are so informative. Keep Posting wonderful content.
Ani international provide the security solutions for all kind of secruity system and other equipment.
Home security system in jaipur
Wireless Home Security System in jaipur
Realtime attendance machine in jaipur
CCTV Camera dealer in jaipur
Hikvision DVR in jaipur at Rajasthan
security system solutions in jaipur
website design in jaipur
website development company in jaipur
seo company in jaipur

sheela said...

Really you have done great job,There are may person searching about that now they will find enough resources by your post
Best Devops online Training
Online DevOps Certification Course - Gangboard

Government Jobs said...

Thanks for sharing this unique information with us. Your post is really awesome. Your blog is really helpful for me..
organic oil
organic oil in jaipur
organic cold pressed oils
ayurvedic oil store in jaipur

sasitamil said...


It seems you are so busy in last month. The detail you shared about your work and it is really impressive that's why i am waiting for your post because i get the new ideas over here and you really write so well.

Selenium training in Chennai
Selenium training in Bangalore
Selenium training in Pune
Selenium Online training
Selenium training in bangalore

aditya said...

Thanks for sharing this unique information with us. Your post is really awesome. Your blog is really helpful for me..
hospital equipment
sarthak maditech
hospital equipment suppliers
hospital equipment manufacturers
medical equipment manufacturers in jaipur
operation theater lights
hospital suction machine
alcohol breath tester
hospital furniture manufacturer

ajay prakash said...

Thank you so much for providing information on this. It was very useful.
Aviation Academy in Chennai
Air hostess training in Chennai
Airport management courses in Chennai
Ground staff training in Chennai
best aviation academy in Chennai
air hostess institute in Chennai
Airline Courses in Chennai
airport ground staff training in Chennai

mary Brown said...

I am glad that I saw this post. It is informative blog for us and we need this type of blog thanks for share this blog, Keep posting such instructional blogs and I am looking forward for your future posts.
Cyber Security Projects for Final Year

JavaScript Training in Chennai

Project Centers in Chennai

JavaScript Training in Chennai

eTechno Soft Solutions said...

It is explained in clear way. It is very easy to understand the information.

Looking for Cloud Computing Training in Bangalore , learn from eTechno Soft Solutions Cloud Computing Training on online training and classroom training. Join today!

Govt Jobz Adda said...


I have read your blog its very attractive and impressive. Nice information. It helped me alot.
Government vacancy
Govt Jobs
Sarkari nokri
latest sarkari vacancy
Online Form
latest govt jobs

Govt Jobz Adda said...

I have read your blog its very attractive and impressive. Nice information. It helped me alot.
Government vacancy
Govt Jobs
Sarkari nokri
latest sarkari vacancy
Online Form
latest govt jobs

Knowledgeshub said...

I have read your blog its very attractive and impressive. Nice information. It helped me alot.
best smartphone
best smartphone in india
best android phone
best mobiles phones 2020
best smartphone in india under 15000
first smartphone
find my phone android
in another world with my smartphone

Anu said...

Thanks for your informative article, Your post helped me to understand the future and career prospects & Keep on updating your blog with such awesome article. DevOps Training | Certification in Chennai | DevOps Training | Certification in anna nagar | DevOps Training | Certification in omr | DevOps Training | Certification in porur | DevOps Training | Certification in tambaram | DevOps Training | Certification in velachery

Knowledgeshub said...

I have read your blog its very attractive and impressive. Nice information. It helped me alot.
best smartphone
best smartphone in india
best android phone
best mobiles phones 2020
best smartphone in india under 15000

Govt Jobz Adda said...

I have read your blog its very attractive and impressive. Nice information. It helped me alot.
Government vacancy
govt jobs
latest govt jobs
Sarkari nokri
govt jobs Adda
govt recruitment
latest sarkari vacancy

praveen said...

First i got a great blog .I will be interested in more similar topics. i see you got really very useful topics, i will be always checking your blog thanks.
sap training in chennai

sap training in porur

azure training in chennai

azure training in porur

cyber security course in chennai

cyber security course in porur

ethical hacking course in chennai

ethical hacking course in porur

jeni said...

Really you have done great job,There are may person searching about that now they will find enough resources by your post
Best Devops online Training
Online DevOps Certification Course - Gangboard