Skip to content
This repository was archived by the owner on Jan 30, 2020. It is now read-only.
This repository was archived by the owner on Jan 30, 2020. It is now read-only.

Example code for automatic reconnection #19

@candlerb

Description

@candlerb

Related to #13: it would be really helpful if there was some example code showing the correct pattern to reconnect to a server when a problem occurs.

The following appears to work (tested using HornetQ 2.2.5 as the server) but I'm not sure if this is a safe or recommended approach.

connection = nil
reconnect = lambda do |exc|
  begin
    connection = JMS::Connection.new(config)
    connection.on_exception(&reconnect)
    connection.start
  rescue => e
    $stderr.puts e
    sleep 5
    retry
  end
end
reconnect.call(nil)

Producer:

count = 0
loop do
  begin
    connection.session do |session|
      session.producer(:queue_name => 'test') do |producer|
        m = "Hello World #{count}"
        puts m
        producer.send(session.message(m))
        count += 1
        sleep 3
      end
    end
  rescue javax.jms.JMSException => e
    puts "Send failed (#{e})"
    sleep 1
  end
end

Consumer:

loop do
  begin
    connection.session do |session|
      session.consume(:queue_name => 'test', :timeout => -1) do |message|
          p message
          p message.jms_reply_to
      end
    end
  rescue javax.jms.JMSException => e
    puts "Consume failed (#{e})"
    sleep 1
  end
end

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions