Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7035657
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:19:49+00:00 2026-05-28T01:19:49+00:00

I have a rabbitmq queue subscriber that spins up a new thread every time

  • 0

I have a rabbitmq queue subscriber that spins up a new thread every time a new message is consumed:

AMQP.start(@conf) do |connection|
  channel = AMQP::Channel.new(connection)

  requests_queue = channel.queue("one")

  requests_queue.subscribe(:ack => true) do |header, body|
    puts "we have a message at #{Time.now} and n is #{n}"

    url_search = MultiJson.decode(body)

    Thread.new do
      5.times do
        lead = get_lead(n, (n == 5))

        puts "message #{n} is_last = #{lead.is_last} at #{Time.now}";

        AMQP::Exchange.default.publish(
                                        MultiJson.encode(lead), 
                                        :routing_key => header.reply_to,
                                        :correlation_id => header.correlation_id
                                      )

        n += 1
        sleep(2)
      end
    end
  end
end

My question is, how do I dispose of the thread after the message is handled? Should I be using the threadpool?

I am using JRuby. Does the above code create a Java JVM thread behind the scenes using the normal ruby syntax or should I be explicitly creating a Java thread?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T01:19:49+00:00Added an answer on May 28, 2026 at 1:19 am

    You don’t have to manually dispose the thread I think, and you should be using ruby threads, from what I gather they are java threads in jruby, which is from what jruby gets it’s nice performance.

    A common thing to do is to spin up a couple of threads and then join all of them before continuing if you want to be sure that all are complete before the next step, but it doesn’t seem to be required here.

    Here’s a little test program:

    # foo.rb
    a = Thread.new { print "a"; sleep(1); print "b"; print "c" }
    require 'pp'
    pp Thread.list
    puts "foo"
    sleep(2); 
    pp Thread.list
    puts "bar"
    

    As you can see the spawned background thread is automatically removed. (Tested in jruby as well as 1.9.2

    $ ruby foo.rb 
    a[#<Thread:0x00000100887678 run>, #<Thread:0x0000010086c7d8 sleep>]
    foo
    bc[#<Thread:0x00000100887678 run>]
    bar
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a ruby script that creates a message using AMQP in RabbitMQ. #
Is that possible to have mutex in RabbitMQ queue, i.e. If a client is
I have a Windows Service that retrieves messages from a RabbitMQ queue. The service
I'm working on a script that listens to a message queue. The only problem
I have a PHP project (Symfony2) that uses RabbitMQ. I use its as simple
I have a problem that involves several machines, message queues, and transactions. So for
Have you used VS.NET Architect Edition's Application and System diagrams to start designing a
I've got this strange problem using py-amqp and the Flopsy module. I have written
I have finished installing RabbitMQ using the following RPM http://www.rabbitmq.com/releases/rabbitmq-server/v2.7.1/rabbitmq-server-2.7.1-1.noarch.rpm i installed it like
Im running some tasks via django-celery (with rabbitmq as backend), the tasks are time

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.