Search in sources :

Example 26 with Environment

use of com.rabbitmq.stream.Environment in project rabbitmq-stream-java-client by rabbitmq.

the class SuperStreamUsage method consumerSimple.

void consumerSimple() {
    Environment environment = Environment.builder().build();
    // tag::consumer-simple[]
    Consumer consumer = environment.consumerBuilder().superStream(// <1>
    "invoices").messageHandler((context, message) -> {
    // message processing
    }).build();
    // ...
    // <2>
    consumer.close();
// end::consumer-simple[]
}
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) Consumer(com.rabbitmq.stream.Consumer) Environment(com.rabbitmq.stream.Environment)

Example 27 with Environment

use of com.rabbitmq.stream.Environment 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 28 with Environment

use of com.rabbitmq.stream.Environment in project rabbitmq-stream-java-client by rabbitmq.

the class EnvironmentUsage method deleteStream.

void deleteStream() {
    Environment environment = Environment.builder().build();
    // tag::stream-deletion[]
    // <1>
    environment.deleteStream("my-stream");
// end::stream-deletion[]
}
Also used : Environment(com.rabbitmq.stream.Environment)

Example 29 with Environment

use of com.rabbitmq.stream.Environment in project rabbitmq-stream-java-client by rabbitmq.

the class EnvironmentUsage method environmentCreationWithTls.

void environmentCreationWithTls() throws Exception {
    // tag::environment-creation-with-tls[]
    X509Certificate certificate;
    try (FileInputStream inputStream = new FileInputStream("/path/to/ca_certificate.pem")) {
        CertificateFactory fact = CertificateFactory.getInstance("X.509");
        // <1>
        certificate = (X509Certificate) fact.generateCertificate(inputStream);
    }
    SslContext sslContext = SslContextBuilder.forClient().trustManager(// <2>
    certificate).build();
    Environment environment = Environment.builder().uri(// <3>
    "rabbitmq-stream+tls://guest:guest@localhost:5551/%2f").tls().sslContext(// <4>
    sslContext).environmentBuilder().build();
// end::environment-creation-with-tls[]
}
Also used : Environment(com.rabbitmq.stream.Environment) CertificateFactory(java.security.cert.CertificateFactory) X509Certificate(java.security.cert.X509Certificate) FileInputStream(java.io.FileInputStream) SslContext(io.netty.handler.ssl.SslContext)

Example 30 with Environment

use of com.rabbitmq.stream.Environment in project rabbitmq-stream-java-client by rabbitmq.

the class EnvironmentUsage method addressResolver.

void addressResolver() throws Exception {
    // tag::address-resolver[]
    // <1>
    Address entryPoint = new Address("my-load-balancer", 5552);
    Environment environment = Environment.builder().host(// <2>
    entryPoint.host()).port(// <2>
    entryPoint.port()).addressResolver(// <3>
    address -> entryPoint).build();
// end::address-resolver[]
}
Also used : X509Certificate(java.security.cert.X509Certificate) Arrays(java.util.Arrays) SslContext(io.netty.handler.ssl.SslContext) CertificateFactory(java.security.cert.CertificateFactory) ByteCapacity(com.rabbitmq.stream.ByteCapacity) Environment(com.rabbitmq.stream.Environment) Duration(java.time.Duration) SslContextBuilder(io.netty.handler.ssl.SslContextBuilder) FileInputStream(java.io.FileInputStream) Address(com.rabbitmq.stream.Address) Address(com.rabbitmq.stream.Address) Environment(com.rabbitmq.stream.Environment)

Aggregations

Environment (com.rabbitmq.stream.Environment)35 Producer (com.rabbitmq.stream.Producer)20 Test (org.junit.jupiter.api.Test)13 Consumer (com.rabbitmq.stream.Consumer)12 Message (com.rabbitmq.stream.Message)12 Address (com.rabbitmq.stream.Address)10 Collections (java.util.Collections)10 OffsetSpecification (com.rabbitmq.stream.OffsetSpecification)9 List (java.util.List)9 CountDownLatch (java.util.concurrent.CountDownLatch)9 ConsumerBuilder (com.rabbitmq.stream.ConsumerBuilder)7 StreamException (com.rabbitmq.stream.StreamException)7 Duration (java.time.Duration)7 ChannelCustomizer (com.rabbitmq.stream.ChannelCustomizer)6 ConfirmationHandler (com.rabbitmq.stream.ConfirmationHandler)6 ProducerBuilder (com.rabbitmq.stream.ProducerBuilder)6 StreamCreator (com.rabbitmq.stream.StreamCreator)6 SslHandler (io.netty.handler.ssl.SslHandler)6 StandardCharsets (java.nio.charset.StandardCharsets)6 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)6