import stomp, gzip, time, xml
from io import StringIO
from datetime import datetime
class MyListener(stomp.ConnectionListener):
msg_list = []
def on_error(self, headers, message):
self.msg_list.append('(ERROR) ' + message)
print('received an error %s' % message)
def on_message(self, headers, message):
fp = gzip.GzipFile(fileobj = StringIO.StringIO(message))
text = fp.readlines()
fp.close()
self.msg_list.append(text)
print('%s\n' % text)
#Conection configuration for Network Rail
conn = stomp.Connection([('datafeeds.nationalrail.co.uk', 61613)])
myListener = MyListener()
conn.set_listener('', myListener)
conn.start()
#Submit credentials
print("Connecting...")
conn.connect("d3user", "d3password", wait=False)
#Access data feed subscription
print("Subscribing to queue...")
conn.subscribe(destination="/queue/<myQueueID", id=1, ack='auto')
#Listening to queue for specified conditions
print("Listening...")
t = 0
while len(myListener.msg_list) < 1:
print(datetime.strftime(datetime.now(), '%Y-%m-%d %H:%M:%S'), " - ", len(myListener.msg_list), " messages")
try:
time.sleep(10)
t = t + 10
except KeyboardInterrupt:
print("User requested program termination")
if conn.is_connected():
print("Connection is live")
conn.disconnect()
conn.stop()
if conn.is_connected():
print("Connection is still live")
else:
print("Connection terminated by user...")
break
#Deal with collected messages
print(len(myListener.msg_list), " messages collected in ", t, " seconds")
messages = myListener.msg_list
print(myListener.msg_list)
#Clean up connection if necessary
if conn.is_connected():
print("Connection persisted until end of program")
conn.disconnect()
conn.stop()
if conn.is_connected():
print("Connection is still live")
else:
print("Connection terminated automatically at close")
else:
print("Connection terminated automatically prior to close")
#Be polite
print("Goodbye")
Connecting...
Subscribing to queue...
Listening...
2016-04-19 16:55:58 - 0 messages
2016-04-19 16:56:08 - 0 messages
2016-04-19 16:56:18 - 0 messages
2016-04-19 16:56:28 - 0 messages
2016-04-19 16:56:38 - 0 messages
...
2016-04-19 17:06:48 - 0 messages
2016-04-19 17:06:58 - 0 messages
2016-04-19 17:07:08 - 0 messages
2016-04-19 17:07:18 - 0 messages
User requested program termination
0 messages collected in 680 seconds
[]
Connection terminated automatically prior to close
Goodbye
--
You received this message because you are subscribed to the Google Groups "A gathering place for the Open Rail Data community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to openraildata-t...@googlegroups.com.
To post to this group, send email to openrail...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Peter,
That was very quick! My queue ID is <<REMOVED>>.
Thank you for your help,
Erin
____________________________________________________________
Electronic mail messages entering and leaving Arup business
systems are scanned for acceptability of content and viruses