Search in sources :

Example 26 with Message

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

the class TestUtils method publishAndWaitForConfirms.

static void publishAndWaitForConfirms(TestUtils.ClientFactory cf, Function<MessageBuilder, Message> messageFactory, int publishCount, String stream) {
    CountDownLatch latchConfirm = new CountDownLatch(publishCount);
    Client.PublishConfirmListener publishConfirmListener = (publisherId, correlationId) -> latchConfirm.countDown();
    Client client = cf.get(new Client.ClientParameters().publishConfirmListener(publishConfirmListener));
    client.declarePublisher(b(1), null, stream);
    for (int i = 1; i <= publishCount; i++) {
        Message message = messageFactory.apply(client.messageBuilder());
        client.publish(b(1), Collections.singletonList(message));
    }
    try {
        assertThat(latchConfirm.await(60, SECONDS)).isTrue();
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        throw new RuntimeException(e);
    }
}
Also used : Message(com.rabbitmq.stream.Message) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) LoggerFactory(org.slf4j.LoggerFactory) Connection(com.rabbitmq.client.Connection) ClientParameters(com.rabbitmq.stream.impl.Client.ClientParameters) AfterEachCallback(org.junit.jupiter.api.extension.AfterEachCallback) StreamException(com.rabbitmq.stream.StreamException) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Duration(java.time.Duration) Map(java.util.Map) Documented(java.lang.annotation.Documented) Method(java.lang.reflect.Method) Host(com.rabbitmq.stream.Host) Tuple(io.vavr.Tuple) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) UUID(java.util.UUID) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) StandardCharsets(java.nio.charset.StandardCharsets) Executors(java.util.concurrent.Executors) TestInfo(org.junit.jupiter.api.TestInfo) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Optional(java.util.Optional) MessageBuilder(com.rabbitmq.stream.MessageBuilder) Channel(com.rabbitmq.client.Channel) Constants(com.rabbitmq.stream.Constants) AnnotatedElement(java.lang.reflect.AnnotatedElement) Assertions.fail(org.junit.jupiter.api.Assertions.fail) IntStream(java.util.stream.IntStream) StreamMetadata(com.rabbitmq.stream.impl.Client.StreamMetadata) CloseableResource(org.junit.jupiter.api.extension.ExtensionContext.Store.CloseableResource) Address(com.rabbitmq.stream.Address) ExtensionContext(org.junit.jupiter.api.extension.ExtensionContext) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) Supplier(java.util.function.Supplier) Retention(java.lang.annotation.Retention) ArrayList(java.util.ArrayList) Answer(org.mockito.stubbing.Answer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) AssertDelegateTarget(org.assertj.core.api.AssertDelegateTarget) ExecutorService(java.util.concurrent.ExecutorService) Namespace(org.junit.jupiter.api.extension.ExtensionContext.Namespace) EventLoopGroup(io.netty.channel.EventLoopGroup) Logger(org.slf4j.Logger) IOException(java.io.IOException) Target(java.lang.annotation.Target) Broker(com.rabbitmq.stream.impl.Client.Broker) ElementType(java.lang.annotation.ElementType) Field(java.lang.reflect.Field) InputStreamReader(java.io.InputStreamReader) BuiltinExchangeType(com.rabbitmq.client.BuiltinExchangeType) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) AtomicLong(java.util.concurrent.atomic.AtomicLong) Level(ch.qos.logback.classic.Level) ConditionEvaluationResult(org.junit.jupiter.api.extension.ConditionEvaluationResult) Tuple2(io.vavr.Tuple2) BeforeAllCallback(org.junit.jupiter.api.extension.BeforeAllCallback) ExecutionCondition(org.junit.jupiter.api.extension.ExecutionCondition) AfterAllCallback(org.junit.jupiter.api.extension.AfterAllCallback) Condition(org.assertj.core.api.Condition) BeforeEachCallback(org.junit.jupiter.api.extension.BeforeEachCallback) BufferedReader(java.io.BufferedReader) Collections(java.util.Collections) SECONDS(java.util.concurrent.TimeUnit.SECONDS) InputStream(java.io.InputStream) RetentionPolicy(java.lang.annotation.RetentionPolicy) ClientParameters(com.rabbitmq.stream.impl.Client.ClientParameters) Message(com.rabbitmq.stream.Message) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 27 with Message

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

the class OutboundMappingCallbackTest method publishList.

@Test
void publishList() throws Exception {
    int batchSize = 10;
    int batchNumber = 1000;
    int messageCount = batchSize * batchNumber;
    CountDownLatch mappingLatch = new CountDownLatch(messageCount);
    CountDownLatch confirmLatch = new CountDownLatch(messageCount);
    Set<Long> mapped = ConcurrentHashMap.newKeySet(messageCount);
    Client client = cf.get(new Client.ClientParameters().publishConfirmListener((publisherId, publishingId) -> confirmLatch.countDown()));
    client.declarePublisher(b(1), null, stream);
    IntStream.range(0, batchNumber).forEach(i -> {
        List<Message> messages = IntStream.range(0, batchSize).mapToObj(messageIndex -> String.valueOf(messageIndex).getBytes()).map(body -> client.messageBuilder().addData(body).build()).collect(Collectors.toList());
        client.publish(b(1), messages, (publishingId, original) -> {
            assertThat(original).isNotNull().isInstanceOf(Message.class);
            mapped.add(publishingId);
            mappingLatch.countDown();
        });
    });
    assertThat(mappingLatch.await(10, TimeUnit.SECONDS)).isTrue();
    assertThat(confirmLatch.await(10, TimeUnit.SECONDS)).isTrue();
    assertThat(mapped).hasSize(messageCount);
}
Also used : IntStream(java.util.stream.IntStream) Message(com.rabbitmq.stream.Message) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) Compression(com.rabbitmq.stream.compression.Compression) Collectors(java.util.stream.Collectors) TestUtils.b(com.rabbitmq.stream.impl.TestUtils.b) ArrayList(java.util.ArrayList) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Message(com.rabbitmq.stream.Message) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test)

