提问人:Javier 提问时间:11/15/2023 更新时间:11/15/2023 访问量:42
如何让nats:Listener在Ballerina中使用自定义ConnectionConfiguration对象?
How can I make nats:Listener use a custom ConnectionConfiguration object in Ballerina?
问:
我正在使用 https://ballerina.io/learn/by-example/nats-basic-sub/ 官方项目页面上提供的代码。
代码如下所示:
import ballerina/log;
import ballerinax/nats;
public type Order record {
int orderId;
string productName;
decimal price;
boolean isValid;
};
// Binds the consumer to listen to the messages published to the 'orders.valid' subject.
service "orders.valid" on new nats:Listener(nats:DEFAULT_URL) {
remote function onMessage(Order 'order) returns error? {
if 'order.isValid {
log:printInfo(string `Received valid order for ${'order.productName}`);
}
}
}
我需要注释或将 ConnectionConfiguration 对象传递给服务的侦听器,以便修改 nats:RetryConfig 对象。我的目标是让服务每 5 秒尝试连接到 NATS 服务器,如果 NATS 服务器不可用,直到 NATS 服务器再次可用,则不会中断。我认为这可以通过将 maxReconnect 设置为 -1 并将 reconnectWait 设置为 5 来实现。
我在服务代码中寻找了一些帮助程序和/或回调,但我认为这不是正确的方法。
我该怎么做?可以通过服务上的任何注释来完成吗?
谢谢!
答:
0赞
Arshika Mohottige
11/15/2023
#1
您可以修改代码,以将记录传递给侦听器初始化,其值如下所示。ConnectionConfiguration
maxReconnect
reconnectWait
import ballerina/log;
import ballerinax/nats;
public type Order record {
int orderId;
string productName;
decimal price;
boolean isValid;
};
final nats:ConnectionConfiguration & readonly config = {
retryConfig: {maxReconnect: -1, reconnectWait: 5}
};
service "orders.valid" on new nats:Listener(nats:DEFAULT_URL, config) {
remote function onMessage(Order 'order) returns error? {
if 'order.isValid {
log:printInfo(string `Received valid order for ${'order.productName}`);
}
}
}
或
import ballerina/log;
import ballerinax/nats;
public type Order record {
int orderId;
string productName;
decimal price;
boolean isValid;
};
service "orders.valid" on new nats:Listener(nats:DEFAULT_URL, {retryConfig: {maxReconnect: -1, reconnectWait: 5}}) {
remote function onMessage(Order 'order) returns error? {
if 'order.isValid {
log:printInfo(string `Received valid order for ${'order.productName}`);
}
}
}
评论