Java SDK中的EventHub“send”挂起,发送永不发生

时间:2018-04-20 00:13:05

标签: azure azure-eventhub

我正在尝试使用示例代码将简单事件发送到Azure EventHub(https://docs.microsoft.com/en-us/azure/event-hubs/event-hubs-java-get-started-send)。它看起来很好,我已经配置好了,但是当我到达:

 ehClient.sendSync(sendEvent);

部分代码,它只是挂起,并且永远不会超过sendAsync方法。我正在使用个人计算机,并且没有运行防火墙。我是否需要在Azure中进行一些网络配置以允许发生这种简单的发送?任何人都有运气这么做吗?

final ConnectionStringBuilder connStr = new ConnectionStringBuilder()
            .setNamespaceName("mynamespace")
            .setEventHubName("myeventhubname")
            .setSasKeyName("mysaskename")
            .setSasKey("mysaskey");

    final Gson gson = new GsonBuilder().create();
    final ExecutorService executorService = Executors.newSingleThreadExecutor();
    final EventHubClient ehClient = EventHubClient.createSync(connStr.toString(), executorService);

    print("Event Hub Client Created");
    try {
        for (int i = 0; i < 100; i++) {

            String payload = "Message " + Integer.toString(i);
            byte[] payloadBytes = gson.toJson(payload).getBytes(Charset.defaultCharset());
            EventData sendEvent = EventData.create(payloadBytes);

            // HANGS HERE - NEVER GETS PAST THIS CALL
            ehClient.sendSync(sendEvent);

        }

    } finally {
        ehClient.closeSync();
        executorService.shutdown();
    }

2 个答案:

答案 0 :(得分:0)

尝试使用其他执行程序服务。例如“窃取工作线程”

final ExecutorService executorService = Executors.newWorkStealingPool();

答案 1 :(得分:0)

下面的代码应该对您有用。

<dependency>
            <groupId>com.microsoft.azure</groupId>
            <artifactId>azure-eventhubs</artifactId>
            <version>2.0.0</version>
 </dependency>
   <dependency>
      <groupId>com.microsoft.azure</groupId>
      <artifactId>azure-eventhubs-spark_2.11</artifactId>
      <version>2.3.7</version>
    </dependency>

import java.util.concurrent.{Executors, ScheduledExecutorService}
import com.google.gson.Gson
import com.microsoft.azure.eventhubs.{EventData, EventHubClient}

object callToPushMessage{
private var executorService : ScheduledExecutorService = null 
def writeMsgToSink(message: PushMessage):Unit={
val connStr = ConnectionStringBuilder()
  .setNamespaceName("namespace")
  .setEventHubName("name")
  .setSasKeyName("policyname")
  .setSasKey("policykey").build


// The Executor handles all asynchronous tasks and this is passed to the EventHubClient instance.
// This enables the user to segregate their thread pool based on the work load.
// This pool can then be shared across multiple EventHubClient instances.
// The following code uses a single thread executor, as there is only one EventHubClient instance,
// handling different flavors of ingestion to Event Hubs here.
if (executorService == null) {
  executorService = Executors.newSingleThreadScheduledExecutor()
}
val ehclient = EventHubClient.createSync(connStr,executorService)
try {
  val jsonMessage = new Gson().toJson(message,classOf[PushMessage])
  val eventData: EventData = EventData.create(jsonMessage.getBytes())
  ehclient.sendSync(eventData)

}
finally {
  ehclient.close()
  executorService.shutdown()
}
}}