简体   繁体   English

从 nodejs 服务器接收来自 ServiceBus 的 BrokeredMessage

[英]Receiving BrokeredMessage from ServiceBus from a nodejs server

I am reading messages from an existing Azure ServiceBus from a new nodejs Server.我正在从新的 nodejs 服务器读取来自现有 Azure ServiceBus 的消息。

This is the way the messages are being sent to the ServiceBus from a .NET server:这是消息从 .NET 服务器发送到 ServiceBus 的方式:

var topicClient = TopicClient.CreateFromConnectionString(serviceBusConnectionString, routingKey);    
var brokeredMessage = new BrokeredMessage(message.ToJson());
topicClient.Send(brokeredMessage);

Where topicClient is Microsoft.ServiceBus.Messaging.TopicClient其中 topicClient 是 Microsoft.ServiceBus.Messaging.TopicClient

I am using the following method to read the messages on a nodejs server using azure-sb package:我正在使用以下方法使用 azure-sb package 在 nodejs 服务器上读取消息:

sbClient = ServiceBusClient.createFromConnectionString(connectionString)  
subscriptionClient = this.sbClient.createSubscriptionClient(topicName, subscriptionName);
receiver = this.subscriptionClient.createReceiver(ReceiveMode.receiveAndDelete);
messages = await this.receiver.receiveMessages(10,10);
console.log(messages.map(message => { message.body }));

message.body is a buffer and when I do message.body.toString('utf-8') I get something like: message.body 是一个缓冲区,当我执行 message.body.toString('utf-8') 时,我得到如下信息:

@string3http://schemas.microsoft.com/2003/10/Serialization/��{VALID JSON}

I am interested of course in the valid JSON in between.我当然对介于两者之间的有效 JSON 感兴趣。 In the .net servers we simply do brokeredMessage.GetBody() and we get the object, so is there an easy way on nodejs to do the same?在 .net 服务器中,我们只需执行 brokeredMessage.GetBody() 并得到 object,那么在 nodejs 上有没有简单的方法来做同样的事情?

According to my test, if we use the standard library Microsoft.Azure.ServiceBus to send messages in.Net application, it will directly JSON parse the message in node application根据我的测试,如果我们使用标准库Microsoft.Azure.ServiceBus在.Net应用程序中发送消息,它会直接JSON在节点应用程序中解析消息

For example例如

This is my C# code to send a message:这是我发送消息的 C# 代码:

class Program
{
    static void Main(string[] args)
    {
        string connectionString = "Endpoint=sb://...";
        var client = new TopicClient(connectionString, "");
        var payload = JsonConvert.SerializeObject(new DemoMessage() { Title = $"hello!!! {DateTime.Now}" });
        var serviceBusMessage = new Message(Encoding.UTF8.GetBytes(payload));
        serviceBusMessage.SessionId = Guid.NewGuid().ToString("D");

        client.SendAsync(serviceBusMessage).Wait();

    }

    private class DemoMessage
    {
        public DemoMessage()
        {
        }

        public string Title { get; set; }
    }

This is my Node.js code to receive a message:这是我接收消息的 Node.js 代码:

const { ServiceBusClient, ReceiveMode } = require("@azure/service-bus");

// Define connection string and related Service Bus entity names here
const connectionString =
  "Endpoint=sb://";
const topicName = "***";
const subscriptionName = "***";

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(1);
    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);
});

在此处输入图像描述

Besides, if you still use the library WindowsAzure.ServiceBus , we need to use BrokeredMessage(Stream messageBodyStream, bool ownsStream) to initialize an object.此外,如果您仍然使用库WindowsAzure.ServiceBus ,我们需要使用BrokeredMessage(Stream messageBodyStream, bool ownsStream)来初始化 object。

Because we use BrokeredMessage(platload) to initialize, it will use DataContractSerializer with a binary XmlDictionaryWriter to initialize an object.因为我们使用BrokeredMessage(platload)进行初始化,所以它将使用 DataContractSerializer 和二进制 XmlDictionaryWriter 来初始化 object。 So, the payload is being serialized using DataContractSerializer with a binary XmlDictionaryWriter and this is the cause that explains why the body of the message has in its start the type indication @string3http://schemas.microsoft.com/2003/10/Serialization/ .因此,负载正在使用带有二进制 XmlDictionaryWriter 的 DataContractSerializer 进行序列化,这就是为什么消息正文在其开头具有类型指示@string3http://schemas.microsoft.com/2003/10/Serialization/的原因.

For example This is my C# code to send a message:例如这是我发送消息的 C# 代码:

var client =TopicClient.CreateFromConnectionString(connectionString, "test");
            var payload = JsonConvert.SerializeObject(new DemoMessage() { Title = $"hello BrokeredMessage!!! {DateTime.Now}" });
            using (Stream stream = new MemoryStream(Encoding.UTF8.GetBytes(payload))) {
                var serviceBusMessage = new BrokeredMessage(stream,true);
                await client.SendAsync(serviceBusMessage);

            }

I use the same code to receive我使用相同的代码接收在此处输入图像描述

For more details, please refer to here .更多详情,请参阅此处

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM