|
|
Line 1: |
Line 1: |
| Install the [https://rubygems.org/gems/stomp Stomp] gem with <code>gem install stomp</code>.
| | #REDIRECT [[Ruby_Examples]] |
| | |
| The following code will connect to the datafeeds service, subscribe to the the RTPPM feed and print all messages to STDOUT.
| |
| | |
| To subscribe to multiple topics in a single connection, copy the <code>client.subscribe</code> section of code, change the topic name and the '-RTPPM' suffix
| |
| | |
| <pre>
| |
| require "stomp"
| |
| | |
| class NrPollerPoller
| |
| | |
| # Initialize the poller
| |
| | |
| def initialize
| |
| | |
| @hostname = 'datafeeds.networkrail.co.uk'
| |
| @username = 'nobody@example.com'
| |
| @password = 'topsecretpassword'
| |
| | |
| puts "Stomp consumer for Network Rail Open Data Distribution Service"
| |
| | |
| end
| |
| | |
| | |
| # Connect to the service and process messages
| |
| | |
| def run
| |
| | |
| client_headers = { "accept-version" => "1.1", "heart-beat" => "5000,10000", "client-id" => Socket.gethostname, "host" => @hostname }
| |
| client_hash = { :hosts => [ { :login => @username, :passcode => @password, :host => @hostname, :port => 61618 } ], :connect_headers => client_headers }
| |
| | |
| client = Stomp::Client.new(client_hash)
| |
| | |
| # Check we have connected successfully
| |
| | |
| raise "Connection failed" unless client.open?
| |
| raise "Unexpected protocol level #{client.protocol}" unless client.protocol == Stomp::SPL_11
| |
| raise "Connect error: #{client.connection_frame().body}" if client.connection_frame().command == Stomp::CMD_ERROR
| |
| | |
| puts "Connected to #{client.connection_frame().headers['server']} server with STOMP #{client.connection_frame().headers['version']}"
| |
| | |
| | |
| # Subscribe to the RTPPM topic and process messages
| |
| | |
| client.subscribe("/topic/RTPPM_ALL", { 'id' => client.uuid(), 'ack' => 'client', 'activemq.subscriptionName' => Socket.gethostname + '-RTPPM' }) do |msg|
| |
| | |
| puts msg.body
| |
| client.acknowledge(msg, msg.headers)
| |
| | |
| end
| |
| | |
| client.join
| |
| | |
| | |
| # We will probably never end up here
| |
| | |
| client.close
| |
| puts "Client close complete"
| |
| | |
| end
| |
| | |
| end
| |
| | |
| e = NrPollerPoller.new
| |
| e.run
| |
| </pre>
| |
| | |
| [[Category:Network Rail Data Feeds]] | |