I'm trying the java getting started sample on https://learn.microsoft.com/en-us/azure/iot-hub/iot-hub-java-java-getstarted on an iot hub with 4 partitions.
I adapted the part about receiving events by creating 1 client instance and calling client.createReceiver
for each partition like below:
public class App {
private static String connStr = "...";
public static void main(String[] args) throws IOException, EventHubException, ExecutionException, InterruptedException {
EventHubClient client = createClient();
String[] partitionIds = client.getRuntimeInformation()
.thenApply(eventHubRuntimeInformation -> eventHubRuntimeInformation.getPartitionIds())
.get();
client.createReceiver(DEFAULT_CONSUMER_GROUP_NAME, "0", Instant.now())
.thenAccept(receiverHandler("0"));
client.createReceiver(DEFAULT_CONSUMER_GROUP_NAME, "1", Instant.now())
.thenAccept(receiverHandler("0"));
client.createReceiver(DEFAULT_CONSUMER_GROUP_NAME, "2", Instant.now())
.thenAccept(receiverHandler("2"));
client.createReceiver(DEFAULT_CONSUMER_GROUP_NAME, "3", Instant.now())
.thenAccept(receiverHandler("3"));
System.out.println("Press ENTER to exit.");
System.in.read();
try {
client.closeSync();
System.exit(0);
} catch (Exception e) {
System.exit(1);
}
}
private static Consumer<PartitionReceiver> receiverHandler(String partitionId) {
return receiver -> {
System.out.println("** Created receiver on partition " + partitionId);
try {
while (true) {
receiver.receive(10)
.thenAccept(receivedEvents -> {
int batchSize = 0;
if (receivedEvents != null) {
System.out.println("Got some evenst");
for (EventData receivedEvent : receivedEvents) {
System.out.println(String.format("Offset: %s, SeqNo: %s, EnqueueTime: %s",
receivedEvent.getSystemProperties().getOffset(),
receivedEvent.getSystemProperties().getSequenceNumber(),
receivedEvent.getSystemProperties().getEnqueuedTime()));
System.out.println(String.format("| Device ID: %s",
receivedEvent.getSystemProperties().get("iothub-connection-device-id")));
System.out.println(String.format("| Message Payload: %s",
new String(receivedEvent.getBytes(), Charset.defaultCharset())));
batchSize++;
}
}
System.out.println(String.format("Partition: %s, ReceivedBatch Size: %s", partitionId, batchSize));
}
).get();
}
} catch (Exception e) {
System.out.println("Failed to receive messages: " + e.getMessage());
}
};
}
private static EventHubClient createClient() {
EventHubClient client = null;
try {
client = EventHubClient.createFromConnectionStringSync(connStr);
} catch (Exception e) {
System.out.println("Failed to create client: " + e.getMessage());
System.exit(1);
}
return client;
}
}
The events that are sent from the simulated device happen to arrive on partition 3. The problem is that no events are received in the following situations:
- the device is not sending events when starting the event receiver
- the device is sending events when starting the event receiver, but is restarted afterwards. In that case the above code stops receiving events after the device restart
The above problems don't occur when we only connect to partition 3.
The above problems don't occur when we only connect to partition 3 and 1 other partition.
The above problems does occur when we connect to partition 3 and 2 or 3 other partitions.
Any clues?