Search in sources :

Example 16 with StreamException

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

the class Client method sendSaslAuthenticate.

private SaslAuthenticateResponse sendSaslAuthenticate(SaslMechanism saslMechanism, byte[] challengeResponse) {
    int length = 2 + 2 + 4 + 2 + saslMechanism.getName().length() + 4 + (challengeResponse == null ? 0 : challengeResponse.length);
    int correlationId = correlationSequence.incrementAndGet();
    try {
        ByteBuf bb = allocateNoCheck(length + 4);
        bb.writeInt(length);
        bb.writeShort(encodeRequestCode(COMMAND_SASL_AUTHENTICATE));
        bb.writeShort(VERSION_1);
        bb.writeInt(correlationId);
        bb.writeShort(saslMechanism.getName().length());
        bb.writeBytes(saslMechanism.getName().getBytes(StandardCharsets.UTF_8));
        if (challengeResponse == null) {
            bb.writeInt(-1);
        } else {
            bb.writeInt(challengeResponse.length).writeBytes(challengeResponse);
        }
        OutstandingRequest<SaslAuthenticateResponse> 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) StreamException(com.rabbitmq.stream.StreamException)

Example 17 with StreamException

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

the class Client method open.

private Map<String, String> open(String virtualHost) {
    int length = 2 + 2 + 4 + 2 + virtualHost.length();
    int correlationId = correlationSequence.incrementAndGet();
    try {
        ByteBuf bb = allocate(length + 4);
        bb.writeInt(length);
        bb.writeShort(encodeRequestCode(COMMAND_OPEN));
        bb.writeShort(VERSION_1);
        bb.writeInt(correlationId);
        bb.writeShort(virtualHost.length());
        bb.writeBytes(virtualHost.getBytes(StandardCharsets.UTF_8));
        OutstandingRequest<OpenResponse> request = new OutstandingRequest<>(this.rpcTimeout);
        outstandingRequests.put(correlationId, request);
        channel.writeAndFlush(bb);
        request.block();
        if (!request.response.get().isOk()) {
            throw new StreamException("Unexpected response code when connecting to virtual host: " + formatConstant(request.response.get().getResponseCode()));
        }
        return request.response.get().connectionProperties;
    } catch (StreamException e) {
        outstandingRequests.remove(correlationId);
        throw e;
    } catch (RuntimeException e) {
        outstandingRequests.remove(correlationId);
        throw new StreamException(e);
    }
}
Also used : ByteBuf(io.netty.buffer.ByteBuf) StreamException(com.rabbitmq.stream.StreamException)

Example 18 with StreamException

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

the class Client method deletePublisher.

public Response deletePublisher(byte publisherId) {
    int length = 2 + 2 + 4 + 1;
    int correlationId = correlationSequence.getAndIncrement();
    try {
        ByteBuf bb = allocate(length + 4);
        bb.writeInt(length);
        bb.writeShort(encodeRequestCode(COMMAND_DELETE_PUBLISHER));
        bb.writeShort(VERSION_1);
        bb.writeInt(correlationId);
        bb.writeByte(publisherId);
        OutstandingRequest<Response> 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) StreamException(com.rabbitmq.stream.StreamException)

Example 19 with StreamException

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

the class Client method delete.

public Response delete(String stream) {
    int length = 2 + 2 + 4 + 2 + stream.length();
    int correlationId = correlationSequence.incrementAndGet();
    try {
        ByteBuf bb = allocate(length + 4);
        bb.writeInt(length);
        bb.writeShort(encodeRequestCode(COMMAND_DELETE_STREAM));
        bb.writeShort(VERSION_1);
        bb.writeInt(correlationId);
        bb.writeShort(stream.length());
        bb.writeBytes(stream.getBytes(StandardCharsets.UTF_8));
        OutstandingRequest<Response> 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) StreamException(com.rabbitmq.stream.StreamException)

Example 20 with StreamException

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

the class Client method sendClose.

private void sendClose(short code, String reason) {
    int length = 2 + 2 + 4 + 2 + 2 + reason.length();
    int correlationId = correlationSequence.incrementAndGet();
    try {
        ByteBuf bb = allocate(length + 4);
        bb.writeInt(length);
        bb.writeShort(encodeRequestCode(COMMAND_CLOSE));
        bb.writeShort(VERSION_1);
        bb.writeInt(correlationId);
        bb.writeShort(code);
        bb.writeShort(reason.length());
        bb.writeBytes(reason.getBytes(StandardCharsets.UTF_8));
        OutstandingRequest<Response> request = new OutstandingRequest<>(this.rpcTimeout);
        outstandingRequests.put(correlationId, request);
        channel.writeAndFlush(bb);
        request.block();
        if (!request.response.get().isOk()) {
            LOGGER.warn("Unexpected response code when closing: {}", formatConstant(request.response.get().getResponseCode()));
            throw new StreamException("Unexpected response code when closing: " + formatConstant(request.response.get().getResponseCode()));
        }
    } catch (RuntimeException e) {
        outstandingRequests.remove(correlationId);
        throw new StreamException(e);
    }
}
Also used : ByteBuf(io.netty.buffer.ByteBuf) StreamException(com.rabbitmq.stream.StreamException)

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