简体   繁体   English

Java 发送到 EventHub(未正常关闭)

[英]Java send to EventHub (not shutting down gracefully)

I am trying to send alot of data from a server over to my event hub, however when I try to use the example code from microsoft doc the data is being sent but the application never stop running.我正在尝试将大量数据从服务器发送到我的事件中心,但是当我尝试使用 microsoft doc 中的示例代码时,数据正在发送,但应用程序从未停止运行。 I fill up my arraylist with 100 data at a time then I run the method.我一次用 100 个数据填充我的数组列表,然后运行该方法。

    public static void publishEvents(List<EventData> allEvents) {
        // create a producer client
        EventHubProducerClient producer = new EventHubClientBuilder()
            .connectionString(connectionString, eventHubName)
            .buildProducerClient();

       

        // create a batch
        EventDataBatch eventDataBatch = producer.createBatch();

        for (EventData eventData : allEvents) {
            // try to add the event from the array to the batch
            if (!eventDataBatch.tryAdd(eventData)) {
                // if the batch is full, send it and then create a new batch
                producer.send(eventDataBatch);
                eventDataBatch = producer.createBatch();

                // Try to add that event that couldn't fit before.
                if (!eventDataBatch.tryAdd(eventData)) {
                    throw new IllegalArgumentException("Event is too large for an empty batch. Max size: "
                        + eventDataBatch.getMaxSizeInBytes());
                }
            }
        }
        // send the last batch of remaining events
        if (eventDataBatch.getCount() > 0) {
            producer.send(eventDataBatch);
        }
        producer.close();
    }

I tried using an async approach which seems to work fine but creating a new connection for each data to be sent seems like a bad idea and when using this for sending alot of data.我尝试使用似乎工作正常的异步方法,但是为要发送的每个数据创建一个新连接似乎是一个坏主意,并且在使用它发送大量数据时。

        EventHubProducerAsyncClient producer = new EventHubClientBuilder()
                .connectionString(connectionString)
                .buildAsyncProducerClient();

        // Create a batch and add a sample log to it
        producer.createBatch().flatMap(batch ->
        {
            batch.tryAdd(new EventData(event));


            return producer.send(batch);
        }).subscribe(unused -> {
                },
                error -> System.err.println("Couldn't send logs, there's an error: " + error.getStackTrace()),
                () ->
                {
                    System.out.println("Send complete!");
                    // Close the connection
                    producer.close();
                });

    }

Would need some help to fix the problem where the application never exiting.需要一些帮助来解决应用程序永远不会退出的问题。

I believe this may be related to the bug here where we do not dispose of a resource to allow this to close properly.我相信这可能与此处的错误有关,我们没有处理资源以允许其正确关闭。 Your repro looks like this: https://github.com/Azure/azure-sdk-for-java/issues/22305你的重现看起来像这样: https : //github.com/Azure/azure-sdk-for-java/issues/22305

For the asynchronous code, the .subscribe() call just sets up the subscription and invokes the call before moving to the next line of code.对于异步代码, .subscribe()调用只是设置订阅并在移动到下一行代码之前调用该调用。 (It's a non-blocking call.) If your main method looked like the one below, the program would have probably ended before it had time to create an AMQP connection/session/link to the Event Hub. (这是一个非阻塞调用。)如果您的主要方法如下所示,则程序可能在它有时间创建到事件中心的 AMQP 连接/会话/链接之前就结束了。 So that would be why the program ended gracefully.所以这就是为什么程序优雅地结束的原因。

public static void main(String[] args) {
    EventHubProducerAsyncClient producer = new EventHubClientBuilder()
            .connectionString(connectionString)
            .buildAsyncProducerClient();

    // Create a batch and add a sample log to it
    producer.createBatch().flatMap(batch -> {
        batch.tryAdd(new EventData(event));
        return producer.send(batch);
    }).subscribe(unused -> {
        },
        error -> System.err.println("Couldn't send logs, there's an error: " + error.getStackTrace()),
        () -> {
            System.out.println("Send complete!");
            // Close the connection
            producer.close();
        });
}

Did you get a solution for this?你有解决方案吗? I have a similar use case我有一个类似的用例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM