Search in sources :

Example 1 with RoutingStrategy

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[]
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) MessageHandler(com.rabbitmq.stream.MessageHandler) List(java.util.List) Message(com.rabbitmq.stream.Message) Environment(com.rabbitmq.stream.Environment) Consumer(com.rabbitmq.stream.Consumer) Producer(com.rabbitmq.stream.Producer) Collections(java.util.Collections) RoutingStrategy(com.rabbitmq.stream.RoutingStrategy) AtomicLong(java.util.concurrent.atomic.AtomicLong) Producer(com.rabbitmq.stream.Producer) RoutingStrategy(com.rabbitmq.stream.RoutingStrategy) Environment(com.rabbitmq.stream.Environment) List(java.util.List)

Example 2 with RoutingStrategy

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;
}
Also used : Producer(com.rabbitmq.stream.Producer) RoutingStrategy(com.rabbitmq.stream.RoutingStrategy)

Aggregations

Producer (com.rabbitmq.stream.Producer)2 RoutingStrategy (com.rabbitmq.stream.RoutingStrategy)2 Consumer (com.rabbitmq.stream.Consumer)1 Environment (com.rabbitmq.stream.Environment)1 Message (com.rabbitmq.stream.Message)1 MessageHandler (com.rabbitmq.stream.MessageHandler)1 Collections (java.util.Collections)1 List (java.util.List)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1