File: stream-receiver.py

package info (click to toggle)
rabbitmq-server 4.0.5-8
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 37,972 kB
  • sloc: erlang: 257,835; javascript: 22,466; sh: 3,037; makefile: 2,517; python: 1,966; xml: 646; cs: 335; java: 244; ruby: 212; php: 100; perl: 63; awk: 13
file content (40 lines) | stat: -rwxr-xr-x 1,201 bytes parent folder | download | duplicates (3)
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
import time
import sys

import stomp
import random
import requests

class MyListener(stomp.ConnectionListener):
  def on_error(self, frame):
    print('received an error "%s"' % frame.body)
  def on_message(self, frame):
    print('received a message "%s"' % frame.body)

# Define a STOMP connection and port
conn = stomp.Connection([("localhost", 61613)])
conn.set_listener('', MyListener())
conn.connect('guest', 'guest', wait=True) # define the username/password

# Setup a subscription
conn.subscribe(destination='/exchange/stomp1', id=1234, ack='client', headers={
    'x-queue-name': 'my-stomp-stream',
    'x-queue-type': 'stream',
    'x-max-age' : '10h',
    'durable': True,
    'auto-delete': False,
    'id': 1234,
    'prefetch-count': 10
})

response = requests.get("http://localhost:15672/api/queues/%2F/my-stomp-stream", auth=("guest", "guest"))
stream = response.json()
print("Stream arguments:")
print("  x-queue-type:" + stream["arguments"]["x-queue-type"])
print("  x-max-age:" + stream["arguments"]["x-max-age"])

while True:
  time.sleep(15) # send a random message every 15 seconds
  conn.send( destination='/exchange/stomp1', body=str(random.randint(1,11)))

conn.disconnect()