Search in sources :

Example 26 with StreamException

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);
    }
}
Also used : ByteBuf(io.netty.buffer.ByteBuf) StreamException(com.rabbitmq.stream.StreamException)

Example 27 with StreamException

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);
    }
}
Also used : ByteBuf(io.netty.buffer.ByteBuf) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) StreamException(com.rabbitmq.stream.StreamException)

Example 28 with StreamException

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);
}
Also used : AMQPMessage(com.swiftmq.amqp.v100.messaging.AMQPMessage) IOException(java.io.IOException) StreamException(com.rabbitmq.stream.StreamException) StreamException(com.rabbitmq.stream.StreamException)

Example 29 with StreamException

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);
}
Also used : LocatorNotAvailableException(com.rabbitmq.stream.impl.StreamEnvironment.LocatorNotAvailableException) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) StreamException(com.rabbitmq.stream.StreamException) LocatorNotAvailableException(com.rabbitmq.stream.impl.StreamEnvironment.LocatorNotAvailableException) StreamException(com.rabbitmq.stream.StreamException) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

StreamException (com.rabbitmq.stream.StreamException)29 ByteBuf (io.netty.buffer.ByteBuf)17 Map (java.util.Map)9 List (java.util.List)7 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)7 CountDownLatch (java.util.concurrent.CountDownLatch)6 Duration (java.time.Duration)5 ArrayList (java.util.ArrayList)5 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)5 Constants (com.rabbitmq.stream.Constants)4 OffsetSpecification (com.rabbitmq.stream.OffsetSpecification)4 HashMap (java.util.HashMap)4 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)4 ExecutorService (java.util.concurrent.ExecutorService)4 Executors (java.util.concurrent.Executors)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 BackOffDelayPolicy (com.rabbitmq.stream.BackOffDelayPolicy)3 ConfirmationHandler (com.rabbitmq.stream.ConfirmationHandler)3 ConfirmationStatus (com.rabbitmq.stream.ConfirmationStatus)3 Environment (com.rabbitmq.stream.Environment)3