Skip to content

Commit c975767

Browse files
committed
Pull in changes from bitbucket to make sure it's up to date.
1 parent f7896eb commit c975767

14 files changed

Lines changed: 140 additions & 82 deletions

File tree

TODO

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
* Get better performance from get_line and parse_frame?
1+
* Be more awesome

docs/reference/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
:Date: |today|
77

88
.. toctree::
9-
:maxdepth: 3
9+
:maxdepth: 4
1010

1111
stompy.stomp
1212
stompy.frame

docs/reference/stompy.simple.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
=====================================
2+
Simple Client - stompy.simple
3+
=====================================
4+
5+
.. currentmodule:: stompy.simple
6+
7+
.. automodule:: stompy.simple
8+
:members:
9+
10+

examples/simple_example.py

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@
55
from itertools import count
66
from optparse import OptionParser
77

8+
def callback_func(message):
9+
print("Using Callback Function")
10+
print(message.headers.get('message-id'))
11+
print(message.body)
12+
stomp.ack(message)
813

9-
def consume(host, port, queue, num=None):
10-
stomp = StompClient(host, port)
11-
# optional connect keyword args "username" and "password" like so:
12-
# stomp.connect(username="user", password="pass")
13-
stomp.connect()
14+
def consume(queue, num=None, callback=None):
1415

1516
stomp.subscribe(queue, ack="client")
1617

@@ -24,17 +25,16 @@ def _handle_message(frame):
2425

2526
try:
2627
for i in it:
27-
frame = stomp.get()
28-
_handle_message(frame)
28+
if callback:
29+
stomp.get(callback=callback_func)
30+
else:
31+
frame = stomp.get()
32+
_handle_message(frame)
2933
finally:
3034
stomp.disconnect()
3135

3236

33-
def produce(host, port, queue, num=1000):
34-
stomp = StompClient(host, port)
35-
# optional connect keyword args "username" and "password" like so:
36-
# stomp.connect(username="user", password="pass")
37-
stomp.connect()
37+
def produce(queue, num=1000):
3838

3939
for i in xrange(0, num):
4040
print("Message #%d" % i)
@@ -58,6 +58,9 @@ def produce(host, port, queue, num=1000):
5858
default=False, dest='produce', help='produce messages')
5959
parser.add_option('-c', '--consume', action='store_true',
6060
default=False, dest='consume', help='consume messages')
61+
parser.add_option('-C', '--use-callback', action='store_true',
62+
default=False, dest='callback',
63+
help='send retrieved message to python callable')
6164
parser.add_option('-n', '--number', action='store',
6265
type='int', dest='number', default="100",
6366
help='produce or consume NUMBER messages')
@@ -69,8 +72,13 @@ def produce(host, port, queue, num=1000):
6972
parser.print_help()
7073
sys.exit(1)
7174

75+
stomp = StompClient(options.host, options.port)
76+
# optional connect keyword args "username" and "password" like so:
77+
# stomp.connect(username="user", password="pass")
78+
stomp.connect()
79+
7280
if options.produce:
7381
print("PRODUCING")
74-
produce(options.host, options.port, options.queue, options.number)
82+
produce(options.queue, options.number)
7583
elif options.consume:
76-
consume(options.host, options.port, options.queue, options.number)
84+
consume(options.queue, options.number, options.callback)

examples/stomp_example.py

Lines changed: 37 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,44 @@
11
#!/usr/bin/env python
22
import sys
3+
import itertools
34
import time
45
from stompy import Stomp
56
from optparse import OptionParser
67

8+
def callback_func(message):
9+
print("Using Callback Function")
10+
print(message.headers.get('message-id'))
11+
print(message.body)
12+
stomp.ack(message)
713

8-
def consume(host, port, queue, num=None):
9-
try:
10-
stomp = Stomp(host, port)
11-
# optional connect keyword args "username" and "password" like so:
12-
# stomp.connect(username="user", password="pass")
13-
stomp.connect()
14-
except:
15-
print("Cannot connect")
16-
raise
14+
def consume(queue, num=None, callback=None):
1715

