use of com.rabbitmq.stream.StreamException in project rabbitmq-stream-java-client by rabbitmq.
the class Client method queryPublisherSequence.
public long queryPublisherSequence(String publisherReference, String stream) {
if (publisherReference == null || publisherReference.isEmpty() || publisherReference.length() > 256) {
throw new IllegalArgumentException("Publisher reference must a non-empty string of less than 256 characters");
}
if (stream == null || stream.isEmpty()) {
throw new IllegalArgumentException("Stream cannot be null or empty");
}
int length = 2 + 2 + 4 + 2 + publisherReference.length() + 2 + stream.length();
int correlationId = correlationSequence.getAndIncrement();
try {
ByteBuf bb = allocate(length + 4);
bb.writeInt(length);
bb.writeShort(encodeRequestCode(COMMAND_QUERY_PUBLISHER_SEQUENCE));
bb.writeShort(VERSION_1);
bb.writeInt(correlationId);
bb.writeShort(publisherReference.length());
bb.writeBytes(publisherReference.getBytes(StandardCharsets.UTF_8));
bb.writeShort(stream.length());
bb.writeBytes(stream.getBytes(StandardCharsets.UTF_8));
OutstandingRequest<QueryPublisherSequenceResponse> request = new OutstandingRequest<>(this.rpcTimeout);
outstandingRequests.put(correlationId, request);
channel.writeAndFlush(bb);
request.block();
QueryPublisherSequenceResponse response = request.response.get();
if (!response.isOk()) {
LOGGER.info("Query publisher sequence failed with code {}", formatConstant(response.getResponseCode()));
}
return response.getSequence();
} catch (RuntimeException e) {
outstandingRequests.remove(correlationId);
throw new StreamException(e);
}
}
use of com.rabbitmq.stream.StreamException in project rabbitmq-stream-java-client by rabbitmq.
the class Client method metadata.
public Map<String, StreamMetadata> metadata(String... streams) {
if (streams == null || streams.length == 0) {
throw new IllegalArgumentException("At least one stream must be specified");
}
// API code, version, correlation ID, size of array
int length = 2 + 2 + 4 + 4;
for (String stream : streams) {
length += 2;
length += stream.length();
}
int correlationId = correlationSequence.incrementAndGet();
try {
ByteBuf bb = allocate(length + 4);
bb.writeInt(length);
bb.writeShort(encodeRequestCode(COMMAND_METADATA));
bb.writeShort(VERSION_1);
bb.writeInt(correlationId);
bb.writeInt(streams.length);
for (String stream : streams) {
bb.writeShort(stream.length());
bb.writeBytes(stream.getBytes(StandardCharsets.UTF_8));
}
OutstandingRequest<Map<String, StreamMetadata>> request = new OutstandingRequest<>(this.rpcTimeout);
outstandingRequests.put(correlationId, request);
channel.writeAndFlush(bb);
request.block();
return request.response.get();
} catch (RuntimeException e) {
outstandingRequests.remove(correlationId);
throw new StreamException(e);
}
}
use of com.rabbitmq.stream.StreamException in project rabbitmq-stream-java-client by rabbitmq.
the class SwiftMqCodec method createMessage.
protected Message createMessage(byte[] data) {
AMQPMessage amqpMessage;
try {
amqpMessage = new AMQPMessage(data);
} catch (Exception e) {
throw new StreamException("Error while decoding AMQP 1.0 message");
}
Object body = extractBody(amqpMessage);
com.rabbitmq.stream.Properties properties = createProperties(amqpMessage);
Map<String, Object> applicationProperties = createApplicationProperties(amqpMessage);
Map<String, Object> messageAnnotations = createMessageAnnotations(amqpMessage);
return new SwiftMqMessage(amqpMessage, body, properties, applicationProperties, messageAnnotations);
}
use of com.rabbitmq.stream.StreamException in project rabbitmq-stream-java-client by rabbitmq.
the class StreamEnvironmentUnitTest method locatorOperationShouldThrowInterruptedExceptionAsCauseIfInterrupted.
@Test
void locatorOperationShouldThrowInterruptedExceptionAsCauseIfInterrupted() throws InterruptedException {
CountDownLatch latch = new CountDownLatch(1);
AtomicReference<Exception> exception = new AtomicReference<>();
Thread thread = new Thread(() -> {
try {
StreamEnvironment.locatorOperation(c -> {
latch.countDown();
throw new LocatorNotAvailableException();
}, () -> null, BackOffDelayPolicy.fixed(Duration.ofMinutes(10)));
} catch (StreamException e) {
exception.set(e);
}
});
thread.start();
latchAssert(latch).completes();
Thread.sleep(100);
thread.interrupt();
Thread.sleep(100);
assertThat(exception.get()).isInstanceOf(StreamException.class).hasCauseInstanceOf(InterruptedException.class);
}
Aggregations