ทำไมไม่ใช้ queue ของ python ? เนื่องด้วย ผมก็มองว่า ภาษา python เป็น ภาษาที่ใช้ในการ Integrate เป็นส่วนใหญ่ เนื่องจากแก้ไขได้ง่าย รวดเร็ว ดู code สะอาดตา แค่นี้ก็พอแล้วสำหรับ python ส่วน java นั้น จะเป็นภาษาที่มีความแข็งแรง และ เสถียรภาพค่อนข้างสูง ถ้าไม่เขียน bug เยอนะครับ
stomp คือ อะไร ? ก็คือ protocal ที่ใช้ในการติดต่อ ประเภทหนึ่ง เป็น text stream
รายละเอียดหาได้จาก http://stomp.codehaus.org/
เกริ่นพอแระ เริ่มดีกว่า
ก่อนอื่นก็ ไป download stomp.py ได้จาก
http://stomp.codehaus.org/Python
เริ่มด้วย การ send message
file name : adaptor.py
import stomp
class adaptor(object):
def __init__(self, host='localhost', port='61613', dest='test'):
print 'initial connection to queue host:', host, ', port:', port, ' for:', dest
self.dest = dest
try:
self.conn = stomp.Connection([(host, int(port))])
self.conn.start()
self.conn.connect()
except Exception, e:
raise Exception('fail connect to queue', e)
def send(self, msg=' '):
print 'send to queuename:', self.dest #,' ,message:', ms' ,message:', msg
while 1:
try:
self.conn.send(msg, destination=self.dest)
break
except Exception, e:
if isinstance(e, NotConnectedException):
self.conn = stomp.Connection([(host, int(port))])
self.conn.start()
self.conn.connect()
else:
raise Exception('fail send data to queue', e)
#defind pool queue name in qname
adappoolqname = adaptor(host='localhost', port='61613', dest='/queue/qname')
-------------------------------------ในกรณีนี้เป็นการเขียน แบบ pool คือ connection ค้างไว้ เพื่อลด header ที่เกิดขึ้นในการติดต่อแต่ละครั้ง และ bug ของ activemq เอง ที่มีปัญหาจากการติดต่อ connection หลายครั้ง ถ้าสงสัยลองทำดูก็ได้ครับ
กรณีนำไปใช้งานจริง ก็จะเป็นประมาณนี้
file name : testadaptor.py
import adaptor
def testsend():
message = 'test message to q'
adaptor.adappoolqname.send(msg=message)
if '__main__' == name:
testsend()
-------------------------------------
ไม่มีความคิดเห็น:
แสดงความคิดเห็น