本文整理汇总了Python中stompest.sync.Stomp.close方法的典型用法代码示例。如果您正苦于以下问题:Python Stomp.close方法的具体用法?Python Stomp.close怎么用?Python Stomp.close使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类stompest.sync.Stomp
的用法示例。
在下文中一共展示了Stomp.close方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_4_integration_stomp_1_1
# 需要导入模块: from stompest.sync import Stomp [as 别名]
# 或者: from stompest.sync.Stomp import close [as 别名]
def test_4_integration_stomp_1_1(self):
if StompSpec.VERSION_1_1 not in commands.versions(VERSION):
print 'This broker does not support STOMP protocol version 1.1'
return
client = Stomp(self.getConfig(StompSpec.VERSION_1_1))
client.connect(host=VIRTUALHOST)
client.send(self.DESTINATION, 'test message 1')
client.send(self.DESTINATION, 'test message 2')
self.assertFalse(client.canRead(self.TIMEOUT))
token = client.subscribe(self.DESTINATION, {StompSpec.ID_HEADER: 4711, StompSpec.ACK_HEADER: 'client-individual'})
self.assertTrue(client.canRead(self.TIMEOUT))
client.ack(client.receiveFrame())
self.assertTrue(client.canRead(self.TIMEOUT))
client.ack(client.receiveFrame())
self.assertFalse(client.canRead(self.TIMEOUT))
client.unsubscribe(token)
client.send(self.DESTINATION, 'test message 3', receipt='4711')
self.assertTrue(client.canRead(self.TIMEOUT))
self.assertEquals(client.receiveFrame(), StompFrame(StompSpec.RECEIPT, {'receipt-id': '4711'}))
self.assertFalse(client.canRead(self.TIMEOUT))
client.subscribe(self.DESTINATION, {StompSpec.ID_HEADER: 4711, StompSpec.ACK_HEADER: 'client-individual'})
self.assertTrue(client.canRead(self.TIMEOUT))
client.ack(client.receiveFrame())
self.assertFalse(client.canRead(self.TIMEOUT))
client.disconnect(receipt='4712')
self.assertEquals(client.receiveFrame(), StompFrame(StompSpec.RECEIPT, {'receipt-id': '4712'}))
self.assertRaises(StompConnectionError, client.receiveFrame)
client.connect(host=VIRTUALHOST)
client.disconnect(receipt='4711')
self.assertEquals(client.receiveFrame(), StompFrame(StompSpec.RECEIPT, {'receipt-id': '4711'}))
client.close()
self.assertRaises(StompConnectionError, client.canRead, 0)
示例2: _test_4_integration_stomp
# 需要导入模块: from stompest.sync import Stomp [as 别名]
# 或者: from stompest.sync.Stomp import close [as 别名]
def _test_4_integration_stomp(self, version):
client = Stomp(self.getConfig(version))
try:
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))
return
client.send(self.DESTINATION, b'test message 1')
client.send(self.DESTINATION, b'test message 2')
self.assertFalse(client.canRead(self.TIMEOUT))
token = client.subscribe(self.DESTINATION, {StompSpec.ID_HEADER: 4711, StompSpec.ACK_HEADER: StompSpec.ACK_CLIENT_INDIVIDUAL})
self.assertTrue(client.canRead(self.TIMEOUT))
client.ack(client.receiveFrame())
self.assertTrue(client.canRead(self.TIMEOUT))
client.ack(client.receiveFrame())
self.assertFalse(client.canRead(self.TIMEOUT))
client.unsubscribe(token)
client.send(self.DESTINATION, b'test message 3', receipt='4711')
self.assertTrue(client.canRead(self.TIMEOUT))
self.assertEqual(client.receiveFrame(), StompFrame(StompSpec.RECEIPT, {StompSpec.RECEIPT_ID_HEADER: '4711'}))
self.assertFalse(client.canRead(self.TIMEOUT))
client.subscribe(self.DESTINATION, {StompSpec.ID_HEADER: 4711, StompSpec.ACK_HEADER: StompSpec.ACK_CLIENT_INDIVIDUAL})
self.assertTrue(client.canRead(self.TIMEOUT))
client.ack(client.receiveFrame())
self.assertFalse(client.canRead(self.TIMEOUT))
client.disconnect(receipt='4712')
self.assertEqual(client.receiveFrame(), StompFrame(StompSpec.RECEIPT, {StompSpec.RECEIPT_ID_HEADER: '4712'}))
self.assertRaises(StompConnectionError, client.receiveFrame)
client.connect(host=VIRTUALHOST)
client.disconnect(receipt='4711')
self.assertEqual(client.receiveFrame(), StompFrame(StompSpec.RECEIPT, {StompSpec.RECEIPT_ID_HEADER: '4711'}))
client.close()
self.assertRaises(StompConnectionError, client.canRead, 0)
示例3: main
# 需要导入模块: from stompest.sync import Stomp [as 别名]
# 或者: from stompest.sync.Stomp import close [as 别名]
def main():
logging.basicConfig()
logging.getLogger().setLevel(logging.WARN)
client = Stomp(stomp_config)
client.connect()
client.send(stomp_queue, body=stomp_body)
client.subscribe(stomp_queue, {StompSpec.ACK_HEADER: StompSpec.ACK_CLIENT, 'activemq.prefetchSize': 1})
if client.canRead(timeout=5):
frame = client.receiveFrame()
print 'Got %s' % frame.info()
client.ack(frame)
frame_body = str(frame.body)
if frame_body == stomp_body:
print "OK: Message received"
status = 'ok'
else:
print "WARNING: Incorrect message body; is %s, should be %s" % (frame_body, stomp_body)
status = 'warning'
else:
print "CRITICAL: Timed out while trying to collect the message"
status = 'critical'
client.disconnect()
client.close(flush=True)
return exit_codes[status]
示例4: test_5_integration_stomp_1_1_heartbeat
# 需要导入模块: from stompest.sync import Stomp [as 别名]
# 或者: from stompest.sync.Stomp import close [as 别名]
def test_5_integration_stomp_1_1_heartbeat(self):
version = StompSpec.VERSION_1_1
if BROKER == 'apollo':
print "Broker %s doesn't properly support heart-beating. Skipping test." % BROKER
return
port = 61612 if (BROKER == 'activemq') else PORT # stomp+nio on 61613 does not work properly, so use stomp on 61612
client = Stomp(self.getConfig(StompSpec.VERSION_1_1, port))
self.assertEquals(client.lastReceived, None)
self.assertEquals(client.lastSent, None)
heartBeatPeriod = 100
try:
client.connect(host=VIRTUALHOST, heartBeats=(heartBeatPeriod, heartBeatPeriod), versions=[version])
except StompProtocolError as e:
print 'Broker does not support STOMP protocol %s. Skipping this test case. [%s]' % (e, version)
return
self.assertTrue((time.time() - client.lastReceived) < 0.1)
if not (client.serverHeartBeat and client.clientHeartBeat):
print 'broker does not support heart-beating. disconnecting ...'
client.disconnect()
client.close()
return
serverHeartBeatInSeconds = client.serverHeartBeat / 1000.0
clientHeartBeatInSeconds = client.clientHeartBeat / 1000.0
start = time.time()
while (time.time() - start) < (2.5 * max(serverHeartBeatInSeconds, clientHeartBeatInSeconds)):
time.sleep(0.5 * min(serverHeartBeatInSeconds, clientHeartBeatInSeconds))
client.canRead(0)
self.assertTrue((time.time() - client.lastReceived) < (2.0 * serverHeartBeatInSeconds))
if (time.time() - client.lastSent) > (0.5 * clientHeartBeatInSeconds):
client.beat()
self.assertTrue((time.time() - client.lastSent) < 0.1)
start = time.time()
try:
while not client.canRead(0.5 * clientHeartBeatInSeconds):
pass
except StompConnectionError:
self.assertTrue((time.time() - start) < (3.0 * clientHeartBeatInSeconds))
self.assertTrue((time.time() - client.lastReceived) < (2.0 * serverHeartBeatInSeconds))
self.assertTrue((time.time() - client.lastSent) > clientHeartBeatInSeconds)
else:
raise
client.close()
示例5: test_5_integration_stomp_1_1_heartbeat
# 需要导入模块: from stompest.sync import Stomp [as 别名]
# 或者: from stompest.sync.Stomp import close [as 别名]
def test_5_integration_stomp_1_1_heartbeat(self):
version = StompSpec.VERSION_1_1
client = Stomp(self.getConfig(StompSpec.VERSION_1_1))
self.assertEqual(client.lastReceived, None)
self.assertEqual(client.lastSent, None)
heartBeatPeriod = 100
try:
client.connect(host=VIRTUALHOST, heartBeats=(heartBeatPeriod, heartBeatPeriod), versions=[version])
except StompProtocolError as e:
print('Broker does not support STOMP protocol %s. Skipping this test case. [%s]' % (e, version))
return
self.assertTrue((time.time() - client.lastReceived) < 0.1)
if not (client.serverHeartBeat and client.clientHeartBeat):
print('broker does not support heart-beating. disconnecting ...')
client.disconnect()
client.close()
return
serverHeartBeatInSeconds = client.serverHeartBeat / 1000.0
clientHeartBeatInSeconds = client.clientHeartBeat / 1000.0
start = time.time()
while (time.time() - start) < (2.5 * max(serverHeartBeatInSeconds, clientHeartBeatInSeconds)):
time.sleep(0.5 * min(serverHeartBeatInSeconds, clientHeartBeatInSeconds))
client.canRead(0)
self.assertTrue((time.time() - client.lastReceived) < (2.0 * serverHeartBeatInSeconds))
if (time.time() - client.lastSent) > (0.5 * clientHeartBeatInSeconds):
client.beat()
self.assertTrue((time.time() - client.lastSent) < 0.1)
start = time.time()
try:
while not client.canRead(0.5 * clientHeartBeatInSeconds):
pass
if client.receiveFrame().command == StompSpec.ERROR:
raise StompProtocolError()
except (StompConnectionError, StompProtocolError):
self.assertTrue((time.time() - start) < (3.0 * clientHeartBeatInSeconds))
self.assertTrue((time.time() - client.lastReceived) < (2.0 * serverHeartBeatInSeconds))
self.assertTrue((time.time() - client.lastSent) > clientHeartBeatInSeconds)
else:
raise
client.close()
示例6: int
# 需要导入模块: from stompest.sync import Stomp [as 别名]
# 或者: from stompest.sync.Stomp import close [as 别名]
from stompest.config import StompConfig
from stompest.sync import Stomp
user = os.getenv('APOLLO_USER') or 'admin'
password = os.getenv('APOLLO_PASSWORD') or 'password'
host = os.getenv('APOLLO_HOST') or 'localhost'
port = int(os.getenv('APOLLO_PORT') or 61613)
destination = sys.argv[1:2] or ['/topic/event']
destination = destination[0]
messages = 10000
data = 'Hello World from Python'
config = StompConfig('tcp://%s:%d' % (host, port), login=user, passcode=password, version='1.1')
client = Stomp(config)
client.connect(host='mybroker')
count = 0
start = time.time()
for _ in xrange(messages):
client.send(destination=destination, body=data, headers={'persistent': 'false'})
count += 1
diff = time.time() - start
print 'Sent %s frames in %f seconds' % (count, diff)
client.disconnect(receipt='bye')
client.receiveFrame()
client.close()
示例7: StompClient
# 需要导入模块: from stompest.sync import Stomp [as 别名]
# 或者: from stompest.sync.Stomp import close [as 别名]
class StompClient(BaseComponent):
""" Send and Receive messages from a STOMP queue """
channel = "stomp"
def init(self, host, port, username=None, password=None,
connect_timeout=3, connected_timeout=3,
version=StompSpec.VERSION_1_2, accept_versions=["1.0", "1.1", "1.2"],
heartbeats=(0, 0), ssl_context=None,
use_ssl=True,
key_file=None,
cert_file=None,
ca_certs=None,
ssl_version=ssl.PROTOCOL_SSLv23,
key_file_password=None,
proxy_host=None,
proxy_port=None,
proxy_user=None,
proxy_password=None,
channel=channel):
""" Initialize StompClient. Called after __init__ """
self.channel = channel
if proxy_host:
LOG.info("Connect to %s:%s through proxy %s:%d", host, port, proxy_host, proxy_port)
else:
LOG.info("Connect to %s:%s", host, port)
if use_ssl and not ssl_context:
ssl_params = dict(key_file=key_file,
cert_file=cert_file,
ca_certs=ca_certs,
ssl_version=ssl_version,
password=key_file_password)
LOG.info("Request to use old-style socket wrapper: %s", ssl_params)
ssl_context = ssl_params
if use_ssl:
uri = "ssl://%s:%s" % (host, port)
else:
uri = "tcp://%s:%s" % (host, port)
# Configure failover options so it only tries to connect once
self._stomp_server = "failover:(%s)?maxReconnectAttempts=1,startupMaxReconnectAttempts=1" % uri
self._stomp_config = StompConfig(uri=self._stomp_server, sslContext=ssl_context,
version=version,
login=username,
passcode=password)
self._heartbeats = heartbeats
self._accept_versions = accept_versions
self._connect_timeout = connect_timeout
self._connected_timeout = connected_timeout
Stomp._transportFactory = EnhancedStompFrameTransport
Stomp._transportFactory.proxy_host = proxy_host
Stomp._transportFactory.proxy_port = proxy_port
Stomp._transportFactory.proxy_user = proxy_user
Stomp._transportFactory.proxy_password = proxy_password
self._client = Stomp(self._stomp_config)
self._subscribed = {}
self.server_heartbeat = None
self.client_heartbeat = None
self.ALLOWANCE = 2 # multiplier for heartbeat timeouts
@property
def connected(self):
if self._client.session:
return self._client.session.state == StompSession.CONNECTED
else:
return False
@property
def subscribed(self):
return self._subscribed.keys()
@property
def stomp_logger(self):
return LOG_CATEGORY
@handler("disconnect")
def _disconnect(self, receipt=None):
if self.connected:
self._client.disconnect(receipt=receipt)
self._client.close(flush=True)
self.fire(disconnected(reconnect=False))
self._subscribed = {}
return "disconnected"
def start_heartbeats(self):
LOG.info("Client HB: %s Server HB: %s", self._client.clientHeartBeat, self._client.serverHeartBeat)
if self._client.clientHeartBeat:
if self.client_heartbeat:
# Timer already exists, just reset it
self.client_heartbeat.reset()
else:
LOG.info("Client will send heartbeats to server")
# Send heartbeats at 80% of agreed rate
self.client_heartbeat = Timer((self._client.clientHeartBeat / 1000.0) * 0.8,
client_heartbeat(), persist=True)
self.client_heartbeat.register(self)
#.........这里部分代码省略.........
示例8: str
# 需要导入模块: from stompest.sync import Stomp [as 别名]
# 或者: from stompest.sync.Stomp import close [as 别名]
client.send(destination=destination, body="scai.fhg.de/voc/ccg", headers=headers)
for i in queries:
client.send(destination=destination, body="scai.fhg.de/concept/" + str(i), headers=headers)
print u"# ErasmusMC ontology file".encode('utf-8')
print u"VR 0.0".encode('utf-8')
print u"ON CCG_Ontology".encode('utf-8')
print u"--".encode('utf-8')
@timeout(1)
def receiveFrames():
frame = clientConsumer.receiveFrame()
xml = ElementTree.fromstring(frame.body)
modelText = xml.find('docelem').find('model').text
if (modelText is not None):
print unicode(modelText).encode('utf-8')
print u"--".encode('utf-8')
for reply in queries:
receiveFrames()
client.disconnect(receipt='bye')
client.receiveFrame()
client.close()
clientConsumer.disconnect(receipt='bye')
clientConsumer.receiveFrame()
clientConsumer.close()
示例9: JMSClient
# 需要导入模块: from stompest.sync import Stomp [as 别名]
# 或者: from stompest.sync.Stomp import close [as 别名]
#.........这里部分代码省略.........
self._mh.demsg('htk_on_debug_info', self._mh._trn.msg(
'htk_jms_connected'), self._mh.fromhere())
ev = event.Event('jms_after_connect')
self._mh.fire_event(ev)
return True
except StompError as ex:
self._mh.demsg('htk_on_error', ex, self._mh.fromhere())
return False
def disconnect(self):
"""Method disconnects from server
Args:
none
Returns:
bool: result
"""
try:
self._mh.demsg('htk_on_debug_info', self._mh._trn.msg(
'htk_jms_disconnecting'), self._mh.fromhere())
if (not self._is_connected):
self._mh.demsg('htk_on_warning', self._mh._trn.msg(
'htk_jms_not_connected'), self._mh.fromhere())
return False
else:
self._client.disconnect()
self._client.close()
self._is_connected = False
self._mh.demsg('htk_on_debug_info', self._mh._trn.msg(
'htk_jms_disconnected'), self._mh.fromhere())
return True
except StompError as ex:
self._mh.demsg('htk_on_error', ex, self._mh.fromhere())
return False
def send(self, destination_name, message, destination_type='queue', headers={}):
"""Method sends message
JMS headers - JMSCorrelationID, JMSExpiration, JMSDeliveryMode, JMSPriority,
JMSReplyTo, JMSType
Args:
destination_name (str): queue|topic name
message (str): message
destination_type (str): queue|topic
headers (dict): JMS headers, key - title, value - string
Returns:
bool: result
Raises:
event: jms_before_send
event: jms_after_send
"""
try: