0

我有一个连接到 AWS MQ 并收集消息的 python 脚本。我所有的连接都完美对齐,执行结果是成功的。但是我的函数执行返回的结果是“null”。更新的错误日志:-

    {
  "errorType": "ConnectFailedException",
  "stackTrace": [
    "  File \"/var/task/one_purchasing.py\", line 21, in lambda_handler\n    conn.connect(login='test_mq', passcode='test_secure_mq',wait=True)\n",
    "  File \"/var/task/stomp/connect.py\", line 164, in connect\n    Protocol11.connect(self, *args, **kwargs)\n",
    "  File \"/var/task/stomp/protocol.py\", line 340, in connect\n    self.transport.wait_for_connection()\n",
    "  File \"/var/task/stomp/transport.py\", line 327, in wait_for_connection\n    raise exception.ConnectFailedException()\n"
  ]
}

更新了 Python Lambda 函数:-

import time
import boto3
import stomp
import json

kinesis_client = boto3.client('kinesis')


class Listener(stomp.ConnectionListener):
    def on_error(self, headers, message):
        print('received an error "%s"' % message)
        kinesis_client.put_record(
            StreamName='OnePurchasing',
            Data=u'{}\r\n'.format(message).encode('utf-8'),
            PartitionKey='0'
        )

    def on_message(self, headers, message):
        print('received a message "%s"' % message)
def lambda_handler(event, context):
    conn = stomp.Connection(host_and_ports=[('b-fa99d7c5-4714-4441-8166-47aae158281a-1.mq.eu-central-1.amazonaws.com', 61614)])
    lst = Listener()
    conn.set_listener('Listener', lst)
    conn.set_ssl(for_hosts=[('b-fa99d7c5-4714-4441-8166-47aae158281a-1.mq.eu-central-1.amazonaws.com', 61614)])
    conn.start()
    print('CONNECTION Started')
    conn.connect(login='test_mq', passcode='test_secure_mq',wait=True)
    print('CONNECTION established')
    conn.subscribe(destination='/queue/OnePurchasing', id=1, ack='auto')
    print('CONNECTION Subscribed')
    time.sleep(10)
    conn.disconnect()
    return

谁能告诉我如何调试更多以从 MQ 获取消息

MQ URL 消息屏幕截图

MQ 主页

队列下的消息

4

1 回答 1

1

原因responsenull你永远不会返回一个值,你只是return. 这与运行此命令的响应相同:

def lambda_handler(event, context):
    return

你可能想要返回一些东西,比如 lambda 内置的示例:

import json

def lambda_handler(event, context):
    # TODO implement
    return {
        'statusCode': 200,
        'body': json.dumps('Hello from Lambda!')
    }

关于您的其他问题,您似乎根本没有收到任何消息。您可以从MQ 实例的 Web 控制台查看队列中是否有消息、消息是否已被使用等等。

我看到的所有示例都涉及使用该wait=True选项,例如conn.connect(wait=True),因此您应该尝试将其添加到您的选项中,conn.connect除非您有充分的理由不使用它。

编辑:我对此进行了测试,我认为您从未建立过连接。如果你添加wait=True,那么你可能会看到连接失败,ConnectFailedException就像我的一样。这可能是要调试的第一件事。

编辑 2:我解决了,您需要使用 SSL 连接到 AWS MQ 实例,如下所示:

conn = stomp.Connection(host_and_ports=[('b-4714-4441-8166-47aae158281a-1.mq.eu-central-1.amazonaws.com', 61614)])
lst = Listener()
conn.set_listener('Listener', lst)
conn.set_ssl(for_hosts=[('b-4714-4441-8166-47aae158281a-1.mq.eu-central-1.amazonaws.com', 61614)])
conn.start()
于 2018-11-22T12:37:10.140 回答