-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathRemoteQueue.py
65 lines (53 loc) · 1.65 KB
/
RemoteQueue.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import sys
import time
from stompy.simple import Client
import json
class RemoteQueue(object):
def __init__(self, host, port, destination):
self.host = host
self.port = port
self.destination = destination
self.stomp = Client(host, port)
self.stomp.connect()
self.stomp.subscribe(destination)
def put(self, message):
message = json.dumps(message)
self.stomp.put(message, self.destination)
def get(self):
mq_message = self.stomp.get(block=True)
message = mq_message.body
message = json.loads(message)
return message
def disconnect(self):
self.stomp.unsubscribe(self.destination)
self.stomp.disconnect()
def main():
TOPIC_1 = '/topic/julian_1'
TOPIC_2 = '/topic/remote_queue'
SERVER = 'pcbunn.cacr.caltech.edu'
PORT = 61613
try:
# Testing remote queue
q = RemoteQueue(host=SERVER, port=PORT, destination=TOPIC_2)
stomp_1 = Client(host=SERVER, port=PORT)
stomp_1.connect()
stomp_1.subscribe(destination=TOPIC_1)
for i in range(4):
stomp_1.put(i, destination=TOPIC_1)
q.put(i+40)
for j in range(4):
message_1 = stomp_1.get(block=True)
message_2 = q.get()
print 'message_1 is ', message_1.body
print 'message_2 is ', message_2
#print 'ack'
#stomp.ack(message)
time.sleep(1.0)
except Exception, err:
print 'Error', err
return
q.disconnect()
stomp_1.unsubscribe(TOPIC_1)
stomp_1.disconnect()
if __name__ == '__main__':
main()