Azure:消息未在主题中推送并作为订阅检索

Azure : message is not getting pushed in Topics and retrieved as subscription

我已经在 Azure 中创建了主题和订阅。当我尝试将我的消息推送到主题中并通过订阅检索它时,我无法收到消息。消息是否存储在队列中。我的消息没有发布吗?

Push in the topic code

 const topicName = 'xxxxxxxxxxxxx';
    async function main(){
      const sbClient = ServiceBusClient.createFromConnectionString(connectionString); 
      const topicClient = sbClient.createTopicClient(topicName);
      const sender = topicClient.createSender();

        try {

              const message= {
                body: req.body.message,
                label: `test`,

              };
              console.log(`Sending message: ${message.body}`);
              await sender.send(message);

              await topicClient.close();
              res.send(message.body)

          } finally {
            await sbClient.close();
          }

    }

    main()
    .catch((err) => {
      console.log("Error occurred: ", err);
    });

Getting Message via subscription code

const topicName = 'xxxxxxxxx';
  const subscriptionName = "subsTest1"; 

  async function main(){

    const sbClient = ServiceBusClient.createFromConnectionString(connectionString); 
    const subscriptionClient = sbClient.createSubscriptionClient(topicName, subscriptionName);
    const receiver = subscriptionClient.createReceiver(ReceiveMode.receiveAndDelete);

    try {

      const messages = await receiver.receiveMessages(10);

      res.send(messages)
      console.log("Received messages:");
      console.log(messages.map(message => message.body));

      await subscriptionClient.close();
    } finally {
      await sbClient.close();
    }
  }

  main().catch((err) => {
    console.log("Error occurred: ", err);
  });

运行 您将消息推送到主题的代码。然后你可以在 Azure 门户中查看订阅以查看消息是否存在。这至少会确认您的代码是否正确发送消息。

我测试你的代码,在我的测试中我删除了请求和响应部分,我可以发送和接收消息。因为在你的测试中你不知道你是否成功发送消息,你可以使用ServiceBusExplorer来查看消息。请记住,当从订阅中接收消息时它很慢。

下面是我的测试结果。查看我的日志,你可以找到它不会立即收到消息的时间间隔。