|
1 |
| -# Azure Event Hubs Checkpoint Store using Redis client library for Java |
| 1 | +# Azure Event Hubs Checkpoint Store client library for Java using the Jedis Client Library for Redis |
| 2 | + |
| 3 | +Azure Event Hubs Checkpoint Store can be used for storing checkpoints while processing events from Azure Event Hubs. |
| 4 | +This package makes use of Redis as a persistent store for maintaining checkpoints and partition ownership information. |
| 5 | +The `JedisRedisCheckpointStore` provided in this package can be plugged in to `EventProcessor`. |
| 6 | + |
| 7 | +[Source code][source_code]| [API reference documentation][api_documentation] | [Product |
| 8 | +documentation][event_hubs_product_docs] | [Samples][sample_examples] |
2 | 9 |
|
3 | 10 | ## Getting started
|
4 | 11 |
|
| 12 | +### Prerequisites |
| 13 | + |
| 14 | +- A [Java Development Kit (JDK)][jdk_link], version 8 or later. |
| 15 | +- [Maven][maven] |
| 16 | +- Microsoft Azure subscription |
| 17 | + - You can create a free account at: [https://azure.microsoft.com](https://azure.microsoft.com) |
| 18 | +- Azure Event Hubs instance |
| 19 | + - Step-by-step guide for [creating an Event Hub using the Azure Portal][event_hubs_create] |
| 20 | +- Azure Redis Cache or a suitable alternative Redis server |
| 21 | + - Step-by-step guide for [creating a Redis Cache using the Azure Portal][redis_cache] |
| 22 | + |
| 23 | +### Include the package |
| 24 | +#### Include the BOM file |
| 25 | + |
| 26 | +Please include the azure-sdk-bom to your project to take dependency on the General Availability (GA) version of the library. In the following snippet, replace the {bom_version_to_target} placeholder with the version number. |
| 27 | +To learn more about the BOM, see the [AZURE SDK BOM README](https://github.com/Azure/azure-sdk-for-java/blob/main/sdk/boms/azure-sdk-bom/README.md). |
| 28 | + |
| 29 | +```xml |
| 30 | +<dependencyManagement> |
| 31 | + <dependencies> |
| 32 | + <dependency> |
| 33 | + <groupId>com.azure</groupId> |
| 34 | + <artifactId>azure-sdk-bom</artifactId> |
| 35 | + <version>{bom_version_to_target}</version> |
| 36 | + <type>pom</type> |
| 37 | + <scope>import</scope> |
| 38 | + </dependency> |
| 39 | + </dependencies> |
| 40 | +</dependencyManagement> |
| 41 | +``` |
| 42 | +and then include the direct dependency in the dependencies section without the version tag as shown below. |
| 43 | + |
| 44 | +```xml |
| 45 | +<dependencies> |
| 46 | + <dependency> |
| 47 | + <groupId>com.azure</groupId> |
| 48 | + <artifactId>azure-messaging-eventhubs-checkpointstore-jedis</artifactId> |
| 49 | + </dependency> |
| 50 | +</dependencies> |
| 51 | +``` |
| 52 | + |
| 53 | +#### Include direct dependency |
| 54 | +If you want to take dependency on a particular version of the library that is not present in the BOM, |
| 55 | +add the direct dependency to your project as follows. |
| 56 | + |
| 57 | +[//]: # ({x-version-update-start;com.azure:azure-messaging-eventhubs-checkpointstore-jedis;current}) |
| 58 | +```xml |
| 59 | +<dependency> |
| 60 | + <groupId>com.azure</groupId> |
| 61 | + <artifactId>azure-messaging-eventhubs-checkpointstore-jedis</artifactId> |
| 62 | + <version>1.0.0-beta.1</version> |
| 63 | +</dependency> |
| 64 | +``` |
| 65 | +[//]: # ({x-version-update-end}) |
| 66 | + |
| 67 | +### Authenticate the storage container client |
| 68 | + |
| 69 | +In order to create an instance of `JedisCheckpointStore`, a `JedisPool` object must be created. To make this `JedisPool` object, a hostname String and a primary key String are required. These can be used as shown below to create a `JedisPool` object. |
| 70 | + |
| 71 | + |
5 | 72 | ## Key concepts
|
6 | 73 |
|
| 74 | +Key concepts are explained in detail [here][key_concepts]. |
| 75 | + |
7 | 76 | ## Examples
|
| 77 | +- [Create and run an instance of JedisRedisCheckpointStore][sample_jedis_client] |
| 78 | +- [Consume events from all Event Hub partitions][sample_event_processor] |
| 79 | + |
| 80 | +### Create an instance of JedisPool with Azure Redis Cache |
| 81 | + |
| 82 | +```java |
| 83 | +String hostname = "yourHostName.redis.cache.windows.net"; |
| 84 | + |
| 85 | +String password = "<PRIMARY KEY FOR AZURE REDIS CACHE>"; |
| 86 | + |
| 87 | +String name = "<NAME OF THE USER CLIENT>"; //this can also be a default value as the connection of Redis Cache is not dependent on this value |
| 88 | + |
| 89 | +JedisPool jedisPool = new JedisPool(poolConfig, hostname, port, 1000, 1000, password, Protocol.DEFAULT_DATABASE, name, true, null, null, null); |
| 90 | +``` |
| 91 | + |
| 92 | +### Consume events using an Event Processor Client |
| 93 | + |
| 94 | +To consume events for all partitions of an Event Hub, you'll create an |
| 95 | +[`EventProcessorClient`][source_eventprocessorclient] for a specific consumer group. When an Event Hub is created, it |
| 96 | +provides a default consumer group that can be used to get started. |
| 97 | + |
| 98 | +The [`EventProcessorClient`][source_eventprocessorclient] will delegate processing of events to a callback function that you |
| 99 | +provide, allowing you to focus on the logic needed to provide value while the processor holds responsibility for |
| 100 | +managing the underlying consumer operations. |
| 101 | + |
| 102 | +In our example, we will focus on building the [`EventProcessor`][source_eventprocessorclient], use the |
| 103 | +[`JedisRedisCheckpointStore`][source_jedisredischeckpointstore], and a simple callback function to process the events |
| 104 | +received from the Event Hubs, writes to console and updates the checkpoint in Blob storage after each event. |
| 105 | + |
| 106 | +```java |
| 107 | +JedisPool jedisPool = new JedisPool(poolConfig, hostname, port, 1000, 1000, password, Protocol.DEFAULT_DATABASE, name, true, null, null, null); |
| 108 | + |
| 109 | +EventProcessorClient eventProcessorClient = new EventProcessorClientBuilder() |
| 110 | + .consumerGroup("<< CONSUMER GROUP NAME >>") |
| 111 | + .connectionString("<< EVENT HUB CONNECTION STRING >>") |
| 112 | + .checkpointStore(new JedisRedisCheckpointStore(jedisPool)) |
| 113 | + .processEvent(eventContext -> { |
| 114 | + System.out.println("Partition id = " + eventContext.getPartitionContext().getPartitionId() + " and " |
| 115 | + + "sequence number of event = " + eventContext.getEventData().getSequenceNumber()); |
| 116 | + }) |
| 117 | + .processError(errorContext -> { |
| 118 | + System.out.println("Error occurred while processing events " + errorContext.getThrowable().getMessage()); |
| 119 | + }) |
| 120 | + .buildEventProcessorClient(); |
| 121 | + |
| 122 | +// This will start the processor. It will start processing events from all partitions. |
| 123 | +eventProcessorClient.start(); |
| 124 | + |
| 125 | +// (for demo purposes only - adding sleep to wait for receiving events) |
| 126 | +TimeUnit.SECONDS.sleep(5); |
| 127 | + |
| 128 | +// When the user wishes to stop processing events, they can call `stop()`. |
| 129 | +eventProcessorClient.stop(); |
| 130 | +``` |
8 | 131 |
|
9 | 132 | ## Troubleshooting
|
10 | 133 |
|
| 134 | +### Enable client logging |
| 135 | + |
| 136 | +Azure SDK for Java offers a consistent logging story to help aid in troubleshooting application errors and expedite |
| 137 | +their resolution. The logs produced will capture the flow of an application before reaching the terminal state to help |
| 138 | +locate the root issue. View the [logging][logging] wiki for guidance about enabling logging. |
| 139 | + |
| 140 | +### Default SSL library |
| 141 | + |
| 142 | +All client libraries, by default, use the Tomcat-native Boring SSL library to enable native-level performance for SSL |
| 143 | +operations. The Boring SSL library is an uber jar containing native libraries for Linux / macOS / Windows, and provides |
| 144 | +better performance compared to the default SSL implementation within the JDK. For more information, including how to |
| 145 | +reduce the dependency size, refer to the [performance tuning][performance_tuning] section of the wiki. |
| 146 | + |
11 | 147 | ## Next steps
|
12 | 148 |
|
| 149 | +Get started by exploring the samples [here][samples_readme]. |
| 150 | + |
13 | 151 | ## Contributing
|
| 152 | + |
| 153 | +If you would like to become an active contributor to this project please refer to our [Contribution |
| 154 | +Guidelines](https://github.com/Azure/azure-sdk-for-java/blob/main/CONTRIBUTING.md) for more information. |
| 155 | + |
| 156 | +<!-- Links --> |
| 157 | +[api_documentation]: https://azure.github.io/azure-sdk-for-java |
| 158 | +[event_hubs_create]: https://docs.microsoft.com/azure/event-hubs/event-hubs-create |
| 159 | +[event_hubs_product_docs]: https://docs.microsoft.com/azure/event-hubs/ |
| 160 | +[java_8_sdk_javadocs]: https://docs.oracle.com/javase/8/docs/api/java/util/logging/package-summary.html |
| 161 | +[jdk_link]: https://docs.microsoft.com/java/azure/jdk/?view=azure-java-stable |
| 162 | +[key_concepts]: https://github.com/Azure/azure-sdk-for-java/blob/main/sdk/eventhubs/azure-messaging-eventhubs-checkpointstore-blob/README.md#key-concepts |
| 163 | +[logging]: https://github.com/Azure/azure-sdk-for-java/wiki/Logging-with-Azure-SDK |
| 164 | +[maven]: https://maven.apache.org/ |
| 165 | +[performance_tuning]: https://github.com/Azure/azure-sdk-for-java/wiki/Performance-Tuning |
| 166 | +[redis_cache]: https://docs.microsoft.com/azure/azure-cache-for-redis/cache-configure |
| 167 | +[samples_readme]: https://github.com/Azure/azure-sdk-for-java/tree/main/sdk/eventhubs/azure-messaging-eventhubs-checkpointstore-jedis |
| 168 | +[//]: # ([sample_jedis_client]: ttps://github.com/Azure/azure-sdk-for-java/sdk/eventhubs/azure-messaging-eventhubs-checkpointstore-jedis/src/samples/java/com/azure/messaging/eventhubs/checkpointstore/jedis/JedisRedisCheckpointStoreSample.java) |
| 169 | +[//]: # ([sample_event_processor]: https://github.com/Azure/azure-sdk-for-java/sdk/eventhubs/azure-messaging-eventhubs-checkpointstore-jedis/src/samples/java/com/azure/messaging/eventhubs/checkpointstore/jedis/EventProcessorJedisRedisCheckpointStoreSample.java) |
| 170 | +[//]: # ([sample_examples]: https://github.com/Azure/azure-sdk-for-java/sdk/eventhubs/azure-messaging-eventhubs-checkpointstore-jedis/src/samples/java/com/azure/messaging/eventhubs/checkpointstore/jedis) |
| 171 | +[sample_jedis_client]: https://github.com/Azure/azure-sdk-for-java/tree/main/sdk/eventhubs/azure-messaging-eventhubs-checkpointstore-jedis |
| 172 | +[sample_event_processor]: https://github.com/Azure/azure-sdk-for-java/tree/main/sdk/eventhubs/azure-messaging-eventhubs-checkpointstore-jedis |
| 173 | +[sample_examples]: https://github.com/Azure/azure-sdk-for-java/tree/main/sdk/eventhubs/azure-messaging-eventhubs-checkpointstore-jedis |
| 174 | +[source_code]: https://github.com/Azure/azure-sdk-for-java/tree/main/sdk/eventhubs/azure-messaging-eventhubs-checkpointstore-jedis |
| 175 | +[source_eventprocessorclient]: https://github.com/Azure/azure-sdk-for-java/blob/main/sdk/eventhubs/azure-messaging-eventhubs/src/main/java/com/azure/messaging/eventhubs/EventProcessorClient.java |
| 176 | +[source_jedisredischeckpointstore]: https://github.com/Azure/azure-sdk-for-java/tree/main/sdk/eventhubs/azure-messaging-eventhubs-checkpointstore-jedis |
0 commit comments