-
Notifications
You must be signed in to change notification settings - Fork 387
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cluster-sharding: single listener per entity type key + Java DSL #1080
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
cluster-sharding/src/test/java/docs/javadsl/ClusterShardingExample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package docs.javadsl; | ||
|
||
import akka.actor.typed.ActorSystem; | ||
import akka.actor.typed.Behavior; | ||
import akka.actor.typed.javadsl.Adapter; | ||
import akka.actor.typed.javadsl.Behaviors; | ||
import akka.cluster.sharding.external.ExternalShardAllocationStrategy; | ||
import akka.cluster.sharding.typed.javadsl.ClusterSharding; | ||
import akka.cluster.sharding.typed.javadsl.Entity; | ||
import akka.cluster.sharding.typed.javadsl.EntityTypeKey; | ||
import akka.kafka.AutoSubscription; | ||
import akka.kafka.ConsumerRebalanceEvent; | ||
import akka.kafka.ConsumerSettings; | ||
import akka.kafka.Subscriptions; | ||
import akka.kafka.cluster.sharding.KafkaClusterSharding; | ||
import akka.kafka.javadsl.Consumer; | ||
import akka.util.Timeout; | ||
import org.apache.kafka.common.serialization.ByteArrayDeserializer; | ||
import org.apache.kafka.common.serialization.StringDeserializer; | ||
|
||
import java.time.Duration; | ||
import java.util.concurrent.CompletionStage; | ||
|
||
public class ClusterShardingExample { | ||
|
||
// #user-entity | ||
final static class User { | ||
public final String id; | ||
public final String mame; | ||
|
||
User(String id, String mame) { | ||
this.id = id; | ||
this.mame = mame; | ||
} | ||
} | ||
// #user-entity | ||
|
||
public static Behavior<User> userBehaviour() { | ||
return Behaviors.empty(); | ||
} | ||
|
||
public static void example() { | ||
ActorSystem<Void> typedSystem = ActorSystem.create(Behaviors.empty(), "ClusterShardingExample"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It should just be |
||
String kafkaBootstrapServers = "localhost:9092"; | ||
|
||
|
||
// #message-extractor | ||
// automatically retrieving the number of partitions requires a round trip to a Kafka broker | ||
CompletionStage<KafkaClusterSharding.KafkaShardingNoEnvelopeExtractor<User>> messageExtractor = KafkaClusterSharding.get(typedSystem).messageExtractorNoEnvelope( | ||
"user-topic", | ||
Duration.ofSeconds(10), | ||
(User msg) -> msg.id, | ||
ConsumerSettings.create(Adapter.toClassic(typedSystem), new StringDeserializer(), new StringDeserializer()) | ||
); | ||
// #message-extractor | ||
|
||
// #setup-cluster-sharding | ||
|
||
String groupId = "user-topic-group-id"; | ||
EntityTypeKey<User> typeKey = EntityTypeKey.create(User.class, groupId); | ||
|
||
messageExtractor.thenAccept(extractor -> ClusterSharding.get(typedSystem).init( | ||
Entity.of(typeKey, ctx -> userBehaviour()) | ||
.withAllocationStrategy(new ExternalShardAllocationStrategy(typedSystem, typeKey.name(), Timeout.create(Duration.ofSeconds(5)))) | ||
.withMessageExtractor(extractor))); | ||
// #setup-cluster-sharding | ||
|
||
// #rebalance-listener | ||
akka.actor.typed.ActorRef<ConsumerRebalanceEvent> rebalanceListener = KafkaClusterSharding.get(typedSystem).rebalanceListener(typeKey); | ||
|
||
ConsumerSettings<String, byte[]> consumerSettings = ConsumerSettings.create(Adapter.toClassic(typedSystem), new StringDeserializer(), new ByteArrayDeserializer()) | ||
.withBootstrapServers(kafkaBootstrapServers) | ||
.withGroupId(typeKey.name());// use the same group id as we used in the `EntityTypeKey` for `User` | ||
|
||
// pass the rebalance listener to the topic subscription | ||
AutoSubscription subscription = Subscriptions | ||
.topics("user-topic") | ||
.withRebalanceListener(Adapter.toClassic(rebalanceListener)); | ||
|
||
Consumer.plainSource(consumerSettings, subscription); | ||
// #rebalance-listener | ||
|
||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use the FCQN for
ActorSystem
instead of importing it to make it more clear.