def test_onDisconnect_waitForOutstandingMessagesToFinish(self): config = self.getConfig(StompSpec.VERSION_1_0) client = async .Stomp(config) client.add(ReceiptListener(1.0)) # connect client = yield client.connect(host=VIRTUALHOST) yield task.cooperate( iter([ client.send(self.queue, self.frame, receipt='message-%d' % j) for j in xrange(self.numMsgs) ])).whenDone() client.subscribe( self.queue, {StompSpec.ACK_HEADER: StompSpec.ACK_CLIENT_INDIVIDUAL}, listener=SubscriptionListener(self._frameHandler)) # wait for disconnect yield client.disconnected # reconnect and subscribe again to make sure that all messages in the queue were ack'ed client = yield client.connect(host=VIRTUALHOST) self.timeExpired = False self.timeoutDelayedCall = reactor.callLater( 1, self._timesUp, client) # @UndefinedVariable client.subscribe( self.queue, {StompSpec.ACK_HEADER: StompSpec.ACK_CLIENT_INDIVIDUAL}, listener=SubscriptionListener(self._eatOneFrameAndDisconnect)) # wait for disconnect yield client.disconnected # time should have expired if there were no messages left in the queue self.assertTrue(self.timeExpired)
def test_unsubscribe(self): config = self.getConfig(StompSpec.VERSION_1_0) client = async .Stomp(config) client = yield client.connect(host=VIRTUALHOST) token = yield client.subscribe( self.queue, {StompSpec.ACK_HEADER: StompSpec.ACK_CLIENT_INDIVIDUAL}, listener=SubscriptionListener(self._eatFrame)) client.send(self.queue, self.frame) while self.framesHandled != 1: yield task.deferLater(reactor, 0.01, lambda: None) client.unsubscribe(token) client.send(self.queue, self.frame) yield task.deferLater(reactor, 0.2, lambda: None) self.assertEquals(self.framesHandled, 1) client.subscribe( self.queue, {StompSpec.ACK_HEADER: StompSpec.ACK_CLIENT_INDIVIDUAL}, listener=SubscriptionListener(self._eatFrame)) while self.framesHandled != 2: yield task.deferLater(reactor, 0.01, lambda: None) client.disconnect() yield client.disconnected
def _test_nack(self, version): if version not in commands.versions(VERSION): print('Skipping test case (version %s is not configured)' % VERSION) defer.returnValue(None) config = self.getConfig(version) client = async .Stomp(config) try: yield client.connect(host=VIRTUALHOST, versions=[version]) except StompProtocolError as e: print( 'Broker does not support STOMP protocol %s. Skipping this test case. [%s]' % (version, e)) defer.returnValue(None) client.subscribe(self.queue, { StompSpec.ACK_HEADER: StompSpec.ACK_CLIENT_INDIVIDUAL, StompSpec.ID_HEADER: '4711' }, listener=SubscriptionListener(self._nackFrame, ack=False)) client.send(self.queue, self.frame) while not self.framesHandled: yield task.deferLater(reactor, 0.01, lambda: None) client.disconnect() yield client.disconnected if BROKER == 'activemq': print( 'Broker %s by default does not redeliver messages. Will not try and harvest the NACKed message.' % BROKER) return self.framesHandled = 0 yield client.connect(host=VIRTUALHOST) client.subscribe(self.queue, { StompSpec.ACK_HEADER: StompSpec.ACK_CLIENT_INDIVIDUAL, StompSpec.ID_HEADER: '4711' }, listener=SubscriptionListener(self._eatFrame, ack=True)) while self.framesHandled != 1: yield task.deferLater(reactor, 0.01, lambda: None) client.disconnect() yield client.disconnected
def test_transaction_abort(self): config = self.getConfig(StompSpec.VERSION_1_0) client = async .Stomp(config) client.add(ReceiptListener()) yield client.connect(host=VIRTUALHOST) client.subscribe(self.queue, { StompSpec.ACK_HEADER: StompSpec.ACK_CLIENT_INDIVIDUAL, StompSpec.ID_HEADER: '4711' }, listener=SubscriptionListener(self._eatFrame, ack=True)) transaction = '4711' yield client.begin(transaction, receipt='%s-begin' % transaction) client.send(self.queue, 'test message with transaction', {StompSpec.TRANSACTION_HEADER: transaction}) yield task.deferLater(reactor, 0.1, lambda: None) client.send(self.queue, 'test message without transaction') while self.framesHandled != 1: yield task.deferLater(reactor, 0.01, lambda: None) self.assertEquals(self.consumedFrame.body, 'test message without transaction') yield client.abort(transaction, receipt='%s-commit' % transaction) client.disconnect() yield client.disconnected self.assertEquals(self.framesHandled, 1)
def run(self): """ Run method to start listening """ client = yield async .Stomp(self.stompConfig).connect() headers = { # client-individual mode is necessary for concurrent processing # (requires ActiveMQ >= 5.2) StompSpec.ACK_HEADER: StompSpec.ACK_CLIENT_INDIVIDUAL, # the maximal number of messages the broker will let you work on at the same time 'activemq.prefetchSize': '1', } if self.config.heartbeat_ping not in self.config.queues: self.config.queues.append(self.config.heartbeat_ping) for q in self.config.queues: headers[StompSpec.ID_HEADER] = 'post-proc-service-%s' % q client.subscribe( q, headers, listener=SubscriptionListener( self.consume, ack=False, errorDestination=self.config.postprocess_error)) try: client = yield client.disconnected except: logging.error("Connection error: %s" % sys.exc_value) reactor.callLater(5, self.run)
def test_disconnect_connection_lost_unexpectedly(self): port = self.connections[0].getHost().port config = StompConfig(uri='tcp://localhost:%d' % port, version='1.1') client = Stomp(config) yield client.connect() self._got_message = defer.Deferred() client.subscribe('/queue/bla', headers={StompSpec.ID_HEADER: 4711}, listener=SubscriptionListener( self._on_message, ack=False)) # we're acking the frames ourselves yield self._got_message disconnected = client.disconnected client.send('/queue/fake', b'shutdown') # tell the broker to drop the connection try: yield disconnected except StompConnectionError: pass else: raise Exception( 'Expected unexpected loss of connection, but disconnect went gracefully.' ) self.wait.callback(None)
def test_replay(self): config = self.getConfig(StompSpec.VERSION_1_0) client = async .Stomp(config) client = yield client.connect(host=VIRTUALHOST) client.subscribe( self.queue, {StompSpec.ACK_HEADER: StompSpec.ACK_CLIENT_INDIVIDUAL}, listener=SubscriptionListener(self._eatFrame)) client.send(self.queue, self.frame) while self.framesHandled != 1: yield task.deferLater(reactor, 0.01, lambda: None) client._protocol.loseConnection() try: yield client.disconnected except StompConnectionError: pass client = yield client.connect(host=VIRTUALHOST) client.send(self.queue, self.frame) while self.framesHandled != 2: yield task.deferLater(reactor, 0.01, lambda: None) yield client.disconnect(failure=RuntimeError('Hi')) try: yield client.disconnected except RuntimeError as e: self.assertEquals(str(e), 'Hi') client = yield client.connect(host=VIRTUALHOST) client.send(self.queue, self.frame) while self.framesHandled != 2: yield task.deferLater(reactor, 0.01, lambda: None) client.disconnect() yield client.disconnected
def test_replay_after_failover(self): ports = tuple(c.getHost().port for c in self.connections) config = StompConfig( uri= 'failover:(tcp://localhost:%d)?startupMaxReconnectAttempts=0,initialReconnectDelay=0,maxReconnectAttempts=1' % ports) client = Stomp(config) queue = '/queue/bla' try: client.subscribe( queue, listener=SubscriptionListener(self._on_message) ) # client is not connected, so it won't accept subscriptions except StompConnectionError: pass else: raise Exception('Unexpected successful subscribe.') self.assertEquals(client.session._subscriptions, {}) # check that no subscriptions have been accepted yield client.connect() self.shutdown = True # the callback handler will kill the broker connection ... client.subscribe(queue, listener=SubscriptionListener(self._on_message)) try: yield client.disconnected # the callback handler has killed the broker connection except StompConnectionError: pass else: raise Exception('Unexpected clean disconnect.') self.shutdown = False # the callback handler will not kill the broker connection, but callback self._got_message self._got_message = defer.Deferred() yield client.connect() self.assertNotEquals(client.session._subscriptions, []) # the subscriptions have been replayed ... result = yield self._got_message self.assertEquals(result, None) # ... and the message comes back yield client.disconnect() yield client.disconnected self.assertEquals( list(client.session.replay()), []) # after a clean disconnect, the subscriptions are forgotten.
def run(self): config = StompConfig('tcp://%s:%d' % (host, port), login=user, passcode=password, version='1.1') client = Stomp(config) yield client.connect(host='mybroker') self.count = 0 self.start = time.time() client.subscribe(destination='atcg', listener=SubscriptionListener(self.handleFrame), headers={'ack': 'auto', 'id': 'required-for-STOMP-1.1'})
def test_onhandlerException_disconnect(self): config = self.getConfig(StompSpec.VERSION_1_0) client = async .Stomp(config) yield client.connect(host=VIRTUALHOST) client.send(self.queue, self.frame1, self.msg1Hdrs) disconnecting = client.disconnect() yield client.disconnect() yield disconnecting yield client.disconnected yield client.connect(host=VIRTUALHOST) # barf on first frame (implicit disconnect) client.subscribe( self.queue, {StompSpec.ACK_HEADER: StompSpec.ACK_CLIENT_INDIVIDUAL}, listener=SubscriptionListener( self._saveFrameAndBarf, ack=False, onMessageFailed=self. _onMessageFailedSendToErrorDestinationAndRaise)) # client disconnected and returned error try: yield client.disconnected except StompestTestError: pass else: raise # reconnect and subscribe again - consuming retried message and disconnecting client = async .Stomp( config ) # take a fresh client to prevent replay (we were disconnected by an error) client = yield client.connect(host=VIRTUALHOST) client.subscribe( self.queue, {StompSpec.ACK_HEADER: StompSpec.ACK_CLIENT_INDIVIDUAL}, listener=SubscriptionListener(self._eatOneFrameAndDisconnect)) # client disconnects without error yield client.disconnected # verify that message was retried self.assertEquals(self.frame1, self.unhandledFrame.body) self.assertEquals(self.frame1, self.consumedFrame.body)
def subscribe(self, id): self.stomp.subscribe('/topic/' + id, { 'id': id, 'persistent': 'true', 'browser': 'true', 'browser-end': 'false', 'include-seq': 'seq', 'from-seq': 0, 'ack': 'client' }, listener=SubscriptionListener(self.sync_consumer)) hb_id = id + '__heartbeat' self.stomp.subscribe('/queue/' + hb_id, headers={ 'id': hb_id, 'ack': 'client' }, listener=SubscriptionListener( self.heartbeat_consumer))
def run(self): client = yield Stomp(self.CONFIG[RequestProcessor.CONFIG]).connect() headers = { # client-individual mode is necessary for concurrent processing (requires ActiveMQ >= 5.2) StompSpec.ACK_HEADER: StompSpec.ACK_CLIENT_INDIVIDUAL, # the maximal number of messages the broker will let you work on at the same time 'activemq.prefetchSize': '100', } client.subscribe(self.CONFIG[RequestProcessor.NAME], headers, listener=SubscriptionListener(self.consume)) client.add(listener=self)
def run(self): client = Stomp(self.config) yield client.connect() headers = { StompSpec.ACK_HEADER: StompSpec.ACK_CLIENT_INDIVIDUAL, 'activemq.prefetchSize': '500' } client.subscribe(self.QUEUE_IN, headers, listener=SubscriptionListener( self.add, errorDestination=self.QUEUE_OUT))
def run(self): client = Stomp(self.config) yield client.connect() headers = { # client-individual mode is necessary for concurrent processing # (requires ActiveMQ >= 5.2) StompSpec.ACK_HEADER: StompSpec.ACK_CLIENT_INDIVIDUAL, # the maximal number of messages the broker will let you work on at the same time 'activemq.prefetchSize': '100', } client.subscribe(self.QUEUE, headers, listener=SubscriptionListener(self.consume, errorDestination=self.ERROR_QUEUE))
def process_message(self): yield self.stomp.connect(host=stompconf.BROKER_NAME) yield self.stomp.subscribe(stompconf.SYNC_QUEUE, headers={ 'id': 'sync_daemon_channel', 'ack': 'client' }, listener=SubscriptionListener( self.controller_consumer)) for id in self.sync_dict: sid = 'sync__' + id self.subscribe(sid)
def test_onhandlerException_ackMessage_filterReservedHdrs_send2ErrorQ_and_no_disconnect( self): config = self.getConfig(StompSpec.VERSION_1_0) client = async .Stomp(config) # connect yield client.connect(host=VIRTUALHOST) # enqueue two messages client.send(self.queue, self.frame1, self.msg1Hdrs) client.send(self.queue, self.frame2) client.disconnect() yield client.disconnected yield client.connect(host=VIRTUALHOST) # barf on first frame, disconnect on second frame client.subscribe( self.queue, {StompSpec.ACK_HEADER: StompSpec.ACK_CLIENT_INDIVIDUAL}, listener=SubscriptionListener(self._barfOneEatOneAndDisonnect, errorDestination=self.errorQueue)) # client disconnects without error yield client.disconnected # reconnect and subscribe to error queue client = yield client.connect(host=VIRTUALHOST) client.subscribe( self.errorQueue, {StompSpec.ACK_HEADER: StompSpec.ACK_CLIENT_INDIVIDUAL}, listener=SubscriptionListener(self._saveErrorFrameAndDisconnect)) # wait for disconnect yield client.disconnected # verify that one message was in error queue (can't guarantee order) self.assertNotEquals(None, self.errorQueueFrame) self.assertTrue(self.errorQueueFrame.body in (self.frame1, self.frame2))
def runrealtime(self): try: client = Stomp(CONFIG) yield client.connect() headers = { StompSpec.ACK_HEADER: StompSpec.ACK_AUTO, 'activemq.prefetchSize': '100', } client.subscribe(QUEUE, headers, listener=SubscriptionListener(self.consume)) except Exception, e: log.error(e) log.error(traceback.format_exc())
def test_multi_subscriptions(self): port = self.connections[0].getHost().port config = StompConfig(uri='tcp://localhost:%d' % port) client = Stomp(config) yield client.connect() listeners = [] for j in range(2): listener = SubscriptionListener(self._on_message) yield client.subscribe('/queue/%d' % j, headers={'bla': j}, listener=listener) listeners.append(listener) for (j, listener) in enumerate(listeners): self.assertEquals(listener._headers['bla'], j) yield client.disconnect() yield client.disconnected
def test_disconnect_timeout(self): port = self.connections[0].getHost().port config = StompConfig(uri='tcp://localhost:%d' % port, version='1.1') client = Stomp(config) yield client.connect() self._got_message = defer.Deferred() client.subscribe('/queue/bla', headers={StompSpec.ID_HEADER: 4711}, listener=SubscriptionListener( self._on_message, ack=False)) # we're acking the frames ourselves yield self._got_message yield client.disconnect(timeout=0.02) try: yield client.disconnected except StompCancelledError: pass else: raise self.wait.callback(None)
def _test_onhandlerException_ackMessage_filterReservedHdrs_send2ErrorQ_and_disconnect(self, version): if version not in commands.versions(VERSION): print 'Skipping test case (version %s is not configured)' % VERSION defer.returnValue(None) if BROKER == 'rabbitmq': print 'RabbitMQ does not support selector header' defer.returnValue(None) config = self.getConfig(version) client = async.Stomp(config) try: yield client.connect(host=VIRTUALHOST, versions=[version]) except StompProtocolError as e: print 'Broker does not support STOMP protocol %s. Skipping this test case. [%s]' % (e, version) defer.returnValue(None) # enqueue two messages messageHeaders = dict(self.msg1Hdrs) defaultHeaders = {StompSpec.ACK_HEADER: StompSpec.ACK_CLIENT_INDIVIDUAL} specialCharactersHeader = u'fen\xeatre:\r\n' if version != StompSpec.VERSION_1_0: defaultHeaders.update(self.headers) messageHeaders[specialCharactersHeader] = u'\xbfqu\xe9 tal?, s\xfc\xdf' client.send(self.queue, self.frame1, messageHeaders) client.send(self.queue, self.frame2) client.disconnect() yield client.disconnected # barf on first message so it will get put in error queue # use selector to guarantee message order (order not necessarily guaranteed) headers = {StompSpec.SELECTOR_HEADER: u"food='barf'"} headers.update(defaultHeaders) yield client.connect(host=VIRTUALHOST, versions=[version]) client.subscribe(self.queue, headers, listener=SubscriptionListener(self._saveFrameAndBarf, errorDestination=self.errorQueue, onMessageFailed=self._onMessageFailedSendToErrorDestinationAndRaise)) # client disconnected and returned error try: yield client.disconnected except StompestTestError: pass else: raise client = async.Stomp(config) # take a fresh client to prevent replay (we were disconnected by an error) # reconnect and subscribe again - consuming second message then disconnecting client = yield client.connect(host=VIRTUALHOST) headers.pop('selector') client.subscribe(self.queue, headers, listener=SubscriptionListener(self._eatOneFrameAndDisconnect, errorDestination=self.errorQueue)) # client disconnects without error yield client.disconnected # reconnect and subscribe to error queue client = yield client.connect(host=VIRTUALHOST) client.subscribe(self.errorQueue, defaultHeaders, listener=SubscriptionListener(self._saveErrorFrameAndDisconnect)) # wait for disconnect yield client.disconnected # verify that first message was in error queue self.assertEquals(self.frame1, self.errorQueueFrame.body) self.assertEquals(messageHeaders[u'food'], self.errorQueueFrame.headers['food']) if version != StompSpec.VERSION_1_0: self.assertEquals(messageHeaders[specialCharactersHeader], self.errorQueueFrame.headers[specialCharactersHeader]) self.assertNotEquals(self.unhandledFrame.headers[StompSpec.MESSAGE_ID_HEADER], self.errorQueueFrame.headers[StompSpec.MESSAGE_ID_HEADER]) # verify that second message was consumed self.assertEquals(self.frame2, self.consumedFrame.body)
def run(self): yield self.client.connect(host='mybroker') self.client.subscribe(self.queue_config['activemq']['ingress'], listener=SubscriptionListener(self.handle_frame), headers={'ack': 'auto', 'id': 'required-for-STOMP-1.1'})
def run(self): client = Stomp(StompConfig('tcp://'+config['queue.host']+':'+str(config['queue.port'])+'?startupMaxReconnectAttempts=-1,initialReconnectDelay=1000,maxReconnectAttempts=-1')) yield client.connect() headers = { StompSpec.ACK_HEADER: StompSpec.ACK_CLIENT_INDIVIDUAL } client.subscribe('/queue/Comets', headers, listener = SubscriptionListener(self.consume, errorDestination = '/queue/error'))