Ruby and ActiveMQ. AMQ339016: Error creating subscription Stomp::Error::BrokerException - ruby

I am running a simple Ruby program to produce and consume ActiveMQ Stomp messages. I keep on getting this error.
/root/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/stomp-1.3.4/lib/stomp/client.rb:132:in `join': AMQ339016: Error creating subscription f7b282396a9bef2da1ccd42d16dc511758234113 (Stomp::Error::BrokerException)
from /root/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/stomp-1.3.4/lib/stomp/client.rb:132:in `join'
from consumer.rb:10:in `<main>'
I made sure ActiveMQ is up and from I can tell it is not necessary to configure the xml config files since ActiveMQ is supposed to support Stomp out of the box. My program looks like this:
require 'rubygems'
require 'stomp'
client = Stomp::Client.open "stomp://localhost:61613"
client.subscribe "/queue/myqueue" do |message|
puts "received: #{message.body} on #{message.headers['destination']}"
end
client.join
client.close
puts "completed process"
The full example code can be found here:
http://arhipov.blogspot.com/2009/05/activemq-ruby-stomp-transport.html

Related

Undefined method 'new' Kafka

I am trying to run the Kafka Consumer example found here:
http://www.rubydoc.info/gems/ruby-kafka/Kafka/Consumer
This is the program I am trying to run:
require "kafka"
kafka = Kafka.new(["kafka1:9092", "kafka2:9092"])
# Create a new Consumer instance in the group `my-group`:
consumer = kafka.consumer(group_id: "my-group")
# Subscribe to a Kafka topic:
consumer.subscribe("messages")
# Loop forever, reading in messages from all topics that have been
# subscribed to.
consumer.each_message do |message|
puts message.topic
puts message.partition
puts message.key
puts message.value
puts message.offset
end
But whenever I run this program I get the error:
example.rb:3:in '<main>': undefined method 'new' for Kafka:Module (NoMethodError)
I have seen this sort of error of Undefined method 'new' in a few questions relating to completely different programs but can't seem to figure out what is wrong as I haven't overwritten a kafka class or anything. I installed kafka on my machine using sudo gem install kafka-rb the version being kafka-rb-0.0.15.
Does anyone have any ideas why the problem is occurring and how to resolve this issue?
As was mentioned in the comments, you are using kafka-rb gem, which is no longer maintained. But if for some reason you want to continue using it, the code would probably look something like this:
require 'kafka'
consumer = Kafka::Consumer.new(topic: 'messages')
consumer.loop do |messages|
puts "Received"
puts messages
end

Setting up resque-pool over a padrino Rakefile throwing errors

I have setup a Padrino bus application using super-cool Resque for handling background process and ResqueBus for pub/sub of events.
The ResqueBus setup creates a resque queue and a worker for it to work on. Everything upto here works fine. Now since the resquebus is only creating a single worker for a single queue, and the process in my bus app can go haywire since many events will be published and subscribed. So a single worker per application queue seems to be inefficient. So thought of integrating the resque-pool gem to handle the worker process.
I have followed all process that resque pool gem has specified. I have edited my Rakefile.
# Add your own tasks in files placed in lib/tasks ending in .rake,
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
require File.expand_path('../config/application', __FILE__)
Ojus::Application.load_tasks
require 'resque/pool/tasks'
# this task will get called before resque:pool:setup
# and preload the rails environment in the pool manager
task "resque:setup" => :environment do
# generic worker setup, e.g. Hoptoad for failed jobs
end
task "resque:pool:setup" do
# close any sockets or files in pool manager
ActiveRecord::Base.connection.disconnect!
# and re-open them in the resque worker parent
Resque::Pool.after_prefork do |job|
ActiveRecord::Base.establish_connection
end
end
Now I tried to run this resque-pool command.
resque-pool --daemon --environment production
This throws an error like this.
/home/ubuntu/.rvm/gems/ruby-2.0.0-p451#notification-engine/gems/activerecord-4.1.7/lib/active_record/connection_adapters/connection_specification.rb:257:in `resolve_symbol_connection': 'default_env' database is not configured. Available: [:development, :production, :test] (ActiveRecord::AdapterNotSpecified)
I tried to debug this and found out that it throws an error at line
ActiveRecord::Base.connection.disconnect!
For now I have removed this line and everything seems working fine. But due to this a problem may arise because if we restart the padrino application the older ActiveRecord connection will be hanging around.
**
I just wanted to know if there is any work around for this problem and
run the resque-pool command by closing all the ActiveRecord
connections.
**
It would have been helpful if you had given your database.rb file of padrino.
Never mind, you can try
defined?(ActiveRecord::Base) && ActiveRecord::Base.connection.disconnect!
instead of ActiveRecord::Base.connection.disconnect!
and
ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations[Padrino.env])
instead of ActiveRecord::Base.establish_connection()
to establish a connection with activerecord you have to pass a parameter to what environment you want to connect otherwise it will search 'default_env' which is default in activerecord.
checkout the source code source code

