Skip to content

Commit d888eef

Browse files
committed
python http增加ack示例
1 parent 45ba85e commit d888eef

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

mq-cloud/src/main/resources/static/wiki/userGuide/httpPython.md

+21-4
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,18 @@ sendMessage()
4141
# coding=utf-8
4242
import requests
4343
import time
44+
import signal
45+
46+
# 是否运行变量
47+
running = True
4448
4549
def httpConsume():
4650
# 定义消费消息的参数
4751
payload = {'topic': 'mqcloud-http-test-topic', 'consumer': 'clustering-mqcloud-http-consumer'}
48-
while True:
52+
while running:
4953
try:
5054
# 拉取消息
51-
response = requests.post('http://${httpConsumerUriPrefix}/mq/message', data=payload)
55+
response = requests.get('http://${httpConsumerUriPrefix}/mq/message', params=payload)
5256
# 解析响应结果
5357
if response.status_code == 200:
5458
data = response.json()
@@ -74,9 +78,22 @@ def httpConsume():
7478
print('reqeust error', e)
7579
7680
# 延时一会
77-
time.sleep(2)
81+
if running:
82+
time.sleep(2)
83+
84+
# 进程退出时,提交ACK
85+
try:
86+
requests.get('http://${httpConsumerUriPrefix}/mq/ack', params=payload)
87+
except Exception as e:
88+
print('ack error', e)
89+
90+
def signal_handler(sig, frame):
91+
global running
92+
running = False
93+
94+
signal.signal(signal.SIGINT, signal_handler)
95+
signal.signal(signal.SIGTERM, signal_handler)
7896
79-
# 消费消息
8097
httpConsume()
8198
```
8299

0 commit comments

Comments
 (0)