我想将事件消息发送到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并遇到连接错误时客户端挂起
发布评论