Search in sources :

Example 11 with Message

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

the class ProducerUsage method producerWithName.

void producerWithName() {
    Environment environment = Environment.builder().build();
    // tag::producer-with-name[]
    Producer producer = environment.producerBuilder().name(// <1>
    "my-app-producer").confirmTimeout(// <2>
    Duration.ZERO).stream("my-stream").build();
    // end::producer-with-name[]
    // tag::message-with-publishing-id[]
    Message message = producer.messageBuilder().publishingId(// <1>
    1).addData("hello".getBytes(StandardCharsets.UTF_8)).build();
    producer.send(message, confirmationStatus -> {
    });
// end::message-with-publishing-id[]
}
Also used : Producer(com.rabbitmq.stream.Producer) Message(com.rabbitmq.stream.Message) Environment(com.rabbitmq.stream.Environment)

Example 12 with Message

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

the class SuperStreamUsage method producerCustomHashFunction.

void producerCustomHashFunction() {
    Environment environment = Environment.builder().build();
    // tag::producer-custom-hash-function[]
    Producer producer = environment.producerBuilder().stream("invoices").routing(message -> message.getProperties().getMessageIdAsString()).hash(// <1>
    rk -> rk.hashCode()).producerBuilder().build();
// end::producer-custom-hash-function[]
}
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) Producer(com.rabbitmq.stream.Producer) Environment(com.rabbitmq.stream.Environment)

Example 13 with Message

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

the class Client method publish.

public List<Long> publish(byte publisherId, List<Message> messages, OutboundEntityMappingCallback mappingCallback, ToLongFunction<Object> publishSequenceFunction) {
    List<Object> encodedMessages = new ArrayList<>(messages.size());
    for (Message message : messages) {
        Codec.EncodedMessage encodedMessage = codec.encode(message);
        checkMessageFitsInFrame(encodedMessage);
        OriginalAndEncodedOutboundEntity wrapper = new OriginalAndEncodedOutboundEntity(message, encodedMessage);
        encodedMessages.add(wrapper);
    }
    return publishInternal(this.channel, publisherId, encodedMessages, new OriginalEncodedEntityOutboundEntityWriteCallback(mappingCallback, OUTBOUND_MESSAGE_WRITE_CALLBACK), publishSequenceFunction);
}
Also used : Codec(com.rabbitmq.stream.Codec) CompressionCodec(com.rabbitmq.stream.compression.CompressionCodec) Message(com.rabbitmq.stream.Message) EncodedMessage(com.rabbitmq.stream.Codec.EncodedMessage) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) EncodedMessage(com.rabbitmq.stream.Codec.EncodedMessage)

Example 14 with Message

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

the class Client method publishBatches.

public List<Long> publishBatches(byte publisherId, List<MessageBatch> messageBatches, ToLongFunction<Object> publishSequenceFunction) {
    List<Object> encodedMessageBatches = new ArrayList<>(messageBatches.size());
    for (MessageBatch batch : messageBatches) {
        EncodedMessageBatch encodedMessageBatch = createEncodedMessageBatch(batch.compression, batch.messages.size());
        for (Message message : batch.messages) {
            Codec.EncodedMessage encodedMessage = codec.encode(message);
            checkMessageFitsInFrame(encodedMessage);
            encodedMessageBatch.add(encodedMessage);
        }
        encodedMessageBatch.close();
        checkMessageBatchFitsInFrame(encodedMessageBatch);
        encodedMessageBatches.add(encodedMessageBatch);
    }
    return publishInternal(this.channel, publisherId, encodedMessageBatches, OUTBOUND_MESSAGE_BATCH_WRITE_CALLBACK, publishSequenceFunction);
}
Also used : Codec(com.rabbitmq.stream.Codec) CompressionCodec(com.rabbitmq.stream.compression.CompressionCodec) Message(com.rabbitmq.stream.Message) EncodedMessage(com.rabbitmq.stream.Codec.EncodedMessage) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) EncodedMessage(com.rabbitmq.stream.Codec.EncodedMessage)

Example 15 with Message

use of com.rabbitmq.stream.Message 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)

Aggregations

Message (com.rabbitmq.stream.Message)29 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)14 Environment (com.rabbitmq.stream.Environment)13 List (java.util.List)13 Producer (com.rabbitmq.stream.Producer)11 StandardCharsets (java.nio.charset.StandardCharsets)11 CountDownLatch (java.util.concurrent.CountDownLatch)11 ExtendWith (org.junit.jupiter.api.extension.ExtendWith)11 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)11 Test (org.junit.jupiter.api.Test)10 EncodedMessage (com.rabbitmq.stream.Codec.EncodedMessage)9 ArrayList (java.util.ArrayList)9 UUID (java.util.UUID)9 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)9 SECONDS (java.util.concurrent.TimeUnit.SECONDS)9 IntStream (java.util.stream.IntStream)9 Codec (com.rabbitmq.stream.Codec)8 OffsetSpecification (com.rabbitmq.stream.OffsetSpecification)8 Collections (java.util.Collections)8 Consumer (com.rabbitmq.stream.Consumer)7