class MQClient(threading.Thread): def __init__(self,host,port,user,password,client_id=None): threading.Thread.__init__(self) self.logger_service = LoggerService(logging.INFO) self.host = host self.user = user self.password = password self.port =port self.mqscoket = MQSocket() if client_id: self.client_id =client_id def __init_connect(self): self.logger_service.info('INIT SOCKET CONNECT ,host->%s,port->%s'%(self.host,str(self.port))) self.mqscoket.connect(self.host, self.port) if self.client_id: connect_frame = ConnectFrame(self.host,self.user,self.password,self.client_id) else: connect_frame = ConnectFrame(self.host,self.user,self.password) self.mqscoket.send_message(connect_frame) data=self.mqscoket.receive_message(MQSocket.MESSAGE_MAX_LENGTH) message_frame = MessageFrame(data) if message_frame.get_command()!='ERROR': self.logger_service.info('CONNECT TO ACTIVEMQ SUCCESSFULLY ') else: self.logger_service.error(data) def subscribe(self,destination,call_back,subscribe_name=None): self.destination = destination self.call_back = call_back self.subscribe_name =subscribe_name def should_run_func(): subscribe_frame = SubscribeFrame(self.destination,subscribe_name=self.subscribe_name) self.mqscoket.send_message(subscribe_frame) running = True while running: data = self.mqscoket.receive_message(MQSocket.MESSAGE_MAX_LENGTH) message_frame = MessageFrame(data) self.logger_service.info(message_frame.get_command()) if message_frame.get_command()!='ERROR': self.call_back(message_frame.get_body(),message_frame.get_header()) else: self.logger_service.error(data) self.should_run_func = should_run_func def send(self,body,destination): send_frame = SendFrame(destination,body) self.mqscoket.send_message(send_frame) def run(self): self.__init_connect() self.should_run_func()
def __init__(self,host,port,user,password,client_id=None): threading.Thread.__init__(self) self.logger_service = LoggerService(logging.INFO) self.host = host self.user = user self.password = password self.port =port self.mqscoket = MQSocket() if client_id: self.client_id =client_id
Created on 2013-4-11 @author: Wong ''' #!/usr/bin/env python # -*- coding:utf-8 from mqclient.frame import ConnectFrame, MessageFrame, SubscribeFrame from mqclient.mqclientscoket import MQSocket import socket if __name__=='__main__': print 'RECEIVER' HOST = 'localhost' # The remote host PORT = 61613 # The same port as used by the server mqsocket = MQSocket() mqsocket.connect(HOST, PORT) connect_frame = ConnectFrame(HOST,login='******',password='******') mqsocket.send_message(connect_frame) data = mqsocket.receive_message(MQSocket.MESSAGE_MAX_LENGTH) message_frame = MessageFrame(data) print message_frame.get_command() header = message_frame.get_header() for key in header: print 'key:%s,value:%s'%(key,header[key]) print message_frame.get_body() subscribe_frame = SubscribeFrame('/queue/foo','tai') mqsocket.send_message(subscribe_frame) while 1: data = mqsocket.receive_message(MQSocket.MESSAGE_MAX_LENGTH) message_frame = MessageFrame(data)