1816
# If using RabbitMQ, the queue seems to 'disappear'
1917
# after disconnecting a consumer, to make the queue persistent
2018
# add the headers 'auto-delete': 'false' and 'durable': 'true'
2119
# to the dictionary below
2220
stomp.subscribe({'destination': queue, 'ack': 'client'})
2321

24-
if not num:
25-
while True:
26-
try:
27-
frame = stomp.receive_frame()
28-
stomp.ack(frame)
29-
print(frame.headers.get('message-id'))
30-
print(frame.body)
31-
except KeyboardInterrupt:
32-
stomp.disconnect()
33-
break
34-
else:
35-
for i in xrange(0, num):
36-
try:
22+
def _handle_message(frame):
23+
print(frame.headers.get('message-id'))
24+
print(frame.body)
25+
stomp.ack(frame)
26+
27+
# if num is not set, iterate forever.
28+
it = xrange(0, num) if num else itertools.count()
29+
30+
try:
31+
for i in it:
32+
if callback:
33+
stomp.receive_frame(callback=callback_func)
34+
else:
3735
frame = stomp.receive_frame()
38-
stomp.ack(frame)
39-
print(frame.headers.get('message-id'))
40-
print(frame.body)
41-
except KeyboardInterrupt:
42-
stomp.disconnect()
43-
break
36+
_handle_message(frame)
37+
finally:
4438
stomp.disconnect()
4539

4640

47-
def produce(host, port, queue, num=1000):
48-
try:
49-
stomp = Stomp(host, port)
50-
# optional connect keyword args "username" and "password" like so:
51-
# stomp.connect(username="user", password="pass")
52-
stomp.connect()
53-
except:
54-
print("Cannot connect")
55-
raise
41+
def produce(queue, num=1000):
5642

5743
for i in xrange(0, num):
5844
print("Message #%d" % i)
@@ -75,6 +61,9 @@ def produce(host, port, queue, num=1000):
7561
default=False, dest='produce', help='produce messages')
7662
parser.add_option('-c', '--consume', action='store_true',
7763
default=False, dest='consume', help='consume messages')
64+
parser.add_option('-C', '--use-callback', action='store_true',
65+
default=False, dest='callback',
66+
help='send retrieved message to python callable')
7867
parser.add_option('-n', '--number', action='store',
7968
type='int', dest='number',
8069
help='produce or consume NUMBER messages')
@@ -94,7 +83,16 @@ def produce(host, port, queue, num=1000):
9483
parser.print_help()
9584
sys.exit(1)
9685

86+
try:
87+
stomp = Stomp(options.host, options.port)
88+
# optional connect keyword args "username" and "password" like so:
89+
# stomp.connect(username="user", password="pass")
90+
stomp.connect()
91+
except:
92+
print("Cannot connect")
93+
raise
94+
9795
if options.produce:
98-
produce(options.host, options.port, options.queue, options.number)
96+
produce(options.queue, options.number)
9997
elif options.consume:
100-
consume(options.host, options.port, options.queue, options.number)
98+
consume(options.queue, options.number, options.callback)

setup.cfg

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
source-dir = docs/
33
build-dir = docs/build
44
all_files = 1
5+
[upload_sphinx]
6+
upload-dir = docs/build/html
57
[nosetests]
68
verbosity = 1
79
detailed-errors = 1