Example 28 with Message

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

the class FrameTest method messageTooBigToFitInOneFrameShouldThrowException.

@Test
void messageTooBigToFitInOneFrameShouldThrowException() {
    try (Client client = cf.get(new Client.ClientParameters().requestedMaxFrameSize(1024))) {
        byte[] binary = new byte[1000];
        Message message = new Message() {

            @Override
            public boolean hasPublishingId() {
                return false;
            }

            @Override
            public long getPublishingId() {
                return 0;
            }

            @Override
            public byte[] getBodyAsBinary() {
                return binary;
            }

            @Override
            public Object getBody() {
                return null;
            }

            @Override
            public Properties getProperties() {
                return null;
            }

            @Override
            public Map<String, Object> getApplicationProperties() {
                return null;
            }

            @Override
            public Map<String, Object> getMessageAnnotations() {
                return null;
            }
        };
        List<ThrowableAssert.ThrowingCallable> publishCalls = Arrays.asList(() -> client.publish(b(1), Arrays.asList(message)));
        publishCalls.forEach(callable -> assertThatThrownBy(callable).isInstanceOf(IllegalArgumentException.class));
    }
}
Also used : Message(com.rabbitmq.stream.Message) Test(org.junit.jupiter.api.Test)

Example 29 with Message

use of com.rabbitmq.stream.Message in project spring-amqp by spring-projects.

the class StreamListenerContainerTests method testAdviceChain.

@Test
void testAdviceChain() throws Exception {
    Environment env = mock(Environment.class);
    ConsumerBuilder builder = mock(ConsumerBuilder.class);
    given(env.consumerBuilder()).willReturn(builder);
    AtomicReference<MessageHandler> handler = new AtomicReference<>();
    willAnswer(inv -> {
        handler.set(inv.getArgument(0));
        return null;
    }).given(builder).messageHandler(any());
    AtomicBoolean advised = new AtomicBoolean();
    MethodInterceptor advice = (inv) -> {
        advised.set(true);
        return inv.proceed();
    };
    StreamListenerContainer container = new StreamListenerContainer(env);
    container.setAdviceChain(advice);
    AtomicBoolean called = new AtomicBoolean();
    MessageListener ml = mock(MessageListener.class);
    willAnswer(inv -> {
        called.set(true);
        return null;
    }).given(ml).onMessage(any());
    container.setupMessageListener(ml);
    Message message = mock(Message.class);
    given(message.getBodyAsBinary()).willReturn("foo".getBytes());
    Context context = mock(Context.class);
    handler.get().handle(context, message);
    assertThat(advised.get()).isTrue();
    assertThat(called.get()).isTrue();
    advised.set(false);
    called.set(false);
    ChannelAwareMessageListener cal = mock(ChannelAwareMessageListener.class);
    willAnswer(inv -> {
        called.set(true);
        return null;
    }).given(cal).onMessage(any(), isNull());
    container.setupMessageListener(cal);
    handler.get().handle(context, message);
    assertThat(advised.get()).isTrue();
    assertThat(called.get()).isTrue();
    called.set(false);
    StreamMessageListener sml = mock(StreamMessageListener.class);
    willAnswer(inv -> {
        called.set(true);
        return null;
    }).given(sml).onStreamMessage(message, context);
    container.setupMessageListener(sml);
    handler.get().handle(context, message);
    assertThat(advised.get()).isTrue();
    assertThat(called.get()).isTrue();
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ArgumentMatchers.isNull(org.mockito.ArgumentMatchers.isNull) MessageListener(org.springframework.amqp.core.MessageListener) MessageHandler(com.rabbitmq.stream.MessageHandler) Message(com.rabbitmq.stream.Message) Environment(com.rabbitmq.stream.Environment) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicReference(java.util.concurrent.atomic.AtomicReference) BDDMockito.willAnswer(org.mockito.BDDMockito.willAnswer) Test(org.junit.jupiter.api.Test) MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) Context(com.rabbitmq.stream.MessageHandler.Context) BDDMockito.given(org.mockito.BDDMockito.given) ChannelAwareMessageListener(org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener) ConsumerBuilder(com.rabbitmq.stream.ConsumerBuilder) Mockito.mock(org.mockito.Mockito.mock) Context(com.rabbitmq.stream.MessageHandler.Context) MessageHandler(com.rabbitmq.stream.MessageHandler) Message(com.rabbitmq.stream.Message) MessageListener(org.springframework.amqp.core.MessageListener) ChannelAwareMessageListener(org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener) AtomicReference(java.util.concurrent.atomic.AtomicReference) ChannelAwareMessageListener(org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) Environment(com.rabbitmq.stream.Environment) ConsumerBuilder(com.rabbitmq.stream.ConsumerBuilder) Test(org.junit.jupiter.api.Test)

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