调用Azure Event Hub并遇到连接错误时客户端挂起

编程入门 行业动态 更新时间:2024-10-28 06:31:57
本文介绍了调用Azure Event Hub并遇到连接错误时客户端挂起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想将事件消息发送到Azure事件中心.我注意到如果我配置不当,那么我的应用会挂起并且不会终止. 我编写了一个非常简单的Java类,尝试将事件消息发送到事件中心.如果我输错了事件中心的端点,则该应用程序将挂起.真令人失望.

I want to send event messages to Azure Event Hub. I noticed if I misconfigured something then my app hangs and not terminates. I wrote a very simple Java class that tries to send event message to the Event Hub. If I mistype the endpoint of the Event Hub then the app hangs. Which is pretty disappointing.

我可能会误解某些事情,但是我想做的只是发送一条简单的消息,仅此而已.我该怎么办?

There is a chance that I misunderstand something but what I want to do is to send a simple message and that's all. How can I do that?

ConnectionStringBuilder connectionStringBuilder = new ConnectionStringBuilder(); connectionStringBuilder .setEndpoint(URI.create("XXXXXXXXX.servsssicebus.windows")) .setTransportType(TransportType.AMQP_WEB_SOCKETS) .setSasKeyName("XXX") .setSasKey("XXX"); ScheduledExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor(); final EventHubClient ehClient = EventHubClient.createFromConnectionStringSync( connectionStringBuilder.toString(), RetryPolicy.getNoRetry(), scheduledExecutorService ); ehClient.sendSync(EventData.create("Test Message".getBytes())); ehClient.closeSync(); scheduledExecutorService.shutdown();

我使用以下依赖项:

compile "com.microsoft.azure:azure-eventhubs:3.2.0"

我将不胜感激! 谢谢!

I'd appreciate any help! Thanks!

推荐答案

我相信您死机的原因是执行程序在出现错误的情况下没有机会关闭.您应该将代码包装在try最终中,如下所示::

ConnectionStringBuilder connectionStringBuilder = new ConnectionStringBuilder(); connectionStringBuilder .setEndpoint(URI.create("XXXXXXXXX.servsssicebus.windows")) .setTransportType(TransportType.AMQP_WEB_SOCKETS) .setSasKeyName("XXX") .setSasKey("XXX"); ScheduledExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor(); EventHubClient ehClient = null; try { ehClient = EventHubClient.createFromConnectionStringSync( connectionStringBuilder.toString(), RetryPolicy.getNoRetry(), scheduledExecutorService ); ehClient.sendSync(EventData.create("Test Message".getBytes())); } finally { if (ehClient != null) ehClient.closeSync(); scheduledExecutorService.shutdown(); }

您的代码使用的是旧的 azure-eventhubs 包(事件中心v3),如本教程.最新的软件包 azure-messaging-eventhubs (使用Producer/Consumer模式的Event Hub v5)具有一些不同的API,这在本教程.如果是新开发的,则应使用新的SDK.

N.B. Your code code is using the old azure-eventhubs package (Event Hub v3) as mentioned in this tutorial. The latest package azure-messaging-eventhubs (Event Hub v5 using Producer/Consumer pattern) has bit different APIs which is described in this tutorial. You should use the new SDK if it's fresh development.

import com.azure.messaging.eventhubs.*; public class Sender { public static void main(String[] args) { final String connectionString = "EVENT HUBS NAMESPACE CONNECTION STRING"; final String eventHubName = "EVENT HUB NAME"; // create a producer using the namespace connection string and event hub name EventHubProducerClient producer = new EventHubClientBuilder() .connectionString(connectionString, eventHubName) .buildProducerClient(); // prepare a batch of events to send to the event hub EventDataBatch batch = producer.createBatch(); batch.tryAdd(new EventData("First event")); batch.tryAdd(new EventData("Second event")); batch.tryAdd(new EventData("Third event")); batch.tryAdd(new EventData("Fourth event")); batch.tryAdd(new EventData("Fifth event")); // send the batch of events to the event hub producer.send(batch); // close the producer producer.close(); } }

请注意,还有从v3到v5的迁移指南此处.

On further note, there is also a migration guide from v3 to v5 here.

即使使用旧程序包,如开头提到的那样,在优雅地关闭Executor时,也无法使用Executors.newScheduledThreadPool(4)或Executors.newSingleThreadScheduledExecutor()重现您的挂起问题.如果我错误地输入了错误的连接字符串,它将立即引发异常:线程"main"中的异常; com.microsoft.azure.eventhubs.CommunicationException:发生通信错误.这可能是由于您的连接字符串中的主机名不正确或网络连接出现问题.

Even with old package, I could not reproduce your hang issue using either Executors.newScheduledThreadPool(4) or Executors.newSingleThreadScheduledExecutor() when closed Executor gracefully as mentioned in the beginning. If I give wrong connection string by mistake, it immediately throws exception: Exception in thread "main" com.microsoft.azure.eventhubs.CommunicationException: A communication error has occurred. This may be due to an incorrect host name in your connection string or a problem with your network connection.

更多推荐

调用Azure Event Hub并遇到连接错误时客户端挂起

本文发布于:2023-10-16 00:45:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1495979.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:挂起   客户端   错误   Azure   Event

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!