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);
}
}
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);
}
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));
}
}
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();
}
Aggregations