Search in sources :

Example 21 with StreamException

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

the class Client method partitions.

public List<String> partitions(String superStream) {
    if (superStream == null) {
        throw new IllegalArgumentException("stream must not be null");
    }
    int length = // API code, version, correlation ID, 1 string
    2 + 2 + 4 + +2 + superStream.length();
    int correlationId = correlationSequence.incrementAndGet();
    try {
        ByteBuf bb = allocate(length + 4);
        bb.writeInt(length);
        bb.writeShort(encodeRequestCode(COMMAND_PARTITIONS));
        bb.writeShort(VERSION_1);
        bb.writeInt(correlationId);
        bb.writeShort(superStream.length());
        bb.writeBytes(superStream.getBytes(StandardCharsets.UTF_8));
        OutstandingRequest<List<String>> 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 : CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) List(java.util.List) ByteBuf(io.netty.buffer.ByteBuf) StreamException(com.rabbitmq.stream.StreamException)

Example 22 with StreamException

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

the class Client method authenticate.

private void authenticate() {
    List<String> saslMechanisms = getSaslMechanisms();
    SaslMechanism saslMechanism = this.saslConfiguration.getSaslMechanism(saslMechanisms);
    byte[] challenge = null;
    boolean authDone = false;
    while (!authDone) {
        byte[] saslResponse = saslMechanism.handleChallenge(challenge, this.credentialsProvider);
        SaslAuthenticateResponse saslAuthenticateResponse = sendSaslAuthenticate(saslMechanism, saslResponse);
        if (saslAuthenticateResponse.isOk()) {
            authDone = true;
        } else if (saslAuthenticateResponse.isChallenge()) {
            challenge = saslAuthenticateResponse.challenge;
        } else if (saslAuthenticateResponse.isAuthenticationFailure()) {
            throw new AuthenticationFailureException("Unexpected response code during authentication: " + formatConstant(saslAuthenticateResponse.getResponseCode()));
        } else {
            throw new StreamException("Unexpected response code during authentication: " + formatConstant(saslAuthenticateResponse.getResponseCode()));
        }
    }
}
Also used : SaslMechanism(com.rabbitmq.stream.sasl.SaslMechanism) AuthenticationFailureException(com.rabbitmq.stream.AuthenticationFailureException) StreamException(com.rabbitmq.stream.StreamException)

Example 23 with StreamException

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

the class Client method subscribe.

/**
 * Subscribe to receive messages from a stream.
 *
 * <p>Note the offset is an unsigned long. Longs are signed in Java, but unsigned longs can be
 * used as long as some care is taken for some operations. See the <code>unsigned*</code> static
 * methods in {@link Long}.
 *
 * @param subscriptionId identifier to correlate inbound messages to this subscription
 * @param stream the stream to consume from
 * @param offsetSpecification the specification of the offset to consume from
 * @param credit the initial number of credits
 * @param properties some optional properties to describe the subscription
 * @return the subscription confirmation
 */
public Response subscribe(byte subscriptionId, String stream, OffsetSpecification offsetSpecification, int credit, Map<String, String> properties) {
    if (credit < 0 || credit > Short.MAX_VALUE) {
        throw new IllegalArgumentException("Credit value must be between 0 and " + Short.MAX_VALUE);
    }
    // misses the offset
    int length = 2 + 2 + 4 + 1 + 2 + stream.length() + 2 + 2;
    if (offsetSpecification.isOffset() || offsetSpecification.isTimestamp()) {
        length += 8;
    }
    int propertiesSize = 0;
    if (properties != null && !properties.isEmpty()) {
        // size of the map
        propertiesSize = 4;
        for (Map.Entry<String, String> entry : properties.entrySet()) {
            propertiesSize += 2 + entry.getKey().length() + 2 + entry.getValue().length();
        }
    }
    length += propertiesSize;
    int correlationId = correlationSequence.getAndIncrement();
    try {
        ByteBuf bb = allocate(length + 4);
        bb.writeInt(length);
        bb.writeShort(encodeRequestCode(COMMAND_SUBSCRIBE));
        bb.writeShort(VERSION_1);
        bb.writeInt(correlationId);
        bb.writeByte(subscriptionId);
        bb.writeShort(stream.length());
        bb.writeBytes(stream.getBytes(StandardCharsets.UTF_8));
        bb.writeShort(offsetSpecification.getType());
        if (offsetSpecification.isOffset() || offsetSpecification.isTimestamp()) {
            bb.writeLong(offsetSpecification.getOffset());
        }
        bb.writeShort(credit);
        if (properties != null && !properties.isEmpty()) {
            bb.writeInt(properties.size());
            for (Map.Entry<String, String> entry : properties.entrySet()) {
                bb.writeShort(entry.getKey().length()).writeBytes(entry.getKey().getBytes(StandardCharsets.UTF_8)).writeShort(entry.getValue().length()).writeBytes(entry.getValue().getBytes(StandardCharsets.UTF_8));
            }
        }
        OutstandingRequest<Response> request = new OutstandingRequest<>(this.rpcTimeout);
        outstandingRequests.put(correlationId, request);
        if (offsetSpecification.isOffset()) {
            subscriptionOffsets.add(new SubscriptionOffset(subscriptionId, offsetSpecification.getOffset()));
        }
        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 24 with StreamException

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

the class Client method unsubscribe.

public Response unsubscribe(byte subscriptionId) {
    int length = 2 + 2 + 4 + 1;
    int correlationId = correlationSequence.getAndIncrement();
    try {
        ByteBuf bb = allocate(length + 4);
        bb.writeInt(length);
        bb.writeShort(encodeRequestCode(COMMAND_UNSUBSCRIBE));
        bb.writeShort(VERSION_1);
        bb.writeInt(correlationId);
        bb.writeByte(subscriptionId);
        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 25 with StreamException

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

the class Client method send.

// for testing
void send(byte[] content) {
    ByteBuf bb = allocateNoCheck(content.length);
    bb.writeBytes(content);
    try {
        channel.writeAndFlush(bb).sync();
    } catch (InterruptedException e) {
        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