use of com.rabbitmq.stream.RoutingStrategy in project rabbitmq-stream-java-client by rabbitmq.
the class SuperStreamUsage method producerCustomRoutingStrategy.
void producerCustomRoutingStrategy() {
Environment environment = Environment.builder().build();
// tag::producer-custom-routing-strategy[]
AtomicLong messageCount = new AtomicLong(0);
RoutingStrategy routingStrategy = (message, metadata) -> {
List<String> partitions = metadata.partitions();
String stream = partitions.get((int) messageCount.getAndIncrement() % partitions.size());
return Collections.singletonList(stream);
};
Producer producer = environment.producerBuilder().stream("invoices").routing(// <1>
null).strategy(// <2>
routingStrategy).producerBuilder().build();
// end::producer-custom-routing-strategy[]
}
use of com.rabbitmq.stream.RoutingStrategy in project rabbitmq-stream-java-client by rabbitmq.
the class StreamProducerBuilder method build.
public Producer build() {
if (subEntrySize == 1 && compression != null) {
throw new IllegalArgumentException("Sub-entry batching must be enabled to enable compression");
}
if (subEntrySize > 1 && compression == null) {
compression = Compression.NONE;
}
this.environment.maybeInitializeLocator();
Producer producer;
if (this.routingConfiguration == null) {
producer = new StreamProducer(name, stream, subEntrySize, batchSize, compression, batchPublishingDelay, maxUnconfirmedMessages, confirmTimeout, enqueueTimeout, environment);
this.environment.addProducer((StreamProducer) producer);
} else {
RoutingStrategy routingStrategy = this.routingConfiguration.routingStrategy;
if (routingStrategy == null) {
if (this.routingConfiguration.hash == null) {
routingStrategy = new RoutingKeyRoutingStrategy(this.routingConfiguration.routingKeyExtractor);
} else {
routingStrategy = new HashRoutingStrategy(this.routingConfiguration.routingKeyExtractor, this.routingConfiguration.hash);
}
}
producer = new SuperStreamProducer(this, this.name, this.stream, routingStrategy, this.environment);
}
return producer;
}
Aggregations