setup.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
url=distmeta.__homepage__,
1313
keywords='stomp activemq jms messaging',
1414
test_suite="nose.collector",
15-
setup_requires=['nose>=0.11', 'dingus'],
1615
classifiers=["Development Status :: 5 - Production/Stable",
1716
"Intended Audience :: Developers",
1817
"License :: OSI Approved :: BSD License",

stompy/distmeta.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Implementation of the STOMP protocol in Python.
22
"""
3-
VERSION = (0, 2, 4)
3+
VERSION = (0, 2, 9)
44
__version__ = ".".join(map(str, VERSION))
55
__author__ = "Benjamin W. Smith"
66
__contact__ = "benjaminwarfield@just-another.net"

stompy/frame.py

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import socket
22
import random
33
from pprint import pformat
4-
from errno import EAGAIN
4+
from errno import EAGAIN, EWOULDBLOCK
55
from Queue import Queue
66
from Queue import Empty as QueueEmpty
77

@@ -66,21 +66,28 @@ def __init__(self, sock=None):
6666
self.iqueue = IntermediateMessageQueue()
6767
self.rqueue = Queue()
6868

69-
def connect(self, sock, username=None, password=None):
69+
def connect(self, sock, username=None, password=None, clientid=None):
7070
"""Connect to the STOMP server and get the session id.
7171
7272
:param sock: Socket object from stompy.stomp.Stomp.
7373
:keyword username: Username for connection.
7474
:keyword password: Password for connection.
75+
:keyword clientid: Client identification for persistent connections
7576
7677
"""
7778
self.sock = sock
79+
80+
headers = {}
81+
7882
if username and password:
79-
frame = self.build_frame({"command": "CONNECT",
80-
"headers": {'login': username,
81-
'passcode': password}})
82-
else:
83-
frame = self.build_frame({"command": "CONNECT", "headers": {}})
83+
headers.update({'login': username,
84+
'passcode': password})
85+
86+
if clientid:
87+
headers.update({'client-id' : clientid})
88+
89+
90+
frame = self.build_frame({"command": "CONNECT", "headers": headers})
8491

8592
self.send_frame(frame.as_string())
8693

@@ -259,10 +266,12 @@ def _getline(self, nb=False):
259266
while not buffer.endswith('\x00'):
260267
try:
261268
partial = self.sock.recv(1)
269+
if not partial or partial == '':
270+
raise UnknownBrokerResponseError('empty reply')
262271
except socket.error, exc:
263-
if exc[0] == EAGAIN:
272+
if exc[0] == EAGAIN or exc[0] == EWOULDBLOCK:
264273
if not buffer or buffer == '\n':
265-
return None
274+
raise UnknownBrokerResponseError('empty reply')
266275
continue
267276
buffer += partial
268277
finally:

stompy/simple.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class Client(object):
1919
>>> from stompy.simple import Client
2020
>>> stomp = Client()
2121
>>> stomp.connect()
22-
>>> stomp.send("The quick brown fox...", destination="/queue/test")
22+
>>> stomp.put("The quick brown fox...", destination="/queue/test")
2323
>>> stomp.subscribe("/queue/test")
2424
>>> message = stomp.get_nowait()
2525
>>> message.body
@@ -35,17 +35,19 @@ def __init__(self, host="localhost", port=61613):
3535
self.stomp = Stomp(host, port)
3636
self._current_transaction = None
3737

38-
def get(self, block=True):
38+
def get(self, block=True, callback=None):
3939
"""Get message.
4040
4141
:keyword block: Block if necessary until an item is available.
4242
If this is ``False``, return an item if one is immediately
4343
available, else raise the :exc:`Empty` exception.
4444
45+
:keyword callback: Optional function to execute when message recieved.
46+
4547
:raises Empty: If ``block`` is off and no message was receied.
4648
4749
"""
48-
frame = self.stomp.receive_frame(nonblocking=not block)
50+
frame = self.stomp.receive_frame(nonblocking=not block, callback=callback)
4951
if frame is None and not block:
5052
raise self.Empty()
5153
return frame
@@ -78,19 +80,20 @@ def put(self, item, destination, persistent=True, conf=None):
7880

7981
return self.stomp.send(conf)
8082

81-
def connect(self, username=None, password=None):
83+
def connect(self, username=None, password=None, clientid=None):
8284
"""Connect to the broker.
8385
8486
:keyword username: Username for connection
8587
:keyword password: Password for connection
88+
:keyword clientid: Client identification for persistent connections
8689
8790
:raises :exc:`stompy.stomp.ConnectionError`:
8891
if the connection was unsuccessful.
8992
:raises :exc:`stompy.stomp.ConnectionTimeoutError`:
9093
if the connection timed out.
9194
9295
"""
93-
self.stomp.connect(username=username, password=password)
96+
self.stomp.connect(username=username, password=password, clientid=clientid)
9497

9598
def disconnect(self):
9699
"""Disconnect from the broker."""

0 commit comments

Comments
 (0)