问题
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("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();
I use the following dependency:
compile "com.microsoft.azure:azure-eventhubs:3.2.0"
I'd appreciate any help! Thanks!
回答1:
I use Maven to create a java project, then add dependency in 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>
And This is the code to send the event message:
package test;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.microsoft.azure.eventhubs.ConnectionStringBuilder;
import com.microsoft.azure.eventhubs.EventData;
import com.microsoft.azure.eventhubs.EventHubClient;
import com.microsoft.azure.eventhubs.EventHubException;
import java.io.IOException;
import java.nio.charset.Charset;
import java.time.Instant;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
public class App
{
public static void main( String[] args ) throws Exception
{
final ConnectionStringBuilder connStr = new ConnectionStringBuilder()
.setNamespaceName("testbowman")
.setEventHubName("test")
.setSasKeyName("testbowman")
.setSasKey("xxxxxx");
final Gson gson = new GsonBuilder().create();
final ScheduledExecutorService executorService = Executors.newScheduledThreadPool(4);
final EventHubClient ehClient = EventHubClient.createSync(connStr.toString(), executorService);
try {
for (int i = 0; i < 10; i++) {
String payload = "Message " + Integer.toString(i);
byte[] payloadBytes = gson.toJson(payload).getBytes(Charset.defaultCharset());
EventData sendEvent = EventData.create(payloadBytes);
ehClient.sendSync(sendEvent);
}
System.out.println(Instant.now() + ": Send Complete...");
System.out.println("Press Enter to stop.");
System.in.read();
} finally {
ehClient.closeSync();
executorService.shutdown();
}
System.out.println( "Hello World!" );
System.out.println( "!!!!!!!!!!!!!" );
}
}
(I hide the sas key, I think you know where to get the sas key.:))
At last, I can see the messages come in on the metrics(Can not see immediately, need wait a few time.):
This is the offcial doc:
https://docs.microsoft.com/en-us/azure/event-hubs/event-hubs-java-get-started-send#write-code-to-send-events
回答2:
Try using work-stealing pool instead of single thread.
final ExecutorService scheduledExecutorService = Executors.newWorkStealingPool();
Or thread pool.
final ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(4);
来源:https://stackoverflow.com/questions/63039023/client-hangs-when-calling-azure-event-hub-and-facing-connection-error