属性说明
需要在发送消息里面添加时间戳,该时间戳用于标记当前消息消费的时间,参数名称为:startdelivertime。
HTTP 定时消息示例代码
下面的示例程序是用 Python 语言编写的,其它类语言只需在相应的 URL 上添加定时消费的时间戳即可。具体添加方法,请参考如下示例。
- class HttpProducer(object):
- def __init__(self):
- """签名值"""
- self.signature = "Signature"
- """生产者组ID"""
- self.producerid = "ProducerId"
- """消息主题"""
- self.topic = "topic"
- """访问码"""
- self.ak = "AccessKey"
- """配置文件解析器"""
- self.cf = ConfigParser.ConfigParser()
- """MD5对象"""
- self.md5 = hashlib.md5()
- """
- 发送Topic主流程
- """
- def process(self):
- """读取配置文件"""
- self.cf.read("user.properties")
- """读取消息主题"""
- topic = self.cf.get("property","topic")
- """存储消息URL路径"""
- url = self.cf.get("property","url")
- """访问码"""
- ak = self.cf.get("property","user_accesskey")
- """密钥"""
- sk = self.cf.get("property","user_secretkey")
- """生产者组ID"""
- pid = self.cf.get("property","producer_group")
- """HTTP请求主体内容"""
- content = U"中文".encode('utf-8')
- """分隔符"""
- newline = "\n"
- """获取URL域名地址"""
- urlname = urlparse(url).hostname
- """根据HTTP主体内容计算MD5值"""
- self.md5.update(content)
- """建立HTTP连接对象"""
- conn = httplib.HTTPConnection(parseURL(urlname))
- try:
- for index in range(0,10):
- """时间戳"""
- date = repr(int(time.time())*1000)[0:13]
- """构造签名字符串"""
- signString = str(topic + newline + pid + newline + self.md5.hexdigest() + newline + date)
- """计算签名"""
- sign = calSignature(signString,sk)
- """内容类型"""
- contentFlag ="Content-type"
- """HTTP请求头部对象"""
- headers = {
- self.signature : sign,
- self.ak : ak,
- self.producerid : pid,
- contentFlag : "text/html;charset=UTF-8"
- }
- """定时消息时间戳,5秒之后该消息开始消费"""
- timeStamp = str(int(time.time()*1000) + 5000);
- """开始发送HTTP定时消息"""
- conn.request(method="POST",url="/message/?topic="+topic+"&time="+date+"&startdelivertime="+timeStamp+"&tag=http&key=http",
- body=content,
- headers=headers)
- """获取HTTP应答消息"""
- response = conn.getresponse()
- """读取HTTP应答内容"""
- msg = response.read()
- print "response:"+msg
- except Exception,e:
- print e
- finally:
- conn.close()
- """流程入口"""
- if __name__ == '__main__':
- """创建消息生产者"""
- producer = HttpProducer()
- """开启生产者流程"""
- producer.process()