Created
September 18, 2020 18:20
-
-
Save sudeeptarlekar/f647393a8b2e3dce81693cfdb0f0c49a to your computer and use it in GitHub Desktop.
Updated example for durable subscribers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- encoding: utf-8 -*- | |
# | |
# The current require dance for different Ruby versions. | |
# Change this to suit your requirements. | |
# | |
if Kernel.respond_to?(:require_relative) | |
require_relative("./stomp_common") | |
else | |
$LOAD_PATH << File.dirname(__FILE__) | |
require "stomp_common" | |
end | |
include Stomp1xCommon | |
=begin | |
A recent experience suggested that we might provide an example of how | |
to establish a "durable" topic subscription using ActiveMQ. | |
This code attempts to do that. | |
References: | |
http://activemq.apache.org/stomp.html | |
http://activemq.apache.org/how-does-a-queue-compare-to-a-topic.html | |
http://activemq.apache.org/how-do-durable-queues-and-topics-work.html | |
Specifically, locate the section titled: ActiveMQ extensions to Stomp. | |
There are two programmatic requirements: | |
1) On CONNECT, indicate to AMQ the code will be using durable topic | |
subscription(s). | |
Done by providing a "cilent-id" CONNECT header. | |
2) On SUBSCRIBE, indicate an AMQ specific (unique) subscription ID. Done | |
by providing a "activemq.subscriptionName" header to SUBSCRIBE. | |
=end | |
# Create test.rb. file manually | |
file = File.open('test.rb', 'r+') | |
# login hash | |
hash = { :hosts => [ | |
{:login => login(), :passcode => passcode(), :host => host(), :port => port(), | |
:ssl => usessl()}, # | |
], | |
:reliable => true, | |
:closed_check => false, | |
:connect_headers => {:host => "localhost", :"accept-version" => "1.2", | |
# Requirement 1, name should be unique. | |
:"client-id" => "dursubcli01", # REF the 1st AMQ link above | |
} | |
} | |
# The topic | |
topic = "/topic/topicName" | |
# Establish the client connection | |
cli = Stomp::Client.open(hash) | |
# SUBSCRIBE Headers | |
sh = { "activemq.subscriptionName" => "subname01" } # REF the 1st AMQ link above | |
# And the client subscribe | |
puts "*****#{Thread.current.object_id}******" | |
cli.subscribe(topic, sh) do |msg| | |
file.puts "msg1: Count: #{msg.body}; Thread ID: #{Thread.current.object_id};" | |
end | |
hash[:connect_headers][:'client-id'] = 'dursubcli02' | |
puts hash | |
cli2 = Stomp::Client.open(hash) | |
sh2 = { "activemq.subscriptionName" => "subname01" } | |
cli2.subscribe(topic, sh2) do |msg| | |
file.puts "msg2: Count: #{msg.body}; Thread ID: #{Thread.current.object_id};" | |
end | |
25.times { |n| cli.publish('/topic/topicName', "#{n}") } | |
=begin | |
At this point open your AMQ admin console (port 8161 usually), and examine | |
the 'subscribers' section. You should see an instance of this client | |
displayed in the "Active Durable Topic Subscribers" section. | |
=end | |
# Wait for a very long time, usually exit via ^C | |
puts "Open AMQ console and examing the 'subscribers' section" | |
puts "Wne done, press ^C to exit, and reexamine the 'subscribers' section" | |
sleep 1000000 | |
=begin | |
After you press ^C to exit this program, return to the AMQ console and | |
refresh the display. (Do not restart AMQ). You should see this client in the | |
"Offline Durable Topic Subscribers" section. | |
=end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment