如何将来自 RabbitMQ 代理的“byte[]”数据转换为 Ballerina 中的记录类型

问题描述 投票:0回答:1
我在 ballerina 包中使用 RabbitMQ 模块以及服务器和客户端。在客户端中,我收到的内容为

byte[]

,需要在处理之前转换为 
CustomerDetails
 记录类型。我们怎样才能做到这一点?

客户端.bal

import ballerina/io; import ballerinax/rabbitmq; type CustomerDetails {| string name; string ssn; string phone; int age; |}; listener rabbitmq:Listener channelListener = new (rabbitmq:DEFAULT_HOST, rabbitmq:DEFAULT_PORT); @rabbitmq:ServiceConfig { queueName: "MOT_HOTEL" } service rabbitmq:Service on channelListener { remote function onMessage(rabbitmq:AnydataMessage message) returns error? { if message.content is () { io:println("Message recived with `null` content"); } io:println(message.content); // byte[] ... } }
服务器.bal

import ballerina/http; import ballerina/io; import ballerinax/rabbitmq; const queueName = "MOT_HOTEL"; const exchangeName = "TheExchange"; service /orders on new http:Listener(8080) { function init() returns error? { check rabbitmqClient->exchangeDeclare(exchangeName, rabbitmq:DIRECT_EXCHANGE); check rabbitmqClient->queueDeclare(queueName); } resource function post .(CustomerDetails payload) returns http:Created|http:InternalServerError|error? { check rabbitmqClient->publishMessage({content: payload, routingKey: queueName}); io:println("Message sent"); return <http:Created>{}; } }
    
rabbitmq ballerina ballerina-swan-lake
1个回答
0
投票
您可以使用

CustomerDetails

 记录代替使用 
rabbitmq:AnydataMessage
,如下所示,

service rabbitmq:Service on channelListener { remote function onMessage(CustomerDetails customerDetails) returns error? { io:println(customerDetails.name) // you can handle fields here } }
参考 

https://ballerina.io/learn/by-example/rabbitmq-consumer/ 示例

© www.soinside.com 2019 - 2024. All rights reserved.