python activemq操作: consumer

跟xml的sax解析器类似, 不过最后一段不是很理想, 另外在linux下后台运行时, log不能随时打印出来, 使用时不是很方便, 不能知道程序的存活情况

需要加入更多的机制,比如心跳,出错挽救等

# coding=utf-8
"""
处理mq队列中的消息, 创建相关的记录,发送邮件给相关人进行后续工作,发送短信给注册用户
"""
import os,sys
import datetime
from django.utils.timezone import utc
import stomp
import time

from uequery.models import SmsBindTemp, TUserBasicInfo
from pysendmail import send_mail, send_msg_to_mq

user = os.getenv("ACTIVEMQ_USER") or "admin"
password = os.getenv("ACTIVEMQ_PASSWORD") or "password"
host = os.getenv("ACTIVEMQ_HOST") or "192.168.1.1"
port = os.getenv("ACTIVEMQ_PORT") or 61613
destination = sys.argv[1:2] or ["IDNO_REQUEST"]
destination = destination[0]


class MyListener(object):
    def __init__(self, conn):
        self.conn = conn


    def on_error(self, headers, message):
        print('[%s]received an error %s' % (message,time.strftime("%Y-%m-%d %H:%M:%S")))
        print('headers: ')
        print headers


    def on_message(self, headers, message):
        print "[%s]收到新的操作请求信息"%time.strftime("%Y-%m-%d %H:%M:%S")
        work_db = "default"
        # headers返回一个dict
        # message中没有内容
        user_name = headers["name"]
        user_id = headers["userId"]
        user_address = headers["address"]
        user_idNumber = headers["idNumber"] # 下面该干嘛干嘛       
conn = stomp.Connection(host_and_ports=[(host, port)]) conn.set_listener("", MyListener(conn)) conn.start() conn.connect(login=user, password=password) conn.subscribe(destination, 'pyscript') print "waiting for messages..." while 1: time.sleep(1000)
原文地址:https://www.cnblogs.com/yeyong/p/4597596.html