Java 调用Azure事件中心并遇到连接错误时客户端挂起

Java 调用Azure事件中心并遇到连接错误时客户端挂起,java,azure,azure-eventhub,Java,Azure,Azure Eventhub,我想将事件消息发送到Azure事件中心。我注意到,如果我配置错误,那么我的应用程序将挂起,而不是终止。 我编写了一个非常简单的Java类,试图将事件消息发送到事件中心。如果我错误地输入了事件中心的端点,那么应用程序将挂起。这很令人失望 有一个机会,我误解了一些东西,但我想做的是发送一个简单的信息,仅此而已。我该怎么做 ConnectionStringBuilder connectionStringBuilder = new ConnectionStringBuilder(); c

我想将事件消息发送到Azure事件中心。我注意到,如果我配置错误,那么我的应用程序将挂起,而不是终止。
我编写了一个非常简单的Java类,试图将事件消息发送到事件中心。如果我错误地输入了事件中心的端点,那么应用程序将挂起。这很令人失望

有一个机会,我误解了一些东西,但我想做的是发送一个简单的信息,仅此而已。我该怎么做

    ConnectionStringBuilder connectionStringBuilder = new ConnectionStringBuilder();
    connectionStringBuilder
            .setEndpoint(URI.create("https://XXXXXXXXX.servsssicebus.windows.net"))
            .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"
我将感谢任何帮助!
谢谢

我使用Maven创建java项目,然后在pom.xml中添加依赖项:

<dependency>
    <groupId>com.microsoft.azure</groupId>
    <artifactId>azure-eventhubs</artifactId>
    <version>2.2.0</version>
</dependency>
<dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>2.8.6</version>
</dependency>

com.microsoft.azure

这是官方文件:

编辑:

我认为您被挂起的原因是执行者没有机会在出现错误时关闭。您应该将代码包装在try finally中,如下所示:

ConnectionStringBuilder connectionStringBuilder = new ConnectionStringBuilder();
    connectionStringBuilder
            .setEndpoint(URI.create("https://XXXXXXXXX.servsssicebus.windows.net"))
            .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包(Event Hub v3),如中所述。最新的包azure messaging eventhubs(使用生产者/消费者模式的Event Hub v5)有一些不同的API,如中所述。如果是新开发的,您应该使用新的SDK

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的迁移指南


即使使用旧的软件包,我也无法使用Executors.newScheduledThreadPool(4)或Executors.newSingleThreadScheduledExecutor()重现挂起问题,如开头所述,当优雅地关闭Executor时。如果我错误地给出了错误的连接字符串,它会立即在线程“main”com.microsoft.azure.eventhubs.CommunicationException中抛出异常:发生通信错误。这可能是由于连接字符串中的主机名不正确或网络连接有问题造成的。

能否尝试在多线程池中运行它?谢谢!我试过了,但结果恐怕还是一样。嗨,谢谢你的回复!我们似乎有类似的代码,但我使用的是不同版本的
azure eventhubs
。我的问题是执行从未通过
EventHubClient.createFromConnectionStringSync
,甚至没有抛出异常。所以它就挂在那里。。。挂着。。。而且。。。我将尝试
2.2.0
,但我可能需要后一个版本中的功能。无论如何谢谢你的帮助!不,恐怕我没有任何更新。你也有同样的问题吗?发送异步消息并处理异常似乎是一项简单的任务。也可能是我严重误解了某些事情。它是否应该放弃com.microsoft.azure:azure eventhubs依赖项,自己写一些东西。。。?这似乎太过分了。嗨,谢谢你的回复!不幸的是,
EventHubClient.createFromConnectionStringSync(…)
只接受
ScheduledExecutorService
。我还尝试了
Executors.newScheduledThreadPool(4)但最终结果是一样的(应用程序挂起)。太棒了,谢谢!你是对的,在例外情况下,执行人没有关闭。也谢谢你提示我使用最新的库,我不知道!
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();
    }
}