Sending message to ZeroMQ from Rails 4

I'm trying to send message to ZeroMQ from Rails 4 application.
I added gem "zmq" to my Gemfile, then I use this code in some method in my Application Controller.
context = ZMQ::Context.new(1)
Rails prints exception:
uninitialized constant ApplicationController::ZMQ
If I add require 'zmq'to application_controller.rb Rails prints other message:
cannot load such file -- zmq
I found the reason of this error. Gem zmq (rb-zmq) doesn't work correctly with ZeroMQ 3.x.x, only with 2.x.x:
https://github.com/zeromq/rbzmq/issues/25

Logging to remote location

I'm writing a client application which runs on a users computer and sends various requests to a server.
What I'd like to do is make logs from the client programs available on the server so that issues can be easily detected.
So, what I was thinking was to make the client log to a remote location over http.
Is this a good idea and are there any gems or libraries that will facilitate this?
You could use DRb + Logger for this. Both are part of the Ruby standard library, so you don't even need to install any gems on either machine.
Here's how it works:
Remote Logging Machine
require 'drb'
require 'logger'
DRb.start_service 'druby://0.0.0.0:9000', Logger.new('foo.log', 'weekly')
DRb.thread.join
Machine Doing the Logging
require 'drb'
$log = DRbObject.new_with_uri 'druby://remote.server.ip:9000'
begin
$log.info "Hello World"
rescue DRb::DRbConnError => e
warn "Could not log because: #{e}"
# Optionally re-log the message somewhere else.
end
puts "Yay, still running!"
I just tested this between two machines 1500 miles apart, where the client machine is even behind NAT, and it worked flawlessly.

Ruby AMQP uninitialized constant error

I was trying out this code (got from an online article here: http://www.randomhacks.net/articles/2009/05/08/chat-client-ruby-amqp-eventmachine-shoes)
require 'rubygems'
gem 'amqp'
require 'mq'
unless ARGV.length == 2
STDERR.puts "Usage: #{$0} "
exit 1
end
$channel, $nick = ARGV
AMQP.start(:host => 'localhost') do
$chat = MQ.topic('chat')
# Print any messages on our channel.
queue = MQ.queue($nick)
queue.bind('chat', :key => $channel)
queue.subscribe do |msg|
if msg.index("#{$nick}:") != 0
puts msg
end
end
# Forward console input to our channel.
module KeyboardInput
include EM::Protocols::LineText2
def receive_line data
$chat.publish("#{$nick}: #{data}",
:routing_key => $channel)
end
end
EM.open_keyboard(KeyboardInput)
end
But ended up the following error:
chat.rb:11:in `': uninitialized constant AMQP (NameError)
After that, I tried different example code with AMQP at my dev env but all shows me that error. So the problem is not in the code, the problem with my dev env. Can anybody point me out the issues with my dev env. Thanks in advance.
I have AMQP installed and integrated with Ruby (via the bunny gem). Maybe I can help?
Most likely the gem install failed to compile the amqp libs. Uninstall the gem and reinstall, taking a very close look at the messages produced. Possibly you're only missing some third-party libs.
Which platform are you on?

Resources