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>{};
}
}
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/ 示例