Ejemplo n.º 1
0
from sample_common import MNSSampleCommon
from mns.mns_account import Account
from mns.mns_topic import *

# 从sample.cfg中读取基本配置信息
## WARNING: Please do not hard code your accessId and accesskey in next line.(more information: https://yq.aliyun.com/articles/55947)
accid, acckey, endpoint, token = MNSSampleCommon.LoadConfig()

# 初始化 my_account, my_topic
my_account = Account(endpoint, accid, acckey, token)
topic_name = MNSSampleCommon.LoadIndexParam(1)
if not topic_name:
    print("Error: get parameter failed")
    sys.exit(0)
my_topic = my_account.get_topic(topic_name)

# 循环发布多条消息
msg_count = 3
print("%sPublish Message To Topic%s\nTopicName:%s\nMessageCount:%s\n" % (
10 * "=", 10 * "=", topic_name, msg_count))

for i in range(msg_count):
    try:
        msg_body = u"I am test message %s." % i
        msg = TopicMessage(msg_body)
        re_msg = my_topic.publish_message(msg)
        print("Publish Message Succeed. MessageBody:%s MessageID:%s" % (
        msg_body, re_msg.message_id))
    except MNSExceptionBase as e:
        if e.type == "TopicNotExist":
region = sys.argv[1]
queue_name = sys.argv[2]
topic_name = sys.argv[3]
sub_name = sys.argv[4]


#从sample.cfg中读取基本配置信息
## WARNING: Please do not hard code your accessId and accesskey in next line.(more information: https://yq.aliyun.com/articles/55947)
accid,acckey,endpoint,token = MNSSampleCommon.LoadConfig()
account_id = endpoint.split("/")[2].split(".")[0]
queue_endpoint = TopicHelper.generate_queue_endpoint(region, account_id, queue_name)

#初始化 my_account, my_topic, my_sub
my_account = Account(endpoint, accid, acckey, token)
my_topic = my_account.get_topic(topic_name)

my_sub = my_topic.get_subscription(sub_name)


#创建订阅, 具体属性请参考mns/subscription.py中的SubscriptionMeta结构
sub_meta = SubscriptionMeta(queue_endpoint, notify_content_format = SubscriptionNotifyContentFormat.SIMPLIFIED)
try:
    topic_url = my_sub.subscribe(sub_meta)
    print("Create Subscription Succeed! TopicName:%s SubName:%s Endpoint:%s\n" % (topic_name, sub_name, queue_endpoint))
except MNSExceptionBase as e:
    if e.type == "TopicNotExist":
        print("Topic not exist, please create topic.")
        sys.exit(0)
    elif e.type == "SubscriptionAlreadyExist":
        print("Subscription already exist, please unsubscribe or use it directly.")
Ejemplo n.º 3
0
    my_queue.batch_delete_message(receipt_handle_list)
    sys.stdout.write("Batch Delete Message Succeed.\n")
except MNSExceptionBase as e:
    sys.stderr.write("Batch Delete Message Fail!\nException:%s\n\n" % e)
    sys.exit(1)

#删除队列
try:
    my_queue.delete()
    sys.stdout.write("Delete Queue Succeed!\n\n")
except MNSExceptionBase as e:
    sys.stderr.write("Delete Queue Fail!\nException:%s\n\n" % e)
    sys.exit(1)

##############Topic 相关操作#####################
my_topic = my_account.get_topic(
    "MyTopic-%s" % time.strftime("%y%m%d-%H%M%S", time.localtime()))

#创建主题
## maximum_message_size 消息的最大长度      10240Byte
topic_meta = TopicMeta()
topic_meta.set_maximum_message_size(10240)
try:
    topic_url = my_topic.create(topic_meta)
    sys.stdout.write("Create Topic Succeed!\nTopicURL:%s\n\n" % topic_url)
except MNSExceptionBase as e:
    sys.stderr.write("Create Topic Fail!\nException:%s\n\n" % e)
    sys.exit(1)

#修改主题属性
## maximum_message_size 消息的最大长度      6144Byte
topic_meta = TopicMeta()
Ejemplo n.º 4
0
    my_queue.batch_delete_message(receipt_handle_list)
    sys.stdout.write("Batch Delete Message Succeed.\n")
except MNSExceptionBase as e:
    sys.stderr.write("Batch Delete Message Fail!\nException:%s\n\n" % e)
    sys.exit(1)

#删除队列
try:
    my_queue.delete()
    sys.stdout.write("Delete Queue Succeed!\n\n")
except MNSExceptionBase as e:
    sys.stderr.write("Delete Queue Fail!\nException:%s\n\n" % e)
    sys.exit(1)

##############Topic 相关操作#####################
my_topic = my_account.get_topic("MyTopic-%s" % time.strftime("%y%m%d-%H%M%S", time.localtime()))

#创建主题
## maximum_message_size 消息的最大长度      10240Byte
topic_meta = TopicMeta()
topic_meta.set_maximum_message_size(10240)
try:
    topic_url = my_topic.create(topic_meta)
    sys.stdout.write("Create Topic Succeed!\nTopicURL:%s\n\n" % topic_url)
except MNSExceptionBase as e:
    sys.stderr.write("Create Topic Fail!\nException:%s\n\n" % e)
    sys.exit(1)

#修改主题属性
## maximum_message_size 消息的最大长度      6144Byte
topic_meta = TopicMeta()