gpt4 book ai didi

java - 如何在azure服务总线中一直监听队列并拦截每条消息

转载 作者:行者123 更新时间:2023-11-30 05:18:05 24 4
gpt4 key购买 nike

我在azure中有一个ServiceBus,有两个队列:第一个是客户端发送消息的地方(我需要拦截消息。QUEUE_IN),第二个是客户端接收消息的队列当我处理完第一个消息 (QUEUE_OUT) 中的消息时发送的消息。

我需要一直监听QUEUE_IN,当他们发送消息时处理它,并将结果发送到QUEUE_OUT

我遇到的问题是我无法拦截QUEUE_IN中的消息,当我运行程序时,我知道队列中有消息,但程序无法看到他们。

private void recibirMensaje() throws Exception {   
QueueClient queueIn= new QueueClient(new ConnectionStringBuilder(stringConection, nameQueue),
ReceiveMode.PEEKLOCK);
ExecutorService executorService = Executors.newSingleThreadExecutor();
this.interceptMessage(queueIn, executorService);
recibirMensajeCliente.close();
executorService.shutdown();

}

private void interceptMessage(QueueClient queueIn, ExecutorService executorService) throws Exception {
queueIn.registerMessageHandler(new IMessageHandler() {
public CompletableFuture<Void> onMessageAsync(IMessage message) {
if (message.getLabel() != null && message.getContentType() != null
&& message.getLabel().contentEquals(etiquetaMensajes)
&& message.getContentType().contentEquals("application/json")) {

byte[] body = message.getBody();
logger.info("Printing message{}", body);

}
return CompletableFuture.completedFuture(null);
}

public void notifyException(Throwable throwable, ExceptionPhase exceptionPhase) {
}
},
// 1 concurrent call, messages are auto-completed, auto-renew duration
new MessageHandlerOptions(1, true, Duration.ofMinutes(1)), executorService);


}

我没有发布发送方法,因为我对此没有问题。我不知道如何一直监听 queue_in 并处理消息,也不知道如何知道队列是否有消息要处理以及如何拦截它。

最佳答案

您最好使用 Azure Functions,它对服务总线队列具有 native 支持。它会监听您的队列,并为您注入(inject)消息内容。之后,您可以使用输出绑定(bind)在作业完成时通知使用者。

更多信息:https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-service-bus?tabs=java

关于java - 如何在azure服务总线中一直监听队列并拦截每条消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60047572/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com