/
BlobCheckpointStoreSample.java
79 lines (69 loc) · 3.13 KB
/
BlobCheckpointStoreSample.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package com.azure.messaging.eventhubs.checkpointstore.blob;
import com.azure.core.http.policy.HttpLogDetailLevel;
import com.azure.core.http.policy.HttpLogOptions;
import com.azure.messaging.eventhubs.models.Checkpoint;
import com.azure.messaging.eventhubs.models.PartitionOwnership;
import com.azure.storage.blob.BlobContainerAsyncClient;
import com.azure.storage.blob.BlobContainerClientBuilder;
import java.util.ArrayList;
import java.util.List;
import java.util.StringJoiner;
/**
* Sample that demonstrates the use {@link BlobCheckpointStore} for storing and updating partition ownership records in
* Storage Blobs.
*/
public class BlobCheckpointStoreSample {
/**
* The main method to run the sample.
*
* @param args Unused arguments to the sample.
* @throws Exception If there are any errors while running the sample.
*/
public static void main(String[] args) throws Exception {
String sasToken = "";
BlobContainerAsyncClient blobContainerAsyncClient = new BlobContainerClientBuilder()
.connectionString("")
.containerName("")
.sasToken(sasToken)
.httpLogOptions(new HttpLogOptions().setLogLevel(HttpLogDetailLevel.BODY_AND_HEADERS))
.buildAsyncClient();
BlobCheckpointStore blobCheckpointStore = new BlobCheckpointStore(blobContainerAsyncClient);
blobCheckpointStore.listOwnership("namespace", "abc", "xyz")
.subscribe(BlobCheckpointStoreSample::printPartitionOwnership);
System.out.println("Updating checkpoint");
Checkpoint checkpoint = new Checkpoint()
.setConsumerGroup("xyz")
.setEventHubName("abc")
.setPartitionId("0")
.setSequenceNumber(2L)
.setOffset(250L);
blobCheckpointStore.updateCheckpoint(checkpoint)
.subscribe(etag -> System.out.println(etag), error -> System.out
.println(error.getMessage()));
List<PartitionOwnership> pos = new ArrayList<>();
for (int i = 0; i < 5; i++) {
PartitionOwnership po = new PartitionOwnership()
.setEventHubName("abc")
.setConsumerGroup("xyz")
.setOwnerId("owner1")
.setPartitionId(String.valueOf(i));
pos.add(po);
}
blobCheckpointStore.claimOwnership(pos).subscribe(BlobCheckpointStoreSample::printPartitionOwnership,
System.out::println);
}
static void printPartitionOwnership(PartitionOwnership partitionOwnership) {
String po =
new StringJoiner(",")
.add("pid=" + partitionOwnership.getPartitionId())
.add("ownerId=" + partitionOwnership.getOwnerId())
.add("cg=" + partitionOwnership.getConsumerGroup())
.add("eh=" + partitionOwnership.getEventHubName())
.add("etag=" + partitionOwnership.getETag())
.add("lastModified=" + partitionOwnership.getLastModifiedTime())
.toString();
System.out.println(po);
}
}