def PostToSCE(responsetime): logging.info ('The Response Timei was: %s' % responsetime) stomp = Client(host='activemq.dashboard.internaldomain.com') stomp.connect() body="{'host' : '%s', 'responsetime' : %s}" %(socket.gethostname(),responsetime) stomp.put(body,"/queue/events",conf={'eventtype' : 'banner_responsetime'}) stomp.disconnect()
def PostToSCE(livecampaigns): logging.info ('Number of live Campaigns: %s' % livecampaigns) stomp = Client(host='activemq.dashboard.internaldomain.com') stomp.connect() body="{'host' : '%s', 'campaigns' : %s}" %(socket.gethostname(),livecampaigns) stomp.put(body,"/queue/events",conf={'eventtype' : 'banner_live_camp'}) stomp.disconnect()
def PostToSCE(lines): logging.info ('Number of impressions: %s' % lines) stomp = Client(host='activemq.dashboard.myinternaldomain.com') stomp.connect() body="{'host' : '%s', 'lines' : %d}" %(socket.gethostname(),lines) stomp.put(body,"/queue/events",conf={'eventtype' : 'banner_line_count'}) stomp.disconnect()
def simple_receive(): stomp = Client("192.168.1.166", 61613) stomp.connect() stomp.subscribe("/queue/hello", ack="client") while True: message = stomp.get() print message.body stomp.ack(message) stomp.unsubscribe("/queue/hello") stomp.disconnect()
def simple_receive(): stomp = Client('192.168.1.166', 61613) stomp.connect() stomp.subscribe("/queue/hello", ack="client") while True: message = stomp.get() print message.body stomp.ack(message) stomp.unsubscribe("/queue/hello") stomp.disconnect()
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 simple(): # 通过simple方式连接JMS服务器 # 指定hostname和port(tips:ActiveMQ支持多种协议连接stomp协议的默认端口为61613,这里不要写为61616) stomp = Client("192.168.1.166", 61613) # stomp = Client()#如果是ActiveMQ和ActiveMQ客户端(本程序)在同一台机器可使用默认值:hostname="localhost",port=61613 # 连接服务器 stomp.connect() # 发送消息到指定的queue stomp.put("The quick brown fox...", destination="/queue/hello") # 从指定的queue订阅消息。ack参数指定为"client",不然可能出现一个问题(具体忘了,以后补充),ack默认值为"auto" stomp.subscribe("/queue/hello", ack="client") # 等待接收ActiveMQ推送的消息 message = stomp.get() # 打印消息的主体 print message.body message.body "quick brown fox..." stomp.ack(message) # 退订 stomp.unsubscribe("/queue/hello") # 关闭连接 stomp.disconnect()
def simple(): # 通过simple方式连接JMS服务器 # 指定hostname和port(tips:ActiveMQ支持多种协议连接stomp协议的默认端口为61613,这里不要写为61616) stomp = Client('192.168.1.166', 61613) #stomp = Client()#如果是ActiveMQ和ActiveMQ客户端(本程序)在同一台机器可使用默认值:hostname="localhost",port=61613 # 连接服务器 stomp.connect() # 发送消息到指定的queue stomp.put("The quick brown fox...", destination="/queue/hello") # 从指定的queue订阅消息。ack参数指定为"client",不然可能出现一个问题(具体忘了,以后补充),ack默认值为"auto" stomp.subscribe("/queue/hello", ack="client") # 等待接收ActiveMQ推送的消息 message = stomp.get() # 打印消息的主体 print message.body message.body 'quick brown fox...' stomp.ack(message) # 退订 stomp.unsubscribe("/queue/hello") # 关闭连接 stomp.disconnect()
def runMain(): clientid = "karlnet_rrd@%s/%d" % (socket.gethostname(), os.getpid()) stomp.connect(clientid=clientid) stomp.subscribe("/topic/karlnet.>") while True: message = stomp.get() kp = jsonpickle.decode(message.body) log.info("updating RRD for: %s", kp) if (config.get(kp.node, None)): # slightly funky safe check args = "N:%f:%d" % (kp.sensors[0].value, kp.sensors[1].value) rrdtool.update(config[kp.node]["cacti_filename"], '--template', 'temp:freq', args) rrdtool.update(config[kp.node]["cgi_filename"], '--template', 'tmp36:onboard', args) else: log.info("Ignoring rrd update for non-configured node: %s", kp.node) if __name__ == "__main__": try: runMain() except KeyboardInterrupt: print "got a keyboard interrupt" log.info("QUIT - quitting due to keyboard interrupt") stomp.disconnect() raise SystemExit
from stompy.simple import Client import sys import time time.sleep(2) queue_name = "/queue/test4" if (len(sys.argv) < 2): print("Usage: send.py total") exit() total = int(sys.argv[1]) count = 0 stomp = Client() stomp.connect() while (count < total): stomp.put("{\"message\": \"addUser\", \"userid\": \"" + str(count) + "\"}", destination=queue_name, persistent=False) count = count + 1; stomp.disconnect()
class WhenUsingSimpleClient(DingusTestCase(Client, exclude=['TransactionError', 'Empty'])): def setup(self): super(WhenUsingSimpleClient, self).setup() self.client = Client() def should_connect(self): self.client.connect() assert self.client.stomp.calls('connect') def should_disconnect(self): self.client.disconnect() assert self.client.stomp.calls('disconnect') def should_subscribe(self): self.client.subscribe('/queue/nose_test') print self.client.stomp.calls assert self.client.stomp.calls('subscribe', {'ack': 'auto', 'destination': '/queue/nose_test'}) def should_unsubscribe(self): self.client.unsubscribe('/queue/nose_test') assert self.client.stomp.calls('unsubscribe', {'destination': '/queue/nose_test'}) def should_begin_transaction(self): self.client.begin('bah') assert self.client.stomp.calls('begin', {"transaction": self.client._current_transaction}) def should_fail_to_begin_already_in_transaction(self): self.client._current_transaction = "meh" nose_tools.assert_raises(TransactionError, self.client.begin, 'bah') def should_commit_transaction(self): self.client._current_transaction = 'meh' self.client.commit('bah') assert self.client.stomp.calls('commit', {'transaction': 'meh'}) def should_fail_to_commit_transaction(self): nose_tools.assert_raises(TransactionError, self.client.commit, 'bah') def should_abort_transaction(self): self.client._current_transaction = 'meh' self.client.abort() assert self.client.stomp.calls('abort', {'transaction': 'meh'}) def should_fail_to_abort_transaction(self): nose_tools.assert_raises(TransactionError, self.client.abort) def should_ack_message(self): self.client.ack("fake_frame") assert self.client.stomp.calls('ack', "fake_frame") def should_make_conf(self): conf = self.client._make_conf(None, destination='/queue/nose_test', ack='auto') assert isinstance(conf, type({})) def should_make_conf_with_transaction(self): self.client._current_transaction = 'meh' conf = self.client._make_conf({}, destination='/queue/nose_test', ack='auto') assert isinstance(conf, type({})) def should_put_item_into_queue(self): self.client.put('bah', '/queue/nose_test') conf = self.client._make_conf(None, body='bah', destination='/queue/nose_test', persistent='true') assert self.client.stomp.calls('send', conf) def should_get_message(self): self.client.get() assert self.client.stomp.calls('receive_frame', nonblocking=False, callback=None) def should_get_message_without_blocking(self): self.client.get_nowait() assert self.client.stomp.calls('receive_frame', nonblocking=True, callback=None) def should_not_get_message(self): self.client.stomp.receive_frame.return_value = None nose_tools.assert_raises(self.client.Empty, self.client.get, block=False)
config.read('trains.conf') topics = { 'tm': 'TRAIN_MVT_ALL_TOC', 'td': 'TD_ALL_SIG_AREA', 'vstp': 'VSTP_ALL', 'rtppm': 'RTPPM_ALL', 'tsr': 'TSR_ALL_ROUTE', } feedname = sys.argv[1].lower() if feedname not in topics: print 'Valid feeds:' print '\n'.join(topics.keys()) dest = '/topic/%s' % topics[feedname] feed = Client(host='datafeeds.networkrail.co.uk', port=61618) feed.connect(config.get('user', 'email'), config.get('user', 'password')) feed.subscribe(dest) print 'Subscribed' while True: message = feed.get() print message data = json.loads(message.body) pprint.pprint(data) feed.unsubscribe(dest) feed.disconnect()